Sub topic 3
Finding the server: DNS
This chapter assumes you know what a URL is and how its host part is written. It also assumes you can run commands in a terminal. It covers how a browser turns a hostname such as example.com into an IP address.
Why DNS exists
Networks route traffic by IP address, such as 93.184.216.34. People use names. The Domain Name System (DNS) is a distributed database that maps hostnames to records, most often IP addresses. A browser must resolve the host from the URL before it can open a TCP connection.
The lookup chain
A lookup passes through several layers. Each layer answers from its cache if it can.
- The browser checks its own cache.
- The operating system checks its cache and the local hosts file.
- The OS asks a recursive resolver. This is usually run by your ISP, your company, or a public provider.
- If the resolver has no cached answer, it queries the DNS hierarchy: a root server, then the top-level domain (TLD) server for
.com, then the authoritative server forexample.com. - The authoritative server returns the answer, and the resolver passes it back and caches it.
Common record types
| Type | Holds | Example use |
|---|---|---|
| A | An IPv4 address | `example.com` points to `93.184.216.34` |
| AAAA | An IPv6 address | The same site over IPv6 |
| CNAME | An alias to another name | `www.example.com` points to `example.com` |
| NS | The name servers for a zone | Delegates a domain to its DNS host |
| MX | Mail servers | Where email for the domain goes |
Caching and TTL
Every record carries a time to live (TTL), in seconds. Resolvers and clients may keep the answer until the TTL runs out. A long TTL reduces lookups. A short TTL lets you move a site to a new IP faster. This is why a DNS change can appear for some users before others.
Looking up a name yourself
The dig tool queries DNS directly.
dig example.com A +noall +answerThe output has one line per record: name, TTL, class, type, and value. Run it twice and watch the TTL count down. That shows you are reading a cached answer.
Common errors
- NXDOMAIN. The name does not exist. Check for typos and check that the record was created.
- Stale answers after a change. Old records stay cached until their TTL expires. Lower the TTL before a planned migration.
- Works by IP, fails by name. This points to DNS, not the server. Compare
curl http://93.184.216.34/with the name-based request. - CNAME at the zone apex. A bare domain such as
example.comcannot normally be a CNAME because it must also hold NS and other records. Use an A record or your DNS provider's alias feature.
Exercise
You change the A record for shop.example.com to a new IP. Its old TTL was 86400. Some users still reach the old server an hour later. Why, and what should you have done beforehand?
Show answer
Resolvers and clients cached the old record and may keep it for up to 86400 seconds (24 hours). Before the change, lower the TTL (for example to 300), wait at least the old TTL, then change the record.