Sub topic 5
How a name is resolved, step by step
This chapter assumes you know what an IP address is, can read an FQDN (fully qualified domain name) such as www.example.com., and know that DNS is split into root, TLD, and authoritative levels. It also assumes you can run commands in a terminal. Here you will follow one lookup from a program's request to the final address, and see which machine does which job.
The three roles in a lookup
A single lookup involves three kinds of participants. Keeping them separate prevents most confusion about where an answer comes from.
| Role | Also called | Job | Typical example |
|---|---|---|---|
| Stub resolver | Client resolver | Asks one question and waits for a final answer. It does no searching itself. | The resolver code in your operating system |
| Recursive resolver | Recursor, caching resolver | Does the searching on the client's behalf and caches what it learns. | Your ISP's resolver, or a public one such as 1.1.1.1 |
| Authoritative server | Name server | Holds the records for a zone and answers only for that zone. | `ns1.example.com` |
The stub resolver sends a recursive query. This is a request that says "give me the final answer, or an error." The recursive resolver then sends iterative queries to authoritative servers. An iterative query accepts a partial answer, such as "I don't know, but ask these servers instead." This partial answer is called a referral.
The full walk, step by step
Suppose your browser needs the address of www.example.com and nothing is cached anywhere.
- The browser asks the operating system to resolve the name. The OS checks its own cache and local overrides, such as the hosts file.
- The stub resolver sends a recursive query to the recursive resolver configured for the network. This is usually handed out by DHCP, or typed in by hand.
- The recursive resolver checks its cache. On a miss, it starts at the top. It knows the addresses of the root servers from a built-in list called the root hints.
- It asks a root server for
www.example.com. The root does not know the answer. It replies with a referral to the servers forcom., listing their names and, as glue, their addresses. - It asks one of the
com.servers. That server replies with a referral to the authoritative servers forexample.com., such asns1.example.comandns2.example.com. - It asks an authoritative server for
example.com. This server holds the zone, so it replies with the actual record: an address, or a CNAME. - The recursive resolver stores the answer in its cache, for as long as the record's TTL allows. It returns the answer to the stub resolver.
- The OS hands the address to the browser, which opens a connection.
Only the recursive resolver performs steps 3 to 7. Your computer sends one question and gets one answer.
Why glue records exist
A referral names servers, but the resolver needs their IP addresses to contact them. Suppose example.com is served by ns1.example.com. To find that name's address, the resolver would have to query example.com, which it cannot reach until it knows that address. This is a circular dependency.
The parent zone breaks the loop by including glue records. Glue is an address record for a name server, stored in the parent zone (here, com.) alongside the delegation. You will see how it is registered in the chapter on delegation. For now, remember that glue is needed only when a name server's name lies inside the zone it serves. A server named ns1.otherprovider.net needs no glue for example.com.
Seeing the walk with dig
The dig tool lets you watch these steps. First, a normal query, which makes dig act like a stub resolver and ask your recursive resolver for the final answer:
dig example.com A +short
dig example.com AAAA +short
dig example.com MX +short
dig example.com NS +shortEach command asks for one record type of example.com. The +short option prints only the answer values.
To see the iterative steps, use +trace. Now dig does the recursive resolver's job itself:
dig +trace www.example.comThe output shows a root server answering with a list of com. servers, a com. server answering with example.com. name servers, and finally an authoritative server giving the record. Each block of output is one referral or answer, so the delegation chain is visible.
You can also send a query to a specific recursive resolver with @:
dig @1.1.1.1 example.com AThis is useful for comparing what different resolvers return.
Reading the header flags
The full output of a normal dig query includes a flags line. Two flags matter here.
rd(recursion desired) is set by the client. It asks the server to do the full search.ra(recursion available) is set by a server that is willing to do that work.aa(authoritative answer) is set when the answer comes directly from a server that owns the zone, not from a cache.
When you query your recursive resolver, you normally see rd and ra but not aa. If you query an authoritative server directly, you see aa. This is a quick way to tell whether an answer is cached or comes straight from the source.
Resolution from a program
Applications rarely build DNS packets themselves. They call the operating system, which runs the stub resolver. In Python, socket.getaddrinfo makes that call:
import socket
for family, _, _, _, sockaddr in socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM):
print(family.name, sockaddr[0])The output lists one line per address, labeled AF_INET for IPv4 and AF_INET6 for IPv6. A name with both A and AAAA records returns both. Programs usually try them in an order set by the OS, which is why a broken IPv6 path can make a site feel slow even when IPv4 works.
Because this call goes through the OS, it also honors things dig ignores. dig talks to DNS directly. getaddrinfo also consults the hosts file and any OS-level cache. If dig and your program disagree, one of those local sources is a likely cause.
Caching at every layer
The walk above is the worst case. In practice most lookups end early, because caches exist at several levels:
- The application, for example a browser's internal DNS cache.
- The operating system's stub resolver, on some systems.
- The recursive resolver, which is the most important cache. One user's lookup of a popular name serves thousands of later users.
The recursive resolver also caches the referrals. After the first lookup under .com, it remembers the com. servers and skips the root for later names. So a lookup for a new name in a known TLD usually needs only two or three queries. Each cached item lives for its TTL, covered in a later chapter.
Negative answers are cached too. If a name does not exist, the server returns NXDOMAIN (non-existent domain), and the resolver remembers that for a time. The SOA record's last field sets how long. This is why a name you just created can keep failing for a while if someone looked it up beforehand.
Edge cases and common errors
CNAME chains
When the answer is a CNAME, the resolver restarts the search for the target name. If www.example.com is a CNAME to example.com, the resolver then needs the address of example.com. Often the same authoritative server returns both in one response. If the target is in another zone, the resolver runs a whole new walk. Long chains add latency, so keep them short.
Lame delegations
A lame delegation happens when the parent zone points to a name server that does not actually answer for the zone. The resolver tries that server, gets no useful answer (or a refusal), and moves on to another one. If every listed server is lame, resolution fails with SERVFAIL. Typical causes are a typo in the name server list, or a DNS provider account that never had the zone created.
Response codes
The status in dig's output tells you the outcome:
| Status | Meaning | Common cause |
|---|---|---|
| NOERROR | The query succeeded. | Normal answer. If the answer section is empty, the name exists but has no record of that type. |
| NXDOMAIN | The name does not exist. | Typo, or the record was never created. |
| SERVFAIL | The resolver could not get a usable answer. | Lame delegation, unreachable servers, broken DNSSEC. |
| REFUSED | The server declined to answer. | Querying a server that does not serve that zone or that blocks you. |
Transport
DNS normally uses UDP port 53 for small queries and replies. When a reply is too large, the server sets a truncation flag and the client retries over TCP port 53. Firewalls that block TCP 53 cause intermittent failures that only affect large answers, such as those with many records or DNSSEC data.
Resolver differences
Different recursive resolvers can return different answers for the same name, because their caches hold records fetched at different times. Some resolvers also filter or rewrite answers, for example to block malware domains. When debugging, compare dig @resolver results rather than trusting a single answer.
Summary of who does what
The stub resolver asks once. The recursive resolver searches and caches. Root, TLD, and authoritative servers each answer only what they own, pointing onward with referrals until a server that holds the zone returns the record. Everything else in DNS troubleshooting comes back to asking which of those parties gave which answer, and how old it is.
Exercise
You run dig example.com A against your normal resolver and get an address. You then run dig @ns1.example.com example.com A and the response flags include aa. Why does the second response have aa while the first does not, and what does dig +trace example.com do differently from the first command?
Show answer
The first query goes to a recursive resolver, which answers from its cache or from data it fetched from elsewhere, so it is not authoritative for the zone and does not set aa. The second query goes directly to a server that owns the zone, so it sets aa. With +trace, dig acts as the recursive resolver itself. It queries a root server, then a com. server, then the example.com. name servers, and prints each referral, so it ignores the caches your normal resolver uses.
Exercise
A site's name server is named ns1.mysite.org, and it serves the zone mysite.org. Why must the org. servers supply a glue record, and would glue be needed if the name server were ns1.dnshost.net?
Show answer
To contact ns1.mysite.org, a resolver needs its IP address, but finding that address requires querying mysite.org, which is served by that same server. The glue record in the parent zone (org.) gives the address directly and breaks the loop. If the name server were ns1.dnshost.net, its address can be found through the net. and dnshost.net. zones without touching mysite.org, so no glue is needed.