How DNS works
Packets travel by IP address, not by name, so before a browser can open www.example.com something has to turn that name into an IP. That something is DNS (Domain Name System): a hierarchy of servers plus caches that answers in a few milliseconds.
81.1 Why is it used?
- Humans remember names, machines need numbers; DNS is the phonebook of the internet.
- The laptop asks its DNS resolver "what is the IP of www.example.com?" and then connects directly to that IP.
- The resolver address comes from DHCP: it can be your router, your ISP or a public resolver like 8.8.8.8 / 1.1.1.1.
- Without DNS every user would have to know and remember every server IP, and servers could never change IP.
81.2 The DNS hierarchy (read right to left)
| Level | Example | What it does |
|---|---|---|
| Root | . (dot) | 13 root server names (a-m), anycast copies worldwide; knows who serves each TLD |
| TLD | .com, .in, .org | Knows the authoritative name servers of every domain under it |
| Authoritative | example.com (ns1.example.com) | Holds the real records of the domain |
| Host | www | One name inside the domain; full name www.example.com. (with trailing dot) |
81.3 How it works: one lookup with an empty cache
- Laptop -> resolver: "A? www.example.com" on UDP port 53 with the RD (recursion desired) flag.
- Resolver -> root: the root does not know the IP, it returns a referral: "for com ask a.gtld-servers.net (192.168.101.11)".
- Resolver -> TLD: another referral: "example.com is served by ns1.example.com", plus its IP 192.168.101.12. That IP inside the referral is a glue record.
- Resolver -> authoritative: the final answer "www.example.com A 192.168.101.80".
- The resolver caches the answer, replies to the laptop, and the laptop connects to the web server. Usually a few milliseconds in total.
- Step 1
1.Laptop 192.168.101.100 -> resolver .53: A? www.example.com (recursive, RD)
- Step 2
2.Resolver -> root .10: referral, "com is at a.gtld-servers.net"
- Step 3
3.Resolver -> TLD .11: referral, "ns1.example.com = 192.168.101.12" (glue)
- Step 4
4.Resolver -> authoritative .12: A 192.168.101.80 (TTL 300)
- Step 5
5.Resolver caches the answer and replies to the laptop
- Step 6
6.Laptop connects to the web server 192.168.101.80
One recursive query from the client, three iterative queries from the resolver
81.4 Comparison: recursive vs iterative query
| Point | Recursive query | Iterative query |
|---|---|---|
| Who sends it | Client -> resolver | Resolver -> root, TLD, authoritative |
| What it asks for | "Give me the final answer, do all the work yourself" | "Give me the answer or tell me whom to ask next" |
| Reply | Final answer (or error) | Answer or a referral |
| Flag | RD set by the client, RA in the reply if the server allows recursion | RD cleared (tcpdump shows % / - instead of +) |
| Typical server | Router, ISP, 8.8.8.8, 1.1.1.1 (a recursive resolver) | Root, TLD and authoritative servers |
81.5 Caching and TTL
- Every record carries a TTL (time to live) in seconds: how long it may stay in a cache. It counts down every second.
- A second user asking the same name gets the answer straight from the resolver cache; root and TLD are skipped.
- Cache layers: browser (Chrome), OS (Windows DNS Client, systemd-resolved) and the resolver (router, ISP, 8.8.8.8).
- TTL reaches 0 -> the record expires and the next query walks the chain again.
- High TTL: fast lookups, less load, but an IP change reaches users late. Low TTL: changes show quickly, but more queries.
- Step 1
1.User 2 -> resolver: A? www.example.com
- Step 2
2.Resolver finds the record in its cache (TTL now 277)
- Step 3
3.Root, TLD and authoritative are skipped
- Step 4
4.Resolver -> User 2: A 192.168.101.80, query time 0 msec
81.6 Lab: a recursive query to the resolver
Docker lab (BIND 9.18): root .10, TLD .11, authoritative .12, resolver .53, laptop .100 on 192.168.101.0/24. The resolver's root hints point only to the lab root, so no query leaves the lab.
dig @192.168.101.53 www.example.com;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1;; ANSWER SECTION:www.example.com. 300 IN A 192.168.101.80;; Query time: 32 msec;; SERVER: 192.168.101.53#53(192.168.101.53) (UDP)- rd = recursion desired (client asked for it), ra = recursion available (resolver offers it), qr = this is a reply.
- 300 is the TTL in seconds, IN is the Internet class, A is an IPv4 address record.
81.7 Lab: watching the resolver's iterative queries
# on the resolver 192.168.101.53tcpdump -n -i eth0 udp port 5322:04:59.703141 IP 192.168.101.100.58418 > 192.168.101.53.53: 9893+ [1au] A? www.example.com. (56)22:04:59.704897 IP 192.168.101.53.40369 > 192.168.101.10.53: 63900% [1au] A? www.example.com. (56)22:04:59.705979 IP 192.168.101.10.53 > 192.168.101.53.40369: 63900- 0/1/1 (107)22:04:59.711554 IP 192.168.101.53.40989 > 192.168.101.11.53: 57285% [1au] A? www.example.com. (56)22:04:59.712320 IP 192.168.101.11.53 > 192.168.101.53.40989: 57285- 0/1/2 (106)22:04:59.713136 IP 192.168.101.53.56183 > 192.168.101.12.53: 26283% [1au] A? www.example.com. (56)22:04:59.729215 IP 192.168.101.12.53 > 192.168.101.53.56183: 26283*- 1/1/2 A 192.168.101.80 (122)22:04:59.734299 IP 192.168.101.53.53 > 192.168.101.100.58418: 9893 1/0/1 A 192.168.101.80 (88)- Line 1: the recursive query from the laptop (+ = RD set).
- Root and TLD replies are 0/1/x: zero answers, one authority record (the referral).
- The authoritative reply 1/1/2 carries * (authoritative answer) and the A record.
- Last line: the resolver hands the final answer back to the laptop.
81.8 Lab: cache and the TTL countdown
# same query, 23 sec laterdig @192.168.101.53 www.example.com# resolver: tcpdump -n -i eth0 udp port 53# 13 sec laterdig @192.168.101.53 www.example.com \ +noall +answerwww.example.com. 277 IN A 192.168.101.80;; Query time: 0 msecresolver# tcpdump22:05:22.348634 IP 192.168.101.100.49894 > 192.168.101.53.53: 23996+ [1au] A? www.example.com. (56)22:05:22.349298 IP 192.168.101.53.53 > 192.168.101.100.49894: 23996 1/0/1 A 192.168.101.80 (88)2 packets captured$ dig @192.168.101.53 www.example.com +noall +answerwww.example.com. 264 IN A 192.168.101.80- TTL dropped 300 -> 277 -> 264: the countdown is running.
- Query time 0 msec and only two packets on the resolver: answered from cache, nothing sent to root or TLD.
81.9 dig +trace: the whole chain
# start at the lab root# (on the internet: dig +trace example.com)dig +trace @192.168.101.10 www.example.com. 518400 IN NS a.root-servers.net.;; Received 87 bytes from 192.168.101.10#53(192.168.101.10) in 0 mscom. 172800 IN NS a.gtld-servers.net.;; Received 107 bytes from 192.168.101.10#53(a.root-servers.net) in 0 msexample.com. 172800 IN NS ns1.example.com.;; Received 106 bytes from 192.168.101.11#53(a.gtld-servers.net) in 0 mswww.example.com. 300 IN A 192.168.101.80;; Received 122 bytes from 192.168.101.12#53(ns1.example.com) in 0 ms- +trace ignores the resolver cache and does the iterative walk itself, starting at the root.
- Each block ends with "Received ... from <server>", so you see exactly which level answered.
- In a DNS outage this shows where the chain breaks (wrong NS at the TLD, dead authoritative server, missing record).
81.10 Common mistakes
- Believing "DNS propagation takes 48 hours": there is no propagation. The authoritative server has the new IP immediately; resolvers holding the old answer keep it only until the TTL expires.
- Changing an IP with TTL 3600 still set: users can get the old IP for up to one hour. Lower the TTL to about 300 well before the change.
- Thinking the root server knows every IP: it only gives referrals to TLD servers.
- Assuming DNS is only UDP: large responses (and zone transfers) use TCP 53.
81.11 Interview tips
- Order: browser cache -> OS cache -> recursive query to the resolver -> iterative queries root -> TLD -> authoritative.
- Explain referral and glue: the TLD returns the NS name plus its IP so the resolver is not stuck in a loop.
- Answers are cached for their TTL; TTL is a trade-off between speed/load and how fast changes are seen.
- DNS runs on UDP 53, falls back to TCP 53 for large answers.
- Debug tool to name: dig +trace shows the full chain and where it breaks.