Sub topic 7
Setting the Hostname and Keeping `/etc/hosts` Consistent
This chapter assumes that you can use a Linux shell, edit files, and run commands with sudo. It also assumes that you understand basic IPv4 addressing. The examples use the hostname web01.example.com and the address 192.168.1.50.
The hostname and the fully qualified domain name
A hostname identifies the machine to local software and other systems. A fully qualified domain name (FQDN) contains both the host name and its DNS domain. In this example:
web01is the short hostname.example.comis the domain.web01.example.comis the FQDN.
Applications use hostnames in logs, prompts, monitoring data, TLS certificates, and cluster membership. Set the hostname deliberately before putting the server into service.
The static hostname is stored in /etc/hostname. The running kernel also has a hostname. hostnamectl updates both values:
sudo hostnamectl set-hostname web01.example.com
hostnamectl --static
hostnamectlThe first command changes the configured hostname and the current hostname. The second command prints only the stored static value. The third displays additional information, including the operating system and kernel.
A hostname does not automatically create a DNS record. Other machines can resolve web01.example.com only if the name exists in DNS or in another name source, such as their own /etc/hosts files.
Keeping `/etc/hosts` consistent
The /etc/hosts file provides local name-to-address mappings. It is useful for the server's own name, bootstrap environments, and small networks without internal DNS.
A common entry is:
127.0.0.1 localhost
127.0.1.1 web01.example.com web01Debian-based systems commonly use 127.0.1.1 for the machine's local hostname. A server with a fixed address can instead map its names to that address:
127.0.0.1 localhost
192.168.1.50 web01.example.com web01Do not create conflicting entries for the same name. For example, mapping web01.example.com to both 127.0.1.1 and 192.168.1.50 can produce confusing results. Choose the convention that matches your distribution and network design.
You can inspect the result with:
hostnamectl --static
hostname
getent hosts web01.example.com
getent hosts web01getent asks the system's configured name service system rather than querying DNS directly. This makes it useful for checking what applications will normally see.
| Name | Typical purpose | Example |
|---|---|---|
| Short hostname | Local machine identity | `web01` |
| FQDN | Complete identity in a domain | `web01.example.com` |
| `/etc/hosts` entry | Local static name mapping | `192.168.1.50 web01.example.com web01` |
How local name resolution uses these settings
Most Linux programs use the Name Service Switch configuration in /etc/nsswitch.conf. Its hosts line determines the order of local files and DNS lookups. A typical line is:
hosts: files dnsWith this order, the resolver checks /etc/hosts first. If the name is not found there, it queries the configured DNS servers.
On systems using systemd-resolved, the line may contain resolve:
hosts: files resolve [!UNAVAIL=return] dnsThe exact line varies by distribution. Inspect it instead of assuming the order:
grep '^hosts:' /etc/nsswitch.confThe normal lookup path is:
Check local and DNS behavior separately when diagnosing a problem:
getent hosts web01.example.com
resolvectl query web01.example.comgetent follows the system name service order. resolvectl query tests the resolver service and its DNS configuration. They can produce different results when /etc/hosts, DNS, or nsswitch.conf is inconsistent.
Exercise
The server is named web01.example.com and has address 192.168.1.50. Which commands set the hostname, and which /etc/hosts entry gives the server both its FQDN and short name?
Show answer
Run sudo hostnamectl set-hostname web01.example.com. Add 192.168.1.50 web01.example.com web01 to /etc/hosts, then verify with hostnamectl --static and getent hosts web01.example.com.