Setting hostname properly in Fedora and Red Hat

I have been playing with hostnames yesterday and I had to stop for a while today doing a little research. Setting up a hostname is not a hard task, in Fedora and Red Hat it's a matter of editing one file:

# grep HOSTNAME /etc/sysconfig/network

Here you set the whole FQDN. If you ever seen entries like DOMAINNAME in this file, they are wrong for Fedora or RHEL. If you want to change hostname without rebooting few steps are necessary:

# hostname test.lan
# service network restart

I skipped one simple, yet important step - setting /etc/hosts. It differs for situations when you use DHCP or not.

Clients with DHCP

With properly configured DHCP server, hostname should be propagated or defined by network administrator, there is no need of adding hostname to the /etc/hosts. For clients, it is really not necessary. Trust me. Keep only localhost there, walk your dog, enjoy life.

Servers with static IP

This is totally different scenario - server with static IP address must have entry in the /etc/hosts file in the following format (assuming a random class C address):

# grep test /etc/hosts test.lan test

Please note the order matters. You must keep the following order: ipaddress fqdn hostname. I recommend to do a check-up after adding entries:

# hostname 
# hostname -s
# hostname -f

With the incorrect order you will see hostname -f returning non-fully qualified response:

# grep test /etc/hosts test test.lan # incorrect order
# hostname -f

The key for this behavior is in the manual page of hostname command:

The function gethostname(2) is used to get the hostname. When the hostname -a, -d, -f or -i is called will gethostbyname(3) be called. The difference in gethostname(2) and gethostbyname(3) is that gethostbyname(3) is network aware, so it consults /etc/nsswitch.conf and /etc/host.conf to decide whether to read information in /etc/sysconfig/network or /etc/hosts.

Servers with DHCP

Wait a minute! A server without static IP address? Does it make sense? Well, in the cloud it is not unusual to provision servers configured with DHCP. But cloud providers usually allow customers to reserve IPs for a while so it does not change. If this is your case, I recommend to stick with the servers with static IP rule. If the IP address changes rapidly, I would recomment clients with DHCP rule, but make sure hostname command returns correct answers for both -s and -f options, as seen above.

In the latter case, contact your cloud provider asking how to set up hostname properly. Sometimes in testing environments servers with IPs delivered by DHCP servers do not have proper DNS setup. And you can have a service that insists on having hostname resolved. In this case, only in this case, I would recommend adding loop back hostname entry:

# grep test /etc/hosts test.lan test

Again, keep the right order and make sure it does not mess up services on that box. Be prepared to see "Cannot determine hostname" warning when starting Apache for example.

07 December 2011 | hostname | fedora | rhel