TExcelTAD

DNS

People remember names like www.example.com; networks deliver packets to IP addresses. The Domain Name System (DNS) translates between the two. It is a giant, distributed, hierarchical database, and almost every connection you make starts with a DNS lookup.

The hierarchy

Domain names are read right to left, from the most general part to the most specific:

text
www.example.com.
                ^ root (the trailing dot, usually omitted)
            ^^^ top-level domain (TLD): com, org, net, vn, de, uk ...
    ^^^^^^^ second-level domain, registered by its owner
^^^ a host (or sub-domain) chosen by the owner

No single server knows every name. Each level delegates to the next: the root servers know who runs .com, the .com servers know who runs example.com, and example.com's own authoritative name servers know www.example.com.

python · try it yourself
def labels(name):
    return name.rstrip(".").split(".")

def zones(name):
    parts = labels(name)
    return ["."] + [".".join(parts[i:]) + "." for i in range(len(parts) - 1, -1, -1)]

for name in ["www.example.com", "mail.cs.uni-example.edu.vn"]:
    print(name)
    for z in zones(name):
        print("   ", z)
www.example.com
    .
    com.
    example.com.
    www.example.com.
mail.cs.uni-example.edu.vn
    .
    vn.
    edu.vn.
    uni-example.edu.vn.
    cs.uni-example.edu.vn.
    mail.cs.uni-example.edu.vn.

Resolution: recursive and iterative

Your computer's stub resolver sends one question to a recursive resolver (your ISP's, your router, or a public one like 1.1.1.1 or 8.8.8.8) and waits for the final answer. The recursive resolver does the legwork, asking the hierarchy iteratively:

python · try it yourself
# A miniature DNS universe: each server either answers or refers you to a closer server.
SERVERS = {
    "a.root-servers.net":   {"com.": ("REFERRAL", "a.gtld-servers.net"),
                             "org.": ("REFERRAL", "a0.org-servers.net")},
    "a.gtld-servers.net":   {"example.com.": ("REFERRAL", "ns1.example.com")},
    "ns1.example.com":      {"www.example.com.": ("A", "93.184.215.14"),
                             "mail.example.com.": ("A", "93.184.215.25")},
}

def best_zone(server, name):
    candidates = [z for z in SERVERS[server] if name == z or name.endswith("." + z)]
    return max(candidates, key=len) if candidates else None

def resolve(name, cache):
    if name in cache:
        print(f"  cache hit: {name} -> {cache[name]}")
        return cache[name]
    server = "a.root-servers.net"
    for step in range(1, 10):
        zone = best_zone(server, name)
        if zone is None:
            print(f"  {step}. ask {server}: NXDOMAIN")
            return None
        kind, value = SERVERS[server][zone]
        if kind == "REFERRAL":
            print(f"  {step}. ask {server:<19} -> referral: '{zone}' is served by {value}")
            server = value
        else:
            print(f"  {step}. ask {server:<19} -> answer: {name} {kind} {value}")
            cache[name] = value
            return value

cache = {}
print("www.example.com.")
resolve("www.example.com.", cache)
print("www.example.com. again")
resolve("www.example.com.", cache)
print("shop.example.com.")
resolve("shop.example.com.", cache)
www.example.com.
  1. ask a.root-servers.net  -> referral: 'com.' is served by a.gtld-servers.net
  2. ask a.gtld-servers.net  -> referral: 'example.com.' is served by ns1.example.com
  3. ask ns1.example.com     -> answer: www.example.com. A 93.184.215.14
www.example.com. again
  cache hit: www.example.com. -> 93.184.215.14
shop.example.com.
  1. ask a.root-servers.net  -> referral: 'com.' is served by a.gtld-servers.net
  2. ask a.gtld-servers.net  -> referral: 'example.com.' is served by ns1.example.com
  3. ask ns1.example.com: NXDOMAIN

A real resolver also caches the referrals, so the second lookup of any example.com name skips the root and .com servers. Caching is what lets DNS handle trillions of queries a day.

Record types

Type Holds Example
A an IPv4 address www.example.com. A 93.184.215.14
AAAA an IPv6 address www.example.com. AAAA 2606:2800:21f:cb07:6820:80da:af6b:8b2c
CNAME an alias to another name shop.example.com. CNAME shops.platform.example.net.
MX mail server for the domain, with a priority example.com. MX 10 mail.example.com.
NS the authoritative name servers of a zone example.com. NS ns1.example.com.
TXT free text (SPF, domain verification) example.com. TXT "v=spf1 -all"
PTR reverse lookup: IP → name 14.215.184.93.in-addr.arpa. PTR www.example.com.
SOA zone metadata (serial, timers)
SRV, CAA, HTTPS services, allowed certificate authorities, HTTPS hints

Reverse lookups use a special domain: the IP's octets reversed, under in-addr.arpa:

python · try it yourself
import ipaddress

for ip in ["93.184.215.14", "192.168.1.20", "2001:db8::1"]:
    print(f"{ip:<15} -> {ipaddress.ip_address(ip).reverse_pointer}")
93.184.215.14   -> 14.215.184.93.in-addr.arpa
192.168.1.20    -> 20.1.168.192.in-addr.arpa
2001:db8::1     -> 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa

TTL and caching

Every record has a TTL (time to live, in seconds) that says how long resolvers may cache it. Long TTLs mean fewer queries and faster lookups; short TTLs let changes spread quickly. Lower the TTL a day before migrating a server, not at the moment you switch.

python · try it yourself
records = {"www.example.com": (300, "93.184.215.14"), "example.com MX": (3600, "mail.example.com")}
cache = {}

def lookup(name, now):
    if name in cache and cache[name][1] > now:
        remaining = cache[name][1] - now
        return f"t={now:>4}s {name}: cached ({remaining}s left) -> {cache[name][0]}"
    ttl, value = records[name]
    cache[name] = (value, now + ttl)
    return f"t={now:>4}s {name}: asked authoritative server, cache for {ttl}s -> {value}"

for name, t in [("www.example.com", 0), ("www.example.com", 120), ("example.com MX", 150),
                ("www.example.com", 301), ("example.com MX", 3000)]:
    print(lookup(name, t))
t=   0s www.example.com: asked authoritative server, cache for 300s -> 93.184.215.14
t= 120s www.example.com: cached (180s left) -> 93.184.215.14
t= 150s example.com MX: asked authoritative server, cache for 3600s -> mail.example.com
t= 301s www.example.com: asked authoritative server, cache for 300s -> 93.184.215.14
t=3000s example.com MX: cached (750s left) -> mail.example.com

Real tools: dig and nslookup

bash
dig www.example.com A +noall +answer +stats

Sample output:

text
www.example.com.        3249    IN      A       93.184.215.14
;; Query time: 12 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Sat Oct 17 10:22:41 UTC 2026
;; MSG SIZE  rcvd: 60

The 3249 is the remaining TTL in the resolver's cache. Add +trace to watch dig walk the hierarchy from the root itself, like the simulation above:

bash
dig +trace www.example.com

Sample output (shortened):

text
.                       518400  IN      NS      a.root-servers.net.
;; Received 239 bytes from 127.0.0.53#53(127.0.0.53) in 0 ms
com.                    172800  IN      NS      a.gtld-servers.net.
;; Received 1170 bytes from 198.41.0.4#53(a.root-servers.net) in 20 ms
example.com.            172800  IN      NS      a.iana-servers.net.
;; Received 343 bytes from 192.5.6.30#53(a.gtld-servers.net) in 24 ms
www.example.com.        3600    IN      A       93.184.215.14
;; Received 60 bytes from 199.43.135.53#53(a.iana-servers.net) in 88 ms

On Windows, nslookup www.example.com does a simple lookup.

Note: Classic DNS is unencrypted, so anyone on the path can see (and an attacker can forge) your lookups. DNS over HTTPS (DoH) and DNS over TLS (DoT) encrypt the query to the resolver; DNSSEC adds signatures so answers can be verified as authentic.

Go further: DNS Spoofing, Cache Poisoning and DNSSEC works out the odds of an off-path race and how DNSSEC ends it.

Test yourself

Which kind of DNS server holds the definitive records for example.com?

  1. The root server
  2. The recursive resolver
  3. The authoritative name server for example.com
  4. The .com TLD server

Which record type maps a name to an IPv6 address?

  1. A
  2. AAAA
  3. PTR
  4. MX

What does a record's TTL control?

  1. How many routers a DNS packet may cross
  2. How long resolvers may cache the record
  3. The priority of mail servers
  4. The size of the zone