Table of Contents
Understanding Hostnames on Linux
On a Linux system, the hostname is the human‑readable name that identifies a machine on a network. It’s used by DNS, monitoring systems, SSH, prompts, and logs.
There are three closely related concepts:
- Short hostname: e.g.
web01 - Domain name: e.g.
example.com - Fully Qualified Domain Name (FQDN): e.g.
web01.example.com
On a typical Linux system:
hostname(without options) usually shows the short hostnamehostname -forhostnamectl --staticmay show the FQDN, depending on configuration
Viewing the Current Hostname
Key commands:
- Show current hostname:
hostname- Show FQDN (if configured and resolvable):
hostname -f- Show details with systemd:
hostnamectl
If hostname -f fails or returns something unexpected, it often means the system’s hostname or name resolution configuration is incomplete.
Setting the Hostname (systemd-based systems)
Most modern distributions use systemd, so the recommended tool is hostnamectl.
- Set a static hostname (survives reboot):
sudo hostnamectl set-hostname web01.example.com
This sets the static hostname to web01.example.com. The short hostname will be web01.
- Optionally set “pretty” and “transient” hostnames:
sudo hostnamectl set-hostname "Web Server 01" --pretty
sudo hostnamectl set-hostname temp-host --transient- static: persistent, used by most tools
- pretty: purely cosmetic, may include spaces
- transient: lost on reboot (e.g. provided by DHCP)
On older non‑systemd systems, hostnames may be set in files such as /etc/hostname or /etc/sysconfig/network, but the exact details are distribution‑specific and usually covered in distro documentation.
Hostnames and `/etc/hostname`
On many distributions, the static hostname is stored in /etc/hostname as a single line:
web01.example.comTo change it manually:
- Edit the file:
sudo nano /etc/hostname- Put the desired hostname (short or FQDN) on a single line.
- Apply without reboot:
sudo hostname "$(cat /etc/hostname)"or simply reboot.
Whenever possible, prefer hostnamectl instead of editing files directly, as it updates all relevant locations.
Name Resolution on Linux
When a program needs to translate a hostname (like web01.example.com) to an IP address, Linux uses the name service switch configuration in /etc/nsswitch.conf. A typical line:
hosts: files dnsThis means:
- Look in local files (e.g.
/etc/hosts) - If not found, query DNS
Other sources (like mdns, ldap, wins) can be added, but in many setups files and dns are enough.
The `/etc/hosts` File
/etc/hosts provides a simple, static way to map hostnames to IP addresses without using DNS.
Example content:
127.0.0.1 localhost
127.0.1.1 web01 web01.example.com
192.0.2.10 db01 db01.example.com
203.0.113.5 backup backup.example.comKey points:
- Multiple names can map to the same IP on one line.
- Common pattern on desktops/servers:
127.0.0.1 localhost- A second line mapping the system’s hostname/FQDN to
127.0.1.1(Debian/Ubuntu style). /etc/hostsis checked before DNS whenhosts: files dnsis configured, so entries here override DNS responses.
Use /etc/hosts for:
- Small static networks
- Temporary overrides (testing new DNS mappings, migration, etc.)
- Ensuring local services can resolve the machine’s own hostname even without network
DNS Configuration Files
For DNS lookups, most systems use a resolver library which reads configuration from:
/etc/resolv.conf(traditional)- Or a local stub resolver (like
systemd-resolved) that is referenced by/etc/resolv.conf
`/etc/resolv.conf` Basics
A minimal resolv.conf might look like:
nameserver 1.1.1.1
nameserver 9.9.9.9
search example.com internal.example.comDirectives:
nameserver IP_ADDRESS- DNS servers the system should query
- Up to 3 listed, order matters
search domain1 domain2 ...- Domains appended when you query a short hostname
- If you run
ping web01, the resolver tries: web01.example.comweb01.internal.example.com- Stops when it finds a match
Many systems do not want you to edit /etc/resolv.conf directly, because it’s managed by:
NetworkManagersystemd-resolveddhclientor other DHCP clients
Edits may be overwritten when the network restarts. The correct way to set DNS servers often depends on your network manager (GUI, nmcli, netplan, or distro-specific tools).
systemd-resolved and Stub Resolvers
On distributions using systemd-resolved, /etc/resolv.conf may be a symlink to something like /run/systemd/resolve/stub-resolv.conf. In that case, configure DNS via:
systemd-resolve --status
sudo resolvectl status # newer syntax
And change settings via NetworkManager, netplan, or resolved configuration, instead of directly editing resolv.conf.
How DNS Names Map to IPs
DNS provides a hierarchical, distributed database mapping names to IP addresses and other information.
Common record types:
A— IPv4 addressAAAA— IPv6 addressCNAME— canonical name (alias)MX— mail exchangeNS— name serverPTR— reverse lookup (IP → hostname)
On a client system, you mainly care about:
- Resolving hostnames to IPs (
A,AAAA) - Occasionally seeing
CNAMEchains - Understanding reverse DNS (
PTR) for tools likesshor logging
Forward vs Reverse DNS
- Forward lookup: hostname → IP
web01.example.com→203.0.113.10(A record) - Reverse lookup: IP → hostname
203.0.113.10→web01.example.com(PTR record)
Reverse DNS is useful for:
- Logging (show hostnames instead of raw IPs)
- Some security checks and access controls
- Mail servers (many reject mail from IPs without proper reverse DNS)
Note that forward and reverse DNS are separate; having one doesn’t guarantee the other is configured.
DNS and Hostnames in Practice
Choosing Hostnames
Guidelines for hostnames (especially for servers):
- Use only letters, digits, and hyphens:
a–z,0–9,- - Avoid spaces, underscores, and special characters
- Start with a letter if possible
- Use a consistent scheme, e.g.:
web01,web02,db01,cache01app-prod-1,app-stg-1,app-dev-1- Include a meaningful domain:
web01.eu.example.com
For internal-only networks, you can use a private domain such as corp.example.com or another domain you control.
How a Typical Lookup Works
When you run:
ping web01The system roughly does:
- Check
/etc/nsswitch.confto seehostsorder. - If
filescomes first, search/etc/hostsforweb01. - If not found and
dnsis listed, applysearchdomains from/etc/resolv.conf: - Try
web01.example.com - Try
web01.internal.example.com - Query the configured
nameserveraddresses for each candidate until a match is found or all fail.
Tools like getent hosts web01 use the same NSS mechanism, so they’re good for debugging.
Testing and Debugging DNS/Hostname Problems
Common CLI tools:
pingorping6— quick test if a name resolves and host respondshost NAME— simple DNS querydig NAME— detailed DNS querynslookup NAME— older but still widely used
Examples:
- Check forward resolution:
host web01.example.com
dig web01.example.com- Check reverse resolution:
host 203.0.113.10
dig -x 203.0.113.10- See what IP your system uses for a name (through NSS, not just raw DNS):
getent hosts web01Common Issues
- Hostname not resolvable:
/etc/hostsmissing an entry for the local hostname- DNS zone misconfigured or missing records
- Wrong
searchdomain in/etc/resolv.conf hostname -fdoes not work:- The system’s hostname is not present in
/etc/hostsor DNS - The FQDN is not properly configured
/etc/resolv.confkeeps changing:- Managed by DHCP/NetworkManager/systemd-resolved
- Configure DNS through your network manager instead of editing directly
Integrating Hostnames with DNS
While running a full DNS server is covered elsewhere, from a client/admin perspective you should understand how hostnames and DNS relate:
- For a host
web01.example.comwith IP203.0.113.10you typically want: - On the server:
hostnamectl set-hostname web01.example.com/etc/hostshas at least:
127.0.0.1 localhost
203.0.113.10 web01.example.com web01- In DNS:
A(and/orAAAA) record:web01.example.com → 203.0.113.10- Optional
PTRrecord:203.0.113.10 → web01.example.com
This alignment ensures:
- Shell prompts and logs show the correct hostname
- Remote systems can resolve the name to an IP
- Reverse lookups return the expected FQDN
Summary
- A hostname identifies a machine; an FQDN includes both hostname and domain.
- Linux resolves names using
nsswitch.conf, combining/etc/hostsand DNS. /etc/hostnamestores the static hostname on many systems;hostnamectlis the preferred way to change it./etc/hostsprovides static name → IP mappings and can override DNS./etc/resolv.conf(or a stub resolver) configures which DNS servers and search domains the system uses.- Tools like
hostnamectl,host,dig, andgetentare essential for inspecting and troubleshooting DNS and hostname configuration.