SkillPilotAI

dnsmasq (1/2): the DNS side

dnsmasq is a small DNS cache and forwarder (plus DHCP, TFTP and IPv6 RA) in one process, used by home routers, NetworkManager and libvirt. This part covers its DNS side: startup, logging, hosts files, listening, upstream routing, filtering, local records, cache and EDNS.

92.1 What it is and where it runs

PlatformHow you get it
Linuxdnsmasq package (dnsmasq-base is used inside NetworkManager, libvirt)
macOSbrew install dnsmasq
WindowsNot native: WSL or a Linux VM
Config/etc/dnsmasq.conf: every line is a long option without the leading -- (e.g. domain-needed)

92.2 How it answers a query

  • First its own local data (/etc/hosts, addn-hosts, host-record...): answered immediately.
  • Then the cache: a repeated question is answered without any upstream packet.
  • Otherwise it forwards to upstream servers (8.8.8.8, 1.1.1.1), caches the reply and answers the client.
  • It is a forwarder, not a recursive resolver: it never walks from the root servers itself.
Local data, then cache, then upstream
  1. 1.Laptop 192.168.112.20 asks A? printer -> dnsmasq 192.168.112.10

  2. 2.printer is in the hosts file: answer 192.168.112.30 at once

  3. 3.A? example.com is not local: forwarded to upstream 8.8.8.8

  4. 4.Reply comes back, dnsmasq saves it in the cache and answers

  5. 5.Same question again: cache hit, no upstream query

92.3 Startup and process control

Terminal
cat /etc/n092/lan.confdnsmasq --test -C /etc/n092/lan.confdnsmasq --test -C /etc/n092/bad.confls /etc/n092/conf.ddnsmasq -d -C /etc/n092/lan.conf \  --conf-dir=/etc/n092/conf.d,*.conf \  --pid-file=/run/n092.pidcat /run/n092.pid; ps -o pid,user,args \  | grep [d]nsmasq
Output
$ cat /etc/n092/lan.conf# n092 base config: never read resolv.confno-resolvserver=8.8.8.8server=1.1.1.1$ dnsmasq --test -C /etc/n092/lan.confdnsmasq: syntax check OK.$ dnsmasq --test -C /etc/n092/bad.conf dnsmasq: bad option at line 3 of /etc/n092/bad.conf$ ls /etc/n092/conf.dcache.confold.bak$ dnsmasq -d -C /etc/n092/lan.conf --conf-dir=/etc/n092/conf.d,*.conf --pid-file=/run/n092.piddnsmasq: started, version 2.90 cachesize 500dnsmasq: using nameserver 8.8.8.8#53dnsmasq: using nameserver 1.1.1.1#53dnsmasq: read /etc/hosts - 9 names$ cat /run/n092.pid; ps -o pid,user,args | grep [d]nsmasqcat: can't open '/run/n092.pid': No such file or directory  109 root     dnsmasq -d -C /etc/n092/lan.conf --conf-dir=/etc/n092/conf.d,*.conf --pid-file=/run/n092.pid
  • --test checks syntax without starting anything; bad.conf had a misspelled cache-size option on line 3.
  • --conf-dir=DIR,*.conf read only .conf files (old.bak skipped), so cachesize became 500.
  • -d is debug mode: foreground, logs to stderr, stays root and writes no pid file.
  • -k also stays in the foreground but otherwise runs normally: the right choice under systemd.
  • Other general options: -C config file, -u/-g user/group, --servers-file, --conf-script, --no-ident, --max-tcp-connections (20).

92.4 Logging and debugging

Terminal
dnsmasq -d -C /etc/n092/lan.conf \  --log-queries=extradig +short example.comdig +short example.comdnsmasq -d -C /etc/n092/lan.conf \  --dumpfile=/dump/n092.pcap \  --dumpmask=0x000fdig +short example.comtshark -r /dump/n092.pcap 2>/dev/null
Output
dnsmasq: 1 192.168.112.20/50035 query[A] example.com from 192.168.112.20dnsmasq: 1 192.168.112.20/50035 forwarded example.com to 8.8.8.8dnsmasq: 1 192.168.112.20/50035 forwarded example.com to 1.1.1.1dnsmasq: 1 192.168.112.20/50035 reply example.com is 104.20.23.154dnsmasq: 1 192.168.112.20/50035 reply example.com is 172.66.147.243dnsmasq: 2 192.168.112.20/40546 query[A] example.com from 192.168.112.20dnsmasq: 2 192.168.112.20/40546 cached example.com is 104.20.23.154dnsmasq: 2 192.168.112.20/40546 cached example.com is 172.66.147.243$ tshark -r /dump/n092.pcap 2>/dev/null    1   0.000000 192.168.112.20 → 0.0.0.0      DNS 80 Standard query 0x9afe A example.com OPT    2   0.006615      0.0.0.0 → 8.8.8.8      DNS 80 Standard query 0xdb3e A example.com OPT    3   0.006735      0.0.0.0 → 1.1.1.1      DNS 80 Standard query 0xdb3e A example.com OPT    4   0.041998      8.8.8.8 → 0.0.0.0      DNS 100 Standard query response 0xdb3e A example.com A 104.20.23.154 A 172.66.147.243 OPT    5   0.042162      0.0.0.0 → 192.168.112.20 DNS 100 Standard query response 0x9afe A example.com A 104.20.23.154 A 172.66.147.243 OPT
OptionEffect
-q / --log-queriesLog the result of every query
--log-queries=extra+ query serial number and client IP/port (follow one query end to end)
-8 / --log-facility=LOCAL5, a file, or - (stderr) instead of syslog
--log-dhcp / --quiet-dhcp, --quiet-ra, --quiet-tftpMore DHCP detail / silence routine logs
--dumpfile=PATHSave packets to a pcap (dnsmasq itself shows as 0.0.0.0)
--dumpmask0x1/0x2 client query/reply, 0x4/0x8 upstream query/reply, 0xf0 DNSSEC, 0xf000 DHCP/RA/TFTP

92.5 Hosts files and local TTLs

Terminal
cat /etc/n092/hosts.landnsmasq -d -C /etc/n092/lan.conf -q \  --addn-hosts=/etc/n092/hosts.lan \  --expand-hosts --domain=shop.lab \  --local-ttl=60 --max-ttl=30dig +noall +answer printerdig +noall +answer printer.shop.labdig +noall +answer -x 192.168.112.31dig +noall +answer example.com
Output
$ cat /etc/n092/hosts.lan192.168.112.30 printer192.168.112.31 nas$ dig +noall +answer printerprinter.		60	IN	A	192.168.112.30$ dig +noall +answer printer.shop.labprinter.shop.lab.	60	IN	A	192.168.112.30$ dig +noall +answer -x 192.168.112.3131.112.168.192.in-addr.arpa. 60	IN	PTR	nas.shop.lab.$ dig +noall +answer example.comexample.com.		30	IN	A	104.20.23.154example.com.		30	IN	A	172.66.147.243
  • --addn-hosts adds an extra hosts file; --local-ttl=60 gives local answers TTL 60 (default 0).
  • --expand-hosts + --domain adds shop.lab to plain names, and the reverse lookup returns the full name.
  • --max-ttl=30 caps the TTL sent to clients, so example.com showed 30.
  • --no-hosts ignores /etc/hosts: the name dns then became NXDOMAIN.
  • --hostsdir picks up new files in a directory automatically; also --neg-ttl, --min/max-cache-ttl.

92.6 Ports, interfaces and listening

Terminal
dnsmasq -d -C /etc/n092/lan.confss -lnu 'sport = :53'dnsmasq -d -C /etc/n092/lan.conf \  --interface=eth0 --bind-interfacesss -lnu 'sport = :53'dnsmasq -d -C /etc/n092/lan.conf \  --listen-address=127.0.0.1 \  --bind-interfacesdig +tries=1 +timeout=2 example.com | tail -3dnsmasq -d -C /etc/n092/lan.conf \  --port=5353dig -p 5353 +short example.com
Output
$ ss -lnu 'sport = :53'State  Recv-Q Send-Q Local Address:Port Peer Address:PortUNCONN 0      0            0.0.0.0:53        0.0.0.0:*UNCONN 0      0               [::]:53           [::]:*$ ss -lnu 'sport = :53'State  Recv-Q Send-Q  Local Address:Port Peer Address:PortUNCONN 0      0           127.0.0.1:53        0.0.0.0:*UNCONN 0      0      192.168.112.10:53        0.0.0.0:*UNCONN 0      0               [::1]:53           [::]:*$ ss -lnu 'sport = :53'State  Recv-Q Send-Q Local Address:Port Peer Address:PortUNCONN 0      0          127.0.0.1:53        0.0.0.0:*$ dig +tries=1 +timeout=2 example.com | tail -3;; global options: +cmd;; no servers could be reached$ ss -lnu 'sport = :5353'UNCONN 0      0            0.0.0.0:5353      0.0.0.0:*$ dig -p 5353 +short example.com172.66.147.243104.20.23.154
  • Default: wildcard 0.0.0.0:53 (all addresses).
  • --interface=eth0 --bind-interfaces: binds only the eth0 IP plus loopback.
  • --listen-address=127.0.0.1: LAN clients get 'no servers could be reached'.
  • --port=0 turns DNS off completely (only DHCP/TFTP); --query-port fixes the upstream source port, but random ports are safer against spoofing.

92.7 Upstream servers and forwarding (split DNS)

Upstream routing: who goes where
  1. 1.A? app.branch.lab -> server=/branch.lab/ -> Branch DNS 192.168.112.53

  2. 2.A? example.com -> normal upstreams 8.8.8.8 and 1.1.1.1

  3. 3.A? ghost.shop.lab -> local=/shop.lab/ -> NXDOMAIN from config

  4. 4.A? nas (no dot) -> domain-needed -> NXDOMAIN, never leaves

One dnsmasq, different upstream per domain

Terminal
dnsmasq -d -C /etc/n092/lan.conf -q \  --server=/branch.lab/192.168.112.53 \  --local=/shop.lab/ --domain-neededdig +short app.branch.labdig +short example.comdig +noall +comments ghost.shop.lab \  | grep statusdig +noall +comments nas | grep statusdnsmasq -d -C /etc/n092/lan.conf -q \  --strict-orderdig +short example.net
Output
dnsmasq: using nameserver 192.168.112.53#53 for domain branch.lab dnsmasq: using only locally-known addresses for shop.labdnsmasq: forwarded app.branch.lab to 192.168.112.53dnsmasq: reply app.branch.lab is 192.168.112.61dnsmasq: forwarded example.com to 8.8.8.8dnsmasq: forwarded example.com to 1.1.1.1dnsmasq: config ghost.shop.lab is NXDOMAINdnsmasq: config nas is NXDOMAIN(restart with --strict-order)dnsmasq: forwarded example.net to 8.8.8.8dnsmasq: reply example.net is 104.20.21.8
  • By default dnsmasq tries both servers to find the fastest; --strict-order asks them in order.
  • --all-servers sends every query to all and uses the first answer.
  • --rev-server=net,IP forwards reverse lookups of a subnet; --dns-loop-detect disables a server that loops.

92.8 Filtering, rebinding protection and answer rewriting

Terminal
dnsmasq -d -C /etc/n092/lan.conf -q \  --server=/branch.lab/192.168.112.53 \  --stop-dns-rebind --bogus-priv \  --address=/ads.example.com/ \  --address=/track.example.com/# \  --filter-AAAAdig +short track.example.com
Output
dnsmasq: possible DNS-rebind attack detected: app.branch.labdnsmasq: config 192.168.50.7 is NXDOMAIN$ dig +noall +comments ads.example.com | grep status;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 14995$ dig +short track.example.com0.0.0.0dnsmasq: config example.com is NODATA-IPv6
OptionResult in the lab
--stop-dns-rebindPrivate IP in an upstream answer rejected (rebind attack)
--rebind-domain-ok=/branch.lab/Trusted domain exempted: 192.168.112.61 returned
--bogus-privReverse lookup of a private range answered NXDOMAIN locally
--address=/ads.example.com/NXDOMAIN (no IP given)
--address=/track.example.com/#0.0.0.0 (ad blocking)
--filter-AAAAIPv6 answers removed (NODATA-IPv6)
--alias=192.168.112.61,192.168.112.200IP in the upstream answer rewritten to .200 (DNS doctoring)
--ipset / --nftsetResolved IPs added to firewall sets (VPN routing)

92.9 Local DNS records (no zone file)

Terminal
# /etc/n092/records.confhost-record=nas.shop.lab,nas,192.168.112.40cname=www.shop.lab,nas.shop.labmx-host=shop.lab,mail.shop.lab,10txt-record=shop.lab,"v=spf1 mx -all"srv-host=_ldap._tcp.shop.lab,nas.shop.lab,389synth-domain=shop.lab,192.168.112.96/28,pc-# dnsmasq -d -C /etc/n092/lan.conf \#   -C /etc/n092/records.conf
Output
$ dig +noall +answer www.shop.labwww.shop.lab.		0	IN	CNAME	nas.shop.lab.nas.shop.lab.		0	IN	A	192.168.112.40$ dig +short MX shop.lab10 mail.shop.lab.$ dig +short TXT shop.lab"v=spf1 mx -all"$ dig +short SRV _ldap._tcp.shop.lab0 0 389 nas.shop.lab.$ dig +short pc-192-168-112-100.shop.lab192.168.112.100$ dig +short -x 192.168.112.105pc-192-168-112-105.shop.lab.
  • host-record creates A and PTR in one line; cname makes a local alias.
  • synth-domain auto-creates names for a whole range: handy for DHCP clients without a name.
  • Also: ptr-record, naptr-record, caa-record, dns-rr (any type), interface-name (name from the router's current interface IP).

92.10 Cache control and EDNS client information

Output
$ dig example.com | grep -E 'IN.A|Query time'example.com.		76	IN	A	104.20.23.154;; Query time: 48 msec$ sleep 3; dig example.com | grep -E 'IN.A|Query time'example.com.		71	IN	A	104.20.23.154;; Query time: 12 msecdnsmasq: started, version 2.90 cache disabled$ dnsmasq -d --no-resolv --do-0x20-encode --test 2>&1 dnsmasq: bad command line options: try --help
  • --cache-size=1000: first query 48 ms (upstream), second 12 ms from cache, TTL counting down 76 -> 71.
  • --cache-size=0 disables the cache: every query goes upstream.
  • --do-0x20-encode exists only from 2.91, so 2.90 rejects it; --no-negcache, --use-stale-cache, --cache-rr are other options.
  • --add-subnet=24,96 adds ECS (Client Subnet 192.168.112.0/24); --add-mac=text and --add-cpe-id=lab42 add EDNS options 65073 / 65074.
  • Privacy: send MAC/subnet only to your own trusted server, never to a public resolver; --strip-mac / --strip-subnet remove incoming info.

92.11 Comparison: dnsmasq vs Unbound vs BIND

ServerWhat it isBest for
dnsmasqForwarder + cache + DHCP in one small processHome routers, small LANs, labs
UnboundFull recursive, validating resolverResolving from the root, DNSSEC validation at scale
BINDAuthoritative (and recursive) serverLarge authoritative zones, enterprise DNS

92.12 Common mistakes

  • Treating dnsmasq as a recursive resolver: with no upstreams it logs 'warning: no upstream servers configured' and clients get REFUSED (EDE 14 Not Ready).
  • Editing the config without --test first: one bad line stops the service.
  • Using --listen-address=127.0.0.1 and wondering why LAN clients cannot reach it.
  • Turning on stop-dns-rebind with an internal domain that returns private IPs, without rebind-domain-ok.
Output
dnsmasq: warning: no upstream servers configured$ dig +noall +comments example.com | grep -E 'status|EDE';; ->>HEADER<<- opcode: QUERY, status: REFUSED, id: 59545; EDE: 14 (Not Ready)

92.13 Interview tips

  • Order of answers: local data -> cache -> upstream; dnsmasq forwards, it does not recurse.
  • Split DNS for a VPN domain: server=/corp.lab/192.168.112.53 (+ rebind-domain-ok=/corp.lab/ if it returns private IPs).
  • domain-needed keeps plain names local; bogus-priv keeps private reverse lookups local.
  • Debug with --test and --log-queries=extra; capture with --dumpfile.
  • Use dnsmasq for LAN/router/lab, Unbound for recursion, BIND for big authoritative zones.