Hostname and FQDN in Ubuntu

Nametag

Hostnames are important; I try to taken the time to give my hosts sensible names and making sure they are correctly configured in the DNS, both forward and reverse, with working RCrDNS (forward confirmed reverse DNS). What I’ve never really given much thought is what name I tell my machines they have. When you install (Ubuntu on) a new machine, it will ask you for a hostname. I will normally just enter the FQDN (fully qualified domain name) at that prompt. Looking into a new light-weight Exim configuration recently gave me a reason to research this further. Turns out there are quite a few ways of configuring the hostname of an Ubuntu server. I’m going to document the correct way of setting the hostname according to the hostname(1) man-page, which states the following:

The  recommended  method  of  setting  the  FQDN is to make the hostname be an alias for the fully qualified name using /etc/hosts, DNS, or NIS.

In practical terms, this means that an Ubuntu server with a FQDN of www.example.com and IP 172.17.2.1 should have an /etc/hostname containing just:

www

And a line in /etc/hosts containing:

172.17.2.1 www.example.com www

This will result in the following output from the hostname(1) command:

www:~$ hostname
www
www:~$ hostname --fqdn
www.example.com

With such a setup, any sensible software should be able to figure out the FQDN of the host on which it is running!