DNS and name resolution
Trace a name from the application's lookup to an authoritative answer, and find where it goes wrong.
Two halves: the client library and the DNS system
A lookup begins inside the application, in the stub resolver provided by the C library or the language runtime. It does not just "ask DNS". It follows /etc/nsswitch.conf, which typically says to check files (that is, /etc/hosts) and then dns. For DNS it reads /etc/resolv.conf for nameservers, search domains, and options. Only then does a query go to a recursive resolver. The recursive resolver walks the hierarchy (root, then .example, then northstar.example) to an authoritative server and caches the answer for its TTL.
A failure can sit in either half, and the tools differ:
getent hosts namefollows the same path as most applications, including/etc/hostsand search domains.dig namesends a DNS query directly and bypassesnsswitchand/etc/hosts.dig @server nameasks one specific server.dig +trace namewalks the delegation yourself.
When dig works and the application does not, the difference is in the client half. When both fail, follow the server half.
Search domains and ndots
search corp.example lets people type short names. ndots:N says that names with fewer than N dots are first tried with each search domain appended. With Kubernetes-style ndots:5, a lookup for api.northstar.example first tries api.northstar.example.corp.example. That costs extra queries. If someone created a matching record in the search domain, the lookup returns the wrong address. A trailing dot (api.northstar.example.) marks a name absolute.
Caching and TTL
Every answer carries a TTL. Recursive resolvers, local caches such as systemd-resolved, and some applications hold answers that long, and some runtimes hold them longer. Two operational rules follow. Plan changes around TTL: lower it ahead of a migration, wait out the old TTL, switch, and then restore it. Negative answers are cached too: an NXDOMAIN returned during a misconfiguration persists for the zone's negative-caching time even after you fix the record.
Containers bring their own resolver
Container runtimes write a resolv.conf into each container. Docker on user-defined networks points it at an embedded DNS server that resolves other containers by service name. Kubernetes points it at the cluster DNS service with search domains for the namespace. Two classic failures follow. A container cannot reach the nameserver it was given. Or a program uses localhost to reach a peer that is actually another container, a name problem disguised as a network one. The Containers in Production track returns to the second.
A resolution checklist
- What exactly does the application report, and with which name?
getent hosts <name>on the failing system: same failure?cat /etc/resolv.conf: which servers, search domains, and options?dig @<that server> <name>: does the configured server answer?dig +trace <name>or the authoritative server: is the record itself right?- If the record changed recently, which TTL is still running?
Key terms
- Stub resolver
- The resolver library inside each program. It reads
/etc/nsswitch.conf,/etc/hosts, and/etc/resolv.conf, and asks a recursive server. - Recursive resolver
- A server that answers clients by querying the hierarchy and caching results.
- Authoritative server
- A server that holds the zone data for a domain and is the source of truth for its records.
- TTL
- Time to live, the number of seconds a record may be cached. Old answers persist for up to the TTL after a change.
Read further
- TCP/IP Illustrated, Volume 1, 2nd edition, Ch. 11, "Name Resolution and the Domain Name System (DNS)" (Purchase)
The namespace and delegation, recursive and iterative queries, caching and TTLs, and the main record types (A, AAAA, CNAME, NS, SOA). Note the role of the resolver library on the client. - UNIX and Linux System Administration Handbook, 5th edition, Ch. 16, "DNS — The Domain Name System" (Purchase)
How DNS works for an administrator,/etc/resolv.confandnsswitch.conf, and the debugging section ondig. Skim the server configuration chapters.