nsupdate, mdig, kdig, drill (1/2)
nsupdate changes DNS records live by sending an RFC 2136 dynamic UPDATE to the primary server, so nobody edits the zone file by hand. mdig is BIND's pipelined dig: it fires many queries back to back and prints each answer as it arrives. (kdig and drill are in part 2.)
87.1 Why is it used?
- Dynamic DNS: DHCP servers, scripts and automation add or delete records without a zone file edit or reload.
- The server bumps the serial and NOTIFYs secondaries by itself, so changes spread in seconds.
- TSIG keys decide who may write into the zone.
- mdig runs bulk queries fast because it does not wait for each answer before sending the next.
- Linux: bind9-dnsutils / bind-utils. macOS: nsupdate built in (older BIND). Windows: WSL (or BIND/Knot Windows builds).
87.2 How a dynamic update works
- Step 1
1.Admin PC 192.168.107.20: nsupdate builds an UPDATE (app A 192.168.107.30) signed with TSIG
- Step 2
2.UPDATE travels to ns1 (primary) 192.168.107.10
- Step 3
3.ns1 checks the key: no key -> REFUSED, wrong secret -> NOTAUTH(BADSIG)
- Step 4
4.Key OK: ns1 adds the record, bumps the serial, replies NOERROR
- Step 5
5.ns1 sends NOTIFY to ns2 (secondary)
- Step 6
6.ns2 pulls the change with IXFR
87.3 Lab setup: TSIG key and allow-update
ddns-confgen -k ddns-key -z example.comddns-confgen -q -k ddns-key \ > /etc/bind/ddns.keycat /etc/bind/ddns.keynamed-checkconf /etc/bind/n087.conf \ && echo conf okgrep -A3 'zone "example.com"' \ /etc/bind/n087.conf$ ddns-confgen -k ddns-key -z example.comkey "ddns-key" { algorithm hmac-sha256; secret "UXU7QIJr4usk2KXXHGeGXbUSVYIGPKq79MAxhMDDGvs=";};update-policy { grant ddns-key zonesub ANY;};$ cat /etc/bind/ddns.keykey "ddns-key" { algorithm hmac-sha256; secret "2dQrJ3eO3T7zGSwcsDm57/2mv1ZNoenWmDe/avhMEBQ=";};conf okzone "example.com" { type primary; file "example.com.zone"; allow-update { key ddns-key; }; };- ddns-confgen creates an hmac-sha256 key and suggests an update-policy; -q writes only the key block.
- allow-update { key ddns-key; } lets only this key write into example.com.
87.4 First session: server, zone, ttl, add, send
cat add.txtnsupdate -k ddns.key add.txt; echo exit=$?dig @192.168.107.10 app.example.com +shortdig @192.168.107.11 app.example.com +shortdig @192.168.107.11 example.com SOA +short# ns1: named loggrep -E 'approved|updating|notifies|IXFR' \ /tmp/named.log$ cat add.txtserver 192.168.107.10zone example.comttl 3600update add app.example.com A 192.168.107.30showsendanswer$ nsupdate -k ddns.key add.txt; echo exit=$?Outgoing update query:;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 0;; flags:; ZONE: 0, PREREQ: 0, UPDATE: 0, ADDITIONAL: 0;; ZONE SECTION:;example.com. IN SOA;; UPDATE SECTION:app.example.com. 3600 IN A 192.168.107.30Answer:;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 35326exit=0$ dig @192.168.107.10 app.example.com +short192.168.107.30$ dig @192.168.107.11 app.example.com +short192.168.107.30$ dig @192.168.107.11 example.com SOA +shortns1.example.com. admin.example.com. 2026110902 3600 600 86400 30008-Nov-2026 22:49:21.803 client @0x779467563360 192.168.107.20#36437/key ddns-key: signer "ddns-key" approved08-Nov-2026 22:49:21.803 client @0x779467563360 192.168.107.20#36437/key ddns-key: updating zone 'example.com/IN': adding an RR at 'app.example.com' A 192.168.107.3008-Nov-2026 22:49:21.819 zone example.com/IN: sending notifies (serial 2026110902)08-Nov-2026 22:49:21.823 client @0x7794675570e0 192.168.107.11#40461 (example.com): transfer of 'example.com/IN': IXFR started (serial 2026110901 -> 2026110902)08-Nov-2026 22:49:21.823 client @0x7794675570e0 192.168.107.11#40461 (example.com): transfer of 'example.com/IN': IXFR ended: 1 messages, 5 records, 203 bytes, 0.001 secs (203000 bytes/sec) (serial 2026110902)- show prints the message being built; nothing goes on the network until send (a blank line also sends).
- answer prints the server's reply. Both ns1 and ns2 have the new IP; the log shows approved -> updating -> notifies -> IXFR.
87.5 TSIG options: -k and -y
nsupdate x.txt; echo exit=$?BAD=c2VjcmV0S2V5QmFzZTY0PQ==nsupdate -y hmac-sha256:ddns-key:$BAD x.txtS=$(awk -F'"' '/secret/{print $2}' ddns.key)nsupdate -y hmac-md5:ddns-key:$S x.txtnsupdate -y hmac-sha256:ddns-key:$S x.txt$ nsupdate x.txt; echo exit=$?update failed: REFUSEDexit=2$ nsupdate -y hmac-sha256:ddns-key:$BAD x.txt; TSIG error with server: tsig indicates errorupdate failed: NOTAUTH(BADSIG)exit=2$ nsupdate -y hmac-md5:ddns-key:$S x.txt; TSIG error with server: tsig indicates errorupdate failed: NOTAUTH(BADKEY)exit=2$ nsupdate -y hmac-sha256:ddns-key:$S x.txtexit=0| Result | Cause |
|---|---|
| REFUSED | No key, but allow-update only permits ddns-key |
| NOTAUTH(BADSIG) | Right key name and algorithm, wrong secret |
| NOTAUTH(BADKEY) | Server has no key with that name + algorithm (hmac-md5 vs hmac-sha256) |
| NOERROR, exit 0 | Correct algorithm, name and secret |
87.6 Other common options
nsupdate -d -k ddns.key d.txt; echo exit=$?nsupdate -p 5353 -d -k ddns.key p.txt 2>&1 \ | grep -E 'Sending|status'time nsupdate -k ddns.key -u 1 -r 1 dead.txttime nsupdate -v -t 3 -k ddns.key dead.txt$ nsupdate -d -k ddns.key d.txt; echo exit=$?Sending update to 192.168.107.10#53Outgoing update query:;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 43858;; flags:; ZONE: 1, PREREQ: 0, UPDATE: 1, ADDITIONAL: 1;; UPDATE SECTION:d.example.com. 300 IN A 192.168.107.41Reply from update query:;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 43858exit=0$ nsupdate -p 5353 -d -k ddns.key p.txt 2>&1 \ | grep -E 'Sending|status'Sending update to 192.168.107.10#5353;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 14437;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id: 14437$ time nsupdate -k ddns.key -u 1 -r 1 dead.txt; Communication with 192.168.107.99#53 failed: timed outreal 0m 2.08s$ time nsupdate -v -t 3 -k ddns.key dead.txt; Communication with 192.168.107.99#53 failed: timed outreal 0m 3.08s| Option | Meaning |
|---|---|
| file | Read commands from a file (or stdin) |
| -k FILE / -y alg:name:secret | TSIG key file (or SIG(0) .private) / key on the command line (avoid: shell history). Not together. |
| -d / -D / -L N | Debug / extra debug (57 lines in the lab) / library logging level |
| -p N | Server port (default 53) |
| -v | TCP even for small updates (tcpdump shows the SYN handshake first); default is UDP |
| -r N / -u N | UDP retries (default 3) / retry interval (default 3) |
| -t N (Linux) | Overall timeout for the update |
| -g / -o | GSS-TSIG / Windows 2000 variant (Kerberos; lab gave a GSSAPI error) |
| -T / -P / -V | IANA RR types (86) / private BIND types (empty here) / version |
87.7 Linux-only and macOS-only behaviour
nsupdate -6 -k ddns.key six.txt; echo exit=$?nsupdate -4 -k ddns.key six.txt; echo exit=$?cat c.txtnsupdate -d -C lab-resolv.conf -k ddns.key \ c.txt 2>&1 | grep -E 'Found|primary|Sending'# ns1: local mode (update-policy local)ls /run/named/; cat l.txtnsupdate -l l.txt; echo exit=$?dig @127.0.0.1 app.local.lab +short$ nsupdate -6 -k ddns.key six.txt; echo exit=$?; Communication with ::ffff:192.168.107.10#53 failed: operation canceledexit=2$ nsupdate -4 -k ddns.key six.txt; echo exit=$?exit=0$ cat c.txtzone example.comupdate add c.example.com 300 A 192.168.107.52send$ nsupdate -d -C lab-resolv.conf -k ddns.key \ c.txt 2>&1 | grep -E 'Found|primary|Sending'Found zone name: example.comThe primary is: ns1.example.comSending update to 192.168.107.10#53$ ls /run/named/; cat l.txtsession.keyupdate add app.local.lab 300 A 127.0.0.2send$ nsupdate -l l.txt; echo exit=$?; Communication with ::1#53 failed: connection refusedexit=0$ dig @127.0.0.1 app.local.lab +short127.0.0.2- Linux: -4 / -6 (IPv6 cancelled in the IPv4 lab), -C resolv.conf file, -i prompt even on a pipe, -l local mode with /run/named/session.key (tried ::1 first, then updated over 127.0.0.1).
- No server line: nsupdate finds the primary itself with an SOA query.
- macOS (docs, not run): -l reads /var/run/named/session.key, -t 60 overall timeout (default 300, -t 0 = none), -R randomness source; update/prereq keywords optional, del = delete.
87.8 Input commands: prerequisites
- A prerequisite is a condition; if it is false the whole UPDATE is rejected.
- nxdomain / yxdomain: name must not exist / must exist. nxrrset / yxrrset: RRset must not exist / must exist (optionally with exact data).
- yxrrset with the old IP + delete + add = safe compare-and-swap.
cat pre2.txtgrep prereq pre*.txtfor f in pre1 pre2 pre2 pre3 pre4 pre5; do nsupdate -k ddns.key $f.txt echo "$f exit=$?"done$ cat pre2.txtserver 192.168.107.10zone example.comprereq yxrrset www.example.com IN A 192.168.107.80update delete www.example.com Aupdate add www.example.com 300 A 192.168.107.81send$ grep prereq pre*.txtpre1.txt:prereq nxdomain www.example.compre2.txt:prereq yxrrset www.example.com IN A 192.168.107.80pre3.txt:prereq yxdomain nope.example.compre4.txt:prereq nxrrset www.example.com IN AAAApre5.txt:prereq yxrrset mail.example.com Aupdate failed: YXDOMAINpre1 exit=2pre2 exit=0update failed: NXRRSETpre2 exit=2update failed: NXDOMAINpre3 exit=2pre4 exit=0pre5 exit=0- pre2 passes once; the second run fails with NXRRSET because the IP is no longer .80.
87.9 Input commands: updates and control
; cleanup of old recordsserver 192.168.107.10local 192.168.107.20 5300key hmac-sha256:ddns-key SECRETzone example.comupdate delete old.example.comupdate delete mail.example.com TXTupdate delete www.example.com 300 IN AAAA 2001:db8::80showsend$ nsupdate del.txt; echo exit=$?;; UPDATE SECTION:old.example.com. 0 ANY ANYmail.example.com. 0 ANY TXTwww.example.com. 0 NONE AAAA 2001:db8::80exit=0$ grep '#5300' /tmp/named.log08-Nov-2026 22:50:57.605 client @0x779467566290 192.168.107.20#5300/key ddns-key: updating zone 'example.com/IN': delete all rrsets from name 'old.example.com'08-Nov-2026 22:50:57.605 client @0x779467566290 192.168.107.20#5300/key ddns-key: updating zone 'example.com/IN': deleting rrset at 'mail.example.com' TXT08-Nov-2026 22:50:57.605 client @0x779467566290 192.168.107.20#5300/key ddns-key: updating zone 'example.com/IN': deleting an RR at www.example.com AAAA| Command | Meaning |
|---|---|
| server NAME [PORT] | Send to this server instead of the SOA primary |
| local ADDR [PORT] | Source IP and port |
| zone Z / class C | Zone, class (default IN; CH was REFUSED) |
| ttl N / ttl none | Default TTL / clear it (then an add without TTL is a syntax error) |
| key [alg:]name secret | TSIG key inside the file |
| check-names no | Turns off the client check only; the server's own check-names still REFUSED bad_name |
| update add N TTL [C] TYPE DATA | Add a record |
| update delete N [TYPE [DATA]] | Delete all records of a name / one RRset / one record |
| show / send / answer | Print message / send it (blank line = send) / print reply |
| gsstsig, oldgsstsig, realm | Kerberos signing |
| debug, version, help, ; comment | Control |
87.10 mdig: pipelined queries
- Step 1
1.dig: send one query, wait for the answer, then send the next
- Step 2
2.mdig: sends www, mail and app queries back to back immediately
- Step 3
3.Answers print in arrival order (here mail came first)
- Step 4
4.The server is always required with @ (mdig does not read resolv.conf)
mdig $NS1 +noall +answer www.example.com \ mail.example.com app.example.commdig www.example.comcat q.txt; mdig $NS1 -f q.txt +shortmdig $NS1 +noall +answer www.example.com -t MXmdig $NS1 +noall +answer -t MX example.com \ www.example.commdig $NS1 +noall +answer -t MX example.com \ -t A www.example.commdig @8.8.8.8 +noall +answer -x 8.8.4.4$ mdig $NS1 +noall +answer www.example.com \ mail.example.com app.example.commail.example.com. 1h IN A 192.168.107.25www.example.com. 5m IN A 192.168.107.81app.example.com. 1h IN A 192.168.107.30$ mdig www.example.commdig: a server '@xxx' is required$ cat q.txt; mdig $NS1 -f q.txt +shortwww.example.com-t MX example.com-t NS example.com192.168.107.8110 mail.example.com.ns2.example.com.ns1.example.com.$ mdig $NS1 +noall +answer www.example.com -t MXwww.example.com. 5m IN A 192.168.107.81$ mdig $NS1 +noall +answer -t MX example.com \ www.example.comexample.com. 1h IN MX 10 mail.example.com.$ mdig $NS1 +noall +answer -t MX example.com \ -t A www.example.comexample.com. 1h IN MX 10 mail.example.com.www.example.com. 5m IN A 192.168.107.81$ mdig @8.8.8.8 +noall +answer -x 8.8.4.44.4.8.8.in-addr.arpa. 2h32m23s IN PTR dns.google.- NS1=@192.168.107.10. -f reads one query per line from a batch file.
- Local options apply to the query after them: -t MX written after the name is ignored.
- Local options are sticky: -t MX stayed in force and the A record of www did not come. Give the type before every query.
- Global options (-4/-6, -b, -p, -m, +short, +multiline, +tcp, +nottlid, +nocl, +continue...) go before the first name and apply to all queries; +dscp prints "obsolete".
- Local +options: +norecurse, +dnssec, +cookie, +nsid, +subnet, +yaml, +retry/+tries/+timeout.
87.11 Common mistakes
- Thinking each nsupdate line applies immediately: all lines build one message, send ships one atomic UPDATE; if a prerequisite fails nothing in it is applied.
- BADKEY with a key copied from the right server: check the algorithm and the key name.
- Using -y in production: the secret lands in shell history; use -k.
- Assuming mdig's -t applies only to the next query: it sticks to the following ones.
87.12 Interview tips
- How DHCP client names reach DNS: allow-update / update-policy grants one TSIG key; the DHCP server or a script runs nsupdate -k; prerequisites reject conflicts.
- The primary bumps the serial and NOTIFYs secondaries, which pull with IXFR.
- Know the errors: REFUSED (no permission), BADSIG (wrong secret), BADKEY (wrong name/algorithm), YXDOMAIN/NXRRSET (prereq failed).
- mdig = pipelined bulk queries, @server mandatory.