On RHEL and derivatives like CentOS, you need to edit two files to change the hostname.
The system sets its hostname at bootup based on the HOSTNAME
line in /etc/sysconfig/network
. The nano
text editor is installed by default on RHEL and its derivatives, and its usage is self-evident:
# nano /etc/sysconfig/network
You also have to change the name in the /etc/hosts
file. If you do not, certain commands will suddenly start taking longer to run. They are trying to find the local host IP from the hostname, and without an entry in /etc/hosts
, it has to go through the full network name lookup process before it can move on. Depending on your DNS setup, this can mean delays of a minute or so!
Having changed those two files, you can either run the hostname
command to change the run-time copy of the hostname (which again, was set from /etc/sysconfig/network
) or just reboot.
Ubuntu differs in that the static copy of the hostname is stored in /etc/hostname
. For that matter, many aspects of network configuration are stored in different places and with different file formats on Ubuntu as compared to RHEL.
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";
Best Answer
(copied from one of my answers on SF)
The
hostname
command returns results from DNS and/etc/hosts
.hostname
is equivilant touname -n
and is the actual "hostname" or "nodename" of the box.All the other
hostname
arguments use this nodename to look up info.So before going any further, I should explain the
/etc/hosts
file format.The first field is fairly obvious, its the IP address all the hostnames on the line should resolve to. The second field is the primary hostname for that IP. The remaining fields are aliases.
So if you run
hostname -f
it will first try to resolve the IP for your nodename. Depending on how you have thehosts:
entry configured in/etc/nsswitch.conf
this method will vary./etc/resolv.conf
until it gets an IP back from DNS./etc/hosts
to find a line where either the primary hostname or the alias name is your current nodename (uname -n
), and then return the IP address in that line.Once it has the IP it will then try a reverse lookup on that IP. Again it will use DNS for this and your hosts file based on your
nsswitch.conf
. In the case of using your hosts file, it will return the primary entry (which is the first field after the IP in the file).hostname -a
will only work with the hosts file since doing a reverse lookup in DNS only gives you 1 result. With the hosts file it return the alises in the matching line (which is everything after the first entry, the primary hostname).So in short, the likely reason for your issue is that you have no entry in
/etc/hosts
that contains your hostname (uname -n
).Examples
If your nodename is 'foobar', and you have an entry in
/etc/hosts
such as this:Then you will get the following command results: