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
. Thehostname
command will return 'fred'. The commandping -c 1 fred
will perform a DNS lookup offred
(either justfred
orfred
fully qualified such asfred.domain.com
if default domain isdomain.com
). Assume that DNS returns IP addressx.x.x.x
.ping
will then perform a reverse DNS lookup of IP addressx.x.x.x
, if no name is returnedping
will output the IP addressx.x.x.x
, otherwiseping
will output whatever named was returned from the reverse lookup which could be a different name such asethel.domain.com
.