# DNS: how names become addresses

Follow a real lookup from the root servers down to the answer, read the record types you will use, and learn why DNS changes take time and how to make them safely.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 4. About 25 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/04-dns

## What you will learn

- Explain how a recursive resolver finds an answer, using a real lookup
- Read A, AAAA, CNAME, MX, NS and TXT records with dig
- Explain TTL and caching, and change a record without an outage
- Find which resolver your Linux machine uses and the order it checks

> **Lab status.** **Verified lab:** every lookup below was made with `dig` against real public DNS (root servers, `.com` servers, Cloudflare, Google and Cloudflare public resolvers). The output shown is real. <br />**Cloud lab:** none needed.

"It's always DNS" is a joke because it's so often true: a wrong record, a stale cache or a missing resolver looks
like "the site is down". This lesson makes DNS something you can check in seconds.

## Root, then .com, then the answer

> **Figure.** Left: curl example.com asks the stub resolver, which checks /etc/hosts and then asks the recursive resolver, 10.255.255.254 here. Right: on a cache miss the resolver asks a root server, which points to the .com servers; a .com server, which points to hera.ns.cloudflare.com; and that authoritative server, which answers 104.20.23.154 and 172.66.147.243 with TTL 300.

You can walk the same chain by hand. `+norecurse` asks each server only what it knows itself:

```text
$ dig @a.root-servers.net com NS +norecurse +noall +authority
com.          172800  IN  NS  l.gtld-servers.net.
com.          172800  IN  NS  j.gtld-servers.net.
...
$ dig @a.gtld-servers.net example.com NS +norecurse +noall +authority
example.com.  172800  IN  NS  hera.ns.cloudflare.com.
example.com.  172800  IN  NS  elliott.ns.cloudflare.com.
$ dig @hera.ns.cloudflare.com example.com A +norecurse +noall +answer
example.com.  300     IN  A   104.20.23.154
example.com.  300     IN  A   172.66.147.243
```

The root doesn't know example.com, but knows who runs `.com`. The `.com` servers know who runs example.com
(Cloudflare). Cloudflare's server is **authoritative**: it holds the real records. Your resolver does this walk once
and then caches each step, so the next lookup is one hop.

## Record types

> **Figure.** Six record types with real lab answers. A: example.com to 104.20.23.154. AAAA: google.com to 2404:6800:4013:813::64. CNAME: www.github.com to github.com. MX: github.com mail to github-com.mail.protection.outlook.com. NS: github.com served by NS1 and Route 53 name servers. TXT: github.com verification strings.

```text
$ dig github.com MX +short
0 github-com.mail.protection.outlook.com.
$ dig github.com NS +short
dns1.p08.nsone.net.
...
ns-421.awsdns-52.com.
```

GitHub runs its DNS on two providers at once (NS1 and Route 53), so one provider's outage doesn't take the name down.
When a name doesn't exist at all, the status says so:

```text
$ dig no-such-domain-xq7z1.com A | grep status
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 58160
```

`NXDOMAIN` is "no such name". `NOERROR` with no answer means the name exists but has no record of that type.
`SERVFAIL` means the resolver couldn't get an answer, often a broken delegation or DNSSEC problem.

## TTL and caching

> **Figure.** Four caches in a row: browser or app, OS resolver, recursive resolver (8.8.8.8 had 282 seconds left, 1.1.1.1 had 23), and the authoritative server with TTL 300. Below, the safe way to change a record: lower the TTL a day before, change it, keep the old server until traffic stops, then raise the TTL.

```text
$ dig @8.8.8.8 example.com A +noall +answer
example.com.   282  IN  A  172.66.147.243
$ dig @1.1.1.1 example.com A +noall +answer
example.com.   23   IN  A  104.20.23.154
```

The second column is the time left in that resolver's cache. Google had 282 of 300 seconds left, Cloudflare 23.
Both are correct; they just cached at different moments. That's all "DNS propagation" is.

## Resolvers on Linux

```text
$ grep hosts /etc/nsswitch.conf
hosts:          files dns
$ cat /etc/resolv.conf
nameserver 10.255.255.254
search lan
```

`files dns` means: check `/etc/hosts` first, then ask the `nameserver` in `/etc/resolv.conf` (WSL's resolver here;
on a VPC server it's the VPC's `.2` address). `search lan` appends `.lan` to short names. Two things to remember:
an entry in `/etc/hosts` beats DNS (great for testing, terrible when forgotten), and `dig` skips `/etc/hosts` while
`getent hosts name` follows the same order your apps do.

> **In production.** Kubernetes runs its own DNS (CoreDNS), so `orders.shop.svc.cluster.local` resolves inside the cluster. A Pod's `resolv.conf` has several search domains and `ndots:5`, which is why lookups of external names can be slow: each short name is tried with every search suffix first. AWS course lesson 8 covers Route 53.

## Mission: Trace a domain you use

Pick a domain you use at work or for a project. Walk its lookup from a root server, list its A, AAAA, MX, NS and TXT records, compare the TTL from two public resolvers, and note which resolver your own machine uses.

- You need: dig
- You'll have: ~/dns.txt

```sh
$ D=github.com
$ dig @a.root-servers.net ${D##*.} NS +norecurse +noall +authority | head -2 > ~/dns.txt
$ for t in A AAAA MX NS TXT; do echo "== $t"; dig $D $t +short; done >> ~/dns.txt
$ dig @8.8.8.8 $D A +noall +answer >> ~/dns.txt; dig @1.1.1.1 $D A +noall +answer >> ~/dns.txt
$ grep nameserver /etc/resolv.conf >> ~/dns.txt
```

Then follow the name servers from the first step down to an authoritative answer yourself.

## Try it

- Run `dig example.com A +noall +answer` and read the TTL column.
- Walk the lookup yourself from a root server with `+norecurse`.
- Look up MX, NS and TXT records for a domain you use.
- Ask two public resolvers and compare the TTLs they return.
- Do the mini mission and keep `~/dns.txt`.

## Quiz

1. You changed an A record ten minutes ago and some users still reach the old server. Why?
   - DNS is broken
   - **Resolvers and clients cached the old answer and keep it until its TTL expires** (answer)
   - The authoritative server didn't save the change
   - Browsers ignore DNS
   Why: "Propagation" is caches expiring. Lower the TTL well before a planned change.

2. dig returns status NXDOMAIN. What does that mean?
   - The server timed out
   - **The name doesn't exist at all** (answer)
   - The name exists but has no A record
   - DNSSEC failed
   Why: NXDOMAIN means no such name. A name that exists but lacks the requested type returns NOERROR with an empty answer.

3. Which record points one name at another name?
   - A
   - MX
   - **CNAME** (answer)
   - TXT
   Why: A CNAME is an alias. It can't be used at the zone apex, which is why DNS providers offer ALIAS or flattening.

## Resources

- [dig(1) manual](https://man7.org/linux/man-pages/man1/dig.1.html) (man7.org)
- [How DNS works (comic)](https://howdns.works/) (howdns.works)
- [Amazon Route 53 routing policies](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy.html) (AWS Docs)
- [DNS lookup tool](https://www.nslookup.io/) (nslookup.io)
