How DNS finds an address
BeginnerHow a resolver turns example.com into an IP address by asking a root server, a .com server and the site’s own name server, and why the answer is cached.Builds on What happens when you type a URL
# Ask your resolver: it does every step for you$ dig example.com A +short203.0.113.10 # The same steps by hand. First, a root server:$ dig @a.root-servers.net example.com A +norecurse;; AUTHORITY SECTION:com. 172800 IN NS a.gtld-servers.net. # Then a .com server:$ dig @a.gtld-servers.net example.com A +norecurse;; AUTHORITY SECTION:example.com. 172800 IN NS ns1.example.net. # Last, example.com's own name server:$ dig @ns1.example.net example.com A +norecurse;; ANSWER SECTION:example.com. 3600 IN A 203.0.113.10Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
4 min read
On this page
What is DNS and how does it find an address?
Computers find each other by IP address, a number like 203.0.113.10. People remember names
like example.com. The Domain Name System (DNS) is the internet's address book: it turns a
name into the address of the server behind it.
No single computer holds the whole book. It is split into a tree, and each part is kept by whoever owns that name:
- The root servers know who is in charge of each ending:
.com,.org,.eg… - The
.comservers know which name servers each.comname has chosen. - A site's own name servers hold its records, such as the address of
example.com.
A walks this tree for you, from the top down, and remembers what it learns.
How does a DNS lookup work, step by step?
- Two kinds of question. Your computer asks the resolver a recursive question: "find me the full answer". The resolver asks every other server an iterative one: "do you know it, or who should I ask next?". Each server answers with either the record or a referral to the servers one level down.
- Records have types. An
Arecord holds an IPv4 address and anAAAArecord an IPv6 one.NSrecords name a domain's name servers,CNAMEmakes one name an alias of another, andMXsays where email for the domain goes. - Every answer has a TTL. The "time to live" is how many seconds an answer may be kept. The browser, the operating system and the resolver all cache answers, referrals included, until it runs out. That is why a second lookup usually takes one trip, or none at all.
- Name servers need addresses too. To ask
ns1.example.net, the resolver needs its IP address. The.comserver usually sends it along with the referral (a "glue" record); otherwise the resolver looks it up first.
What do people get wrong about DNS?
Mini example
dig (built into macOS and Linux) asks DNS questions from the terminal. On Windows,
nslookup example.com does the same job.
# The address, as your resolver sees it$ dig example.com A +short # The IPv6 address and the name servers$ dig example.com AAAA +short$ dig example.com NS +short # The whole walk, from the root down$ dig +trace example.comRun dig example.com A twice in a row: the number before IN A is the TTL, and it goes down on the
second run because the resolver answered from its cache.
Quick check
A resolver has just looked up example.com. Why can it look up shop.example.com without asking a root server?
Further reading
All steps as text
- Step 1:
The browser wants to open example.com but needs its IP address. It looks in its own cache and the operating system’s: nothing yet.
- Step 2:
So the system asks the DNS resolver, usually run by your internet provider. The question means: find me the full answer, whatever it takes.
Lines 1–2
- Step 3:
The resolver checks its own cache first: nothing. It doesn’t even know who handles .com, only the addresses of the root servers, which are built in.
- Step 4:
So it starts at the top of the tree and asks a root server about example.com. There are 13 root server names, each run as hundreds of machines worldwide.
Line 6
- Step 5:
The root server doesn’t know example.com, but it knows who is in charge of .com. It answers with a referral: go ask a.gtld-servers.net.
Lines 7–8
- Step 6:
The resolver asks the same question again, this time to a .com server.
Line 11
- Step 7:
The .com server doesn’t have the address either, but it knows example.com’s name servers, the ones its owner chose. Another referral: ask ns1.example.net.
Lines 12–13
- Step 8:
Third question, to example.com’s own name server. This one is authoritative: it holds the site’s records and doesn’t have to ask anyone.
Line 16
- Step 9:
At last, an answer: an A record with the IP address 203.0.113.10. The 3600 before it is the TTL, how many seconds the answer may be kept.
Lines 17–18
- Step 10:
The resolver saves the address for an hour. It also keeps both referrals, for two days: next time it can skip straight to the servers it already knows.
Line 18
- Step 11:
Only now does the resolver answer the browser. That one line from
dig +shorthid three trips across the internet.Lines 2–3
- Step 12:
The browser and the system cache the address too, for at most the TTL. Now the browser can open a connection to 203.0.113.10.
- Step 13:
A minute later, the page needs shop.example.com, a name nobody has looked up yet. The browser’s cache can’t help, so it asks the resolver.
- Step 14:
The resolver remembers example.com’s name server, so it skips the root and .com servers and asks ns1.example.net directly.
- Step 15:
One trip instead of three. The resolver caches this answer too and passes it on to the browser.
- Step 16:
DNS asks from the top down: root, then .com, then the site’s own name server. Every answer is cached for its TTL, so most lookups take one trip or none.