Linux Hostname Configuration

March 5, 2008

It sounds crazy, but setting or changing the hostname of a Linux system can be an extremely confusing process. There is a lot of misinformation on various weblogs, mailing lists, and forums that needs to be avoided.1 The primary source of confusion seems to be the distinction between the hostname and the domain name which are set and determined in different ways, not simultaneously.

The kernel maintains the current hostname which is set (in a distribution-specific way) at boot time and can be changed on the fly. The domain name is determined by the resolver system, usually from the static hosts database (/etc/hosts) or via DNS. For further reading, the most coherent sources of information I have found are the Hostname HOWTO at and Chapters 3 and 5 of the Debian Reference.

To be concrete, I will describe how to setup a server called gauss which has a “live” static IP and a laptop called syd with an ever-changing static IP address assigned via DHCP by various wireless access points. gauss belongs to the domain while syd uses a dynamic DNS provider at, say,

The System Hostname

The kernel maintains the current (unqualified) hostname of the system. Before trying to set your hostname, it’s important to know how to tell when you’ve got it right. Here are a few of the numerous ways to obtain it:

uname -n
cat /proc/sys/kernel/hostname
sysctl kernel.hostname

The hostname can be changed temporarily by running

hostname gauss

as root. This tells the kernel to use the hostname gauss until told otherwise.

Each distribution has a script that runs at boot time which sets the hostname. The way to change the hostname permanently thus differs by distribution.

in /etc/sysconfig/network. To make the change immediate you also need to run hostname gauss as root or simply reboot.

The Fully Qualified Domain Name (FQDN)

The domain name is distinct from the hostname and is determined by the resolver subsystem. Putting together the hostname and the domain name yields the fully qualified domain name (FQDN) of the system. The current FQDN of the system can be found as follows:

% hostname -f

To obtain this information the system first finds asks for the hostname: gauss. Then it asks for the IP address of gauss, which in my case is Finally, it asks for the full hostname and domain that corresponds to

The hostname(1) manpage states that the hostname -f command should

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.

So in most cases all of this information is stored in the /etc/hosts file. As described in the hosts(5) manpage:

This file is a simple text file that associates IP addresses with hostnames, one line per IP address. For each host a single line should be present with the following information:

IP_address canonical_hostname [aliases...]

Thus, to set the static IP for gauss the /etc/hosts file contains localhost gauss

Note that “static” here means either a live IP address and or a statically assigned IP address on your LAN, such as

For syd, which has no permanent IP address, we have syd localhost

A related command, hostname -a, will return a list of aliases for the hostname. The result of this command depends on the configuration in /etc/hosts.

% hostname -a

% hostname
syd localhost

As far as the system is concerned, the effects of changing /etc/hosts are immediate. Some applications such as Firefox seem to cache this information and may need to be restarted.

Problems with X

Apparently one’s X server is bound to a particular hostname so if you change the hostname, you can no longer connect to it. That means you will get error messages like the following when running an application:

Xlib: connection to ":0.0" refused by server
Xlib: No protocol specified

and you might see something like the following in /var/log/Xorg.0.log:

AUDIT: Sun Mar  2 11:42:28 2008: 5488 X: client 24 rejected from local host (uid 1000)

I do not know of a solution to this other than restarting X by either logging out or rebooting.


That there is a related name, the mailname, which is the name that appears after the @ in mail sent from the system. Apparently setting the mailname is just as confusing as setting the hostname since each email client behaves differently. As such, I will only note the following:

According to mailname(5), the /etc/mailname file should contain the “the visible mail name of the system” and is usually used by “programs that wish to send or relay mail, and need to know the name of the system.” More specifically:

The file contains only one line describing the fully qualified domain name that the program wishing to get the mail name should use (that is, everything after the @).

See also Section 6.3.3 of the Debian Reference.

  1. I don’t want to be guilty of the same so please let me know if any of the information here is unclear or incorrect.