Why the difference between network addresses reported by hostname and ping

dnshostnamenetworkingping

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.

Best Answer

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.

Related Question