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 movalong.org 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 66.103.254.221
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 xbeta.org
while
syd
uses a dynamic DNS provider at, say, no-ip.org
.
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:
hostname
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.
Debian has an init script,
/etc/init.d/hostname.sh
, which is called at boot time and sets the hostname what is given in/etc/hostname
. To change the hostname, place only the unqualified hostname (e.g.,gauss
) in/etc/hostname
and runsudo /etc/init.d/hostname.sh
.Ubuntu, like Debian, also uses
/etc/hostname
, but to update the hostname after making a change, runsudo service hostname start
.Slackware uses
/etc/HOSTNAME
. Set the appropriate hostname there and runhostname -F /etc/HOSTNAME
as root.Red Hat systems look for a line like
HOSTNAME=gauss
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
gauss.xbeta.org
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 66.103.254.221
. Finally, it asks for the full hostname and domain
that corresponds to 66.103.254.221
: gauss.xbeta.org
.
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
127.0.0.1 localhost
66.103.254.221 gauss.xbeta.org gauss
Note that “static” here means either a live IP address and or a statically assigned IP address on your LAN, such as 192.168.1.2.
For syd
, which has no permanent IP address, we have
127.0.0.1 syd.no-ip.org 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
gauss
% 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.
Mailname
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:
It appears that Mutt reads the mailname from
/etc/mailname
when, for example, setting theMessage-ID
header.Other programs, such as Gnus, seem to get this information from the same source as tools such as
hostname
(ultimately, it seems, by determining the name forlocalhost
and making a call togethostbyname
).On Debian, to configure the
exim4
MTA runsudo dpkg-reconfigure exim4-config
.
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.
-
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. ↩