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
diglike 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/hostsjumps 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:
- Your app asks the computer's resolver, a small part of the operating system. It checks the
/etc/hostsfile and its own cache first. - 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) or8.8.8.8(Google). Its job is to find the answer for you. - The recursive resolver asks a root server: "where's
club.example.org?" The root doesn't know, but it knows who runs.org. - It asks the
.orgservers. They don't know either, but they know which servers are in charge ofexample.org. - It asks those authoritative servers, and they give the real answer:
203.0.113.10. - 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:
| Type | Holds | Example |
|---|---|---|
| A | An IPv4 address | club.example.org → 203.0.113.10 |
| AAAA | An IPv6 address | club.example.org → 2001:db8:10::10 |
| CNAME | "This name is another name for…" | www.club.example.org → club.example.org |
| MX | Which server receives the domain's email (lower number = try first) | 10 mail.club.example.org |
| TXT | Text, often for proving ownership or email rules (SPF) | "v=spf1 mx -all" |
| NS | Which servers are in charge of (authoritative for) the zone | a.iana-servers.net |
| SOA | Facts about the zone itself: who runs it, and how long to remember "no such name" | ns.icann.org. noc.dns.icann.org. … |
| PTR | The 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
| Try | To… |
|---|---|
dig NAME | look up the A record |
dig NAME MX | ask for another type (MX, AAAA, TXT, NS…) |
dig NAME +short | just the answer, nothing else |
dig @1.1.1.1 NAME | ask a particular DNS server instead of your usual one |
dig +trace NAME | walk the whole journey yourself: root, then .org, then the zone's own servers |
dig -x 203.0.113.10 | reverse 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.
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:
| What | Rocky / RHEL | Ubuntu / Debian |
|---|---|---|
Who writes /etc/resolv.conf | NetworkManager, with the real DNS servers in it | systemd-resolved: it just says nameserver 127.0.0.53 |
| Which servers are really used | cat /etc/resolv.conf or nmcli dev show | grep DNS | resolvectl status |
| A local DNS cache? | No: every lookup goes out to the server | Yes: systemd-resolved remembers answers |
| Empty the local cache | (nothing to empty) | sudo resolvectl flush-caches |
| Change the DNS servers | nmcli 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.
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?
✓ Check the truth with dig +trace, and look at the TTL in the cached answer.
2. dig shows the new address, but ping and your web app still use the old one. Where do you look?
✓ dig only asks DNS. getent follows the same order as your apps.
3. Which record says where a domain's email should go?
✓ MX = mail exchanger. The lowest number is tried first.