Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have tried hostname and ping in a cluster machine, with different outputs. I am wondering what is the difference between the two? For example, on the same machine, hostname outputs node4.XXX and

ping -c 1 $(hostname)

outputs pc333.XXX.

share|improve this question

1 Answer 1

up vote 10 down vote accepted

The hostname command outputs the hostname of the system from the systems local hostname configuration (could be /etc/hostname or /proc/sys/kernel/hostname or other depending on OS).

The command ping -c 1 <hostname> is going to perform a lookup through the libc resolver (which may or may not be DNS. e.g., /etc/hosts is not DNS) of the <hostname> specified and then perform a reverse DNS lookup of the IP address returned and report that name in the output of the ping command.

As a concrete example, suppose that the local system hostname is fred as specified in /etc/hostname. The hostname command will return 'fred'. The command ping -c 1 fred will perform a DNS lookup of fred (either just fred or fred fully qualified such as fred.domain.com if default domain is domain.com). Assume that DNS returns IP address x.x.x.x. ping will then perform a reverse DNS lookup of IP address x.x.x.x, if no name is returned ping will output the IP address x.x.x.x, otherwise ping will output whatever named was returned from the reverse lookup which could be a different name such as ethel.domain.com.

share|improve this answer
2  
Well, actually, its going to be a lookup through the libc resolver. Which may or may not be DNS. e.g., /etc/hosts is not DNS. –  derobert Sep 25 '12 at 16:26
    
@derobert Thank you, I updated my answer accordingly. –  HeatfanJohn Sep 25 '12 at 16:33
    
thanks, that clears things up. I have additional question : what is the typical scenario of using address returned by hostname. For example, can it be used by other machines? –  Richard Sep 25 '12 at 16:57
    
@Richard - you should ask that as a new question. –  ire_and_curses Sep 25 '12 at 17:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.