What is the difference between hostname, hostname --fqdn, and hostname -A
hostname
returns the configured hostname or nodename. In practice, it can either be a short name (in most configurations) or a long name (normally the FQDN in this case). The short name is given by hostname --short
.
hostname --fqdn
returns the FQDN, which is gethostbyname
on the nodename (as returned by the uname
system call, see uname(2)
man page).
hostname -A
is something obscure and non-intuitive. In particular, despite its name and description ("all FQDNs"), it doesn't give the standard FQDN, by design. Thus I would say: do not use it. One reason is that it misses valid IP addresses of the machine, such as 127.0.1.1, with which the FQDN may be associated in the /etc/hosts
file (this is currently the default under Debian and Ubuntu, for instance). Another issue with the hostname -A
method is that the reverse resolution of an IP address doesn't necessarily give a FQDN; it can just be a short name.
Concerning your problem with python, it may be a bug there. I don't know. I suggest that you try the following Perl script:
#!/usr/bin/env perl
use strict;
use POSIX;
my $nodename = (POSIX::uname)[1];
print "Nodename: $nodename\n";
my @ghbn = gethostbyname $nodename;
print "FQDN: $ghbn[0]\n";
$ghbn[0] !~ /\./ && $ghbn[1] =~ /(\S+\.\S+)/
and print "Fixed FQDN from aliases: $1\n";
Related videos on Youtube
Comments
-
Ketan over 1 year
I have a machine I changed the hostname of. I changed it in
/etc/hosts
,/etc/sysconfig/network
, and withhostname
command.hostname
returns the correct shortname,hostname --fqdn
returns the expected FQDN, buthostname -A
returns an old name. I changed the IP address of the machine to make sure it wasn't external DNS cache.If I open python and run
import socket print(socket.gethostname()) print(socket.getfqdn())
Both return the old hostname/fqdn, the same as
hostname -A
From the hostname man page it appears
--fqdn
just takes the shortname and domain and puts them together.Display the FQDN (Fully Qualified Domain Name). A FQDN consists of a short host name and the DNS domain name. Unless you are using bind or NIS for host lookups you can change the FQDN and the DNS domain name (which is part of the FQDN) in the /etc/hosts file.
What is the difference between these commands and how can I better troubleshoot why the old hostname is sticking around.
-
Ketan almost 10 yearsRed Hat Enterprise Linux 6.4
-
-
Ketan almost 10 yearsSo where does hostname -A get its information? I was having a problem with other software installed on the system that I couldn't fix until hostname -A showed the correct name.
-
Ketan almost 10 yearsps, I tried to fix your 127.0.1.1 address to 127.0.0.1 but edits require 6 characters minimum. Maybe you can fix it.
-
vinc17 almost 10 years@Rothgar
hostname -A
looks at the configured interfaces and reverse-resolves them (well, this is the information given in its man page). 127.0.1.1 is the right IP address in my answer, not 127.0.0.1. The fact that 127.0.1.1 is used for the FQDN (instead of 127.0.0.1) is one cause of the problem withhostname -A
. -
vinc17 almost 10 years@Gnouc No,
hostname -A
does not use DNS lookup: I can check that on my machine where I getxvii
while I don't have any DNS set-up. It appears that it just uses standard reverse lookup, similar togetent hosts
, and for theeth0
IP address,getent hosts 192.168.1.2
givesxvii
, which agrees withhostname -A
. Butxvii
is just a short name, not a FQDN. -
cuonglm almost 10 years@vinc17: run
strace hostname -A
to see what happen. -
vinc17 almost 10 years@Gnouc Here
strace
gives very little information. It's better to look at the source code. And forALL_FQDNS
, I can see that thegetnameinfo
function is used, i.e. standard resolution, which takes/etc/nsswitch.conf
into account, not a direct DNS resolution like withhost
ordig
. -
cuonglm almost 10 years@vinc17: Can you give me the source you use? Remember
hostname
inRedhat/Centos
is different withDebian
. Trystrace -e trace=connect,sendto,recvfrom,write hostname -A
, you can see it contact your DNS server. -
vinc17 almost 10 yearsLet us continue this discussion in chat.