What happens between typing a name and getting an address? Let us follow a lookup for www.example.com.
Step 1: local sources
An application asks the C library to resolve the name. The file /etc/nsswitch.conf says where to look, and in what order:
$ grep '^hosts' /etc/nsswitch.conf
hosts: files dnsfiles means /etc/hosts and dns means ask a DNS server. Your line may list more sources, such as resolve or mdns4_minimal, depending on the distribution.
Step 2: ask the recursive resolver
If the name is not found locally, your machine sends a query to the resolver in /etc/resolv.conf. It asks for recursion, meaning "give me the final answer, not a hint". DNS normally uses port 53, over UDP for most queries and over TCP when needed, for example for large answers.
If the resolver already has the answer cached and its TTL has not expired, it replies at once and the journey ends here.
Step 3: walk the tree
With an empty cache, the recursive resolver works from the top down, using iterative queries:
- It asks a root server. The root does not know the address, but refers it to the
comservers. - It asks a
comserver, which refers it to the name servers forexample.com. - It asks an authoritative server for
example.com, which returns the record.
A reply that points elsewhere is called a referral.
Step 4: answer and cache
The recursive resolver caches what it learned and sends the address back to your machine. Cached referrals help too: the next .com lookup can often skip the root.
"Does not exist" answers are cached as well. This is negative caching, and it explains why a record you have just created can seem missing for a while.
Watching it happen
dig +trace follows the referrals itself, starting at the root:
$ dig +trace www.example.com
. 518400 IN NS a.root-servers.net.
com. 172800 IN NS a.gtld-servers.net.
example.com. 172800 IN NS elliott.ns.cloudflare.com.
www.example.com. 300 IN A 192.0.2.80This is heavily shortened, and the final address is a placeholder. Real output lists several servers at each level, and names and numbers change over time. Some networks block direct queries to outside servers, in which case the trace stalls.
Try it
- Check your
hosts:line in/etc/nsswitch.conf. - Run
dig example.orgtwice and compare theQuery timelines. The second is often faster because of caching. - Run
dig +trace example.organd find each referral.
Check your understanding
DNS Process
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.