Sub topic 8
How the system turns a name into an address
This chapter explains what happens between an application asking for web01.example.com and the kernel getting an IP address to connect to. It assumes you can read basic shell output, know what an IPv4 address and a DNS server are, and have already seen the commands from the inspection chapter (ip, hostnamectl, resolvectl). Understanding this path explains most "DNS is broken" reports, many of which have nothing to do with DNS.
The lookup is a library call, not a DNS query
Applications do not talk to DNS servers directly. A program such as curl, ssh, or nginx calls a C library function, usually getaddrinfo() (the older name is gethostbyname()). That function belongs to the C library (glibc on most distributions). The library decides where to look and in what order.
This matters in practice. A tool that uses the library, such as ping, curl, or ssh, follows the system's full lookup rules. A tool that talks to DNS on its own, such as dig or nslookup, skips the hosts file and asks a DNS server directly. When the two disagree, the cause is usually that one source (like /etc/hosts) answers first.
Step 1: nsswitch.conf sets the order
The file /etc/nsswitch.conf is the Name Service Switch configuration. Its hosts: line lists the sources for host names, in the order they are tried. Look at yours:
grep '^hosts:' /etc/nsswitch.confTypical results:
| Distribution style | Typical `hosts:` line |
|---|---|
| Ubuntu with systemd-resolved | `hosts: files mdns4_minimal [NOTFOUND=return] dns` |
| Debian | `hosts: files mdns4_minimal [NOTFOUND=return] dns` or `files dns` |
| RHEL family with systemd-resolved | `hosts: files resolve [!UNAVAIL=return] myhostname dns` |
| RHEL family without it | `hosts: files dns myhostname` |
Each word is a source module:
filesreads/etc/hosts.dnssends a classic DNS query using the servers in/etc/resolv.conf.resolveaskssystemd-resolvedthrough its own interface.myhostnameanswers for the machine's own hostname and forlocalhost, even if/etc/hostshas no entry.mdns4_minimalhandles.localnames with multicast DNS.
Bracketed items such as [NOTFOUND=return] are actions. They say what to do after a source reports a status. [NOTFOUND=return] means "if this source says the name does not exist, stop here and do not try later sources." This is how a .local name can fail quickly without ever reaching DNS.
The order is the key point. Because files normally comes first, an entry in /etc/hosts overrides DNS for that machine, even if the DNS record is different or missing.
Step 2: /etc/hosts is a local table
The hosts file is a plain-text list: one address, then one or more names. The first name is the canonical name and the rest are aliases.
127.0.0.1 localhost
127.0.1.1 web01.example.com web01
192.168.1.50 web01.example.com web01Rules to remember:
- Matching is exact and is done on whole names.
web01andweb01.example.commatch only if listed. - The first matching line wins for a forward lookup.
- A reverse lookup (address to name) returns the first name on the matching line, so put the fully qualified name first.
- Changes take effect immediately. No service needs a restart, because
filesreads the file on each lookup.
The Debian-style 127.0.1.1 line exists so that the machine's own name resolves even when it has no fixed address. On a server with a static address, mapping the name to that address is cleaner, because some software binds or reports the address that the hostname resolves to. If the name maps to 127.0.1.1, a service may advertise a loopback address to other hosts.
A common mistake is to change the hostname with hostnamectl and forget the hosts file. Programs that look up their own name (for example sudo, mail servers, and some cluster tools) then print warnings such as sudo: unable to resolve host web01 or pause for several seconds while a DNS query times out.
Step 3: the resolver and /etc/resolv.conf
When files has no answer, the library moves on to DNS. The file /etc/resolv.conf tells the C library's stub resolver which DNS servers to use. It has three common directives:
nameservergives a DNS server address. Up to three are used, tried in order.searchlists domains to append to short names.optionstunes behavior, such astimeout:2andattempts:2.
On a server running systemd-resolved, /etc/resolv.conf is usually a symbolic link, and its content is generated rather than edited by hand. Check:
ls -l /etc/resolv.conf
cat /etc/resolv.conf
resolvectl statusThree arrangements are common:
| `/etc/resolv.conf` is... | What it means |
|---|---|
| A link to `/run/systemd/resolve/stub-resolv.conf` | The file lists `nameserver 127.0.0.53`. Applications query a local stub that forwards to the real servers. This is the Ubuntu default. |
| A link to `/run/systemd/resolve/resolv.conf` | The file lists the real upstream servers directly. `systemd-resolved` still runs but is bypassed by libraries reading the file. |
| A regular file | A tool such as NetworkManager, a DHCP client script, or a person wrote it. Manual edits last only until that tool rewrites it. |
Because the file may be generated, editing it by hand is a common source of "my change disappeared after a reboot." Set DNS servers in the network configuration (Netplan or NetworkManager), which is covered in the next chapters, and let the tool produce the file.
Search domains and short names
A search domain lets you type a short name. With search example.com in place, ping db01 is tried as db01.example.com. The rules are:
- If a name contains fewer dots than the
ndotsoption (default 1), the resolver tries the search domains first, then the name as typed. - If it has at least
ndotsdots, it tries the name as typed first, then the search domains. - A name that ends in a dot, such as
db01.example.com., is absolute and never gets a suffix.
So db01 goes through the search list, while www.example.org is tried as typed first. Search domains cost extra queries for every miss, so keep the list short.
Step 4: systemd-resolved adds its own logic
When systemd-resolved is active, it does more than forward queries. It keeps DNS servers per network interface, caches answers, and can send a query only to the interfaces whose domains match. You can see this in resolvectl status, where each link lists its own DNS Servers and DNS Domain values.
Two features are worth knowing:
- Routing domains. A domain written with a leading tilde, such as
~corp.example.com, sends matching queries to that interface's servers and is not added as a search suffix. A plain domain does both. - Caching. Repeated lookups are answered locally.
resolvectl statisticsshows cache hits, andresolvectl flush-cachesclears them.
The two tools below look similar but use different paths, which is useful when diagnosing:
resolvectl query example.com
getent hosts example.comresolvectl query talks to systemd-resolved directly and reports which interface and server answered. getent hosts goes through nsswitch.conf exactly like an application, so it includes /etc/hosts. If resolvectl query works and getent hosts fails, look at nsswitch.conf. If getent hosts works only for names in /etc/hosts, DNS is the problem.
Seeing the order work
This short experiment shows that files wins over DNS. Pick a name that you know resolves in DNS, add a fake entry, and compare the tools.
getent hosts example.com
echo '203.0.113.99 example.com' | sudo tee -a /etc/hosts
getent hosts example.com
resolvectl query example.comThe second getent prints 203.0.113.99, because the hosts file answered first. resolvectl query still prints the real DNS answer. Remove the test line afterward:
sudo sed -i '/^203\.0\.113\.99 example\.com$/d' /etc/hosts
getent hosts example.comThe address 203.0.113.99 is in a range reserved for documentation, so it never belongs to a real host.
Common failure patterns
Match the symptom to the layer before changing anything.
| Symptom | Likely layer | First check |
|---|---|---|
| `ping 9.9.9.9` works, `ping example.com` fails | Resolver or DNS server | `resolvectl status`, `ls -l /etc/resolv.conf` |
| `dig example.com` works, `ping example.com` fails | `nsswitch.conf` or missing resolver module | `grep '^hosts:' /etc/nsswitch.conf` |
| `ping example.com` goes to the wrong address | Stale `/etc/hosts` entry or cache | `getent hosts example.com`, `resolvectl flush-caches` |
| Short name fails, full name works | Search domain missing | `resolvectl status` and look at `DNS Domain` |
| Each command pauses several seconds, then works | First DNS server is down, or own hostname missing from hosts | `resolvectl query`, check `/etc/hosts` |
| `/etc/resolv.conf` loses edits | File is generated | `ls -l /etc/resolv.conf` |
Exercise
A server has hosts: files dns in /etc/nsswitch.conf. /etc/hosts contains 10.0.0.5 app.example.com, but DNS says app.example.com is 10.0.0.9. Running dig app.example.com shows 10.0.0.9, yet curl http://app.example.com connects to 10.0.0.5. Why, and how do you check what applications will see?
Show answer
dig queries a DNS server directly and ignores /etc/hosts. curl calls the C library, which follows nsswitch.conf. The files source is listed first, so /etc/hosts answers with 10.0.0.5 and DNS is never asked. Run getent hosts app.example.com to see the answer applications get. To fix it, remove or correct the hosts entry.
Exercise
After you change a server's hostname to web01.example.com with hostnamectl, sudo prints unable to resolve host and pauses. What file do you check, and what line would you add if the server's static address is 192.168.1.50?
Show answer
Check /etc/hosts. The new name has no entry, so the library falls through to DNS, which has no answer or times out. Add the line 192.168.1.50 web01.example.com web01. The lookup then succeeds from files immediately.
Exercise
/etc/resolv.conf on a server shows nameserver 127.0.0.53, and your manual edit of it disappears after a reboot. Where should the DNS servers be set instead, and which command shows the servers actually in use?
Show answer
Set them in the network configuration that manages the interface, such as the nameservers section of the Netplan file or ipv4.dns in the NetworkManager connection. systemd-resolved receives them and the file is regenerated. Run resolvectl status to see the servers for each interface.