Lesson 17 intermediate

DNS, the Domain Name System

How names become addresses: the DNS hierarchy, recursive resolvers and authoritative servers, a full lookup step by step, record types (A, AAAA, CNAME, MX, TXT, NS, SOA, PTR, SRV, CAA, HTTPS), TTLs and caching, dig and nslookup, DNSSEC, encrypted DNS and the classic DNS failures.

People remember names; networks route numbers. The Domain Name System is the global, distributed database that turns blog.jefersonjeronimoai.online into an IP address, tells mail servers where to deliver your email and proves you own a domain to certificate authorities. It answers billions of queries a second, and when it breaks, everything looks broken. Hence the saying: “It’s always DNS.”

The hierarchy

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

blog . jefersonjeronimoai . online . (root)
host   second-level domain  TLD
LevelWho runs itExample
Root13 named root server identities (hundreds of anycast instances).
TLD (top-level domain)Registry operators.com, .org, .online, .br
Second-level domainYou, through a registrarjefersonjeronimoai.online
SubdomainsYou, in your zoneblog., k8s-game., api.

Each level delegates the next one with NS records. The root knows who runs .online, .online knows who runs jefersonjeronimoai.online, and that zone holds the actual records.

Who answers

  • Stub resolver: the small client in your OS. It asks one question and expects a final answer.
  • Recursive resolver: does the legwork for you and caches results. Usually your ISP’s, your router’s, or a public one such as 1.1.1.1, 8.8.8.8 or 9.9.9.9.
  • Authoritative server: holds the zone and gives the official answer for it. It never chases other servers for you.

A lookup step by step

You type blog.example.com with an empty cache:

  1. The stub asks the recursive resolver: “A record for blog.example.com?”
  2. The resolver asks a root server, which replies: “I don’t know, but .com is served by these servers” (a referral).
  3. The resolver asks a .com server, which refers it to example.com‘s name servers.
  4. The resolver asks the authoritative server for example.com, which answers: blog.example.com A 203.0.113.25.
  5. The resolver caches every answer for its TTL and returns the final result to the stub.

Step 1 is a recursive query (“give me the answer”); steps 2 to 4 are iterative queries (“tell me what you know”). dig +trace performs the iteration itself and shows every step:

$ dig +trace blog.example.com | grep -E 'IN\s+(NS|A)\s' | head -6
.                       518400  IN  NS  a.root-servers.net.
com.                    172800  IN  NS  a.gtld-servers.net.
example.com.            172800  IN  NS  ns1.example-dns.net.
example.com.            172800  IN  NS  ns2.example-dns.net.
blog.example.com.       300     IN  A   203.0.113.25

Record types

TypeHoldsExample
AIPv4 addressblog A 203.0.113.25
AAAAIPv6 addressblog AAAA 2001:db8::25
CNAMEAlias to another namewww CNAME blog.example.com.
MXMail server and priority (lower first)@ MX 10 mail.example.com.
TXTFree text: SPF, DKIM, DMARC, domain verification@ TXT "v=spf1 mx -all"
NSName servers for a zone@ NS ns1.example-dns.net.
SOAZone metadata: primary server, serial, timersone per zone
PTRReverse lookup: IP to name25.113.0.203.in-addr.arpa PTR blog.example.com.
SRVService host and port_sip._tcp SRV 10 60 5060 sip.example.com.
CAAWhich CAs may issue certificates@ CAA 0 issue "letsencrypt.org"
HTTPS / SVCBConnection hints: HTTP/3 support, alternative endpoints@ HTTPS 1 . alpn="h3,h2"

Two rules trip people up: a CNAME cannot coexist with other records on the same name (so the bare domain example.com cannot be a CNAME; providers offer “ALIAS” or “flattening” instead), and MX and NS records must point to names, never to IP addresses.

Querying DNS

$ dig blog.example.com

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40112
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; ANSWER SECTION:
blog.example.com.       287     IN      A       203.0.113.25

;; Query time: 14 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)

Things to read: status (NOERROR, NXDOMAIN for “name does not exist”, SERVFAIL for “the resolver could not get an answer”), the flags (aa means an authoritative answer, ra recursion available), the TTL (287 seconds left in the cache) and which server answered.

Handy variations:

$ dig +short example.com MX
10 mail.example.com.
$ dig @1.1.1.1 +short blog.example.com
203.0.113.25
$ dig @ns1.example-dns.net blog.example.com +norecurse
$ dig -x 203.0.113.25 +short
blog.example.com.
$ resolvectl query blog.example.com
blog.example.com: 203.0.113.25    -- link: enp3s0

@server asks a specific resolver, which is how you compare what different resolvers see. Asking the authoritative server directly with +norecurse shows the truth without any cache. On Windows, use Resolve-DnsName blog.example.com -Type A or nslookup.

TTL and caching

Every record has a TTL (time to live) in seconds. Resolvers cache answers for that long, which makes DNS fast but means changes are not instant. Before a migration:

  1. Lower the TTL (for example from 3600 to 60) at least one old TTL ahead of the change.
  2. Change the record.
  3. Raise the TTL again once everything works.

“DNS propagation” is really just caches expiring. Failed lookups are cached too (negative caching, governed by the SOA’s minimum field), so querying a name before you create it can make it look missing for a while.

Where your machine gets its answers

On Linux the order is set in /etc/nsswitch.conf (hosts: files dns means /etc/hosts first). Many distributions run systemd-resolved as a local cache at 127.0.0.53:

$ resolvectl status enp3s0 | grep -E 'DNS Servers|DNS Domain'
       DNS Servers: 192.168.1.1
        DNS Domain: lan
$ grep -v '^#' /etc/hosts
127.0.0.1 localhost
192.168.1.50 nas.lan
$ sudo resolvectl flush-caches

On Windows: Get-DnsClientServerAddress shows the servers and ipconfig /flushdns clears the cache.

DNS security

  • DNSSEC signs records so resolvers can verify they were not forged in transit (it does not encrypt them). The ad flag in a dig answer means the resolver validated the signatures.
  • Encrypted DNS hides queries from the network: DoT (DNS over TLS, TCP 853), DoH (DNS over HTTPS, 443) and DoQ (DNS over QUIC). Browsers and phones increasingly use DoH by default.
  • Cache poisoning tries to inject fake answers into a resolver; random source ports, random query IDs and DNSSEC defend against it.
  • Open resolvers that answer anyone get abused for amplification attacks; only serve your own clients.
  • Split-horizon DNS gives internal clients different answers (private IPs) from external ones.

Classic DNS failures

SymptomLikely causeCheck
ping 1.1.1.1 works, ping example.com failsNo working resolverresolvectl status, dig @1.1.1.1 example.com
NXDOMAINTypo, missing record, wrong zone, or a delegation not set at the registrardig +trace
SERVFAILAuthoritative servers unreachable, or DNSSEC validation faileddig +cd (skips validation) to compare
Old IP still returnedCached record with a long TTLQuery the authoritative server directly
Works on one machine, not anotherDifferent resolvers, /etc/hosts overrides, VPN DNSCompare dig @server results
Mail bouncesMissing or wrong MX, SPF, DKIM or DMARCdig MX, dig TXT

Exercise

dig returns status: NXDOMAIN for api.example.com. What does that mean?

  1. The resolver is down
  2. The authoritative servers say the name does not exist
  3. The record exists but the server is slow
  4. DNSSEC validation failed
Show answer

Answer: B. NXDOMAIN is a definitive “no such name”. A resolver problem or a failed DNSSEC validation shows up as SERVFAIL instead.

Exercise

You will move a website to a new IP tomorrow. Its A record has a TTL of 86,400 seconds. What should you do today?

  1. Nothing; DNS updates everywhere instantly
  2. Delete the A record now
  3. Add a CNAME next to the A record
  4. Lower the TTL to something short, such as 300, at least a day before the change
Show answer

Answer: D. Resolvers may keep the old answer for up to the old TTL. Lowering it a full old TTL ahead means caches refresh quickly when you switch.

Exercise

Which record tells other mail servers where to deliver email for your domain?

  1. TXT
  2. PTR
  3. MX
  4. CAA
Show answer

Answer: C. MX records list mail servers with priorities. TXT holds SPF, DKIM and DMARC policies that help receivers trust your mail, but delivery targets come from MX.

Key takeaways

  • DNS is a hierarchy: root, TLD, your zone, delegated with NS records.
  • Recursive resolvers chase and cache answers; authoritative servers hold the truth.
  • Know A, AAAA, CNAME, MX, TXT, NS, PTR and CAA by heart.
  • TTLs control caching; lower them before migrations.
  • dig, dig +trace and dig @server solve most DNS mysteries.

FAQ

What is the difference between NXDOMAIN and SERVFAIL?

NXDOMAIN is a valid answer: the name does not exist. SERVFAIL means the resolver could not get any trustworthy answer, often because the authoritative servers are unreachable or DNSSEC signatures do not validate.

Does DNS use TCP or UDP?

Usually UDP port 53. It switches to TCP for large answers, zone transfers and when a UDP reply is truncated. Encrypted variants use TCP 853 (DoT), HTTPS 443 (DoH) or QUIC (DoQ).

Why does my change show up for some people and not others?

Their resolvers still hold the old record in cache until its TTL runs out. Querying the authoritative server shows whether your change is live.

What is reverse DNS used for?

PTR records map IPs to names. Mail servers check them to judge senders, and logs and traceroute use them to show names. The owner of the IP block (usually your ISP or cloud provider) controls them.

↑ ↓ navigate Enter open Esc close