Sub topic 6
What happens on your machine first
Before any packet leaves your computer for a DNS server, your machine decides whether it needs to send one at all. This chapter covers that local stage: the application's lookup call, the hosts file, the stub resolver, and search domains. It assumes you know what an IP address is, that you can run commands in a terminal, and that you have seen the DNS hierarchy of root, TLD and authoritative servers from earlier chapters.
The application asks the operating system
Most programs do not speak DNS themselves. A program calls a library function, usually getaddrinfo, and passes a name and a port. The library returns a list of socket addresses to try.
import socket
results = socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM)
for family, socktype, proto, canonname, sockaddr in results:
print(family.name, sockaddr[0])On a host with both IPv4 and IPv6 connectivity, the output lists AF_INET6 and AF_INET entries. The program never learns whether an answer came from a file, a cache, or the network. The call hides all of that.
Some applications bypass this path. Browsers keep their own short-lived host cache, and some can send DNS over HTTPS directly. Tools like dig also skip the system lookup path, as shown later in this chapter.
The order of lookup sources
On Linux with glibc, the file /etc/nsswitch.conf controls which sources answer a name lookup and in what order.
hosts: files dnsfiles means the hosts file. dns means the DNS stub resolver. A line such as files mdns4_minimal [NOTFOUND=return] dns adds multicast DNS for .local names, and the [NOTFOUND=return] action stops the search there if mDNS says the name does not exist. macOS and Windows use different mechanisms, but the idea is the same: a fixed order of sources, with DNS usually last.
The hosts file
The hosts file is a plain text map from names to addresses. It is at /etc/hosts on Linux and macOS and at C:\Windows\System32\drivers\etc\hosts on Windows. Each line holds an address, a canonical name, and optional aliases.
127.0.0.1 localhost
::1 localhost
192.0.2.99 test.example.com testA match here wins over DNS, because files comes first. This is useful for local development and for testing a server before you change public DNS. It also causes confusing failures. If someone left an old entry in the file, the name will keep resolving to the old address while every DNS tool you run says something different.
Why dig can disagree with your browser
dig sends DNS queries to a resolver. It does not read the hosts file and does not use nsswitch.conf. getent hosts goes through the full system path. Comparing them is the quickest way to find out whether a local override exists.
getent hosts test.example.com
dig +short test.example.com AWith the hosts entry above, getent prints 192.0.2.99. dig prints whatever DNS says, which may be nothing. If ping or a browser behaves differently from dig, check the hosts file, the search domains, and the stub cache before you blame the DNS servers.
The stub resolver
The stub resolver is the small client piece of DNS on your machine. It does not walk the hierarchy. It sends one query to a recursive resolver with the RD (recursion desired) flag set and waits for a final answer. The recursive resolver does the work of contacting the root, TLD and authoritative servers, which is the subject of the next chapters.
On Linux, the stub's configuration is traditionally in /etc/resolv.conf.
nameserver 192.0.2.1
nameserver 192.0.2.2
search corp.example.net
options ndots:1 timeout:2 attempts:2nameserverlines give the recursive resolvers to ask. glibc uses at most three. It tries them in order and moves to the next one only after a timeout or failure.searchlists suffixes to try on short names.optionstunes behavior.timeoutis the seconds to wait for each server,attemptsis how many times to go through the server list, andndotsis covered below.
You rarely write these values by hand. DHCP or your network manager supplies the resolver addresses and the search domain when you join a network. A VPN client often replaces them, which is why internal names may start working or stop working when you connect.
Local caching stubs
glibc alone does not cache answers, so every lookup goes to the network. Many systems add a caching layer.
| Platform | Local cache | Inspect | Flush |
|---|---|---|---|
| Linux with systemd-resolved | systemd-resolved | `resolvectl statistics` | `resolvectl flush-caches` |
| Windows | DNS Client service | `ipconfig /displaydns` | `ipconfig /flushdns` |
| macOS | mDNSResponder | `dscacheutil -q host -a name example.com` | `sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder` |
With systemd-resolved, /etc/resolv.conf often contains nameserver 127.0.0.53. That address is a listener on your own machine, not a real DNS server. It receives the query, answers from its cache or forwards it, and applies per-interface rules such as "send corp.example.net to the VPN's resolver." Run resolvectl status to see the real upstream servers, and resolvectl query example.com to test a name through this path.
Search domains and ndots
Inside an organization you may type intranet instead of intranet.corp.example.net. The stub makes this work by appending the search suffixes from your configuration.
The ndots option decides the order. It is the minimum number of dots a name must contain to be tried as-is first.
- If the name has at least
ndotsdots, the stub tries it as written first. If that fails, it tries each search suffix in turn. - If the name has fewer dots, the stub tries the suffixes first and the bare name last.
- A name that ends with a dot, such as
example.com., is fully qualified. The stub never appends suffixes to it.
The default ndots is 1, so intranet gets the suffix treatment and example.com is tried as-is. Kubernetes pods commonly set ndots:5. With that value, api.example.com (two dots) is tried against every search suffix before the real name is asked, and each failed attempt is a wasted query. Writing api.example.com. with the trailing dot, or lowering ndots, removes those extra queries.
Each candidate name is usually queried for both A and AAAA records, so the number of packets grows quickly with a long search list.
Putting it together
When you type www.example.com in a program that uses the system resolver, the sequence is:
- The program calls
getaddrinfo. - The system checks the hosts file. On a match, the lookup ends.
- The system checks its local cache, if there is one. A fresh entry ends the lookup.
- The stub builds candidate names from the search list and
ndots. - The stub sends the query to the first configured recursive resolver, and retries or moves to the next on timeout.
- The answer is cached locally for its TTL and returned to the program.
If the stub gets a negative answer (NXDOMAIN) for one candidate, it moves to the next candidate. It stops only when a candidate resolves or the list runs out.
Exercise
A machine has search corp.example.net and options ndots:5 in its resolver configuration, and no hosts-file entry or cache entry for the name. A user runs ping api.example.com. In what order are names queried, and how would you make the stub ask for only the real name?
Show answer
The name has two dots, fewer than ndots (5), so the stub tries the search suffix first and queries api.example.com.corp.example.net. That query returns NXDOMAIN (unless a wildcard exists), and only then does the stub query api.example.com as written. Each candidate is normally asked for A and AAAA. Typing api.example.com. with a trailing dot marks the name as fully qualified, so no suffix is appended and only the real name is queried. Lowering ndots to 2 or less would also make the stub try the name as written first.
Exercise
You add the line 192.0.2.99 test.example.com to /etc/hosts. ping test.example.com goes to 192.0.2.99, but dig +short test.example.com prints a different address. Is something wrong with DNS?
Show answer
No. The system lookup path reads the hosts file first, because files precedes dns in the lookup order, so ping uses the override. dig sends DNS queries directly and never reads the hosts file, so it reports what DNS says. Use getent hosts test.example.com to see what applications see.