Knowledge

The DNS protocol

Updated 2026-11-06 · Present

Estimated time: 45 minutes.

Quick summary

Step 1 — Basic dig — view the A record

dig is the number one DNS debugging tool. Understanding dig's output = understanding the real DNS response, knowing how much TTL is left and who the authoritative server is. Without dig, debugging DNS is just guesswork.

dig is a DNS client that lets you query a DNS server directly and see the raw response. The output includes the QUESTION SECTION (the domain queried), the ANSWER SECTION (record + TTL), and SERVER (the resolver in use). The TTL in the ANSWER SECTION is the number of seconds remaining in the resolver's cache. The Query time in the output is the RTT to the resolver — low latency (<5ms) is usually a cache hit, higher is a fresh lookup from the authoritative server, but the exact threshold depends on geographic location and the resolver.

When it breaks: If you skip dig and only use ping/curl to debug DNS → you cannot see the TTL, you do not know which resolver is in use, and you cannot tell a cache hit from an authoritative response → debugging blind.

# Query the default A record
dig example.com

# Important output:
# QUESTION SECTION: the domain being asked
# ANSWER SECTION: the result (IP)
# Query time: latency
# SERVER: the resolver in use

Observe with:

dig example.com — look at the TTL column in the ANSWER SECTION and the SERVER line to identify the resolver in use. Compare Query time: <5ms is a cache hit, >50ms is a fresh lookup.

Symptoms when it breaks:

How to fix:

  1. Check whether /etc/resolv.conf has a search domain

    cat /etc/resolv.conf | grep -E 'nameserver|search|domain'
    
  2. Add a search domain temporarily for testing (not persistent across reboots)

    echo 'search internal.example.com' | sudo tee -a /etc/resolv.conf && dig api.internal.example.com
    

Step 2 — dig +trace — view the full recursive path

dig +trace does not ask the resolver; it walks the delegation chain step by step itself: from the root servers, down to the TLD servers (for example com.), then to the domain's authoritative nameserver. Each block of output is one layer of NS delegation, and the last block is the record returned by the authoritative server. This is how you tell 'the authoritative server has been updated' apart from 'the resolver still holds an old cache' when debugging propagation.

RFC 1034 §4.3.2 describes the recursive resolution chain: Root → TLD → Authoritative. +trace forces dig to bypass the resolver's cache and perform the whole chain itself with iterative queries starting from the root. If +trace returns the new record but a normal dig still returns the old one → it is confirmed to be a cache issue at the resolver, not an authoritative server that has not been updated.

When it breaks: Not using +trace when debugging propagation → you cannot tell 'has the authoritative server been updated yet' apart from 'the resolver still has the old cache' → hours of pointless waiting or a mistaken rollback.

# Trace each hop: Root → TLD → Authoritative
dig example.com +trace

# Sample result (illustrative — NS names, TTLs and IPs change over time):
# .       518400  IN  NS  a.root-servers.net.
# com.    172800  IN  NS  a.gtld-servers.net.
# example.com. 172800 IN NS hera.ns.cloudflare.com.
# example.com. 300    IN A  104.20.23.154

Observe with:

dig example.com +trace — read each NS delegation line from the root down. If you see the new IP at the authoritative server but dig example.com (without +trace) still returns the old IP → it is confirmed to be a cache issue, not a config error.

Step 3 — View other record types

Besides the A record, DNS holds many other record types, each for its own purpose: MX points to the mail servers that receive the domain's email (with a priority), TXT holds arbitrary strings such as SPF/DKIM, NS lists the delegated nameservers, and AAAA is an IPv6 address. Just add the type to the command, for example dig example.com MX, to ask for exactly the record type you need to check. When email lands in spam, TXT is the first record to look at.

When it breaks: Only knowing how to query A records → blind to email (MX/TXT), blind to nameserver delegation (NS), blind to IPv6 (AAAA). When email lands in spam and you do not check the TXT SPF/DKIM records → you debug in the wrong direction.

# MX record — mail server
dig example.com MX

# TXT record — SPF/DKIM
dig example.com TXT

# NS record — nameservers
dig example.com NS

# AAAA record — IPv6
dig example.com AAAA

Observe with:

dig example.com TXT | grep -i spf — view the SPF value. dig example.com MX +short — list mail servers and their priority. dig example.com NS +short — confirm which nameservers are active.

Step 4 — TTL and cache — verify in practice

The TTL decreases each time you ask (because the resolver already has it cached). First query TTL=3600, second query a few minutes later TTL=3540. When the TTL reaches 0, the resolver asks the authoritative server again. Understand this to avoid the confusion of 'why did I change the IP but still not see it'.

When it breaks: Not checking the TTL before a migration → changing the A record while the TTL is still 86400 → users stuck on the old IP for 24h → perceived downtime even though the new server is perfectly fine.

# Ask twice, watch the TTL decrease
dig example.com | grep -A2 'ANSWER'
# Wait 5 seconds
sleep 5
dig example.com | grep -A2 'ANSWER'
# The TTL will drop by ~5

Observe with:

watch -n 5 'dig example.com +short' — runs continuously every 5s so you can see the moment the cache expires and the new IP appears. Or dig example.com | grep -i ttl to see the current TTL.

Symptoms when it breaks:

How to fix:

  1. Check the current TTL of the cached record

    dig example.com | awk '/ANSWER SECTION/{getline; print "TTL remaining:", $2, "seconds"}'
    
  2. Bypass the cache with +trace to confirm the authoritative server has been updated

    dig example.com +trace +short | tail -5
    
  3. For the next migration: lower the TTL to 300s at least 24h before changing the IP

    dig example.com | grep -E 'IN\s+A' | awk '{print "Current TTL:", $2, "seconds — wait this long for full propagation"}'
    

Scripting it: Create /etc/dns-check.conf with one line per entry: 'example.com 93.184.216.34'. Run it after a migration to monitor propagation. Cron: */5 * * * * /usr/local/bin/dns-batch-check.sh 8.8.8.8

#!/bin/bash
# Batch check many DNS records, alert if the IP differs from expected or the TTL is low
# Use after a migration to monitor propagation
RESOLVER=${1:-8.8.8.8}
INPUT_FILE=${2:-/etc/dns-check.conf}  # format: domain expected_ip
echo "=== DNS Batch Check $(date '+%Y-%m-%d %H:%M:%S') ==="
while IFS=' ' read -r DOMAIN EXPECTED; do
  [ -z "$DOMAIN" ] || [[ "$DOMAIN" == \#* ]] && continue
  RESULT=$(dig "$DOMAIN" A @"$RESOLVER" +short | head -1)
  TTL=$(dig "$DOMAIN" A @"$RESOLVER" | awk '/ANSWER SECTION/{getline; print $2}')
  STATUS="OK"
  [ "$RESULT" != "$EXPECTED" ] && STATUS="MISMATCH (got $RESULT)"
  [ -z "$TTL" ] && TTL="N/A"
  echo "[$STATUS] $DOMAIN → $RESULT (TTL:${TTL}s expected:$EXPECTED)"
done < "$INPUT_FILE"

Step 5 — Ask the authoritative server directly — bypass the cache

The authoritative nameserver holds the original copy of the record, while an ordinary resolver such as 8.8.8.8 only returns its cached copy until the TTL expires. Find the authoritative servers with dig example.com NS +short, then ask one of them directly with dig @"$NS" example.com A to see the value that is actually configured. Comparing that with the result through the resolver tells you whether a difference comes from a cache that has not expired yet or from configuration that has not been updated yet.

When it breaks: Only querying through the usual resolver → you do not know whether the authoritative server has been updated → you may wait for nothing or think the DNS provider has a problem when in fact the cache simply has not expired.

# Find the authoritative server
dig example.com NS +short

# Take the first nameserver and ask it directly (no cache)
NS=$(dig +short NS example.com | head -1)
dig @"$NS" example.com A

# Compare with the usual resolver:
dig example.com @8.8.8.8 A

Observe with:

diff <(dig @"$NS" example.com A +short | sort) <(dig @8.8.8.8 example.com A +short | sort) — if the outputs differ, resolver 8.8.8.8 is caching the old record.

Step 6 — Reverse DNS lookup (PTR record)

Reverse DNS maps IP → domain. Mail servers often check PTR as an anti-spam measure: if the IP has no PTR or the PTR does not match → the email is rejected. Hosting servers usually need PTR (rDNS) configured in the VPS control panel.

When it breaks: A VPS sends email without a configured PTR record (rDNS) → Outlook and Gmail fail the PTR check → the email is rejected or marked as spam. Many email providers refuse mail from IPs without a PTR.

# Reverse lookup for an IP (192.0.2.10 is an RFC 5737 documentation address — substitute your server's IP)
dig -x 192.0.2.10
# Or
nslookup 192.0.2.10

# Illustrative output (when the IP has a PTR):
# 10.2.0.192.in-addr.arpa. PTR example.com.

Observe with:

dig -x $(curl -s https://ipinfo.io/ip) — check the PTR record of the current VPS IP. If it returns NXDOMAIN or a generic hosting address → go to the VPS control panel to configure rDNS.

Step 7 — DoH — checking with DNS over HTTPS

Checking via DoH confirms the record directly from Cloudflare/Google without going through the OS resolver. Useful when you suspect the ISP is tampering with DNS or when debugging from a corporate network with DNS filtering.

When it breaks: An app hardcodes a DoH endpoint (1.1.1.1:443) in an environment with a restrictive firewall → the connection is blocked → DNS fails completely even though UDP 53 still works. Hard to debug because the error message is usually a generic timeout.

# Query Cloudflare DoH with curl
curl -s -H 'accept: application/dns-json' \
  'https://cloudflare-dns.com/dns-query?name=example.com&type=A' \
  | python3 -m json.tool

# Query Google DoH
curl -s 'https://dns.google/resolve?name=example.com&type=A' \
  | python3 -m json.tool

Observe with:

curl -s 'https://dns.google/resolve?name=example.com&type=A' | python3 -m json.tool | grep -E 'data|TTL' — compare the DoH result with dig example.com @8.8.8.8 to confirm whether the ISP is tampering.

Common misconceptions

When debugging a DNS failure, confusing the roles leads you to check the wrong place: if the stub resolver sends the query to the recursive resolver and the recursive resolver fails, the error shows up at the stub — you must check the recursive resolver, not the authoritative server.

Not lowering the TTL before a migration → users hit split-brain: some see the new server (cache expired), others the old server (cache still valid). The correct procedure: lower the TTL to 300s, wait a full cycle of the old TTL, and only then change the A record.

Some DNS providers allow a CNAME at the apex, but the behaviour is undefined on other providers — changing provider will break the domain. Worse: a CNAME at the apex usually conflicts with MX records → email for the whole domain fails.

Try at home

1. View the full recursive path from Root → TLD → Authoritative — understand how DNS finds the IP

dig example.com +trace

Observe: Read each block of the output: the first lines are the root servers, then the TLD NS, then the domain's NS, and finally the A record. Note the TTL in the 2nd column — each level has its own TTL.

2. Check a record's TTL — see how long until the cache expires, ask twice and watch the TTL decrease

dig example.com | grep -A5 'ANSWER SECTION'
# Wait 10 seconds
sleep 10
dig example.com | grep -A5 'ANSWER SECTION'

Observe: Compare the TTL between the 2 queries — the 2nd must be about 10 seconds lower than the 1st. If the TTL resets to its original value → the resolver has just asked the authoritative server again.

3. View SPF/DKIM/DMARC — check why the domain's email keeps landing in spam

# SPF
dig example.com TXT | grep spf

# DMARC
dig _dmarc.example.com TXT

# DKIM (you need to know the selector, usually 'google' or 'default')
dig google._domainkey.gmail.com TXT

Observe: A valid SPF must contain v=spf1 and end with -all or ~all. DMARC must contain v=DMARC1 and a p= field (none/quarantine/reject). Missing either one → email easily lands in spam.

4. Ask the authoritative server directly — bypass the resolver cache to see the record as it really is now

# Get the domain's NS
dig example.com NS +short

# Ask the first authoritative nameserver directly
NS=$(dig +short NS example.com | head -1)
dig @"$NS" example.com A

# Compare with the Google resolver
dig example.com A @8.8.8.8

Observe: If the IP from the authoritative server differs from the IP from @8.8.8.8 → Google's resolver is caching the old one; wait for the TTL to expire. If the authoritative server returns SERVFAIL → the problem is in the DNS config, not the cache.

5. Query DoH with curl — see the DNS response without going through the OS resolver; check this when you suspect the ISP is tampering with DNS

curl -s -H 'accept: application/dns-json' \
  'https://cloudflare-dns.com/dns-query?name=example.com&type=A' \
  | python3 -m json.tool | grep -E 'name|data|TTL'

Observe: Compare the data field (IP) from the DoH response with the output of dig example.com +short. If they differ → the ISP is redirecting DNS. The TTL in the JSON response is the real TTL from the authoritative server.

6. Reverse lookup — view an IP's PTR record, important for debugging email deliverability

# PTR of an IP
dig -x 8.8.8.8

# View the PTR of the server's IP
MY_IP=$(curl -s https://ipinfo.io/ip)
dig -x $MY_IP

Observe: A valid PTR record returns a hostname in the ANSWER SECTION. If you see NXDOMAIN or a generic hosting hostname (for example compute.amazonaws.com) → configure rDNS in the VPS control panel so email is not rejected.

Review questions

Question 1. You type 'example.com' in the browser — what is the first step?

  1. Open a TCP connection to 93.184.216.34 port 80
  2. The stub resolver asks the recursive resolver for the IP
  3. The browser asks the root server directly
  4. The OS looks in /etc/hosts before asking DNS

Answer: The OS looks in /etc/hosts before asking DNS. The OS checks /etc/hosts (or C:\Windows\System32\drivers\etc\hosts) before asking DNS. If there is an entry in the hosts file → it is used immediately, no DNS needed. This is the override mechanism commonly used for local testing.

Question 2. How does a recursive resolver differ from a stub resolver?

  1. The recursive resolver has a static IP, the stub resolver uses DHCP
  2. The stub resolver only forwards the query, the recursive resolver itself asks Root → TLD → Authoritative
  3. The recursive resolver stores the entire DNS database
  4. The stub resolver is faster because it does not need the internet

Answer: The stub resolver only forwards the query, the recursive resolver itself asks Root → TLD → Authoritative. The stub resolver (in the OS) does not do recursion — it only forwards the question to 1 configured address (usually the gateway or 8.8.8.8). The recursive resolver is the one that actually asks each level of the DNS tree.

Question 3. A website is running and its A record is changed from 1.1.1.1 to 192.0.2.20. Users still see 1.1.1.1 after 1 hour. What is the most likely cause?

  1. The authoritative server has not updated the new record yet
  2. DNS over HTTPS is turned off
  3. The old record's TTL is still high (for example 86400s), and the resolver is caching it
  4. The browser caches the website's IP

Answer: The old record's TTL is still high (for example 86400s), and the resolver is caching it. Resolvers cache records according to the TTL. If the TTL is 86400s (24h, an example value per RFC 1035 §3.2.1) and the record has just been changed, the resolver keeps using its cache until the TTL expires. Fix: before changing the IP, lower the TTL to 300s and wait at least 24h for the old cache to expire.

Question 4. Which command shows the full path from Root → TLD → Authoritative?

  1. dig example.com @8.8.8.8
  2. nslookup example.com
  3. dig example.com +trace
  4. curl -v https://example.com

Answer: dig example.com +trace. dig +trace skips the recursive resolver's cache and performs each step itself: ask the root → get the TLD's NS → ask the TLD → get the domain's NS → ask the authoritative server → get the record. This is a must-have command when debugging DNS propagation.

Question 5. Email sent from a server keeps landing in spam. Which DNS record should you check first?

  1. A record — the mail server's IP
  2. CNAME record — an alias for mail
  3. TXT record — SPF, DKIM, DMARC
  4. NS record — nameserver

Answer: TXT record — SPF, DKIM, DMARC. SPF (a TXT record) confirms which servers are allowed to send email for the domain. DKIM (a TXT record) adds a digital signature to the email. DMARC (a TXT record) specifies how to handle email that fails. Missing or wrong in any of them → spam filters block it.

Question 6. Which transport protocol does DNS use by default, and when does it switch to TCP?

  1. Always TCP — because reliable delivery is needed
  2. UDP port 53 by default; switches to TCP when the response is > 512 bytes or for a zone transfer
  3. Always UDP port 53, never TCP
  4. HTTP port 80 because of DNS over HTTPS

Answer: UDP port 53 by default; switches to TCP when the response is > 512 bytes or for a zone transfer. UDP 53 is used for ordinary queries because it is fast (no handshake). When the response is truncated (TC=1) — for example many MX records — the client reconnects over TCP 53. Zone transfers (AXFR) always use TCP because the data is large.

Question 7. A CNAME record must not be placed at the apex domain (root domain). Why?

  1. CNAME is slower than an A record
  2. A CNAME at the apex violates the RFC because it cannot coexist with the SOA/NS records of the same name
  3. The root domain needs IPv6, so it must use AAAA
  4. CNAME only works with subdomains starting with www

Answer: A CNAME at the apex violates the RFC because it cannot coexist with the SOA/NS records of the same name. RFC 2181: a CNAME must not be placed alongside other records with the same name. The apex domain must have SOA and NS records → a CNAME cannot be placed there. This is why CDNs such as Cloudflare created 'CNAME flattening' or the 'ALIAS record' as a workaround.

Question 8. What is negative caching and when does it cause problems?

  1. A cache storing broken IPs — always causes problems
  2. A cache storing NXDOMAIN results — causes problems when a new record has just been created but you still get 'domain not found'
  3. A cache that is cleared when there is a network error
  4. A cache in the browser, not in DNS

Answer: A cache storing NXDOMAIN results — causes problems when a new record has just been created but you still get 'domain not found'. When a domain does not exist, the resolver caches the NXDOMAIN result according to the TTL from the SOA minimum field. If a new A record is created, a resolver that is caching NXDOMAIN will not see the new record until the cache expires. Fix: dig +trace to bypass the cache and see the authoritative response.

Flashcards