Day 6: DNS and DHCP: dig, resolvectl and dnsmasq
Goal: Understand how the host resolves names and why the first query sometimes takes three seconds. Read DNS answers with
dig. Then serve a small network of your own with dnsmasq: addresses via DHCP, fixed assignments, a local domain and forwarding to the outside · Duration: approx. 90 min · Prerequisite: Day 2
Briefing
- Programs do not ask a DNS server directly. They read
/etc/resolv.conf, and on Ubuntu it says127.0.0.53: the stub of systemd-resolved. It asks the real servers, caches the answers and decides which server is currently in use. digasks a DNS server directly and shows the whole answer,resolvectlshows what systemd-resolved knows and does.- dnsmasq is a DNS forwarder, cache and DHCP server in one program, small enough for any router. It sits inside home routers, libvirt, many lab installers and, on Day 15, the vRouter.
- DNS has already stood out twice in this course: the first DNS server from the lab’s DHCP does not answer (Day 1), and the stub does not work in namespaces (Day 2). Both get resolved today.
Program ───▶ /etc/resolv.conf ──▶ 127.0.0.53 (systemd-resolved) ──▶ 172.16.0.15 ✗
Cache └─▶ 8.8.8.8 ✓, 1.1.1.1
Drill 6–9: c1 ─┐
c2 ─┼─ br-lan 10.80.0.1 ── gw: dnsmasq (DNS + DHCP) ── up0 ──▶ Host ──NAT──▶ 1.1.1.1
c3 ─┘
Drill 1: How the host resolves
# on netz
ls -l /etc/resolv.conf
grep -v '^#' /etc/resolv.conf
sudo ss -ulpn 'sport = :53'
resolvectl status eth0 | grep -E "Current|DNS Servers|DNS Domain"Expected:
lrwxrwxrwx 1 root root 39 Jun 15 13:43 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
nameserver 127.0.0.53
options edns0 trust-ad
search localdomain
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
UNCONN 0 0 127.0.0.54:53 0.0.0.0:* users:(("systemd-resolve",pid=563,fd=16))
UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=563,fd=14))
Current DNS Server: 8.8.8.8
DNS Servers: 172.16.0.15 8.8.8.8 1.1.1.1
DNS Domain: localdomain
/etc/resolv.conf is a link managed by systemd-resolved.
If you edit the file by hand, you overwrite the link or lose the change
at the next restart. The stub listens on 127.0.0.53 (with
cache and search domains) and 127.0.0.54 (simply passes
through). search localdomain means: a name without a dot is
tried as name.localdomain first. The DNS servers come from
the lab’s DHCP.
Save your progress? Sign up for free
Drill 2: Reading an answer
# on netz
dig deb.debian.org | sed -n '/QUESTION/,$p'Expected:
;; QUESTION SECTION:
;deb.debian.org. IN A
;; ANSWER SECTION:
deb.debian.org. 1677 IN CNAME debian.map.fastlydns.net.
debian.map.fastlydns.net. 33 IN A 151.101.66.132
debian.map.fastlydns.net. 33 IN A 151.101.2.132
debian.map.fastlydns.net. 33 IN A 151.101.194.132
debian.map.fastlydns.net. 33 IN A 151.101.130.132
;; Query time: 3094 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Mon Nov 23 21:29:45 UTC 2026
;; MSG SIZE rcvd: 145
The question was for an A record (IPv4). The answer is a
CNAME, a pointer to another name, and only its A
records carry addresses. The number after the name is the
TTL: the answer may be cached for that many seconds.
And 3094 msec is conspicuously slow, more on that
shortly.
The most useful forms:
# on netz
dig +short deb.debian.org
dig +short @1.1.1.1 deb.debian.org A
dig +short debian.org MX
dig +short debian.org AAAA | head -2
dig +short debian.org TXT | head -2
dig +short -x 1.1.1.1Expected:
debian.map.fastlydns.net.
151.101.2.132
151.101.194.132
151.101.66.132
151.101.130.132
debian.map.fastlydns.net.
151.101.66.132
151.101.130.132
151.101.194.132
151.101.2.132
0 mailly.debian.org.
0 muffat.debian.org.
0 mitropoulos.debian.org.
2a04:4e42:200::644
2a04:4e42:600::644
"google-site-verification=3u3n3jrPTlPrumIxQAl4r0utY6hrkj1_DSvdxjiTSP8"
"v=spf1 include:_spf.debian.org ~all"
one.one.one.one.
| Form | Effect |
|---|---|
+short |
data only |
@1.1.1.1 |
ask exactly this server, bypassing the stub |
MX, AAAA, TXT,
NS |
record type (mail server, IPv6, text, authoritative servers) |
-x 1.1.1.1 |
reverse lookup: name for an address (PTR) |
You can see the cache from the TTL counting down:
# on netz
dig +noall +answer +stats example.org | grep -E "IN|Query time"
dig +noall +answer +stats example.org | grep -E "IN|Query time"
sleep 3; dig +noall +answer example.orgExpected:
example.org. 300 IN A 172.66.157.237
example.org. 300 IN A 104.20.26.136
;; Query time: 19 msec
example.org. 299 IN A 172.66.157.237
example.org. 299 IN A 104.20.26.136
;; Query time: 0 msec
example.org. 296 IN A 104.20.26.136
example.org. 296 IN A 172.66.157.237
The second answer comes from the systemd-resolved cache after 0 ms, with the remaining TTL.
Save your progress? Sign up for free
Drill 3: The dead first server
On Day 1, 172.16.0.15 showed up as FAILED
in the neighbor table. Asked directly:
# on netz
dig +tries=1 +time=2 @172.16.0.15 example.org | tail -3
dig +short @8.8.8.8 example.orgExpected:
; (1 server found)
;; global options: +cmd
;; no servers could be reached
172.66.157.237
104.20.26.136
systemd-resolved remembers which server worked last
(Current DNS Server: 8.8.8.8). After a restart, however, it
starts at the top of the list again:
# on netz
sudo systemctl restart systemd-resolved
sleep 1
dig +noall +stats debian.net | grep "Query time"
resolvectl status eth0 | grep CurrentExpected:
;; Query time: 3083 msec
Current DNS Server: 8.8.8.8
That was the slowness from Drill 2. The first query goes to
172.16.0.15, waits in vain, and only then does resolved
switch to 8.8.8.8. A dead entry in the DHCP configuration
thus costs seconds after every restart and every network change. The
real fix lies with the lab’s DHCP server, not on the VM.
Save your progress? Sign up for free
Drill 4: resolvectl and the path to the root
resolvectl query asks like a program does, via the stub,
and tells you where the answer came from:
# on netz
resolvectl query example.net
sudo resolvectl statistics | sed -n '/Cache/,/Misses/p'
sudo resolvectl flush-caches
sudo resolvectl statistics | sed -n '/Cache/,/Misses/p'Expected:
example.net: 172.66.175.59 -- link: eth0
104.20.21.8 -- link: eth0
2606:4700:10::ac42:af3b -- link: eth0
2606:4700:10::6814:1508 -- link: eth0
-- Information acquired via protocol DNS in 24.4ms.
-- Data is authenticated: no; Data was acquired via local or encrypted transport: no
-- Data from: network
Cache
Current Cache Size: 3
Cache Hits: 3
Cache Misses: 13
Cache
Current Cache Size: 0
Cache Hits: 3
Cache Misses: 13
statistics needs sudo, otherwise you get
Permission denied. flush-caches empties the
cache, the counters stay. And if you really want to know who is
authoritative for a name, let dig itself ask starting from
the root, without a cache:
# on netz
dig +trace +nodnssec debian.org A | grep -E "Received|^debian\.org\.\s+[0-9]+\s+IN\s+A|^org\.\s+[0-9]+\s+IN\s+NS" | head -10Expected:
;; Received 239 bytes from 127.0.0.53#53(127.0.0.53) in 11 ms
org. 172800 IN NS v0n3.pir-ns.org.
org. 172800 IN NS v0n0.pir-ns.org.
org. 172800 IN NS v2n1.pir-ns.org.
org. 172800 IN NS v0n2.pir-ns.org.
org. 172800 IN NS v0n1.pir-ns.org.
org. 172800 IN NS v2n0.pir-ns.org.
;; Received 424 bytes from 192.58.128.30#53(j.root-servers.net) in 11 ms
;; Received 147 bytes from 199.249.112.1#53(v2n0.pir-ns.org) in 11 ms
debian.org. 300 IN A 151.101.130.132
Three steps: the list of root servers (from the stub), a root server
refers to the servers for org., one of those to the servers
for debian.org., and they deliver the address.
Save your progress? Sign up for free
Drill 5: Install dnsmasq, but do not let it run
# on netz
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y dnsmasq
dpkg -l dnsmasq | awk '/^ii/{print $2,$3}'
systemctl is-active dnsmasq
sudo journalctl -u dnsmasq --no-pager -n 5 | tail -4Expected (excerpt):
dnsmasq 2.91-0ubuntu0.24.04.2
failed
Nov 23 21:30:42 netz dnsmasq[8761]: failed to create listening socket for port 53: Address already in use
Nov 23 21:30:42 netz systemd[1]: dnsmasq.service: Failed with result 'exit-code'.
Nov 23 21:30:42 netz dnsmasq[8761]: FAILED to start up
Nov 23 21:30:42 netz systemd[1]: Failed to start dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server.
Tried it: The package starts its service right away, and the service wants to listen on port 53 on all addresses. But the systemd-resolved stub already sits there. The service fails, luckily: had it won, the host’s name resolution would have broken. We do not need the global service, dnsmasq will run in a namespace in a targeted way shortly.
# on netz
sudo systemctl disable --now dnsmasq
sudo systemctl reset-failed dnsmasq
systemctl is-enabled dnsmasq
dig +short example.org | head -1Expected:
Removed "/etc/systemd/system/multi-user.target.wants/dnsmasq.service".
disabled
104.20.26.136
Save your progress? Sign up for free
Drill 6: A network for dnsmasq
A gateway namespace gw with a bridge
br-lan, three clients without addresses attached to it,
plus an uplink to the host with NAT (Day 2) for forwarding to the
outside. c2 gets a fixed MAC, for which a fixed address
will be assigned shortly:
# on netz
sudo ip netns add gw
sudo ip -n gw link set lo up
sudo ip -n gw link add br-lan type bridge
sudo ip -n gw link set br-lan up
sudo ip -n gw addr add 10.80.0.1/24 dev br-lan
sudo ip netns add c1
sudo ip netns add c2
sudo ip netns add c3
sudo ip -n gw link add p-c1 type veth peer name eth0 netns c1
sudo ip -n gw link add p-c2 type veth peer name eth0 netns c2
sudo ip -n gw link add p-c3 type veth peer name eth0 netns c3
sudo ip -n gw link set p-c1 master br-lan
sudo ip -n gw link set p-c2 master br-lan
sudo ip -n gw link set p-c3 master br-lan
sudo ip -n gw link set p-c1 up
sudo ip -n gw link set p-c2 up
sudo ip -n gw link set p-c3 up
sudo ip -n c1 link set lo up
sudo ip -n c2 link set lo up
sudo ip -n c3 link set lo up
sudo ip -n c1 link set eth0 up
sudo ip -n c2 link set eth0 up
sudo ip -n c3 link set eth0 up
sudo ip -n c2 link set eth0 address 02:00:00:00:00:c2
sudo ip link add h-gw type veth peer name up0 netns gw
sudo ip addr add 10.80.255.1/30 dev h-gw
sudo ip link set h-gw up
sudo ip -n gw addr add 10.80.255.2/30 dev up0
sudo ip -n gw link set up0 up
sudo ip -n gw route add default via 10.80.255.1
sudo ip route add 10.80.0.0/24 via 10.80.255.2
sudo sysctl -qw net.ipv4.ip_forward=1
sudo ip netns exec gw sysctl -qw net.ipv4.ip_forward=1
sudo nft add table ip labor
sudo nft add chain ip labor post '{ type nat hook postrouting priority srcnat; }'
sudo nft add rule ip labor post ip saddr 10.80.0.0/16 oifname eth0 masquerade
sudo ip netns exec gw ping -c1 -W2 1.1.1.1 | grep -o '1 received'Expected: 1 received
The configuration goes into its own file, not into
/etc/dnsmasq.d: that is read by the global service, and we
do not want that one.
# on netz
sudo tee /etc/dnsmasq-labor.conf >/dev/null <<'EOF'
# dnsmasq nur für das Labor: läuft im Namespace gw, lauscht nur auf br-lan
interface=br-lan
bind-interfaces
except-interface=lo
no-resolv
server=1.1.1.1
domain=labor.lan
local=/labor.lan/
expand-hosts
dhcp-range=10.80.0.100,10.80.0.150,12h
dhcp-option=option:router,10.80.0.1
dhcp-host=02:00:00:00:00:c2,c2,10.80.0.20
host-record=drucker.labor.lan,10.80.0.30
address=/apps.labor.lan/10.80.0.50
log-queries
log-dhcp
log-facility=/var/log/dnsmasq-labor.log
dhcp-leasefile=/var/lib/misc/dnsmasq-labor.leases
pid-file=/run/dnsmasq-labor.pid
EOF
sudo dnsmasq --test --conf-file=/etc/dnsmasq-labor.conf
sudo ip netns exec gw dnsmasq --conf-file=/etc/dnsmasq-labor.conf
sleep 1
sudo ip netns exec gw ss -tulpn | grep dnsmasqExpected:
dnsmasq: syntax check OK.
udp UNCONN 0 0 10.80.0.1:53 0.0.0.0:* users:(("dnsmasq",pid=9256,fd=6))
udp UNCONN 0 0 0.0.0.0%br-lan:67 0.0.0.0:* users:(("dnsmasq",pid=9256,fd=4))
udp UNCONN 0 0 [fe80::b0a5:a3ff:fe3f:ab1e]%br-lan:53 [::]:* users:(("dnsmasq",pid=9256,fd=8))
tcp LISTEN 0 32 10.80.0.1:53 0.0.0.0:* users:(("dnsmasq",pid=9256,fd=7))
tcp LISTEN 0 32 [fe80::b0a5:a3ff:fe3f:ab1e]%br-lan:53 [::]:* users:(("dnsmasq",pid=9256,fd=9))
| Line | Effect |
|---|---|
interface, bind-interfaces,
except-interface=lo |
Listen only on br-lan, not on all addresses. This
prevents the port 53 conflict |
no-resolv, server=1.1.1.1 |
Ignore /etc/resolv.conf, pass everything foreign on to
1.1.1.1 |
domain, local=/labor.lan/ |
The local domain. dnsmasq itself answers unknown names in it with “does not exist” instead of sending them outside |
dhcp-range, dhcp-option=option:router |
Address range with a 12 h lease time, gateway for the clients |
dhcp-host |
Fixed address and fixed name for a MAC |
host-record, address=/…/ |
A custom entry, and a wildcard: apps.labor.lan
and everything below it |
DNS listens on 10.80.0.1:53, DHCP on port 67 on
br-lan. The log shows what was read at startup:
# on netz
sudo tail -5 /var/log/dnsmasq-labor.logExpected:
Nov 23 21:31:37 dnsmasq-dhcp[9256]: DHCP, IP range 10.80.0.100 -- 10.80.0.150, lease time 12h
Nov 23 21:31:37 dnsmasq-dhcp[9256]: DHCP, sockets bound exclusively to interface br-lan
Nov 23 21:31:37 dnsmasq[9256]: using nameserver 1.1.1.1#53
Nov 23 21:31:37 dnsmasq[9256]: using only locally-known addresses for labor.lan
Nov 23 21:31:37 dnsmasq[9256]: read /etc/hosts - 14 names
Remember the last line for Drill 8.
Save your progress? Sign up for free
Drill 7: Addresses via DHCP
The DHCP client is dhcpcd, which is already installed on
Ubuntu 24.04. Each client first gets its own resolv.conf
(Day 2), and dhcpcd must not touch the DNS
settings (-C resolv.conf):
Tried it: On the first attempt,
dhcpcdran without-C resolv.conf. The address arrived, but the DNS hook talked via D-Bus to the systemd-resolved of the host. The namespace does not isolate D-Bus, and becauseeth0in the client had the same index aseth0on the host,DNS Servers: 10.80.0.1andsearch labor.lansuddenly appeared on the real interface. Fixed withsudo resolvectl revert eth0andsudo networkctl reconfigure eth0. Namespaces separate the network, not every connection to the host’s services.
# on netz
sudo mkdir -p /etc/netns/c1 /etc/netns/c2 /etc/netns/c3
printf "nameserver 10.80.0.1\nsearch labor.lan\n" | sudo tee /etc/netns/c1/resolv.conf /etc/netns/c2/resolv.conf /etc/netns/c3/resolv.conf >/dev/null
sudo ip netns exec c1 dhcpcd -4 -1 -t 30 -C resolv.conf eth0 2>&1 | grep leased
sudo ip netns exec c2 dhcpcd -4 -1 -t 30 -C resolv.conf eth0
sudo ip netns exec c3 dhcpcd -4 -1 -t 30 -C resolv.conf eth0 2>&1 | grep leased
resolvectl status eth0 | grep -E "DNS Servers|DNS Domain"Expected:
eth0: leased 10.80.0.149 for 43200 seconds
eth0: probing address 10.80.0.20/24
eth0: leased 10.80.0.20 for 43200 seconds
eth0: adding route to 10.80.0.0/24
eth0: adding default route via 10.80.0.1
exiting due to oneshot
dhcpcd exited
eth0: leased 10.80.0.109 for 43200 seconds
DNS Servers: 172.16.0.15 8.8.8.8 1.1.1.1
DNS Domain: localdomain
c2 gets the fixed 10.80.0.20, the others an
address from the range. -1 (oneshot) fetches a lease and
then exits dhcpcd, otherwise one daemon per namespace would
be left behind. The last two lines are the cross-check: the host is
untouched.
Tried it: With
-t 10,c2once got only an address from169.254.0.0/16, although dnsmasq had confirmed the lease.dhcpcdfirst checks an offered address via ARP for a few seconds (probing), and the timeout ran out in the meantime. Hence-t 30.
What the server handed out is in the lease file, with expiry time (seconds since 1970), MAC, address, name and client ID:
# on netz
sudo cat /var/lib/misc/dnsmasq-labor.leases
sudo grep "02:00:00:00:00:c2" /var/log/dnsmasq-labor.log | tail -4Expected:
1795512762 d2:34:38:4f:a5:1b 10.80.0.109 * ff:38:4f:a5:1b:00:01:00:01:32:97:6d:42:26:90:a6:e1:73:dc
1795512753 02:00:00:00:00:c2 10.80.0.20 c2 ff:00:00:00:c2:00:01:00:01:32:97:6d:42:26:90:a6:e1:73:dc
1795512710 26:90:a6:e1:73:dc 10.80.0.149 * ff:a6:e1:73:dc:00:01:00:01:32:97:6d:42:26:90:a6:e1:73:dc
Nov 23 21:32:33 dnsmasq-dhcp[9256]: 3030812148 DHCPDISCOVER(br-lan) 02:00:00:00:00:c2
Nov 23 21:32:33 dnsmasq-dhcp[9256]: 3030812148 DHCPOFFER(br-lan) 10.80.0.20 02:00:00:00:00:c2
Nov 23 21:32:33 dnsmasq-dhcp[9256]: 3030812148 DHCPREQUEST(br-lan) 10.80.0.20 02:00:00:00:00:c2
Nov 23 21:32:33 dnsmasq-dhcp[9256]: 3030812148 DHCPACK(br-lan) 10.80.0.20 02:00:00:00:00:c2 c2
The four DHCP steps, “DORA”: Discover, Offer, Request, Ack. Where the
log gets stuck is where the fault lies: if no DISCOVER
arrives, the client does not reach the server. If the
REQUEST is missing, the client did not receive the offer or
rejected it.
Save your progress? Sign up for free
Drill 8: Names in your own network
# on netz
sudo ip netns exec c1 dig +short drucker.labor.lan
sudo ip netns exec c1 dig +short c2.labor.lan
sudo ip netns exec c1 dig +short c2
sudo ip netns exec c1 dig +short irgendwas.apps.labor.lan
sudo ip netns exec c1 dig +short -x 10.80.0.20
sudo ip netns exec c1 dig +short example.org
sudo ip netns exec c1 dig gibtsnicht.labor.lan | grep -E "status|SERVER"Expected:
10.80.0.30
10.80.0.20
10.80.0.20
10.80.0.50
c2.labor.lan.
172.66.157.237
104.20.26.136
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 62456
;; SERVER: 10.80.0.1#53(10.80.0.1) (UDP)
The fixed entry, the DHCP name c2 (even without the
domain, thanks to search labor.lan), the wildcard, the
reverse lookup, a name from outside via 1.1.1.1, and an
unknown local name with NXDOMAIN. What dnsmasq did is in
the log, thanks to log-queries:
# on netz
sudo grep -E "apps|example.org|gibtsnicht" /var/log/dnsmasq-labor.log | tail -7Expected:
Nov 23 21:33:26 dnsmasq[9256]: query[A] irgendwas.apps.labor.lan from 10.80.0.149
Nov 23 21:33:26 dnsmasq[9256]: config irgendwas.apps.labor.lan is 10.80.0.50
Nov 23 21:33:26 dnsmasq[9256]: query[A] example.org from 10.80.0.149
Nov 23 21:33:26 dnsmasq[9256]: forwarded example.org to 1.1.1.1
Nov 23 21:33:26 dnsmasq[9256]: reply example.org is 172.66.157.237
Nov 23 21:33:26 dnsmasq[9256]: reply example.org is 104.20.26.136
Nov 23 21:33:26 dnsmasq[9256]: query[A] gibtsnicht.labor.lan from 10.80.0.149
config means: answered from its own configuration,
forwarded/reply: passed on. And now the line
from startup, read /etc/hosts - 14 names:
# on netz
sudo ip netns exec c1 dig +short netz.labor.lanExpected: 127.0.1.1
Tried it: By default, dnsmasq reads the host’s
/etc/hostsand serves it, withexpand-hostseven under the local domain. For the clients,netz.labor.lanis now127.0.1.1, i.e., themselves. On a router that has other jobs too,no-hoststherefore belongs in the configuration.
# on netz
echo "no-hosts" | sudo tee -a /etc/dnsmasq-labor.conf >/dev/null
sudo kill $(cat /run/dnsmasq-labor.pid)
sleep 1
sudo ip netns exec gw dnsmasq --conf-file=/etc/dnsmasq-labor.conf
sleep 1
sudo ip netns exec c1 dig netz.labor.lan | grep statusExpected:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 26037
Changes to the configuration file need a restart of dnsmasq. The leases stay, they are in the file.
Save your progress? Sign up for free
Drill 9: When the upstream is missing
What happens if the server to the outside does not answer? Test with
192.0.2.53, an address from a documentation network that
does not exist:
# on netz
sudo sed -i 's/^server=1.1.1.1/server=192.0.2.53/' /etc/dnsmasq-labor.conf
sudo kill $(cat /run/dnsmasq-labor.pid)
sleep 1
sudo ip netns exec gw dnsmasq --conf-file=/etc/dnsmasq-labor.conf
sleep 1
sudo ip netns exec c1 dig +tries=1 +time=5 example.net | grep -vE "^$|^; <<>>|global options"
sudo ip netns exec c1 dig +short drucker.labor.lanExpected:
;; communications error to 10.80.0.1#53: timed out
;; no servers could be reached
10.80.0.30
Local names keep working, foreign ones go nowhere. To the client,
this looks as if the DNS server itself were dead, although dnsmasq is
running. The log tells the truth:
forwarded example.net to 192.0.2.53 without a
reply. Back to the real upstream:
# on netz
sudo sed -i 's/^server=192.0.2.53/server=1.1.1.1/' /etc/dnsmasq-labor.conf
sudo kill $(cat /run/dnsmasq-labor.pid)
sleep 1
sudo ip netns exec gw dnsmasq --conf-file=/etc/dnsmasq-labor.conf
sleep 1
sudo ip netns exec c1 dig +short example.net | head -1Expected: 104.20.21.8
Save your progress? Sign up for free
Exercise: Speed drills
- Which MAC does the client with
10.80.0.109have? From the lease file only. Target time: 30 s - Ask dnsmasq directly from the host for
drucker.labor.lan. Target time: 15 s - How many DNS queries has dnsmasq logged so far? Target time: 15 s
Solution
# on netz
sudo awk '$3=="10.80.0.109"{print $2}' /var/lib/misc/dnsmasq-labor.leases
dig +short @10.80.0.1 drucker.labor.lan
sudo grep -c 'query\[' /var/log/dnsmasq-labor.logExpected (the number depends on how often you asked):
d2:34:38:4f:a5:1b
10.80.0.30
18
Number 2 works because the host has a route into
10.80.0.0/24 via gw, and dnsmasq on
br-lan answers anyone who arrives there.
Save your progress? Sign up for free
Challenge
A colleague “tidies up the configuration” and comments out the upstream line:
# on netz
sudo sed -i 's/^server=1.1.1.1/#server=1.1.1.1/' /etc/dnsmasq-labor.conf
sudo kill $(cat /run/dnsmasq-labor.pid)
sleep 1
sudo ip netns exec gw dnsmasq --conf-file=/etc/dnsmasq-labor.conf
sleep 1
sudo ip netns exec c1 dig +short drucker.labor.lan
sudo ip netns exec c1 dig example.org | grep statusExpected:
10.80.0.30
;; ->>HEADER<<- opcode: QUERY, status: REFUSED, id: 43874
Internal names work, everything else is rejected with
REFUSED. Find out why in the log, and fix it.
Hint: Where does dnsmasq get its upstream servers
from when server= is missing? And what does
no-resolv have to say about that?
Solution
# on netz
sudo grep -E "example.org|REFUSED" /var/log/dnsmasq-labor.log | tail -2Expected:
Nov 23 21:33:54 dnsmasq[9857]: query[A] example.org from 10.80.0.149
Nov 23 21:33:54 dnsmasq[9857]: config error is REFUSED (EDE: not ready)
Without server=, dnsmasq would have taken the servers
from /etc/resolv.conf, but no-resolv forbids
exactly that. So it has no upstream at all and rejects everything it
does not know itself. REFUSED always means “I will not or
may not answer this”, not “does not exist” (NXDOMAIN) and
not “I tried and failed” (SERVFAIL). Re-enable the
line:
# on netz
sudo sed -i 's/^#server=1.1.1.1/server=1.1.1.1/' /etc/dnsmasq-labor.conf
sudo kill $(cat /run/dnsmasq-labor.pid)
sleep 1
sudo ip netns exec gw dnsmasq --conf-file=/etc/dnsmasq-labor.conf
sleep 1
sudo ip netns exec c1 dig +short example.org | head -1Expected: 172.66.157.237
Save your progress? Sign up for free
Checkpoint
# on netz
sudo ip netns exec gw ss -Hulpn 'sport = :67' | wc -lExpected: 1
# on netz
sudo ip -n c2 -br -4 a show eth0 | awk '{print $3}'Expected: 10.80.0.20/24
# on netz
sudo ip netns exec c3 dig +short c2.labor.lanExpected: 10.80.0.20
# on netz
sudo ip netns exec c1 dig +short foo.apps.labor.lanExpected: 10.80.0.50
# on netz
systemctl is-enabled dnsmasqExpected: disabled
# on netz
resolvectl status eth0 | grep "DNS Domain"Expected: DNS Domain: localdomain
Clean-up
The dnsmasq package stays installed (Day 15 needs it again), the service stays off:
# on netz
sudo kill $(cat /run/dnsmasq-labor.pid)
sudo ip netns del c1
sudo ip netns del c2
sudo ip netns del c3
sudo ip netns del gw
sudo rm -r /etc/netns
sudo ip route del 10.80.0.0/24
sudo nft delete table ip labor
sudo sysctl -qw net.ipv4.ip_forward=0
sudo rm -f /etc/dnsmasq-labor.conf /var/log/dnsmasq-labor.log /var/lib/misc/dnsmasq-labor.leases /run/dnsmasq-labor.pid
ip netns list | wc -l
pgrep -a dnsmasq || echo "kein dnsmasq"
systemctl is-active dnsmasq
resolvectl status eth0 | grep -E "DNS Servers|DNS Domain"Expected:
0
kein dnsmasq
inactive
DNS Servers: 172.16.0.15 8.8.8.8 1.1.1.1
DNS Domain: localdomain
h-gw disappears together with the namespace
gw, and you deleted the route through it beforehand.
Practice note
- Slow first query? Check the first DNS server.
dig @<server>for every entry fromresolvectl statusshows within seconds who is not answering. A dead first server costs time after every restart until the resolver falls back. - Read the status codes.
NXDOMAIN: the name does not exist.SERVFAIL: resolution failed, often DNSSEC or a broken upstream.REFUSED: the server will not, usually because it lacks an upstream or you are not allowed to ask it. And a timeout means: no answer, from anyone. - Bind dnsmasq explicitly. On Ubuntu always with
interface=andbind-interfaces, otherwise it collides with the systemd-resolved stub. Addno-hostsso the router’s/etc/hostsdoes not go out to the clients. - Namespaces are not a sandbox for everything.
Whatever talks to the host via D-Bus or files, here the DNS hook of
dhcpcd, affects the host. Disable such hooks in test networks.