---
title: "DNS: How Names Become Addresses"
domain: networking
level: beginner
duration: "50 min"
objectives:
  - "Explain the DNS hierarchy and how a name is resolved: stub resolver, recursive resolver, root, top-level domain (TLD) and authoritative servers, caching and time to live (TTL)."
  - "Identify the common record types (A, AAAA, CNAME, MX, NS, TXT, PTR, SOA, CAA) and what each is used for."
  - "Query DNS with `dig` and interpret the answer, authority and additional sections and the response flags."
  - "Compare DNS transports: UDP and TCP on port 53, DNS over TLS (port 853) and DNS over HTTPS (port 443)."
  - "Assess DNS threats (cache poisoning, domain hijacking) and explain what DNSSEC protects and what it doesn't."
---

# DNS: How Names Become Addresses

## Why this matters
It's Monday morning and "the Internet is down." Email doesn't arrive, the intranet won't load, and the video call can't connect. Yet a `ping` to the router at `192.0.2.1` works, and so does a ping to a known server's IP address. The network is fine. What failed is the **Domain Name System (DNS)**: the service that turns `www.example.com` into an Internet Protocol (IP) address your computer can connect to. Without it, almost every application looks broken, because almost every application starts by looking up a name.

DNS also fails in quieter ways. A record changed on Friday still points to the old server for some users on Monday, because their resolver cached it. A company loses its domain for a few hours because an attacker logged in to its registrar account and changed the name servers. To run a network, publish a website or investigate an incident, you need to know how a name is resolved, what the answer means, and how that answer can be tampered with. This lesson gives you that.

## Learning objectives
- Explain the DNS hierarchy and how a name is resolved: stub resolver, recursive resolver, root, top-level domain (TLD) and authoritative servers, caching and time to live (TTL).
- Identify the common record types (A, AAAA, CNAME, MX, NS, TXT, PTR, SOA, CAA) and what each is used for.
- Query DNS with `dig` and interpret the answer, authority and additional sections and the response flags.
- Compare DNS transports: UDP and TCP on port 53, DNS over TLS (port 853) and DNS over HTTPS (port 443).
- Assess DNS threats (cache poisoning, domain hijacking) and explain what DNSSEC protects and what it doesn't.

## Key concepts

### The namespace: names, labels and zones
Think of DNS as a phone directory that no single company could print. Instead of one giant book, each organization keeps the page for its own names, and a chain of "ask over there" pointers leads you to the right page. DNS is a **distributed, hierarchical database**, defined in RFC 1034 (concepts) and RFC 1035 (implementation).

Names form an upside-down tree:

- At the top is the **root**, written as a single dot (`.`).
- Below it are the **top-level domains (TLDs)**: generic ones such as `com`, `org` and `net`, country codes such as `es` and `fr`, and the special `arpa` domain used for reverse lookups.
- Below each TLD are second-level domains such as `example.com`, and below those, as many levels as the owner wants: `www.example.com`, `mail.example.com`, `eu.api.example.com`.

Each part between dots is a **label** (up to 63 bytes); the full name is at most 255 bytes. A **fully qualified domain name (FQDN)** names a node all the way to the root, and strictly ends with a dot: `www.example.com.`. You usually leave the final dot out in browsers and commands, but it matters in zone files, as you'll see. Names are case-insensitive: `WWW.Example.COM` and `www.example.com` are the same name.

![The DNS namespace tree: the root "." at the top, served by 13 root server identities a–m with anycast; the TLDs com., org., net. and arpa. below it; example.com. under com., example.org. under org. and in-addr.arpa. under arpa.; www and mail under example.com. and www under example.org. A shaded zone surrounds example.com., www and mail, served by ns1/ns2.example.com; the line from com. to example.com. is labelled "delegation: NS + DS in com.". A strip at the bottom reads www.example.com. as host label, second-level label, TLD label and root, read right to left](../../../assets/diagrams/dns-namespace-tree.svg)

*Figure 1. The namespace is a tree; a zone is the part of it one set of servers is responsible for. The com. zone delegates example.com. to the organization's own name servers.*

The tree is split into **zones** for administration. A zone is a part of the tree managed as a unit and served by its own **authoritative name servers**. The `com` zone doesn't hold `www.example.com`; it only holds a **delegation**: "for anything under `example.com`, ask `ns1.example.com` and `ns2.example.org`." That delegation is a set of NS records in the parent zone. The point where responsibility changes hands is called a **zone cut**. Delegation is what lets DNS scale: the root zone has around 1,500 TLDs, `com` has well over a hundred million names, and neither has to know about your `www`.

Who manages what:
- The **Internet Assigned Numbers Authority (IANA)**, under the Internet Corporation for Assigned Names and Numbers (ICANN), manages the root zone.
- A **registry** operates each TLD (for example, Verisign for `com`).
- You buy a domain through a **registrar**, which tells the registry which name servers to delegate your domain to.

> [!CHECK]
> Which servers store the record that says `www.example.com` has the address `192.0.2.10`?
>
> - [ ] The root servers, because every name is ultimately under the root
> - [ ] The `com` TLD servers, because `example.com` is registered in `com`
> - [x] The authoritative servers for the `example.com` zone
> - [ ] Every recursive resolver on the Internet, permanently
>
> The root and `com` zones only hold delegations (NS records) that point further down the tree. The data itself lives in the `example.com` zone; resolvers only keep temporary cached copies.

### The cast: stub, recursive and authoritative servers
Four kinds of participants take part in every lookup:

| Role | What it does | Example |
|---|---|---|
| **Stub resolver** | A small library in your operating system. It doesn't walk the tree: it sends the whole question to a recursive resolver and waits for the final answer. | The resolver behind `getaddrinfo()` on your laptop. |
| **Recursive resolver** (also "caching resolver" or "full-service resolver") | Does the work: starts at the root, follows referrals down the tree, and caches what it learns. | Your Internet service provider's (ISP's) resolver, a company resolver, or a public one such as `1.1.1.1`, `8.8.8.8` or `9.9.9.9`. |
| **Root servers** | Serve the root zone: they know the servers of every TLD. | `a.root-servers.net` to `m.root-servers.net` |
| **Authoritative servers** | Hold the real data for a zone and answer for it with authority. TLD servers are authoritative for their TLD zone. | `a.gtld-servers.net` for `com`; `ns1.example.com` for `example.com` |

There are **13 root server identities**, lettered `a` to `m`, run by 12 independent organizations. Each letter is not one machine: it is announced with **anycast** from many sites, so there are well over a thousand root server instances worldwide and your query goes to a nearby one. Recursive resolvers find the root through a small built-in **root hints** file with those 13 names and addresses.

Two kinds of query happen:
- A **recursive query** (the stub sets the "recursion desired" flag, `rd`) says "give me the final answer, please do the work."
- **Iterative queries** are what the recursive resolver sends to root, TLD and authoritative servers. Each server either answers or replies with a **referral**: "I don't know, but these servers are responsible for the next level down."

**Caching** keeps this fast. Every record carries a **time to live (TTL)** in seconds, set by the zone owner. A resolver may reuse a record until its TTL runs out, then must ask again. The TTL you see in a cached answer counts down: if a record has a TTL of 300 and you ask the resolver 60 seconds after it was cached, you see 240. Resolvers also cache "this name doesn't exist" answers; this is **negative caching** (RFC 2308), and its lifetime comes from the zone's SOA record (see below).

TTL is a trade-off. Long TTLs (hours, days) mean fewer queries, faster answers and resilience if your servers are briefly down. Short TTLs (seconds, minutes) let you change records quickly, at the cost of more traffic. When you plan a change, lower the TTL a day or so in advance, make the change, then raise it again.

> [!CHECK]
> A record has a TTL of 3600. You change it at 10:00, and a user whose resolver cached the old value at 09:50 still reaches the old server. When will that user's resolver fetch the new value at the latest?
>
> - [ ] Immediately: authoritative servers push changes to every resolver
> - [x] At about 10:50, when the cached copy's one-hour TTL expires
> - [ ] At 11:00, one hour after the change was made
> - [ ] Only when the user reboots their computer
>
> DNS has no push mechanism to resolvers. The cached copy lives for the TTL it was received with, counted from when it was cached: 09:50 + 1 hour. Lower the TTL before a planned change to shrink this window.

### Resource records
Each piece of data in a zone is a **resource record (RR)** with five fields: owner name, TTL, class (always `IN`, Internet, in practice), type, and data (RDATA). Records with the same name, class and type form a **record set (RRset)**, such as two A records for one name. These are the types you'll meet most:

| Type | Holds | Example (data) | Used for |
|---|---|---|---|
| **A** | An IPv4 address | `192.0.2.10` | Reaching a host over IPv4. |
| **AAAA** | An IPv6 address (RFC 3596) | `2001:db8:10::10` | Reaching a host over IPv6. |
| **CNAME** | Canonical name: an alias to another name | `web.example.com.` | Pointing several names at one, for example at a content delivery network (CDN) host name. |
| **MX** | Mail exchanger: preference + host name | `10 mail.example.com.` | Where to deliver email for the domain. The lowest preference number is tried first. |
| **NS** | A name server for the zone | `ns1.example.com.` | Delegation (in the parent) and the list of authoritative servers (in the zone itself). |
| **TXT** | Free text strings | `"v=spf1 mx -all"` | Email policies (Sender Policy Framework, SPF; DomainKeys Identified Mail, DKIM; Domain-based Message Authentication, Reporting and Conformance, DMARC) and proofs of domain ownership. |
| **PTR** | Pointer: an IP address back to a name | `web.example.com.` | Reverse lookups, under `in-addr.arpa` (IPv4) and `ip6.arpa` (IPv6). |
| **SOA** | Start of authority: zone metadata | `ns1.example.com. hostmaster.example.com. 2026110601 7200 900 1209600 300` | One per zone: primary server, contact, serial number, timers, negative-caching TTL. |
| **CAA** | Certification Authority Authorization (RFC 8659) | `0 issue "ca.example.net"` | Which certificate authorities (CAs) may issue certificates for the domain. |

A few rules and details are worth knowing:

- **CNAME stands alone.** A name that has a CNAME can't have any other record (RFC 1034, clarified in RFC 2181). That is why you can't put a CNAME at the **zone apex** (`example.com` itself), which must have SOA and NS records. Some DNS providers offer non-standard "ALIAS" or "flattening" features to work around this.
- **MX and NS point to names, never to IP addresses**, and those names must not be CNAMEs. A domain that sends and receives no email can publish a "null MX", `0 .` (RFC 7505). `example.com` does exactly this.
- **Reverse DNS** writes the address backwards under `arpa`: `192.0.2.10` becomes `10.2.0.192.in-addr.arpa`. For IPv6, each hexadecimal digit (nibble) becomes a label under `ip6.arpa`. Reverse zones are delegated to whoever holds the address block (your ISP or hosting provider), not to the owner of the forward name, so forward and reverse records can disagree.
- **SOA fields**: MNAME (primary server), RNAME (the contact mailbox, with the `@` written as a dot: `hostmaster.example.com.`), **serial** (a version number that must go up on every change; secondaries compare it to decide whether to transfer the zone), refresh, retry and expire (timers for secondary servers), and minimum. Today the minimum field is the **negative-caching TTL**: a resolver caches a "no such name" answer for the smaller of the SOA record's own TTL and this value (RFC 2308).
- **CAA is checked by CAs, not by browsers.** Before issuing a certificate, a CA must look up CAA records and refuse if it isn't listed. It reduces the risk of mis-issuance; it does nothing for certificates that already exist.
- **Glue records.** If `example.com` is delegated to `ns1.example.com`, a resolver can't look up `ns1.example.com` without first knowing the servers for `example.com`: a chicken-and-egg loop. The parent zone therefore also publishes the address of `ns1.example.com`, as **glue** in the additional section of its referral.

> [!CHECK]
> You want `shop.example.com` to follow your e-commerce provider's host name `stores.example.net`, whose IP addresses change without notice. Which record do you create?
>
> - [ ] An A record for `shop.example.com` with the provider's current address
> - [x] A CNAME record from `shop.example.com` to `stores.example.net.`
> - [ ] An MX record from `shop.example.com` to `stores.example.net.`
> - [ ] A PTR record from `stores.example.net` to `shop.example.com`
>
> A CNAME makes `shop.example.com` an alias, so resolvers follow it and always get the provider's current addresses. An A record would go stale; MX is only for mail; PTR is for reverse lookups.

### Reading a DNS answer with `dig`
`dig` (domain information groper), from the BIND (Berkeley Internet Name Domain) utilities, is the standard tool to ask DNS questions and see the whole answer, not just an address. Its basic form is:

```bash
dig [@server] [name] [type] [+options]
```

`@server` is the resolver or authoritative server to ask (by default, the one your system uses); `type` defaults to `A`. Useful options: `+short` (just the data), `+noall +answer` (only the answer section), `+trace` (walk the tree from the root yourself), `+norec` (clear the `rd` flag), `+tcp`, `+dnssec`, `-x <address>` (reverse lookup), and on BIND 9.18 and later `+tls` and `+https`.

A DNS message has a header and four sections:

| Section | Contains |
|---|---|
| **Header** | Message ID, **response code (RCODE)**, flags, and how many records are in each section. |
| **Question** | What was asked: name, class, type. |
| **Answer** | Records that answer the question, including any CNAME chain followed. |
| **Authority** | Records about the zone: NS records in a referral, or the SOA record in a negative answer. |
| **Additional** | Helpful extras, such as glue addresses for the NS names. `dig` also shows the **Extension Mechanisms for DNS (EDNS(0))** "OPT pseudo-section" here. |

![An annotated dig answer for www.example.com. Header: status NOERROR, flags qr rd ra ad, record counts. EDNS(0) OPT pseudo-section: version 0, flags do, UDP size 1232. Question: www.example.com IN A. Answer, with columns name, TTL, class, type and data: www.example.com 3600 IN CNAME web.example.com, then web.example.com 300 IN A 192.0.2.10. Statistics: query time 24 ms, server 203.0.113.53 port 53 over UDP, message size 95. Legend: qr response, aa authoritative answer, rd recursion desired, ra recursion available, ad DNSSEC validated, tc truncated, retry over TCP](../../../assets/diagrams/dns-dig-anatomy.svg)

*Figure 2. Anatomy of a `dig` answer. Every answer record has the same five columns: name, TTL, class, type and data.*

The **flags** in the header tell you who answered and how:

| Flag | Meaning | What it tells you |
|---|---|---|
| `qr` | Query response | This is a response, not a query. |
| `aa` | Authoritative answer | It came from a server authoritative for the zone, not from a cache. |
| `tc` | Truncated | The answer didn't fit in the UDP message; retry over TCP. |
| `rd` | Recursion desired | The client asked for recursion (copied from the query). |
| `ra` | Recursion available | The server offers recursion: it is a recursive resolver. |
| `ad` | Authentic data | The resolver validated the answer with DNSSEC. |
| `cd` | Checking disabled | The client asked the resolver not to validate DNSSEC. |

And the **status** (RCODE) tells you the outcome:

| Status | Meaning |
|---|---|
| `NOERROR` | Success. If the answer section is empty, the name exists but has no record of that type; this is called **NODATA**. |
| `NXDOMAIN` | Non-existent domain: the name doesn't exist at all. The authority section carries the zone's SOA for negative caching. |
| `SERVFAIL` | The resolver couldn't get a valid answer: authoritative servers unreachable or broken, or DNSSEC validation failed. |
| `REFUSED` | The server won't answer this query, for example an authoritative server asked about a zone it doesn't serve, or a resolver that only serves its own clients. |

> [!CHECK]
> You query `dig @203.0.113.53 www.example.com` and the header shows `status: NOERROR` and `flags: qr rd ra`, with two A records in the answer. What can you conclude?
>
> - [ ] The answer came directly from the authoritative server for `example.com`
> - [x] A recursive resolver answered, possibly from its cache, and did not validate it with DNSSEC
> - [ ] The response was truncated and must be retried over TCP
> - [ ] The name doesn't exist, but the resolver returned a default address
>
> `ra` shows a recursive resolver; without `aa`, the answer isn't authoritative, so it may well come from the cache. There is no `ad` flag, so no DNSSEC validation was reported, and no `tc` flag, so nothing was truncated.

### Transports: UDP, TCP, TLS and HTTPS
Classic DNS uses **port 53** over both the User Datagram Protocol (UDP) and the Transmission Control Protocol (TCP):

- **UDP 53** is the default for queries: one datagram out, one back, no handshake. It's fast, but the original limit was 512 bytes of DNS payload (RFC 1035).
- **EDNS(0)** (RFC 6891) lets the client advertise a bigger UDP buffer, for example 1232 bytes, the value recommended by the DNS Flag Day 2020 initiative to avoid IP fragmentation. EDNS also carries the `do` ("DNSSEC OK") bit and other options.
- If an answer is still too large, the server sets the **`tc` (truncated)** flag, and the client repeats the query over **TCP 53**. TCP is also used for **zone transfers** between primary and secondary servers (AXFR for a full transfer, RFC 5936; IXFR for incremental, RFC 1995). RFC 7766 makes TCP support **required** for DNS implementations, not optional: a firewall that allows only UDP 53 breaks large answers, including many DNSSEC answers.

UDP and TCP on port 53 are **plaintext**: anyone on the path (the café's Wi-Fi, your ISP) can read every name you look up and, without DNSSEC, alter the answers. Two standards encrypt the hop between the client and its recursive resolver:

- **DNS over TLS (DoT)** (RFC 7858) wraps DNS in Transport Layer Security (TLS) on **TCP port 853**. Its dedicated port makes it easy for network administrators to recognize, allow or block. Android's "Private DNS" setting uses DoT.
- **DNS over HTTPS (DoH)** (RFC 8484) sends DNS messages as Hypertext Transfer Protocol (HTTP) requests over HTTPS (HTTP Secure) on **TCP port 443** (and HTTP/3 over UDP 443), with the media type `application/dns-message`. To an observer it looks like any other web traffic. Browsers often enable it on their own, per application, with a resolver the browser vendor chose.

![Four transports compared. UDP 53: DNS over UDP, RFC 1035, the default, one datagram each way, large answers set tc and retry over TCP; plaintext. TCP 53: DNS over TCP, required by RFC 7766, large answers and zone transfers, 2-byte length prefix; plaintext. DoT 853: DNS over TLS, RFC 7858, TCP 853, dedicated port, easy to spot, allow or block; encrypted. DoH 443: DNS over HTTPS, RFC 8484, HTTPS 443, looks like web traffic, often set per browser; encrypted. An on-path observer sees every name and answer for UDP and TCP 53, but only a TLS connection to the resolver on 853 or an HTTPS connection on 443](../../../assets/diagrams/dns-transports.svg)

*Figure 3. DNS transports. Encryption hides the names from on-path observers, but the resolver you send them to still sees everything.*

> [!NOTE]
> DNS over QUIC (DoQ, RFC 9250) also exists and uses UDP port 853. DoT, DoH and DoQ normally protect only the hop from your device to its recursive resolver; the resolver's own queries to root, TLD and authoritative servers are usually still plaintext UDP or TCP on port 53.

> [!CHECK]
> A company firewall allows outbound UDP 53 to the internal resolver and blocks everything else to port 53 and 853. A browser on a laptop still resolves names through an external resolver. Which transport is it most likely using?
>
> - [ ] DNS over TCP on port 53
> - [ ] DNS over TLS on port 853
> - [x] DNS over HTTPS on port 443
> - [ ] Plain UDP on port 53 to the external resolver
>
> TCP 53 and 853 are blocked, and the plaintext path goes only to the internal resolver. DoH uses port 443 like any website, so a port-based firewall can't tell it apart from normal browsing.

### DNSSEC: signed answers
Plain DNS has no way to prove that an answer is genuine. **DNS Security Extensions (DNSSEC)** (RFC 4033, 4034 and 4035) add digital signatures to zones, so a validating resolver can check that records really come from the zone owner and weren't changed on the way.

DNSSEC adds a few record types:
- **DNSKEY**: the zone's public keys. Usually a **key-signing key (KSK)** that signs the key set, and a **zone-signing key (ZSK)** that signs the other records.
- **RRSIG** (resource record signature): a signature over one RRset, with validity dates.
- **DS** (delegation signer): a hash of the child zone's KSK, published in the **parent** zone. This is what links one zone to the next.
- **NSEC / NSEC3** (Next Secure): signed proof that a name or type does **not** exist (**authenticated denial of existence**), so an attacker can't forge an NXDOMAIN either.

Validation follows a **chain of trust** down the tree. The resolver is configured with a **trust anchor**: the root zone's KSK (or its digest). It checks the root's DNSKEY against the anchor, the `com` DS record against the root's keys, `com`'s DNSKEY against that DS, and so on down to the RRSIG over the record you asked for.

![DNSSEC chain of trust in four steps. 1. Trust anchor: the root key (KSK) digest configured in the validating resolver. 2. Root zone: DNSKEY matches the anchor; DS for com. with RRSIG, signed by the root. 3. com. zone: DNSKEY matches the root's DS; DS for example.com. with RRSIG, signed by com. 4. example.com.: DNSKEY matches com.'s DS; RRSIG over the www A record 192.0.2.10. If all links are valid the resolver sets ad; if any link is broken it returns SERVFAIL; DNSSEC gives no confidentiality, use DoT or DoH for that](../../../assets/diagrams/dns-dnssec-chain.svg)

*Figure 4. The DNSSEC chain of trust. Each parent vouches for its child's key with a signed DS record.*

If every link verifies, the resolver returns the answer with the `ad` flag. If any link is broken (a bad signature, an expired RRSIG, a DS that doesn't match the child's key), a validating resolver returns **SERVFAIL** instead of a possibly forged answer.

What DNSSEC **does** protect:
- **Data origin authentication** and **integrity**: the records come from the zone's key holder and weren't modified.
- Authenticated denial of existence (NSEC/NSEC3).

What it **does not** protect:
- **Confidentiality**: signed answers are still readable by anyone on the path. Use DoT or DoH for privacy.
- **Availability**: it doesn't stop denial-of-service attacks, and bigger signed answers can even make amplification worse.
- **The last mile**, unless the stub validates itself: most stubs trust the `ad` flag from the resolver, so the path from the resolver to your device must be trusted or encrypted.
- **Wrong but correctly signed data**: if an attacker controls your registrar account or your DNS provider, they can publish records (and DS keys) that validate perfectly.

> [!CHECK]
> A validating resolver receives an answer for `www.example.com` whose RRSIG signature doesn't match the A record. What does it return to the client?
>
> - [ ] The A record with the `ad` flag cleared, so the client can decide
> - [x] `SERVFAIL`, because the data failed validation
> - [ ] `NXDOMAIN`, because the record is treated as non-existent
> - [ ] The A record encrypted, so attackers can't read it
>
> A validating resolver refuses to hand out bogus data and returns SERVFAIL. A client that really needs the unvalidated data can set the `cd` (checking disabled) flag. DNSSEC never encrypts anything.

## How it works
Follow a laptop that opens `https://www.example.com` with an empty cache everywhere. Its stub resolver is configured (by DHCP, the Dynamic Host Configuration Protocol, or by IPv6 Router Advertisements) to use the recursive resolver `203.0.113.53`.

![Resolving www.example.com in four numbered steps between the stub resolver on your laptop, the recursive resolver 203.0.113.53, the root server a.root-servers.net, the TLD server a.gtld-servers.net and the authoritative server ns1.example.com. 1: the stub asks "A? www.example.com" with rd. The resolver has a cache miss and starts from the root hints. 2: it asks the root, which refers it to the com. servers (a.gtld-servers.net plus glue). 3: it asks the TLD server, which refers it to ns1.example.com plus glue. 4: it asks the authoritative server, which answers with the aa flag: CNAME web.example.com. with TTL 3600 and web A 192.0.2.10 with TTL 300. The resolver caches every record and referral for its own TTL and answers the stub with ra](../../../assets/diagrams/dns-resolution.svg)

*Figure 5. A full resolution. The stub sends one recursive query; the recursive resolver makes the iterative queries. Numbers match the steps below.*

1. **Stub to resolver.** The browser asks the operating system for the address of `www.example.com`. The stub sends a query with `rd` set to `203.0.113.53` over UDP port 53, from a random source port and with a random 16-bit message ID. (Modern stubs send separate A and AAAA queries, for IPv4 and IPv6.)
2. **Resolver to root.** The resolver has nothing cached, so it picks a root server from its root hints. The root doesn't know `www.example.com` but returns a **referral**: the NS records of `com` in the authority section, and their addresses as glue in the additional section. No `aa` flag: it isn't the answer.
3. **Resolver to TLD.** The resolver asks a `com` server, which returns another referral: `example.com` is served by `ns1.example.com` and `ns2.example.org`, with glue for `ns1.example.com` (it's inside the zone being delegated, so it couldn't be found otherwise).
4. **Resolver to authoritative.** The resolver asks `ns1.example.com`, which answers with `aa` set: `www.example.com` is a CNAME for `web.example.com`, and because the same server is authoritative for that name too, it includes `web.example.com A 192.0.2.10`. (If the CNAME pointed into another domain, the resolver would start a new lookup for the target.)
5. **Caching and answer.** The resolver caches everything it learned: the root's and `com`'s referrals (TTLs of days), the CNAME for 3600 seconds and the A record for 300 seconds. It returns the full answer to the stub with `ra` set. The stub may cache it too, and the browser connects to `192.0.2.10`.

The next laptop that asks the same resolver for `www.example.com` within five minutes gets an answer in about a millisecond, straight from the cache. A lookup for `mail.example.com` skips the root and `com` (their referrals are cached) and goes straight to `ns1.example.com`. That's why a busy resolver almost never talks to the root.

> [!NOTE]
> Figure 5 shows the classic behaviour, where the resolver sends the full name to every server. Many modern resolvers use **QNAME minimisation** (RFC 9156): they ask the root only about `com.` and the TLD only about `example.com.`, so fewer servers learn the full name you're looking up. The referrals and the final answer are the same.

You can watch steps 2 to 4 yourself with `dig +trace www.example.com`, which makes `dig` do the iterative queries instead of your resolver. You'll do that in the lab.

> [!CHECK]
> In step 2, the root server replied without the `aa` flag and with an empty answer section. Why did the resolver not treat that as a failure?
>
> - [ ] Root servers never set `aa`, so the resolver ignores the flag
> - [x] It was a referral: the authority section listed the `com` name servers to ask next
> - [ ] The empty answer means `NXDOMAIN`, and the resolver retries at another root server
> - [ ] The answer was truncated, so the resolver retried over TCP
>
> A referral is a normal step of iterative resolution: no answer yet, but NS records (and glue) for the zone one level down. The resolver follows it until an authoritative server answers with `aa`.

## Worked example
Example Corp runs the zone `example.com` on two authoritative servers: `ns1.example.com` (`198.51.100.10`, in its own data centre) and `ns2.example.org` (at a different provider, under a different TLD, so one outage can't take out both). The web site is behind `web.example.com`, and email is handled by `mail.example.com`. All addresses are from the documentation ranges of RFC 5737 and RFC 3849.

**The zone file.** Zone files use the "master file" format of RFC 1035. `$ORIGIN` sets the default domain, `@` means the origin itself, `$TTL` is the default TTL, and a name **without** a final dot gets the origin appended:

```text
$ORIGIN example.com.
$TTL 3600
@     IN  SOA   ns1.example.com. hostmaster.example.com. (
                2026110601 ; serial (YYYYMMDDnn)
                7200       ; refresh: secondaries check every 2 h
                900        ; retry: if the check failed, retry after 15 min
                1209600    ; expire: secondaries stop answering after 14 days without contact
                300 )      ; minimum: negative-caching TTL, 5 min
      IN  NS    ns1.example.com.
      IN  NS    ns2.example.org.
      IN  MX    10 mail.example.com.
      IN  TXT   "v=spf1 mx -all"
      IN  CAA   0 issue "ca.example.net"
ns1   IN  A     198.51.100.10
www   IN  CNAME web.example.com.
web   300 IN A     192.0.2.10
web   300 IN AAAA  2001:db8:10::10
mail  IN  A     192.0.2.25
mail  IN  AAAA  2001:db8:10::25
```

Read it line by line:
- The SOA says `ns1.example.com` is the primary and `hostmaster@example.com` is the contact. Serial `2026110601` means "6 November 2026, change 01"; any edit must increase it.
- Two NS records list the authoritative servers. The same NS records must also exist in the `com` zone (the delegation, set through the registrar), plus glue for `ns1.example.com`.
- MX: mail for `@example.com` goes to `mail.example.com`, preference 10. TXT: the SPF policy says only the MX hosts send mail for this domain (`-all`: reject the rest). CAA: only the CA `ca.example.net` may issue certificates.
- `www` is an alias for `web`. `web` has its own TTL of 300 seconds, because the team moves it between servers more often than anything else; every other record uses the default 3600.

**The queries.** Asking a recursive resolver:

```bash
dig www.example.com +noall +answer
```

```text
www.example.com.	3600	IN	CNAME	web.example.com.
web.example.com.	300	IN	A	192.0.2.10
```

Asked again 100 seconds later, the same resolver shows `3500` and `200`: both copies are counting down in its cache. Asking the authoritative server directly with `dig @198.51.100.10 www.example.com +norec` returns the same records with the full TTLs and `flags: qr aa`.

A name that doesn't exist:

```bash
dig nosuchhost.example.com
```

```text
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 5150
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
...
;; AUTHORITY SECTION:
example.com.		300	IN	SOA	ns1.example.com. hostmaster.example.com. 2026110601 7200 900 1209600 300
```

The SOA in the authority section tells the resolver how long to cache this NXDOMAIN: the smaller of the SOA record's TTL (3600) and its minimum field (300), so **300 seconds**. Many authoritative servers already write that smaller value as the SOA's TTL in negative answers, as here. If someone creates `nosuchhost.example.com` one minute later, this resolver keeps saying "doesn't exist" for up to four more minutes.

**Reverse DNS.** The ISP that holds `192.0.2.0/24` delegated the reverse zone `2.0.192.in-addr.arpa` to Example Corp, which publishes:

```text
10.2.0.192.in-addr.arpa.   3600  IN  PTR  web.example.com.
```

`dig -x 192.0.2.10 +short` returns `web.example.com.`. Mail servers often check that a sending server's PTR record and A record match (forward-confirmed reverse DNS), so `mail.example.com` should have one too.

**A planned move.** On Monday, Example Corp will move the web site to `192.0.2.20`:
1. **Friday:** confirm `web` has a TTL of 300 (already low). If it were 86400, they'd lower it at least a day before, so old long-lived copies expire first.
2. **Monday 09:00:** change the A record to `192.0.2.20` and raise the serial to `2026110901`. The primary reloads; secondaries see the higher serial and transfer the zone.
3. **Until 09:05:** some users still reach `192.0.2.10` from caches. Keep the old server running for at least one TTL (in practice longer: some clients and applications cache beyond the TTL).
4. **After a few days without problems:** raise the TTL again if you want fewer queries.

## Common pitfalls
- **Forgetting to bump the SOA serial.** The primary may keep serving the old zone, and secondaries won't transfer the change. Use a date-based serial (`YYYYMMDDnn`) and increase it on every edit.
- **Missing final dot in a zone file.** `www IN CNAME web.example.com` (no dot) becomes `web.example.com.example.com.`. Write FQDNs with the dot, or use relative names on purpose.
- **Expecting changes to be instant.** Old answers live until their TTL expires, plus negative caching for names that didn't exist. Lower TTLs before a change; "DNS propagation" is just caches expiring.
- **CNAME at the apex or next to other records.** `example.com CNAME …` conflicts with the SOA and NS records. Use A/AAAA at the apex, or your provider's ALIAS/flattening feature.
- **Blocking TCP 53.** Large answers (DNSSEC, many records) come back with `tc` and then fail. RFC 7766 requires TCP support; allow it wherever UDP 53 is allowed.
- **Lame delegation.** The parent's NS records point to a server that doesn't actually serve the zone (it answers REFUSED or not at all). Keep the NS records at the registrar and in the zone in sync, and test each server with `dig @server name +norec`.
- **Testing against the wrong server.** `dig name` asks your resolver, which may have an old cached answer. Use `dig @<authoritative> name +norec` to see what the zone really contains.
- **Reading NODATA as NXDOMAIN.** `NOERROR` with an empty answer means the name exists but has no record of that type (for example no AAAA); `NXDOMAIN` means the name doesn't exist at all.

## Security considerations
DNS was designed in the 1980s for a cooperative network. Most attacks exploit either the lack of authentication in classic DNS or the accounts that control a domain.

**Cache poisoning.** An attacker tries to get a recursive resolver to cache a forged record, so every client of that resolver is sent to the attacker's address. Over UDP, the resolver accepts the first response that matches its pending query: the same question, the right server address, the right destination port and the 16-bit message ID. An off-path attacker sends a flood of forged responses guessing those values, racing the real server.
- In 2008, Dan Kaminsky showed how to make this practical. Instead of waiting for a cached record to expire, the attacker triggers lookups of many random, non-existent names (`a1.example.com`, `a2.example.com`…), each one a fresh race, and puts a forged **referral** in the fake responses that hijacks the whole `example.com` zone.
- The urgent fix was **source port randomization** (RFC 5452): resolvers send each query from a random UDP source port, so the attacker must guess about 16 bits of port as well as 16 bits of ID. That makes blind guessing far harder, but it is a probability game, not a proof.
- Other hardening: accept only in-bailiwick data (records about the zone the server was asked about), randomize the letter case of query names ("0x20" encoding), use DNS cookies (RFC 7873) and run resolvers that are kept up to date.
- The real fix is **DNSSEC validation**: a forged record has no valid signature, so a validating resolver rejects it. This works only for signed zones, and only if the resolver validates.

**Domain hijacking.** Why attack DNS packets when you can change the source of truth? Attackers phish or guess the password of the **registrar** account (or of the DNS hosting account) and change the NS records or the records themselves. Traffic, email and even new TLS certificates (issued to whoever controls DNS) then go to the attacker. DNSSEC does not help: the attacker publishes new, correctly signed data or removes the DS record. Defenses:
- Multi-factor authentication (MFA) and limited, audited access on registrar and DNS provider accounts.
- **Registrar lock** (Extensible Provisioning Protocol, EPP, status codes such as `clientTransferProhibited` and `clientUpdateProhibited`) and, for important domains, **registry lock**, which requires an out-of-band manual verification before the registry accepts changes.
- Monitoring of NS, DS and key records, and of Certificate Transparency logs for unexpected certificates. CAA records narrow which CAs can issue.
- Watch for **dangling records**: a CNAME that still points to a deleted cloud resource (a storage bucket, an app host name) can let someone else claim that resource and serve content under your name ("subdomain takeover"). Delete the DNS record when you delete the resource.

**Privacy and DoT/DoH trade-offs.** Plain DNS reveals every site you visit to anyone on the path. DoT and DoH hide queries from the local network and the ISP. But:
- The **resolver operator still sees everything**: you move trust, you don't remove it. Choose a resolver whose privacy policy you accept.
- **DoH can bypass enterprise controls.** An application that uses its own DoH resolver skips the company resolver, its malware-domain filtering and its logs. Organizations manage this with endpoint policies (for example browser enterprise settings that disable or point DoH to the internal resolver) rather than port blocking.
- Encrypted DNS doesn't hide the destination IP address or, in most TLS connections, the server name sent in the TLS handshake.

**Open resolvers and amplification.** A recursive resolver that answers anyone on the Internet (an **open resolver**) can be abused for **reflection and amplification** distributed denial-of-service (DDoS) attacks. Conceptually: the attacker sends small UDP queries with the victim's address forged as the source; the resolver sends much larger answers to the victim. A 60-byte query can produce a response dozens of times larger, especially with DNSSEC records. Defenses:
- Don't run open resolvers: restrict recursion to your own clients (by address range or interface).
- On authoritative servers, use **response rate limiting (RRL)** and minimal responses to `ANY` queries (RFC 8482).
- Networks should apply source address validation (BCP 38, RFC 2827), so forged source addresses never leave them.

Other things to keep in mind: **zone transfers** (AXFR) should be allowed only to your own secondaries (and preferably authenticated with TSIG, transaction signatures, RFC 8945); otherwise anyone can download your whole zone. DNS is also a channel for malware command-and-control and data exfiltration ("DNS tunneling"), which is why resolver logs are a valuable security data source.

> [!IMPORTANT]
> Only test DNS servers and domains you own or are explicitly authorized to test. Normal lookups of public names are fine; zone transfer attempts, floods, spoofed queries or "testing" someone else's resolver for openness or amplification are not. The lab in this module queries public names normally and runs its own server on your machine.

## Summary
- DNS is a distributed, hierarchical database: root (`.`), TLDs, and zones delegated with NS records (plus glue) down the tree. 13 root server identities (`a`–`m`), each anycast, serve the root zone.
- Your stub resolver sends one recursive query (`rd`); the recursive resolver follows referrals from root to TLD to authoritative servers with iterative queries and caches each record for its TTL. Negative answers are cached too (RFC 2308), for the SOA's minimum or TTL, whichever is lower.
- Know the core records: A/AAAA (addresses), CNAME (alias; never at the apex or beside other data), MX (mail, lowest preference first), NS (delegation), TXT (SPF, DKIM, DMARC, verification), PTR (reverse, under `in-addr.arpa`/`ip6.arpa`), SOA (zone metadata, serial), CAA (which CAs may issue).
- `dig` shows the header (status: NOERROR, NXDOMAIN, SERVFAIL, REFUSED; flags: `qr`, `aa`, `tc`, `rd`, `ra`, `ad`, `cd`) and the question, answer, authority and additional sections. `aa` means authoritative; `ra` means a recursive resolver; `ad` means DNSSEC-validated.
- DNS uses UDP 53 by default and TCP 53 for large answers and zone transfers (TCP support is required, RFC 7766); EDNS(0) raises the UDP size. DoT (TCP 853) and DoH (443) encrypt the client-to-resolver hop.
- Cache poisoning is countered by source port and ID randomization and, properly, by DNSSEC validation. DNSSEC gives authenticity and integrity through a chain of trust (DNSKEY, RRSIG, DS); it gives no confidentiality and doesn't stop registrar account hijacking. Protect registrar accounts with MFA and locks, and never run an open resolver.

## Glossary
| Term | Definition |
|---|---|
| Domain Name System (DNS) | The distributed, hierarchical system that maps names to addresses and other data (RFC 1034, RFC 1035). |
| Distributed, hierarchical database | Data split across many independently run servers, organized as a tree in which each level delegates to the next. |
| Root | The top of the DNS tree, written `.`; the root zone lists the servers of every TLD. |
| Top-level domain (TLD) | A domain directly below the root, such as `com`, `org`, `es` or `arpa`. |
| Label | One part of a domain name between dots; at most 63 bytes. |
| Fully qualified domain name (FQDN) | A complete name up to the root, strictly ending with a dot, such as `www.example.com.`. |
| Zone | A part of the namespace administered as a unit and served by its own authoritative servers. |
| Authoritative name server | A server that holds a zone's data and answers for it with the `aa` flag. |
| Delegation | NS records in a parent zone that hand responsibility for a child zone to other servers. |
| Zone cut | The boundary between a parent zone and a delegated child zone. |
| Internet Assigned Numbers Authority (IANA) | The function, operated under ICANN, that manages the DNS root zone and other Internet number registries. |
| Registry | The organization that operates a TLD's zone and database. |
| Registrar | The company through which you register a domain; it sends your NS (and DS) records to the registry. |
| Stub resolver | The simple DNS client in an operating system; it asks a recursive resolver for the final answer. |
| Recursive resolver | A server that resolves names on behalf of clients by following referrals, and caches the results. |
| Root servers | The servers authoritative for the root zone: 13 identities, `a` to `m.root-servers.net`. |
| Anycast | Announcing the same IP address from many locations so that each client reaches the nearest one. |
| Root hints | The built-in list of root server names and addresses a recursive resolver starts from. |
| Recursive query | A query with the `rd` flag asking the server to return the final answer. |
| Iterative query | A query answered with either the data or a referral, without the server resolving further on the client's behalf. |
| Referral | A response with no answer but with NS records (and glue) for the zone one level closer to the name. |
| Caching | Keeping received records for reuse until their TTL expires. |
| Time to live (TTL) | How many seconds a record may be cached. |
| Negative caching | Caching the fact that a name or type doesn't exist, for a time derived from the zone's SOA (RFC 2308). |
| Resource record (RR) | One DNS record: owner name, TTL, class, type and data. |
| A / AAAA record | A record holding an IPv4 / IPv6 address for a name. |
| CNAME record | Canonical name: makes a name an alias of another name; it can't coexist with other records for the same name. |
| MX record | Mail exchanger: a preference number and the host that receives email for the domain. |
| NS record | Names an authoritative server for a zone; in the parent zone it forms the delegation. |
| TXT record | Free-text strings, used for SPF, DKIM, DMARC and domain-ownership proofs. |
| PTR record | Pointer from an address (under `in-addr.arpa` or `ip6.arpa`) back to a name; used for reverse lookups. |
| SOA record | Start of authority: a zone's primary server, contact, serial, timers and negative-caching TTL. |
| CAA record | Certification Authority Authorization: lists the CAs allowed to issue certificates for the domain (RFC 8659). |
| Record set (RRset) | All records with the same name, class and type. |
| Zone apex | The top name of a zone (for example `example.com` itself), where the SOA and NS records live. |
| Serial | The SOA version number that must increase on every zone change. |
| Glue record | An address record for a name server, published in the parent zone so that a delegation to a name inside the child zone can be followed. |
| Response code (RCODE) | The status of a DNS response, such as NOERROR, NXDOMAIN, SERVFAIL or REFUSED. |
| Header / question / answer / authority / additional | The five parts of a DNS message: metadata and flags; what was asked; the records that answer; NS or SOA records about the zone; helpful extras such as glue. |
| `aa` / `rd` / `ra` / `tc` / `ad` / `cd` flags | Authoritative answer / recursion desired / recursion available / truncated / authentic data (DNSSEC-validated) / checking disabled. |
| NODATA | A NOERROR response with an empty answer: the name exists but has no records of the requested type. |
| Extension Mechanisms for DNS (EDNS(0)) | An extension (RFC 6891) that allows larger UDP messages, the DNSSEC OK bit and other options. |
| Zone transfer | Copying a zone from a primary to a secondary server over TCP: AXFR (full) or IXFR (incremental). |
| DNS over TLS (DoT) | DNS carried inside TLS on TCP port 853 (RFC 7858). |
| DNS over HTTPS (DoH) | DNS messages carried in HTTPS requests on port 443 (RFC 8484). |
| DNS Security Extensions (DNSSEC) | Extensions that sign DNS data so resolvers can verify its origin and integrity (RFC 4033–4035). |
| DNSKEY / RRSIG / DS / NSEC(3) | DNSSEC records: a zone's public key / a signature over an RRset / a parent's hash of a child's key / signed proof of non-existence. |
| Key-signing key (KSK) / zone-signing key (ZSK) | The DNSSEC key that signs a zone's DNSKEY set / the key that signs the zone's other records. |
| Authenticated denial of existence | Signed proof (NSEC or NSEC3 records) that a name or record type does not exist. |
| Chain of trust | The sequence of DS and DNSKEY records linking a trust anchor to the zone being validated. |
| Trust anchor | A public key (or its digest) a validating resolver trusts without further proof; normally the root KSK. |
| Data origin authentication | Proof that data was published by the holder of the zone's keys. |
| Integrity | Assurance that data was not modified in transit. |
| Confidentiality | Protection of data from being read by unauthorized parties; DNSSEC does not provide it. |
| QNAME minimisation | A resolver behaviour that sends each server only as much of the name as it needs (RFC 9156). |
| Cache poisoning | Getting a resolver to cache forged DNS data. |
| Source port randomization | Sending each query from a random UDP port to make forged responses harder to match (RFC 5452). |
| Domain hijacking | Taking control of a domain, typically through its registrar or DNS hosting account, and changing its records. |
| Registrar lock / registry lock | Status flags that block transfers or changes of a domain; registry lock adds a manual out-of-band verification. |
| Dangling record | A DNS record that points to a resource that no longer exists, which someone else might claim. |
| Open resolver | A recursive resolver that answers queries from anyone on the Internet. |
| Reflection and amplification | A DDoS technique: queries with the victim's forged address trigger larger responses sent to the victim. |
| Response rate limiting (RRL) | An authoritative server feature that limits identical responses to the same destination to blunt amplification. |

## Check your understanding
1. Explain, step by step, what happens between typing `www.example.org` in a browser and the browser getting an address, if the recursive resolver's cache is empty. Which messages are recursive and which are iterative?
2. Your zone has `$TTL 86400` and you must move `www` to a new IP address next Tuesday. Write a short plan with dates and TTL values, and explain why the old server must stay up after the change.
3. Why can't you create `example.com. CNAME hosting.example.net.`, and what can you do instead?
4. `dig @198.51.100.10 example.com MX` returns `status: REFUSED`. Give two possible causes and the command you would run next.
5. A security team proposes blocking port 853 to "stop employees from hiding DNS." Explain why this alone won't work and what else they need to consider.
6. Your domain is fully DNSSEC-signed. An attacker phishes your registrar password. Explain why DNSSEC doesn't stop the attack, and list three controls that would.

## References
- [RFC 1034](https://www.rfc-editor.org/rfc/rfc1034) — Domain Names: Concepts and Facilities.
- [RFC 1035](https://www.rfc-editor.org/rfc/rfc1035) — Domain Names: Implementation and Specification.
- [RFC 2181](https://www.rfc-editor.org/rfc/rfc2181) — Clarifications to the DNS Specification.
- [RFC 2308](https://www.rfc-editor.org/rfc/rfc2308) — Negative Caching of DNS Queries (DNS NCACHE).
- [RFC 3596](https://www.rfc-editor.org/rfc/rfc3596) — DNS Extensions to Support IP Version 6.
- [RFC 7505](https://www.rfc-editor.org/rfc/rfc7505) — A "Null MX" No Service Resource Record for Domains That Accept No Mail.
- [RFC 8659](https://www.rfc-editor.org/rfc/rfc8659) — DNS Certification Authority Authorization (CAA) Resource Record.
- [RFC 6891](https://www.rfc-editor.org/rfc/rfc6891) — Extension Mechanisms for DNS (EDNS(0)).
- [RFC 7766](https://www.rfc-editor.org/rfc/rfc7766) — DNS Transport over TCP — Implementation Requirements.
- [RFC 5936](https://www.rfc-editor.org/rfc/rfc5936) — DNS Zone Transfer Protocol (AXFR).
- [RFC 1995](https://www.rfc-editor.org/rfc/rfc1995) — Incremental Zone Transfer in DNS.
- [RFC 7858](https://www.rfc-editor.org/rfc/rfc7858) — Specification for DNS over Transport Layer Security (TLS).
- [RFC 8484](https://www.rfc-editor.org/rfc/rfc8484) — DNS Queries over HTTPS (DoH).
- [RFC 9250](https://www.rfc-editor.org/rfc/rfc9250) — DNS over Dedicated QUIC Connections.
- [RFC 9156](https://www.rfc-editor.org/rfc/rfc9156) — DNS Query Name Minimisation to Improve Privacy.
- [RFC 4033](https://www.rfc-editor.org/rfc/rfc4033), [RFC 4034](https://www.rfc-editor.org/rfc/rfc4034), [RFC 4035](https://www.rfc-editor.org/rfc/rfc4035) — DNS Security Introduction and Requirements; Resource Records; Protocol Modifications.
- [RFC 5452](https://www.rfc-editor.org/rfc/rfc5452) — Measures for Making DNS More Resilient against Forged Answers.
- [RFC 7873](https://www.rfc-editor.org/rfc/rfc7873) — Domain Name System (DNS) Cookies.
- [RFC 8482](https://www.rfc-editor.org/rfc/rfc8482) — Providing Minimal-Sized Responses to DNS Queries That Have QTYPE=ANY.
- [RFC 8945](https://www.rfc-editor.org/rfc/rfc8945) — Secret Key Transaction Authentication for DNS (TSIG).
- [RFC 2827 (BCP 38)](https://www.rfc-editor.org/rfc/rfc2827) — Network Ingress Filtering.
- [RFC 5737](https://www.rfc-editor.org/rfc/rfc5737) and [RFC 3849](https://www.rfc-editor.org/rfc/rfc3849) — IPv4 and IPv6 address blocks reserved for documentation.
- [Root Server Technical Operations Association](https://root-servers.org/) — root server identities, operators and instances.
- [DNS Flag Day 2020](https://www.dnsflagday.net/2020/) — recommended EDNS buffer size of 1232 bytes.
