Sub topic 5
Finding the server with DNS
This chapter assumes you know what an IP address is and can read a URL's parts, as covered in earlier chapters. After the browser parses the URL, it holds a host name such as example.com. Networks route packets by IP address, so the browser must first turn the name into an address. The Domain Name System (DNS) does this.
The working model
Your machine does not know every name. It asks a chain of servers, and each one either answers or points to the next.
- The browser checks its own DNS cache.
- The operating system's stub resolver checks its cache and the
hostsfile. - If both miss, the stub resolver asks a recursive resolver. This is usually run by your ISP, your company, or a public provider.
- The recursive resolver checks its cache. If that misses, it queries the DNS hierarchy: a root server, then the server for the top-level domain (
.com), then the authoritative name server forexample.com. - The authoritative server returns the record. Every resolver along the way caches it.
Records and TTL
DNS stores data as records. The ones you meet most often are:
| Type | Meaning |
|---|---|
| A | Host name to an IPv4 address |
| AAAA | Host name to an IPv6 address |
| CNAME | Alias that points to another name |
| NS | Name servers responsible for a domain |
Each record carries a time to live (TTL), in seconds. Caches may keep the record for that long and must then ask again. A long TTL reduces lookups. A short TTL lets you change the address faster.
Querying DNS directly
dig sends a query and shows the raw answer.
dig +short example.com A
dig example.com NSThe first command prints only the IPv4 addresses. The second prints the full response, including the NS records and the TTL column. Run dig example.com A twice in a row. The TTL in the second answer is usually lower, because the resolver is counting down its cached copy.
Common errors
- NXDOMAIN. The name does not exist. Check for a typo, or for a record that was never created.
- Stale answers after a change. You updated an A record but clients still reach the old server. Caches keep the old value until its TTL expires. Lower the TTL before a planned migration, not after.
- Local overrides. An entry in
/etc/hostswins over DNS. Ifdigand the browser disagree, look there first.digignores the hosts file, and the browser does not. - Different resolvers, different answers. A corporate resolver may return internal addresses for a name that resolves differently on the public internet. Query a specific resolver with
dig @8.8.8.8 example.com. - Several addresses. A name can return multiple A or AAAA records. The browser picks one and may try another if the connection fails. This is normal, not a fault.
When the lookup finishes, the browser has an IP address. The next chapter opens a TCP connection to it.
Exercise
You change the A record for www.example.com at your DNS provider. The old record had a TTL of 86400. An hour later, some users still reach the old server. Why, and what should you have done beforehand?
Show answer
Resolvers that cached the old record keep serving it until the 86400-second (24-hour) TTL expires, so some users see the old address for up to a day. Before the change, lower the TTL to something short, such as 300 seconds. Then wait at least one old TTL so caches pick up the short value, and make the change.