Sub topic 14
Adding a secondary server with zone transfers and TSIG
This chapter assumes you can serve an authoritative zone with BIND, as in the earlier chapters. It also assumes you can write a zone file with a correct SOA record, and that you can open TCP and UDP port 53 in your firewall. Here you add a second server that copies the zone from the first and authenticates each transfer with a shared secret.
Why run a secondary
Every public zone needs at least two name servers, listed as NS records. If the only server fails, nobody can resolve your names once cached answers expire. A secondary server holds a copy of the zone and answers queries just like the primary. You edit the zone in one place only, and the copy follows.
In BIND 9.16 and later the roles are named primary and secondary. Older configuration uses master and slave, and BIND still accepts those words. A secondary is authoritative for the zone, so clients cannot tell it apart from the primary.
How a transfer happens
The secondary does not poll constantly. The SOA record controls the timing, and the primary can also prompt the secondary with a NOTIFY message.
- You raise the serial in the zone file and reload the zone on the primary.
- The primary sends a NOTIFY message to each secondary.
- The secondary asks the primary for the SOA record and compares serial numbers.
- If the primary's serial is higher, the secondary requests a transfer over TCP.
If the NOTIFY is lost, the secondary still checks the SOA every refresh seconds (7200 in the earlier zone file). After a failed check it retries every retry seconds. If it cannot reach the primary for expire seconds, it stops answering for the zone. That is why the example uses an expire of 1209600 seconds (14 days).
A full transfer is AXFR and sends the whole zone. An incremental transfer is IXFR and sends only the changes between two serials. The primary can only build an IXFR if it kept a journal of those changes. If it cannot, it falls back to AXFR. Both types run over TCP.
Why a transfer needs authentication
A zone transfer hands over every record in the zone, including internal hostnames you may not want to publish. Restricting by IP address alone is weak because source addresses can be spoofed. TSIG (Transaction Signature) adds a shared secret. Each message carries an HMAC of its contents, computed with the secret, so each side can check who sent it and that the message was not altered. The clocks on both servers must agree to within a few minutes, because TSIG signatures include a timestamp. Run chrony or systemd-timesyncd on both.
Create the key
Generate the key on the primary and copy it to the secondary over a secure channel such as SSH.
tsig-keygen transfer-key | sudo tee /etc/bind/transfer.key
sudo chown root:bind /etc/bind/transfer.key
sudo chmod 640 /etc/bind/transfer.keyThe output is a key statement with the algorithm (hmac-sha256) and a base64 secret. Keeping it in its own file means you can include it from named.conf without exposing the secret in the main file. The file must be readable by the named user and by nobody else. Copy the same file to the secondary.
Configure the primary
Include the key and require it for transfers. The server statement tells BIND to sign its own messages to the secondary, which covers NOTIFY.
include "/etc/bind/transfer.key";
server 192.0.2.11 {
keys { transfer-key; };
};
options {
directory "/var/cache/bind";
listen-on { 127.0.0.1; 192.0.2.10; };
allow-query { any; };
recursion no;
};
zone "example.com" {
type primary;
file "/etc/bind/db.example.com";
notify yes;
also-notify { 192.0.2.11; };
allow-transfer { key transfer-key; };
};allow-transfer { key transfer-key; } accepts a request only if it is signed with that key. A request from the right address without a signature is refused. You can combine conditions, but a list such as { 192.0.2.11; key transfer-key; } means either one is enough. If you want both, use a nested ACL, or keep the key alone, which is usually the simplest and the strongest.
notify yes is the default. It sends NOTIFY to every server listed in the zone's NS records. also-notify adds servers that are not in the NS list, such as a hidden secondary. Listing the secondary in both places is harmless.
Configure the secondary
The secondary defines the same key, names the primary, and says where to store the zone copy.
include "/etc/bind/transfer.key";
server 192.0.2.10 {
keys { transfer-key; };
};
options {
directory "/var/cache/bind";
listen-on { 127.0.0.1; 192.0.2.11; };
allow-query { any; };
recursion no;
};
zone "example.com" {
type secondary;
primaries { 192.0.2.10; };
file "/var/cache/bind/db.example.com";
};Store the file under /var/cache/bind (or /var/lib/bind on RHEL-family systems with the matching setting). The secondary writes the file itself, so the directory must be writable by named. A path under /etc/bind usually fails because of file permissions or an AppArmor or SELinux policy. The secondary has no zone file to edit. BIND stores its copy in a binary format by default, so do not edit it by hand.
The secondary's allow-notify defaults to the addresses in primaries. A secondary also does not need allow-transfer unless it serves further secondaries.
Test the transfer
Check the configuration on both servers, then reload or start BIND.
sudo named-checkconf
sudo rndc reconfig
# On the secondary, force an immediate transfer
sudo rndc retransfer example.com
# Compare serials on both servers
dig @192.0.2.10 example.com SOA +short
dig @192.0.2.11 example.com SOA +short
# An unsigned transfer must be refused
dig @192.0.2.10 example.com AXFR
# A signed transfer must work (replace the secret with yours)
dig @192.0.2.10 example.com AXFR -y hmac-sha256:transfer-key:BASE64SECRET==The unsigned AXFR should print Transfer failed or a REFUSED status. The signed one should list every record. Passing the secret on the command line stores it in your shell history and shows it in the process list, so run that test only on a trusted host and clear the history entry afterwards.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Secondary log shows `refused` | The primary's `allow-transfer` does not match, or the key is missing from the `server` statement on the secondary | Compare both key files with `diff` and check the `server` statements |
| `tsig verify failure (BADSIG)` | Different secrets or algorithms on the two servers | Copy the key file again |
| `BADTIME` | Clocks differ by more than the allowed fudge (five minutes by default) | Fix time synchronization |
| Transfer times out | Firewall blocks TCP port 53 | Open TCP and UDP on port 53 between the servers |
| Secondary keeps the old data | The serial was not raised, or NOTIFY did not arrive | Increase the serial, then run `rndc retransfer` on the secondary |
| Cannot write the zone file | Wrong directory or a mandatory access control policy | Use a writable directory such as `/var/cache/bind` |
Exercise
The primary's zone statement has allow-transfer { key transfer-key; };. A colleague's server at 192.0.2.50 runs dig @192.0.2.10 example.com AXFR without a key. What happens, and which two checks would you make on the secondary if its copy of the zone stays out of date?
Show answer
The primary refuses the transfer, because the request is not signed with transfer-key and the source address alone does not satisfy the key rule. If the secondary stays out of date, first compare the SOA serials on both servers with dig +short SOA to confirm the primary's serial really was raised. Then read the secondary's log for refused, BADSIG or BADTIME messages, and run rndc retransfer example.com to test the transfer directly.