Hostname
Introduction
In Unix and Unix-like operating systems, a hostname is the label assigned to a system to identifies it. It is the system’s own name and is an identifier that the kernel and user processes use to distinguish the system from others. The hostname is set during installation and stored in one or more configuration files (such as /etc/hostname) so that it persists across reboots.
The hostname is primarily a local identifier. It can be as short as a single word, such as apollo, workstation01 or moffat, and does not need to be tied to a global naming system. On most Unix systems, the hostname can be viewed or changed with the hostname command, and it is commonly displayed in the shell prompt to help users keep track of which machine they are working on.
FQDN
The Fully-Qualified Domain Name (FQDN) was originally the name of the system within the global Domain Name System (DNS).
Unfortunately, many pieces of documentation and software written with an assumption that a system has a unique FQDN associated with a unique IP address, but this is no longer true - a system may be associated with one, none or several names in the DNS, and it may have hundreds of IP addresses or none at all.
In practice, many administrators set a system's FQDN based on a short hostname and a domain. For example, a system may be given moffat as hostname (based on some local policy) and an FQDN moffat.example.com, where example.com is a domain owned by the administrator (or their employer). This can be done regardless of whether moffat.example.com is registered in the global DNS or not, and there is no need for the hostname and the first part of the FQDN before the first dot to be related like this: administrators may configure the hostname and FQDN however they like (for example, a hostname can include dots, so moffat.example.com is a valid hostname). This distinction is important: a system always has a hostname set by its operating system, whereas an FQDN is, technically, a construct of the DNS hierarchy.
To avoid confusion with the FQDN, the UNIX host name is commonly referred to as the short hostname.
In Debian
In Debian, the short hostname is persistently stored in /etc/hostname (see hostname(5)) and /etc/hosts is used to store the FQDN and to ensure that software can quickly convert between the either type of hostname and an IP address.
The Init system sets the hostname to the contents of /etc/hostname when the system boots. For example systemd uses hostnamectl(1) to do this.
The FQDN of the system is set by the user at installation and stored in /etc/hosts. The format of /etc/hosts depend on whether the system was installed had a static or dynamic (i.e., set by DHCP) IP address.
On a system that has a static IP address, /etc/hosts is initialized with
IPADDRESS FQDN shorthostname
for example, for a system with FQDN apollo.example.com and a static IP 2001:db8:1:2::100 you would get
2001:db8:1:2::100 apollo.example.com apollo
which ensures both the system can associate the fixed IP address to either the short hostname or FQDN without needing to contact a, possibly slow, DNS server.
If the initial network configuration was using DHCP, the IPv4 address 127.0.1.1 (which is part of a block of IP addresses reserved for referring to the local machine) substitutes the fixed IP address in /etc/hosts:
127.0.1.1 FQDN shorthostname
for example, for apollo.example.com as
127.0.1.1 apollo.example.com apollo
In both cases /etc/hosts also contains an entry to associate 127.0.0.1 with localhost.
Why 127.0.1.1 and not 127.0.0.1?
From https://lists.debian.org/debian-devel/2025/10/msg00146.html:
Even though most people associate 127.0.0.1 with the loopback interface, the whole 127.0.0.0/8 block is actually reserved for that purpose, and *any* address in that block loops back to your own host (just for fun, try "ssh 127.234.56.78" if you have an SSH server running on your computer).
127.0.1.1 is not special, it merely happens to be an address that:
- maps to your own host, and
- has no other name assigned to it which would conflict in a reverse lookup (127.0.0.1 is mapped to "localhost").
The usual "trick" to get your own domain name is via hostname canonicalization, i.e, you run gethostbyname("apollo"), which returns 127.0.1.1, and afterwards gethostbyaddr(127.0.1.1), which returns "apollo.example.com". The 127.0.1.1 entry in /etc/hosts makes this work and still lets the software on your computer behave exactly as if your computer had a static IP with a proper DNS entry, regardless of your actual network setup.</p>
libnss-myhostname
Lately, libnss-myhostname was introduced to make the /etc/hosts entry unnecessary by putting a dedicated library into the resolver via /etc/nsswitch.conf. This is causing some troubles when running mail systems (2025-09), and is currently being investigated. If you happen to know more about this, please share your knowledge here.
