E · 05 Computer Networks & Security

DNS: The Internet's Phonebook

DNS: The Internet's Phonebook

You type example.com and press Enter, and in under a tenth of a second your laptop finds the one server that knows its address. Here's how: the browser and OS caches, the stub resolver, your recursive resolver walking from the root to .com to the authoritative name server, glue records, and the A record that comes back with its TTL. Then why caching makes DNS fast, the record types you'll see every day (A, AAAA, CNAME, MX, NS, TXT), why DNS uses UDP, and what can go wrong: cache poisoning, the 2008 Kaminsky attack, DNSSEC, and encrypted DNS (DoT and DoH).

Chapters

Cheat sheet

Transcript

You type example.com and press Enter. Your laptop has no idea where that is. Neither does your Wi-Fi router. Somewhere out there, a couple of servers know the answer for sure. Your computer has never heard of them.

And yet, usually in less than a tenth of a second, it will find one, ask the question, and get the answer back. That's DNS, and "walk me through a DNS lookup" is one of the most common networking interview questions there is. Today, we follow one lookup all the way to the root.

This is Just Known. From unknown, to known. Episode five: DNS, the internet's phonebook. Computers route by numbers, but people remember names. So something has to translate.

In the early internet, that something was a single text file listing every computer's name and address, and everyone downloaded fresh copies. It worked while the network was small. But by the early 1980s, with hundreds of machines and more every month, one central file couldn't keep up. So in 1983, Paul Mockapetris designed its replacement: DNS, the Domain Name System. The trick is that no one holds the whole phonebook.

A name is read from right to left. That final dot you never type is the root. Then dot com, the top-level domain. Then example, the domain itself. And www, a name inside it.

Each level delegates the next level to whoever owns it. The root knows who runs dot com, dot com knows who runs example.com, and only example.com knows its own addresses. Before your laptop asks anyone, it checks what it already knows. The browser keeps its own small cache of recent names. The operating system checks its own cache, and a local file called the hosts file.

All misses. So a small piece of the operating system, called the stub resolver, passes the question on to a recursive resolver: usually the one DHCP handed out back in episode three, or one you set yourself. Let's say it's 1.1.1.1. The question is tiny. It travels in a single UDP packet to port 53, and it carries a random ID number, a flag that says "please do the whole job for me", and the question itself: what's the address of example.com?

This resolver is where the real work happens. Your laptop asked it a recursive question, which means: do the whole job, and just give me the final answer. The resolver then asks other servers iterative questions. Each one either answers, or says: I don't know, but here's who to ask next. That's the core idea of DNS resolution, and it's a classic interview question: recursive between you and your resolver, iterative between the resolver and everyone else.

First stop: the root. You might hear there are only thirteen root servers. That's true, sort of. There are thirteen root server names, a through m, run by twelve different organizations. But each name isn't one machine.

Behind most of them are dozens or hundreds of servers spread around the world, all sharing the same address. That's called anycast: your packet simply goes to the nearest copy, in network terms. In total, there are over two thousand root server instances. And the root doesn't know example.com. It doesn't need to.

It answers with a referral: here are the servers for dot com. Ask them. Second stop: the dot com servers, run by the dot com registry. They don't know example.com's address either. But they know who's in charge of it: example.com's name servers, called ns1 and ns2 dot example.com.

Notice the chicken-and-egg problem here. To find example.com, you have to ask ns1 dot example.com. But to find ns1 dot example.com, you'd have to ask example.com. So the dot com servers also hand over the name server's address directly. That extra piece is called a glue record, and it breaks the loop.

Third stop: example.com's own name server. This one is authoritative: it doesn't refer anyone, it owns the answer. And the answer comes back as a record. The name, example.com. A number, 3600.

The class, IN, for internet. The type, A, meaning an IPv4 address. And the value: 203.0.113.10. The resolver passes that back to your laptop, and now the browser can finally open a connection, which is where we'll pick up in episode seven. Four questions out, four answers back, all in a fraction of a second.

Unless, of course, someone already asked. That number, 3600, is the time to live, or TTL, in seconds. It tells everyone who stores this answer how long they may keep using it. So the resolver can remember it for up to an hour. So can your operating system.

That's why DNS is so fast in practice. The next ten thousand people using the same resolver get the answer instantly, without bothering the root, dot com, or example.com at all. Most lookups never leave a cache. Choosing a TTL is a trade-off. A long TTL means fewer lookups and faster answers, but if you change your server's address, the world keeps using the old one until its caches expire.

That's what people mean by "DNS propagation". A short TTL makes changes spread quickly, at the cost of more queries. Pro tip: before you move a server, lower the TTL, and wait at least one old TTL before you switch. And even "no such name" gets cached, for a while, so a typo can't hammer the servers either. The A record is only one type.

There are a handful you'll see all the time. A, for an IPv4 address. Quad A, for an IPv6 address: four times the bits, an easy way to remember the four A's. CNAME, an alias: www.example.com is just another name for example.com, so go look that up instead. A CNAME can't share its name with any other record, which is why you usually can't put one on the bare domain itself.

MX tells the world where to deliver email for the domain, with a preference number, where a lower number wins. NS lists the domain's own name servers, the same ones dot com pointed us to. And TXT holds free text, which the world uses for proofs and policies: who's allowed to send email for this domain, and "yes, I really own this domain". Why does DNS use UDP? Because a lookup is one small question and one small answer.

With UDP, there's no handshake: you send the question, the answer comes back, and you've spent a single round trip. TCP would add a whole extra round trip first, as we'll see in episode seven. But classic DNS caps UDP answers at 512 bytes. If an answer is too big, the server sends back a truncated reply with a flag that says so, and the client asks again over TCP, on the same port, 53. A newer extension called EDNS lets UDP carry bigger answers, but every DNS server must still support TCP.

TCP is also used when servers copy whole zones to each other. So what can go wrong? Remember that the question travels over UDP, with no handshake, and the resolver trusts the first answer that matches. What if an attacker answers first? That's cache poisoning.

The attacker floods the resolver with fake answers, saying example.com lives at the attacker's address. To be accepted, a fake answer has to match the question's ID number, but that ID is only sixteen bits: sixty-five thousand possibilities. And if one forged answer gets in, the resolver caches it, and hands it to everyone who asks, for as long as the TTL says. In 2008, a researcher named Dan Kaminsky showed how to make this attack fast and repeatable. Ask for made-up names that aren't in any cache yet, so every try is a fresh race, and slip a fake name server for the whole domain into the forged answers.

In July 2008, vendors shipped a coordinated emergency patch: randomize the source port too, so an attacker has to guess both numbers at once. That turns sixty-five thousand guesses into billions. Randomness makes forgery hard. DNSSEC makes it detectable. With DNSSEC, a domain signs its records with a cryptographic key, and its parent signs a fingerprint of that key, all the way up to the root, whose key every validating resolver already has.

The root zone has been signed since 2010. A validating resolver checks each link in that chain, and if the domain is signed, a forged answer simply fails the check. Most domains still aren't signed. But notice what DNSSEC doesn't do. It proves who wrote the answer, and that nobody changed it.

It doesn't hide your question. Anyone on the path can still see which names you look up. For privacy, there's encrypted DNS. DNS over TLS wraps your questions in the same encryption as the web, on port 853. DNS over HTTPS goes a step further: it sends them as ordinary HTTPS traffic on port 443, so they blend in with everything else.

Many browsers and operating systems can do this today. Now the café Wi-Fi and your internet provider can't read your lookups, though they can still see where you connect next. And be clear about who you're trusting: the resolver at the other end still sees every name you ask for. Encryption changes who can watch, not whether anyone can. We'll unpack TLS itself in episode ten.

You can watch all of this yourself. On a Mac or Linux, run dig, followed by a name. On Windows, nslookup does the same basic job. Look at the answer section: there's the record, with its TTL, and the address. Below it, the query time, and which server answered.

Now run it again. The TTL has gone down, and the query time drops to just the trip to your resolver. That's the resolver's cache. Then try dig, plus trace. It asks your resolver only for the list of root servers, then does the whole walk itself: the root, then dot com, then example.com's own name servers, printing every referral along the way.

And add MX or TXT to the end to ask for other record types. The commands are in the description. Interview corner. Walk me through what happens when you look up a domain name. Pause here, and try answering in thirty seconds.

Here's a strong answer. First, the browser and operating system check their caches and the hosts file. If they miss, the stub resolver sends a recursive query, over UDP port 53, to a recursive resolver. If the resolver hasn't cached the answer, it queries iteratively: a root server refers it to the dot com servers, they refer it to example.com's authoritative name servers, and those return the A record with a TTL. The resolver caches it and returns it, and the browser connects to that address.

To stand out, name the two kinds of query, recursive and iterative, and say why DNS uses UDP: one round trip, no handshake, with TCP as the fallback for big answers. Bonus question: what's a CNAME, and why can't you put one on the bare domain? It's an alias, and an alias can't share its name with other records. But the bare domain always has records of its own, like its name servers. Let's recap.

DNS is a phonebook nobody holds: the root knows who runs dot com, dot com knows who runs example.com, and only example.com knows its own addresses. Your resolver does the walking for you, with recursive questions in and iterative questions out. And caching, controlled by the TTL, is what makes it fast enough to use for every single request. Next stop: we have the address. Now the packet has to get there, across several networks run by different companies.

How does each router know which way to send it? We'll follow it hop by hop in episode six. This is Just Known. From unknown, to known.

Note: example.com is reserved for documentation (RFC 2606), and its zone, name servers and the dig output on screen are illustrative; your real output will differ. Example addresses are from reserved documentation ranges (RFC 5737 IPv4, RFC 3849 IPv6); 1.1.1.1 is a real public resolver. Real root and .com server names are shown because they're the topic; root server map positions are illustrative.
Narration voice generated with Google Gemini text-to-speech.