DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

NetworkingLesson 4 of 12
Your progress · 0%
On this page
  1. Root, then .com, then the answer
  2. Record types
  3. TTL and caching
  4. Resolvers on Linux
↖ Course roadmap
Lesson 4 · DNS

DNS: how names become addresses

Follow a real lookup from the root servers down to the answer, read the record types you will use, and learn why DNS changes take time and how to make them safely.

25 min read · beginner · hands-on

By the end of this lesson you'll be able to

0 of 4 complete
Lab status

Verified lab: every lookup below was made with dig against real public DNS (root servers, .com servers, Cloudflare, Google and Cloudflare public resolvers). The output shown is real.
Cloud lab: none needed.

"It's always DNS" is a joke because it's so often true: a wrong record, a stale cache or a missing resolver looks like "the site is down". This lesson makes DNS something you can check in seconds.

Root, then .com, then the answer

You can walk the same chain by hand. +norecurse asks each server only what it knows itself:

$ dig @a.root-servers.net com NS +norecurse +noall +authority
com.          172800  IN  NS  l.gtld-servers.net.
com.          172800  IN  NS  j.gtld-servers.net.
...
$ dig @a.gtld-servers.net example.com NS +norecurse +noall +authority
example.com.  172800  IN  NS  hera.ns.cloudflare.com.
example.com.  172800  IN  NS  elliott.ns.cloudflare.com.
$ dig @hera.ns.cloudflare.com example.com A +norecurse +noall +answer
example.com.  300     IN  A   104.20.23.154
example.com.  300     IN  A   172.66.147.243

The root doesn't know example.com, but knows who runs .com. The .com servers know who runs example.com (Cloudflare). Cloudflare's server is authoritative: it holds the real records. Your resolver does this walk once and then caches each step, so the next lookup is one hop.

Record types

$ dig github.com MX +short
0 github-com.mail.protection.outlook.com.
$ dig github.com NS +short
dns1.p08.nsone.net.
...
ns-421.awsdns-52.com.

GitHub runs its DNS on two providers at once (NS1 and Route 53), so one provider's outage doesn't take the name down. When a name doesn't exist at all, the status says so:

$ dig no-such-domain-xq7z1.com A | grep status
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 58160
dig no-such-domain-xq7z1.com A | grep status
  • |pipe: send output to the next command
  • grepfind lines that match a pattern

NXDOMAIN is "no such name". NOERROR with no answer means the name exists but has no record of that type. SERVFAIL means the resolver couldn't get an answer, often a broken delegation or DNSSEC problem.

TTL and caching

$ dig @8.8.8.8 example.com A +noall +answer
example.com.   282  IN  A  172.66.147.243
$ dig @1.1.1.1 example.com A +noall +answer
example.com.   23   IN  A  104.20.23.154

The second column is the time left in that resolver's cache. Google had 282 of 300 seconds left, Cloudflare 23. Both are correct; they just cached at different moments. That's all "DNS propagation" is.

Resolvers on Linux

$ grep hosts /etc/nsswitch.conf
hosts:          files dns
$ cat /etc/resolv.conf
nameserver 10.255.255.254
search lan
grep hosts /etc/nsswitch.conf
  • grepfind lines that match a pattern
cat /etc/resolv.conf
  • catprint a file

files dns means: check /etc/hosts first, then ask the nameserver in /etc/resolv.conf (WSL's resolver here; on a VPC server it's the VPC's .2 address). search lan appends .lan to short names. Two things to remember: an entry in /etc/hosts beats DNS (great for testing, terrible when forgotten), and dig skips /etc/hosts while getent hosts name follows the same order your apps do.

In production

Kubernetes runs its own DNS (CoreDNS), so orders.shop.svc.cluster.local resolves inside the cluster. A Pod's resolv.conf has several search domains and ndots:5, which is why lookups of external names can be slow: each short name is tried with every search suffix first. AWS course lesson 8 covers Route 53.

⌘
Mini mission

Trace a domain you use

Pick a domain you use at work or for a project. Walk its lookup from a root server, list its A, AAAA, MX, NS and TXT records, compare the TTL from two public resolvers, and note which resolver your own machine uses.

Input: digOutput: ~/dns.txt
Reveal one safe solution
$ D=github.com
$ dig @a.root-servers.net ${D##*.} NS +norecurse +noall +authority | head -2 > ~/dns.txt
$ for t in A AAAA MX NS TXT; do echo "== $t"; dig $D $t +short; done >> ~/dns.txt
$ dig @8.8.8.8 $D A +noall +answer >> ~/dns.txt; dig @1.1.1.1 $D A +noall +answer >> ~/dns.txt
$ grep nameserver /etc/resolv.conf >> ~/dns.txt
dig @a.root-servers.net ${D##*.} NS +norecurse +noall +authority | head -2 > ~/dns.txt
  • |pipe: send output to the next command
  • headshow the first lines
  • >write output to a file, replacing it
for t in A AAAA MX NS TXT; do echo "== $t"; dig $D $t +short; done >> ~/dns.txt
  • forloop over a list
  • dostart of what the loop runs each time
  • echoprint text
  • doneend of the loop
  • >>append output to a file
grep nameserver /etc/resolv.conf >> ~/dns.txt
  • grepfind lines that match a pattern

Then follow the name servers from the first step down to an authoritative answer yourself.

Try it yourself

0 of 5 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 3

You changed an A record ten minutes ago and some users still reach the old server. Why?

Go deeper

Official reading for this lesson

  • Docsdig(1) manualman7.org ↗
  • ArticleHow DNS works (comic)howdns.works ↗
  • DocsAmazon Route 53 routing policiesAWS Docs ↗
  • ToolDNS lookup toolnslookup.io ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousTCP, UDP and ports: refused is not timed outNext lesson →HTTP: read a request like a log