Day 6: DNS
Goal: You run your own DNS server for the training ground, read every
diganswer by its status (NOERROR, NXDOMAIN, SERVFAIL, no answer), distinguish whatdigasks from what your applications ask, and identify search domains,/etc/hostsentries and dead name servers as the cause · Duration: approx. 90 min · Prerequisite: Day 5 including clean-up
Briefing
- “It’s always DNS” is a joke because it is so often true. Almost every connection starts with a name resolution, and when that stalls, the fault looks like anything else: timeout, wrong server, “Could not resolve host”, or simply a page that loads three seconds too slowly.
- The most important insight of the day:
digand your applications don’t ask the same thing.digsends a DNS query to a server. curl, Python, Java and almost everything else ask the C library (getaddrinfo), which looks into/etc/hostsfirst, appends search domains and tries the name servers one after another. If you only test withdig, you miss part of that. - Today the training ground gets its own DNS server with the internal
zone
firma.lab. It stays on for the rest of the course.
Drill 1: Reading dig
# on netz
# DNS query for the A record (IPv4 address) of example.com
# dig <Name> asks the first name server from /etc/resolv.conf, default type A
sudo ip netns exec client dig example.comExpected (addresses, ID, TTL and times differ):
; <<>> DiG 9.18.39-0ubuntu0.24.04.9-Ubuntu <<>> example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5035
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;example.com. IN A
;; ANSWER SECTION:
example.com. 67 IN A 104.20.23.154
example.com. 67 IN A 172.66.147.243
;; Query time: 6 msec
;; SERVER: 1.1.1.1#53(1.1.1.1) (UDP)
;; WHEN: Thu Nov 26 11:51:51 UTC 2026
;; MSG SIZE rcvd: 72
Five fields are enough for almost any diagnosis:
| Field | Meaning |
|---|---|
status: NOERROR |
result of the query; the most important values are in Drill 3 |
flags: … aa … |
missing here: the answer comes from a cache, not from the
authoritative server (aa = authoritative answer) |
ANSWER: 2 |
number of answers; 0 with NOERROR means:
the name exists, but not with this type |
67 in the answer line |
TTL in seconds: how much longer the answer may be cached |
SERVER: 1.1.1.1#53 (UDP) |
who answered, over which port and which protocol |
For scripts and quick looks there is the short form, and for comparison the question curl would ask:
# on netz
# +short only the answers, without header and sections
sudo ip netns exec client dig +short example.com
# How the C library resolves the name (the same function curl uses)
# getent ahosts <Name> calls getaddrinfo(), like an application
# grep STREAM only one line per address (STREAM = TCP)
sudo ip netns exec client getent ahosts example.com | grep STREAMExpected:
172.66.147.243
104.20.23.154
104.20.23.154 STREAM example.com
172.66.147.243 STREAM
2606:4700:10::6814:179a STREAM
2606:4700:10::ac42:93f3 STREAM
getent also returns IPv6 addresses, because an
application always asks for A and AAAA at the same time, and sorts them
by a fixed rule (RFC 6724): the client has no IPv6 route, so the IPv4
addresses come first. getent writes the name only in the
first line. Remember getent ahosts as “What does my
application see?” – you need it in the Exercise.
Save your progress? Sign up for free
Drill 2: Your own DNS server
In company networks an internal DNS server resolves the company’s own names and forwards everything else. You build that with dnsmasq in namespace router. Only the program is installed, without a system service – that would otherwise take port 53 on the VM and fight with the systemd resolver:
# on netz
# Only the dnsmasq program, no service
sudo apt-get install -y dnsmasq-base
# DNS server in namespace router as a transient unit
# --keep-in-foreground don't go to the background (systemd supervises the process)
# --no-resolv don't read /etc/resolv.conf
# --no-hosts don't serve the VM's /etc/hosts
# --server=1.1.1.1 forward everything unknown to this server
# --local=/firma.lab/ be authoritative for firma.lab itself, never forward
# --listen-address=10.1.0.1 --bind-interfaces listen only on this address
# --host-record=<Name>,<IP> create an A record
# --log-queries --log-facility=- log every query, to standard output (into the journal)
sudo systemd-run --unit=dns-router ip netns exec router dnsmasq --keep-in-foreground \
--no-resolv --no-hosts --server=1.1.1.1 --local=/firma.lab/ \
--listen-address=10.1.0.1 --bind-interfaces \
--host-record=web.firma.lab,10.2.0.10 --host-record=db.firma.lab,10.2.0.20 \
--log-queries --log-facility=-
# Is it listening? (Day 5)
sudo ip netns exec router ss -ltunp 'sport = :53'Expected:
Running as unit: dns-router.service; invocation ID: 724ad7fc8dbe47b2ab5d056a04c0d534
Netid State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
udp UNCONN 0 0 10.1.0.1:53 0.0.0.0:* users:(("dnsmasq",pid=51979,fd=4))
tcp LISTEN 0 32 10.1.0.1:53 0.0.0.0:* users:(("dnsmasq",pid=51979,fd=5))
DNS listens on UDP and TCP. Normal queries go over UDP; if an answer is too large, the client repeats the query over TCP. A firewall that only allows UDP 53 therefore breaks some names but not others.
Now you switch the client to the new server and give it a search domain:
# on netz
# Rewrite the client's resolv.conf (mechanism from Day 0)
# printf '…\n…\n' print two lines
# nameserver 10.1.0.1 ask this DNS server
# search firma.lab append .firma.lab to short names
printf 'nameserver 10.1.0.1\nsearch firma.lab\n' | sudo tee /etc/netns/client/resolv.conf
# @10.1.0.1 ask exactly this server, regardless of resolv.conf
sudo ip netns exec client dig +short @10.1.0.1 web.firma.lab
sudo ip netns exec client dig +short example.com
# The server's log
# -o cat only the text
sudo journalctl -u dns-router -o cat --no-pager | tail -5Expected:
nameserver 10.1.0.1
search firma.lab
10.2.0.10
104.20.23.154
172.66.147.243
dnsmasq[51979]: config web.firma.lab is 10.2.0.10
dnsmasq[51979]: query[A] example.com from 10.1.0.10
dnsmasq[51979]: forwarded example.com to 1.1.1.1
dnsmasq[51979]: reply example.com is 104.20.23.154
dnsmasq[51979]: reply example.com is 172.66.147.243
config means: answered from its own configuration,
forwarded means: forwarded. A DNS server’s log is often the
fastest proof of whether a query arrives at all.
Save your progress? Sign up for free
Drill 3: Four answers, four causes
# on netz
# Name that does not exist
sudo ip netns exec client dig gibtsnicht.firma.lab | grep status
# Name that exists, but without an IPv6 address
# AAAA record type: IPv6 address
# grep -E 'status|ANSWER:' status line and the line with the counters
sudo ip netns exec client dig AAAA web.firma.lab | grep -E 'status|ANSWER:'
# Domain with a deliberately broken DNSSEC signature (test domain on the internet)
sudo ip netns exec client dig dnssec-failed.org | grep -E 'status|EDE'Expected:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 4307
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23577
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 46298
; EDE: 9 (DNSKEY Missing): (no SEP matching the DS found for dnssec-failed.org.)
And the fourth case: no answer comes at all. For this you deliberately ask two servers that aren’t DNS servers:
# on netz
# Address without a machine (like Day 4)
# +tries=1 +timeout=2 only one attempt, wait 2 seconds (default: 3 attempts of 5 seconds each)
time sudo ip netns exec client dig +tries=1 +timeout=2 @10.1.0.99 example.com | head -1
# Machine with no DNS server running
time sudo ip netns exec client dig +tries=1 +timeout=2 @10.2.0.10 example.com | head -1Expected (for each command the first line and the
real line):
;; communications error to 10.1.0.99#53: timed out
real 0m2.091s
;; communications error to 10.2.0.10#53: connection refused
real 0m0.073s
| Answer | who says so | meaning | where you keep looking |
|---|---|---|---|
NXDOMAIN |
authoritative server | name does not exist | typo, wrong zone, search domain (Drill 4) |
NOERROR, ANSWER: 0 |
authoritative server | name exists, but not with this type (NODATA) | record type: A vs. AAAA vs. CNAME |
SERVFAIL |
resolver | resolver could not obtain a valid answer | DNSSEC, authoritative server broken, forwarding dead;
EDE often names the reason |
timed out |
nobody | server does not answer | wrong address, firewall drops UDP 53, server off |
connection refused |
target machine | machine is there, but no DNS server | wrong server address, service dead |
EDE (Extended DNS Error) is new enough that many people
overlook it: it only appears in the long output of dig and
often explains a SERVFAIL in one sentence. The counter-proof for DNSSEC
comes from +cd (checking disabled):
dig +cd dnssec-failed.org gets an address, because the
resolver skips the validation. A name that only resolves with
+cd has a DNSSEC problem.
Without +tries and +timeout,
dig would have waited 3 × 5 seconds on the first server.
That is more than most people’s patience – and less than that of many
applications, which simply hang silently.
Save your progress? Sign up for free
Drill 4: Search domains and who asks what
# on netz
# Short name via the C library
sudo ip netns exec client getent ahosts web | grep STREAM
# curl with a short name
sudo ip netns exec client curl -s http://web/
# the same short name with dig
sudo ip netns exec client dig +short web
# What did dig really ask?
# -A1 also print one line after the match
sudo ip netns exec client dig web | grep -A1 'QUESTION SECTION'Expected:
10.2.0.10 STREAM web.firma.lab
hallo von web
;; QUESTION SECTION:
;web. IN A
curl finds web, dig +short prints nothing
at all. The C library reads search firma.lab from
resolv.conf and asks for web.firma.lab.
dig ignores the search domain and literally asks for
web. – the name of a top-level domain on the internet that
actually exists, just without an A record. That is why you get an empty
answer instead of NXDOMAIN. With +search, dig
behaves like an application:
# on netz
# +search apply search domains from resolv.conf
sudo ip netns exec client dig +short +search webExpected:
10.2.0.10
The rule behind it: a name with fewer dots than
ndots (default 1) is tried with the search domains first.
web has zero dots, so it becomes
web.firma.lab. On Day 13 you see what happens when
Kubernetes sets ndots:5 and four search domains – then
every query for api.example.com turns into a small
avalanche.
On the VM itself, systemd-resolved manages the name
servers. There you don’t ask resolv.conf but the
service:
# on netz (root namespace)
# Name servers per interface, as they came via DHCP
# status eth0 only this interface
resolvectl status eth0
# Resolution via systemd-resolved, showing where the answer came from
resolvectl query example.comExpected (your servers and domain differ):
Link 2 (eth0)
Current Scopes: DNS
Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 8.8.8.8
DNS Servers: 172.16.0.15 8.8.8.8 1.1.1.1
DNS Domain: localdomain
example.com: 104.20.23.154 -- link: eth0
172.66.147.243 -- link: eth0
2606:4700:10::6814:179a -- link: eth0
2606:4700:10::ac42:93f3 -- link: eth0
-- Information acquired via protocol DNS in 22.1ms.
-- Data is authenticated: no; Data was acquired via local or encrypted transport: no
-- Data from: network
Look at Current DNS Server: it is not the first one in
the list. systemd-resolved switches servers when one does not answer,
and then stays with the new one. In the lab the first
server apparently failed at some point. If a colleague asks “Why doesn’t
the VM know our internal name anymore?”, this is a good first
hypothesis: the internal DNS server was briefly gone, resolved switched
to a public one, and that one doesn’t know firma.lab.
Finally, the path of a query through the internet, without a cache, starting at the root:
# on netz
# +trace query from the root servers downward yourself, instead of letting the resolver do it
# +nodnssec omit DNSSEC signatures (shorter output)
# grep Received only the lines that show who answered at each level
sudo ip netns exec client dig +trace +nodnssec example.com | grep ReceivedExpected (which root and which com server answers varies):
;; Received 239 bytes from 10.1.0.1#53(10.1.0.1) in 6 ms
;; Received 836 bytes from 192.5.5.241#53(f.root-servers.net) in 5 ms
;; Received 359 bytes from 192.31.80.30#53(d.gtld-servers.net) in 25 ms
;; Received 72 bytes from 173.245.58.162#53(hera.ns.cloudflare.com) in 6 ms
Four levels: list of root servers from your own resolver, a root
server refers to the servers for com, those refer to the
servers of example.com, and those answer with the address
(the full section is in the output without grep).
+trace helps when a resolver returns a stale answer: What
does the authoritative server say now?
Save your progress? Sign up for free
Exercise
Build in the fault without looking at the solution:
# on netz
# separate hosts file for the client (laid over /etc/hosts like resolv.conf)
printf '127.0.0.1 localhost\n10.2.0.99 web.firma.lab web\n' | sudo tee /etc/netns/client/hosts >/dev/nullA developer reports: “curl http://web.firma.lab/ hangs
for three seconds and fails. But DNS is correct, dig
returns 10.2.0.10.” Prove why both statements are true, and
find the file.
Solution
# on netz
# Symptom
sudo ip netns exec client curl -sS -m5 http://web.firma.lab/
# What dig says
sudo ip netns exec client dig +short web.firma.lab
# What the application sees
sudo ip netns exec client getent ahosts web.firma.lab | grep STREAM
# Where curl really connects to
# -v verbose: connection setup on stderr
# 2>&1 redirect stderr into the pipe
# grep Trying only the line with the destination address
sudo ip netns exec client curl -sv -m5 http://web.firma.lab/ 2>&1 | grep Trying
# In which order does the C library ask?
# grep ^hosts: only the line for host names from nsswitch.conf
grep ^hosts: /etc/nsswitch.confExpected:
curl: (7) Failed to connect to web.firma.lab port 80 after 3063 ms: Couldn't connect to server
10.2.0.10
10.2.0.99 STREAM web.firma.lab
* Trying 10.2.0.99:80...
hosts: files dns mymachines
files comes before dns: the C library looks
into /etc/hosts first, and a hit there ends the search.
dig asks only DNS and never sees the entry. You know the
three seconds from Days 3 and 4: the router looks for
10.2.0.99 via ARP and gives up. In the namespace,
/etc/hosts comes from /etc/netns/client/hosts,
on a normal machine directly from /etc/hosts. Fix and
proof:
# on netz
sudo rm /etc/netns/client/hosts
sudo ip netns exec client getent ahosts web.firma.lab | grep STREAM
sudo ip netns exec client curl -s http://web.firma.lab/Expected:
10.2.0.10 STREAM web.firma.lab
hallo von web
Such entries are created for good reasons – a test before a
migration, a workaround during an incident – and then stay for years. In
containers Docker writes /etc/hosts itself, in Kubernetes
hostAliases add to it. When dig and the
application contradict each other, getent is the
referee.
Save your progress? Sign up for free
Challenge
Build in the fault:
# on netz
# A second name server is now first in line – one that doesn't exist
printf 'nameserver 10.1.0.53\nnameserver 10.1.0.1\nsearch firma.lab\n' | sudo tee /etc/netns/client/resolv.conf >/dev/nullThe ticket: “Since the DNS change every page is slow, but everything works.” Measure how much time goes where, find the cause with a command that asks only one server, and prove the fix with the same measurement.
Solution
# on netz
# Measure name resolution time and total time separately
# -w '%{time_namelookup} %{time_total}\n' seconds until the name is resolved, seconds in total (more on Day 7)
sudo ip netns exec client curl -s -o /dev/null -w '%{time_namelookup} %{time_total}\n' http://web.firma.lab/
# Which servers are in the list?
sudo ip netns exec client cat /etc/resolv.conf
# Ask each server individually, short timeout
sudo ip netns exec client dig +tries=1 +timeout=2 @10.1.0.53 web.firma.lab | head -1
sudo ip netns exec client dig +tries=1 +timeout=2 +short @10.1.0.1 web.firma.labExpected:
3.073628 3.077048
nameserver 10.1.0.53
nameserver 10.1.0.1
search firma.lab
;; communications error to 10.1.0.53#53: timed out
10.2.0.10
Of 3.08 seconds, 3.07 go into name resolution; the connection itself
takes 3 milliseconds. The C library asks the name servers one
after another: only when the first gives up does the second get
its turn. Here the kernel gives up after about 3 seconds, because
10.1.0.53 is in the local network and nobody answers ARP
(ip neigh show 10.1.0.53 shows INCOMPLETE). If
a dead name server sits behind a router, the C library waits out its own
timeout: 5 seconds per attempt. And because the C library doesn’t
remember which server is dead, every new connection
pays the delay. Fix and proof:
# on netz
printf 'nameserver 10.1.0.1\nsearch firma.lab\n' | sudo tee /etc/netns/client/resolv.conf >/dev/null
sudo ip netns exec client curl -s -o /dev/null -w '%{time_namelookup} %{time_total}\n' http://web.firma.lab/Expected:
0.003514 0.008014
Multiple name servers in resolv.conf are for failover,
not load balancing. If the first one fails, everything gets slow, but
nothing breaks – which is exactly why it is often noticed only days
later. options timeout:1 attempts:1 in
resolv.conf reduces the damage, options rotate
spreads the queries, but neither replaces monitoring the DNS
servers.
Save your progress? Sign up for free
Checkpoint
# on netz
# DNS server in router is running
systemctl is-active dns-router
# client asks it, with search domain, and has no hosts file of its own
sudo ip netns exec client cat /etc/resolv.conf
ls /etc/netns/client/
# internal and external names resolve
sudo ip netns exec client getent ahosts db | grep STREAM
sudo ip netns exec client dig +short example.com | head -1
# Unknown name in its own zone is answered locally, not forwarded to the internet
sudo ip netns exec client dig gibtsnicht.firma.lab | grep -o 'status: [A-Z]*'
# -n 1 only the last entry
sudo journalctl -u dns-router -o cat -n 1 --no-pagerExpected (the address of example.com may differ):
active
nameserver 10.1.0.1
search firma.lab
resolv.conf
10.2.0.20 STREAM db.firma.lab
104.20.23.154
status: NXDOMAIN
dnsmasq[52313]: config gibtsnicht.firma.lab is NXDOMAIN
Clean up
Nothing. The DNS server dns-router and the client’s new
resolv.conf are part of the training ground from today on –
from Day 7 you address the services by name. After a reboot of the VM,
restart the unit dns-router with the command from Drill 2
(Appendix A); the resolv.conf under
/etc/netns/client/ is preserved.
Interview note
“dig finds the name but the application doesn’t – or the
other way around. Why?” dig asks only DNS; the application
asks getaddrinfo(), which reads
/etc/nsswitch.conf: first /etc/hosts, then DNS
with search domains and ndots. The command that asks like
the application is getent ahosts. Bonus points: NXDOMAIN is
an answer, SERVFAIL is a resolver error (check DNSSEC with
+cd), and a dead first name server makes everything slow
but breaks nothing.
Enterprise note
With --local=/firma.lab/, the internal server answers
questions about its own zone itself – including with NXDOMAIN. Without
this setting it forwards typos like wbe.firma.lab to the
public resolver, and internal names end up in other people’s logs. This
happens unnoticed in many companies that introduce split DNS; that is
why the checkpoint checks it too. And: after an outage of the internal
DNS server, you should check on all machines with systemd-resolved which
server is the Current DNS Server – resolved only switches
back when the current server fails or the network configuration changes;
when in doubt, sudo systemctl restart systemd-resolved
helps.