Setting Your System's Hostname

Every GNU/Linux system has a hostname, whether you like it or not. A system's hostname usually has a corresponding entry in the domain name system (DNS), but it doesn't necessarily have to. The hostname is the name that programs such as mail and web servers use to identify the system that they are running on. Many of these programs will issue warnings, or in some cases completely fail to run if the hostname is not set correctly.

If you have a system, it has a hostname. If you haven't set its hostname yourself, then one has likely been set for you. This is OK under some circumstances, but if you intend to use your GNU/Linux system for most server tasks (and most of them are servers, after all), you'll need to choose and assign one that is proper for your system.

Choosing A Name

Experienced system administrators use a lot of different schemes and conventions for choosing a hostname. Some take a structured approach, for example if the system is their third mail server, they might choose the name mail3, smtp3, or something along those lines. Others will pick a name based on a theme, for example at my workplace, there are systems named mercury, neptune, orion, posiden, triton, and venus, after classical gods. At one time, the Massachusettes Institute of Technology had all of its name servers named after breakfast cereals, for example wheaties.mit.edu, cheerios.mit.edu, and frootloops.mit.edu. Be creative, be utilitarian, be random... it's completely up to you. The only limitations are:

The only recommendations that I will make are that the hostname you choose should be unique among the systems that you're in charge of naming, and it should be easy to type, because you will type it a lot over the lifetime of the system

The hostname Command

Before we begin, let's see what your system's hostname is currently set to by running the hostname command. This is what the it prints on one system that I run, called dorothy:

$ hostname
dorothy

The name "dorothy" is my system's unqualified hostname. There is another form, called the fully-qualified domain name, or FQDN. The FQDN is your system's hostname combined with its domain, and you can use the hostname command to see the FQDN by giving it the -f option. On my system it prints this:

$ hostname -f
dorothy.movealong.org

From this example, you can see that the FQDN is just the unqualified hostname, dorothy, combined with the domain, movealong.org, separated by a dot. This is the fundamental form of all FQDNs.

Setting the Hostname Using hostname

You can also use the hostname command to set the hostname, either by specifying the hostname as a parameter, or by specifying the name of a file which contains the hostname. For example, you could issue this command to set a system's hostname to "toto" (you will have to issue it as root):

# hostname toto

The hostname command can also use the contents of a file to set the hostname.

# cat /etc/hostname
toto
# hostname -F /etc/hostname
# hostname
toto

In this sequence of commands, we first see that the file /etc/hostname contains the word 'toto' and nothing else. The second command reads that file and uses its contents to set the hostname, which we then see in the third command.

Configuring Your System's Hostname

There should be a script included in your GNU/Linux distribution which runs when your system boots and sets the hostname. Its exact location and operation varies from one distribution to another, but in all cases, it will use the hostname command in one of the two forms I've described to set the hostname.

Find this script, and study how it works. In particular, find out from where the script obtains the hostname that it sets. On many systems, the script uses hostname -F to read /etc/hostname, just like my second example. Once you have determined the mechanism that your distribution uses to set the hostname when the system starts up, you can permanently change the system's hostname by putting the new one at that location.

On my system (which uses the Debian distribution of GNU/Linux), this information is stored in /etc/hostname.

$ cat /etc/hostname
dorothy

From this example, you can see that on a Debian system, the file /etc/hostname contains the hostname and nothing more.

Your System's Domain

You may be wondering at this point how your GNU/Linux system - in particular, the hostname command - determines your system's domain. There are a whole host of subsystems that can go to work when the hostname command sets about determining your system's domain. In general, though, there are two sources of data that it might use. One is /etc/hosts, and the other is the Domain Name System (DNS).

Once a system's unqualified hostname is set, it is stored in the system's running kernel. It is normally set when the system boots, so for most purposes, it is always available via a simple system call. When you use the hostname command to get the FQDN, it uses this system call to retrieve the unqualifed hostname, and then uses a facility called the resolver to resolve the unqualified hostname into a fully-qualified domain name (FQDN). It is the resolver that then tries the different data sources that are at its disposal.

The Static hosts Database

/etc/hosts is a simple text database of addresses and names. Each line in this file starts with an IP address, which is followed by one or more names. The first name listed is the FQDN associated with the address, and the rest are aliases. The first component of the FQDN should always be the unqualified hostname. On my system, /etc/hosts contains this line:

69.56.173.86	dorothy.movealong.org dorothy

The hostname command first asks the resolver for the address of 'dorothy', the unqualified hostname, and the response is '69.56.173.86'. The hostname command then asks the resolver for the name of '69.56.173.86', and the response is 'dorothy.movealong.org'. This is how the hostname command is able to determine the FQDN on my system. All of the information that the resolver needs to answer these two requests is contained in the /etc/hosts file.

Other Databases

What if the information is not in /etc/hosts? There are some very good reasons why the information might not be in that file. For example, the system might have a dynamic IP address, and so it would have to somehow update the /etc/hosts file every time the address changes. In some cases, this is exactly what happens. Many DHCP clients can update the file if they are configured to do so, but there are better options. One of them is to forego the /etc/hosts file and have the resolver get the information from the DNS. This method will allow the resolver to determine the FQDN of your system only if two conditions are met. First, that the resolver is configured to search for unqualified names by appending a specific domain, and second, that the FQDN of your system is present in that domain. However, configuring these services is beyond the scope of this HOWTO.