How the internet works · Lesson 1 · 35 min

DNS: how names become addresses

Computers find each other by number, like 203.0.113.10. People remember names, like club.example.org. DNS (the Domain Name System) is how one becomes the other, billions of times a second, all over the world. When it goes wrong, nothing works, and it's often the last thing people check. There's a saying among admins: "It's always DNS."

You will learn

  • The journey of a DNS question, from your app to the root servers and back
  • The record types you'll meet: A, AAAA, CNAME, MX, TXT, NS, SOA, PTR
  • Reading dig like a pro, and asking any DNS server you like
  • TTLs and caching, and why a DNS change takes time to reach everyone
  • Where your server gets its DNS on each family, and how /etc/hosts jumps the queue

The journey of a question

When you open club.example.org, nobody has a list of every name on the internet. Instead, the question is passed along until it reaches someone who knows:

  1. Your app asks the computer's resolver, a small part of the operating system. It checks the /etc/hosts file and its own cache first.
  2. The resolver asks a recursive resolver, usually your home router, your company's DNS server, or a public one like 1.1.1.1 (Cloudflare) or 8.8.8.8 (Google). Its job is to find the answer for you.
  3. The recursive resolver asks a root server: "where's club.example.org?" The root doesn't know, but it knows who runs .org.
  4. It asks the .org servers. They don't know either, but they know which servers are in charge of example.org.
  5. It asks those authoritative servers, and they give the real answer: 203.0.113.10.
  6. Everyone on the way back remembers the answer for a while (its TTL, "time to live"), so the next person gets it instantly.

Step 6 is why DNS is fast, and also why DNS changes are slow. Hold that thought.

Record types

A DNS name can hold different kinds of records. These are the ones you'll use:

TypeHoldsExample
AAn IPv4 addressclub.example.org → 203.0.113.10
AAAAAn IPv6 addressclub.example.org → 2001:db8:10::10
CNAME"This name is another name for…"www.club.example.org → club.example.org
MXWhich server receives the domain's email (lower number = try first)10 mail.club.example.org
TXTText, often for proving ownership or email rules (SPF)"v=spf1 mx -all"
NSWhich servers are in charge of (authoritative for) the zonea.iana-servers.net
SOAFacts about the zone itself: who runs it, and how long to remember "no such name"ns.icann.org. noc.dns.icann.org. …
PTRThe reverse: address → name (used by mail servers and logs)203.0.113.10 → club.example.org

Reading dig

dig is the DNS tool admins reach for. It's on almost every Linux server (package bind-utils on Rocky, bind9-dnsutils on Ubuntu, and preinstalled on macOS). The useful parts of its answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41231      # NOERROR, NXDOMAIN (no such name) or SERVFAIL
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; ANSWER SECTION:
club.example.org.   2391   IN   A   198.51.100.20        # name, TTL left (seconds), type, value

;; SERVER: 192.168.1.1#53(192.168.1.1) (UDP)              # who answered
TryTo…
dig NAMElook up the A record
dig NAME MXask for another type (MX, AAAA, TXT, NS…)
dig NAME +shortjust the answer, nothing else
dig @1.1.1.1 NAMEask a particular DNS server instead of your usual one
dig +trace NAMEwalk the whole journey yourself: root, then .org, then the zone's own servers
dig -x 203.0.113.10reverse lookup (address → name)

One thing to know: dig only ever asks DNS. It skips /etc/hosts. To see what your apps will actually get, use getent hosts NAME.

TTLs and "DNS propagation"

People say a DNS change "takes time to propagate", as if it were slowly spreading. Really, the change is instant at the authoritative servers. What takes time is caches expiring. Every resolver that already asked keeps its old answer until the TTL runs out. With a TTL of 3600, that can be up to an hour.

Planning a move? Lower the TTL first

A day before you change an address, lower the record's TTL to something short, like 300 (five minutes). Once the old long TTL has run out everywhere, make the change: everyone switches over within five minutes. Afterwards, put the TTL back up.

Where your server gets its DNS

This part is Linux-specific, and the two families do it differently:

WhatRocky / RHELUbuntu / Debian
Who writes /etc/resolv.confNetworkManager, with the real DNS servers in itsystemd-resolved: it just says nameserver 127.0.0.53
Which servers are really usedcat /etc/resolv.conf or nmcli dev show | grep DNSresolvectl status
A local DNS cache?No: every lookup goes out to the serverYes: systemd-resolved remembers answers
Empty the local cache(nothing to empty)sudo resolvectl flush-caches
Change the DNS serversnmcli con mod … ipv4.dns (Linux Sysadmin, lesson 1)netplan nameservers: (Linux Sysadmin, lesson 1)

Flushing your own cache only helps with your cache. If the router or your company's DNS server still remembers the old answer, you'll keep getting it until their TTL runs out. (On a Windows laptop, the same flush is ipconfig /flushdns; on a Mac, sudo dscacheutil -flushcache.)

/etc/hosts jumps the queue

Before asking DNS at all, Linux checks /etc/hosts (that order is set by the hosts: line in /etc/nsswitch.conf, normally files dns). A line there wins over anything DNS says. That's handy for testing a new server before the real DNS change, and a classic trap when someone forgets to remove it afterwards.

"It's always DNS": a checklist

1. What do apps get? getent hosts NAME. 2. What does DNS say? dig NAME. 3. What's the truth? dig +trace NAME. 4. Old answer? Look at the TTL, the server that answered, and /etc/hosts.

Try it: the club moved its website 🌐

Twenty minutes ago the coding club moved its website to a new server: club.example.org changed from 198.51.100.20 to 203.0.113.10. Some people see the new site, some the old. You're on the club's other server, and you're going to find out why.

Quick check

1. You changed an A record ten minutes ago, but your laptop still gets the old address. Most likely?

2. dig shows the new address, but ping and your web app still use the old one. Where do you look?

3. Which record says where a domain's email should go?

Finished the missions and the quiz? Mark it done to track your progress.