Computer Networking Cheatsheet

DNS

Use this Computer Networking reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

Resolution path

DNS turns a name into an address by walking down the hierarchy. Nothing in the chain knows the whole answer.

browser cache → OS stub resolver → recursive resolver (ISP, 1.1.1.1)
                                        |
                                        | iterative queries
                                        v
                          root servers (.)  →  "ask the .com TLD"
                          TLD servers (.com) → "ask ns1.example.com"
                     authoritative (example.com) → 93.184.216.34 (answer)

The recursive resolver does the walking and caches the result. The authoritative server holds the zone file and answers with the aa flag set. A stub resolver just asks its configured resolver and trusts the reply.

Record types

TypeHoldsExample value
AIPv4 address93.184.216.34
AAAAIPv6 address2606:2800:220:1:248:1893:25c8:1946
CNAMEalias to another namewww → lb.example.net.
MXmail exchanger plus priority10 mail.example.com.
NSauthoritative nameservers for the zonens1.example.com.
TXTarbitrary text, SPF, DKIM, domain proofs"v=spf1 include:_spf.google.com ~all"
SOAzone authority, serial, timerssee below
PTRreverse lookup, IP → name34.216.184.93.in-addr.arpa
SRVservice host plus port and priority_sip._tcp 10 60 5060 sip.example.com.
CAAwhich CAs may issue certs0 issue "letsencrypt.org"
SVCB / HTTPSendpoint hints, ALPN, ECH1 . alpn="h3,h2"
DNSKEY / DS / RRSIGDNSSEC keys and signatures—
ALIAS / ANAMEvendor CNAME-at-apex workaroundprovider specific

A CNAME cannot coexist with any other record at the same name, which is why you cannot put one at the zone apex (example.com) where SOA and NS must live. Providers solve it with ALIAS records or flattening.

Query and inspect with dig

dig example.com                       # A record, full answer sections
dig +short example.com                # just the answer values
dig AAAA example.com +short
dig MX example.com +short
dig TXT _dmarc.example.com +short
dig NS example.com +short
dig SOA example.com
dig -x 8.8.8.8 +short                 # reverse, PTR
dig @1.1.1.1 example.com              # ask a specific resolver
dig @ns1.example.com example.com      # bypass caches, ask authoritative
dig +trace example.com                # walk root → TLD → authoritative
dig +norecurse @a.root-servers.net example.com
dig +dnssec example.com               # show RRSIG, check the ad flag
dig example.com any                   # usually refused now, RFC 8482

Flags in the header tell you what happened:

FlagMeaning
qrthis is a response
aaauthoritative answer, straight from the zone
rdrecursion desired, the client asked for it
rarecursion available on that server
adauthenticated data, DNSSEC validated
RCODEMeaning
NOERRORsuccess, though ANSWER may still be 0 records
NXDOMAINthe name does not exist at all
SERVFAILresolver failed, often a DNSSEC validation failure
REFUSEDthe server will not answer you, ACL or wrong zone
NODATAname exists, no record of that type (NOERROR, 0 answers)

NXDOMAIN and NOERROR with zero answers are different bugs. The first means the name is wrong, the second usually means you asked for the wrong type.

TTL, caching, and zone timers

TTL is set by the zone owner in seconds and every cache along the way honors it. dig shows the remaining TTL, so a value counting down means you hit a cache.

example.com. 3600 IN SOA ns1.example.com. admin.example.com. (
    2026091501 ; serial, bump on every change
    7200       ; refresh, how often secondaries check
    3600       ; retry, after a failed check
    1209600    ; expire, secondaries stop answering after this
    3600 )     ; minimum, negative-caching TTL for NXDOMAIN

Migration discipline: lower the TTL to 60-300 s at least one old-TTL period before you change a record, cut over, verify, then raise it back. Skipping that is why "DNS propagation" seems to take days. There is no propagation, only expiring caches.

Local resolver configuration

cat /etc/resolv.conf                  # nameserver, search, options
resolvectl status                      # systemd-resolved, per-link resolvers
resolvectl query example.com
resolvectl flush-caches
resolvectl dns eth0 1.1.1.1 1.0.0.1
getent hosts example.com               # honors nsswitch, unlike dig
nslookup example.com                   # portable, less detail than dig
host example.com
scutil --dns                           # macOS resolver config
dscacheutil -flushcache                # macOS, plus killall -HUP mDNSResponder

Two traps. /etc/hosts wins over DNS for getent and for applications, but dig ignores it entirely, so a host that pings fine while dig disagrees is almost always a hosts-file entry. And search domains append suffixes to short names, which can turn db into db.corp.example.com and produce surprising answers.

Security and privacy layers

MechanismProtectsPort · transport
DNSSECintegrity, proves the answer is the zone'sUDP/TCP 53, adds RRSIG · DS
DoT (DNS over TLS)privacy from the networkTCP 853
DoH (DNS over HTTPS)privacy, blends with web trafficTCP 443
DoQ (DNS over QUIC)privacy, lower latencyUDP 853
DNS cookiesoff-path spoofingEDNS0 option

DNSSEC signs, it does not encrypt, and DoH encrypts, it does not authenticate the zone. They solve different problems and are commonly deployed together.

Classic attacks to know: cache poisoning (inject a forged answer, mitigated by source-port randomization, 0x20 encoding, and DNSSEC), subdomain takeover (a dangling CNAME to a deprovisioned cloud host that an attacker can claim), and NXDOMAIN hijacking by resolvers that return ad pages instead of an honest error.

dig +dnssec example.com | grep -E 'flags:|RRSIG'
delv example.com                       # validating resolver, states the chain
kdig -d @1.1.1.1 +tls example.com      # DoT probe, knot-dnsutils
curl -sH 'accept: application/dns-json' \
  'https://cloudflare-dns.com/dns-query?name=example.com&type=A'