How to set your hostname and domain name correctly

Published on 2012-01-18.

This tutorial describes how to set your hostname, domain name and fully qualified domain name properly. Both a mail server and a web server needs to run with the right hostname and domain name.

A mail server or a web server must have a fully qualified domain name such as www.example.com or mail.example.com.

Mail servers such as Postfix and Exim automatically uses the hostname that you assign to the server when greeting remote mail clients and servers, unless you manually configure another name.

I am going to use Debian GNU/Linux as a reference in this article, if you are running something else, you might find your files located elsewhere.

Hostname and Domain Name

On the Internet, the terms "hostname" and "domain name" are often used interchangeably and this can cause a bit of confusion. Sometimes the term "hostname" refers to the name you choose for your system (a nickname if you like). At other times the term "hostname" actually refers to the "domain name" of your system.

Hostnames are used differently by various naming systems, NIS, DNS, SMB, etc., and so the meaning of the word hostname will vary according to the naming system in question, which in turn varies by type of network. A hostname meaningful to a Microsoft NetBIOS workgroup may be an invalid Internet hostname. When presented with a hostname and no context, it is usually safe to assume that the network is the Internet and DNS is the hostname's naming system.

Host names are typically used in an administrative capacity and may appear in computer browser lists, active directory lists, IP address to hostname resolutions, email headers, etc. They are human-readable nicknames, which ultimately correspond to unique network hardware MAC addresses. In some cases the host name may contain embedded domain names and/or locations, non-dotted IP addresses, etc.

When we talk about the hostname as a nickname for your system, you can view your hostname by issuing the command:

$ hostname -s

The nickname is a name you choose. If for example you are running Linux on your laptop, you might call it "mylaptop". Or if you have two different computers at home and one of them is running Linux, you might call it "linux1".

When we talk about the hostname as a domain name it is then a name that identifies one or more IP addresses like wikipedia.com identifies the IP address 208.80.152.2.

The domain name consists of two parts and every domain name ends in a top-level domain (TLD) name, which is always either one of a small list of generic names (three or more characters), or a two-character territory code based on ISO-3166 (there are few exceptions and new codes are integrated case by case). Top-level domains are sometimes also called first-level domains. Examples: .com, .org, .net, etc.

The main purpose of a domain name is to provide a name to a numerically addressed Internet resources.

You can see your domain name by issuing the command:

$ hostname -d

Fully qualified domain name

The fully qualified domain name (FQDN) consists of two parts and it is a combination of the nickname of your computer and your domain name. A FQDN consists of three words and two dots such as mylaptop.example.com

You can see your FQDN by issuing the command:

$ hostname -f

Configure everything correctly

Let's say you want to name your server mybox, and that you have registered the domain example.com with the static IP address 208.77.188.166, then /etc/hosts needs to be configured as follows:

127.0.0.1 localhost.localdomain localhost
208.77.188.166 mybox.example.com

If your IP address is assigned to you by a DHCP server, then /etc/hosts is configured as follows:

127.0.0.1 mybox.example.com localhost.localdomain localhost mybox

Next, you need to set your FQDN in your /etc/hostname file. You can do that by running the following command:

# hostname mybox.example.com

Now the file /etc/hostname will contain the words mybox.example.com

With no arguments, hostname prints the name of the current host system. With one argument, it sets the current host name to the specified string.

Could not reliably determine the server's fully qualified domain name

If you just need to work locally on Apache or on your private LAN and doesn't own a domain, you can get rid of the above warning from Apache by fixing your ServerName. On Debian it is done in /etc/apache2/httpd.conf

ServerName "mybox"

A final note

RFCs mandate that a hostname's labels may contain only the ASCII letters 'a' through 'z' (case-insensitive), the digits '0' through '9', and the hyphen. Hostname labels cannot begin or end with a hyphen. No other symbols, punctuation characters, or blank spaces are permitted.