Sub topic 1
How DNS resolution works
Prerequisites
This chapter assumes you can use a Linux shell and have a basic grasp of TCP/IP, including IP addresses, ports, and the difference between UDP and TCP. You need dig from the dnsutils (Debian/Ubuntu) or bind-utils (RHEL family) package to run the examples. Later chapters install and configure BIND and Unbound. This one builds the mental model you need before touching either.
The namespace is a tree
DNS (the Domain Name System) maps names to data, most often IP addresses. The names form a tree. The top is the root, written as a single dot. Below it sit top-level domains (TLDs) such as com, org and de. Below those sit the domains that organisations register, such as example.com, and below those sit the names inside them, such as www.example.com.
A fully qualified domain name (FQDN) is read right to left, from the root down. The name www.example.com. has a trailing dot that stands for the root. Each dot-separated piece is a label. A label can be up to 63 bytes, and a whole name can be up to 253 characters in text form.
No single server holds the whole tree. The tree is cut into zones, and each zone is managed by the servers that are authoritative for it. A zone is an administrative slice. example.com can be one zone that contains www.example.com. It can also hand part of itself to another zone, for example lab.example.com, through delegation.
Delegation
Delegation is how a parent zone says "someone else answers for this child." The parent publishes NS records for the child name. Those NS records name the child's servers. The servers listed there are the ones a resolver goes to next.
Sometimes the child's name servers sit inside the child zone itself. If example.com is served by ns1.example.com, a resolver cannot look up the address of ns1.example.com without already asking example.com. The parent breaks this loop by publishing glue: an A or AAAA record for the name server, included in the parent's referral. Missing or stale glue is a common cause of lookups that work for some people and fail for others.
The three actors
Three kinds of software take part in one lookup:
- A stub resolver is the small library inside every client program. On Linux it reads
/etc/resolv.confor asks the localsystemd-resolvedstub, sends one question to a configured server, and waits. It does no walking of the tree. - A recursive resolver does the work on the client's behalf. It starts at the root, follows referrals down the tree, caches what it learns, and returns the final answer. Unbound and BIND in recursive mode are examples.
- An authoritative server holds the zone data and answers only for the zones it serves. It does not chase other servers for you.
The client sets the RD (recursion desired) flag in its query. The recursive resolver sets RA (recursion available) in its reply. An authoritative-only server ignores the request to recurse and either answers from its zones or refers you elsewhere.
Following one lookup
Suppose a client asks for www.example.com and the resolver's cache is empty. The resolver works down the tree:
- It asks a root server. The root does not know the answer. It replies with a referral: the NS records for
comand glue for those servers. - It asks a
comserver. That server replies with a referral to the name servers forexample.com. - It asks one of the
example.comservers. That server is authoritative and replies with the answer, with the AA (authoritative answer) flag set. - The resolver stores the answer in its cache for the record's TTL and returns it to the client.
The resolver does not need to know every root server by address. It ships with a root hints file, a short list of root server names and addresses, and it updates its view of the root from the first answer it gets.
Seeing it with dig
You can replay the walk yourself. The +trace option makes dig act as an iterative resolver, starting at the root and printing each referral:
dig +trace www.example.com ATo ask one server a single question without asking it to recurse, send +norecurse. Here the question goes to a root server, and the reply is a referral, not an answer:
dig @a.root-servers.net www.example.com A +norecurseIn the output, look at the header line. The flags: field shows qr (this is a response), and aa when the server is authoritative. It shows rd and ra when recursion was requested and offered. The status shows NOERROR, NXDOMAIN (the name does not exist) or SERVFAIL (the server could not get an answer). The later chapter on testing covers dig in detail.
Caching and TTLs
Without a cache, every lookup would start at the root. The resolver caches every record it receives, including the NS records and glue from referrals, so most queries skip the first steps. If .com referrals are already cached, a new query for another .com name goes straight to a com server.
Each record carries a TTL (time to live) in seconds. The resolver counts the TTL down and discards the record at zero. When you run dig twice against the same resolver, the second TTL in the answer section is smaller. That is the cache counting down.
Negative answers are cached too. When a name does not exist, the resolver remembers the NXDOMAIN for a period taken from the zone's SOA record. This is why a record you just created may stay invisible for a while after someone queried it before it existed.
Caching has a cost. A change you make on the authoritative server does not reach clients until the old TTL runs out in every resolver that cached it. Lower the TTL before a planned change, not after.
Transport, size and privacy
Queries normally go over UDP port 53. If an answer is too large, the server sets the TC (truncated) flag and the client retries over TCP port 53. Modern resolvers also use EDNS0 to announce a larger UDP buffer size. A firewall that blocks TCP port 53 breaks only the large answers, which makes the fault hard to spot. Keep both UDP and TCP open on port 53 to your servers.
Classic DNS sends the full name to every server on the path. Query name minimisation (QNAME minimisation) limits this: the resolver asks the root only for com, and asks the com server only for example.com. The authoritative server sees the full name only at the last step. Unbound enables it with qname-minimisation: yes, which you will meet in the Unbound chapter.
Common errors in the model
- Treating a recursive resolver and an authoritative server as the same thing. They answer different questions and need different access rules.
- Expecting a change to appear immediately. Cached copies live until the TTL expires.
- Forgetting glue. A delegation to a name server inside the delegated zone fails without it.
- Blocking TCP on port 53 and then seeing only some queries fail.
- Reading a
SERVFAILas "the name does not exist." That isNXDOMAIN.SERVFAILmeans the resolver failed to get a usable answer, for example because of a broken delegation or a failed DNSSEC check.
Exercise
You run dig @a.root-servers.net www.example.com A +norecurse. The reply has no answer section, but it lists NS records for the com servers with their addresses. What kind of reply is this, which flag is missing from the header that would mark an authoritative answer, and what does the resolver do next?
Show answer
It is a referral. The root server is not authoritative for www.example.com, so the aa flag is absent. The addresses attached to the NS records are glue. The resolver caches the referral and sends the same question to one of the com servers, which then refers it to the example.com servers.
Exercise
A record has a TTL of 3600. You change its address on the authoritative server at 10:00. A resolver cached the old value at 09:30. When can that resolver's clients first see the new address, and what would you have done in advance to shorten the wait?
Show answer
At 10:30, when the cached copy expires (3600 seconds after 09:30). Clients that query after that time get the new value. To shorten the wait, lower the TTL on the record (for example to 300) at least one old-TTL period before making the change, then raise it again afterwards.