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.

1 Answer 1


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.

  • 3
    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, 2012 at 16:26
  • @derobert Thank you, I updated my answer accordingly. Sep 25, 2012 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, 2012 at 16:57
  • @Richard - you should ask that as a new question. Sep 25, 2012 at 17:35
  • This is why your locally configured hostname should resolve to your actual IP address.
    – Anthony
    Dec 22, 2023 at 2:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .