Names and DNS
DNS
The distributed directory that turns names like bank.lab into addresses.
DNS is the system that turns a name into an address. A device asks its resolver, which follows referrals from the root down to the authoritative server for the name, then caches the answer for its TTL (time to live, in seconds). Classic DNS is one unencrypted UDP packet each way with no proof of who answered: DNSSEC and encrypted DNS came later.
How DNS works, step by step
Names form a tree read from the right: www.example.lab. is www inside example, inside lab, inside
the root, the final dot. Each part of the tree, a zone, is served by its owner's authoritative name
servers. A parent zone does not know its children's names; it only knows which servers to ask, and delegates to
them with NS records.
When a program on a laptop needs the address of www.example.lab:
- The stub asks the resolver. The laptop's resolver library reads
/etc/resolv.confand sends one UDP packet to port 53 of the server named there. The DNS header holds a 16-bit id, the RD flag (recursion desired: "do the whole job for me") and the question: name, typeA, classIN. - The resolver checks its cache. If it holds an unexpired answer, it replies at once.
- Otherwise it walks the tree. It asks a root server, from the list it ships with (the root hints). The root
does not know the name and answers with a referral: no answer, an NS record in the authority section
(
lab. NS ns.lab.) and the address of that server in the additional section, the glue. - It asks
ns.lab, which refers it one level down tons.example.lab. ns.example.labanswers with the address and the AA flag set: an authoritative answer.- The resolver answers the laptop with RA set (recursion available), and caches every answer and referral for its TTL in seconds.
The resolver does the walking and the servers only point the way: that is iterative resolution. A forwarder, like the DNS server in a home router, skips the walk and hands every question to one upstream resolver.
What it looks like on a Linux machine
In the DNS lab the name servers run BIND and the resolver runs unbound. dig
shows the flags and the TTL:
$ dig www.example.lab
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41721
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; ANSWER SECTION:
www.example.lab. 60 IN A 10.0.2.13
;; SERVER: 10.0.1.11#53(10.0.1.11) (UDP)
No aa: the resolver is passing on what it found. You can play resolver yourself. +norecurse clears RD and
@ picks the server:
$ dig +norecurse @10.0.2.10 www.example.lab
;; flags: qr; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 2
;; AUTHORITY SECTION:
lab. 60 IN NS ns.lab.
;; ADDITIONAL SECTION:
ns.lab. 60 IN A 10.0.2.11
Ask ns.example.lab the same way and the flags read qr aa. dig +trace does the whole walk, one level at a
time. Ask the resolver twice and the second answer comes from its cache, its TTL counting down, while a capture on the
resolver's outside shows no query at all.
What goes wrong, and how you see it
NXDOMAIN. The name does not exist. The answer comes from the server responsible for where it would be, with the zone's SOA record, whose last field says how long "does not exist" may be cached (negative caching, RFC 2308).
SERVFAIL. The resolver tried and failed: an authoritative server is unreachable, a delegation is broken, or DNSSEC validation failed. The DNSSEC lab shows the last one.
No answer at all. dig prints ;; connection timed out; no servers could be reached. The resolver in
resolv.conf is down, or a firewall drops port 53. Programs report it as a failed lookup: curl exits with code 6,
Could not resolve host.
An old answer. A record changed, but resolvers keep the cached one until its TTL runs out. That is what "DNS propagation" means. Lower the TTL a day before a move.
A false answer. Classic DNS has no proof of origin: whoever answers first with the right id and port wins. The DNS spoofing city sends a laptop to the wrong server this way, and shows what TLS does about it.
Questions people ask
Does DNS use UDP or TCP? Both, on port 53. Queries go over UDP; an answer too big for the UDP size the client offered comes back truncated (the TC flag) and the client retries over TCP. Zone transfers always use TCP.
What is the difference between a resolver and an authoritative server? A resolver looks names up for clients and caches them. An authoritative server holds a zone and answers only for it. Keeping the jobs on separate servers is good practice.
What are A, AAAA and CNAME records? A maps a name to an IPv4 address, AAAA to an IPv6 address, CNAME
makes a name an alias of another name, which the resolver then follows.
Read next
- Recursive resolver The DNS server a device asks: it does the whole search and caches the answers.
- Authoritative server A DNS server that holds a zone's records and answers for them with the aa flag.
- DNS records The typed entries of a zone: A for an address, CNAME for an alias, NS for delegation, and more.
- DNSSEC and encrypted DNS Signed DNS answers, and DNS carried over an encrypted connection to a resolver you chose.
- Hosts file A local list of names and addresses, consulted before DNS on most systems.
- DNS response codes The status on every DNS answer: found it, no such name, or couldn't get an answer.
- Zone transfer How a secondary name server copies a zone from its primary: whole (AXFR) or just the changes (IXFR), prompted by NOTIFY.
- Filtering resolver A resolver that refuses names on a blocklist: it answers NXDOMAIN or a block page without looking them up.
- Encrypted DNS DNS questions sent inside TLS (DoT, port 853) or HTTPS (DoH, port 443), so the path cannot read or change them.
- DNS sinkhole A resolver that answers known-bad domains with a harmless address or NXDOMAIN, blocking them for every client.
- DNS tunneling and exfiltration Smuggling data in and out of a network by encoding it inside DNS queries, which firewalls usually let through.
- DNS spoofing A forged DNS answer that sends a name to the attacker's address, believed by a client or cached by a resolver.
- DNS amplification Bouncing small spoofed queries off open resolvers so large answers flood a victim.
- DNS footprinting Reading an organisation's own name server for intelligence — the hosts, mail path and infrastructure it describes — before touching any of them.
- Lookalike domain A domain registered to pass for a real one at a glance — a typo, a swapped letter pair, an extra word or a different ending — used to host phishing, harvesters or misdirected mail.