Sub topic 10
Writing a zone file
This chapter assumes you know what resource record types are (A, NS, MX, PTR) and what TTLs do. It also assumes you can edit files as root and run dig. The chapter covers the zone file format, the SOA record and its serial number, and the mistakes that break zones in practice.
What a zone file is
A zone file is a plain text file holding the resource records for one zone. A primary (authoritative) server such as BIND reads it at startup and on reload, then answers queries from it. The format comes from RFC 1035 and is called the master file format. Every authoritative server that loads zone files uses it, so what you learn here carries over to NSD, Knot DNS and PowerDNS.
A file is a list of entries. Each entry is either a directive such as $TTL, or a resource record with this shape:
owner ttl class type rdataOnly type and rdata are always required. The other fields have defaults, which is why most zone files look terse.
A complete example
This file defines example.com with two name servers, a mail exchanger and three address records.
$TTL 3600
@ IN SOA ns1.example.com. hostmaster.example.com. (
2026112801 ; serial
7200 ; refresh
3600 ; retry
1209600 ; expire
3600 ) ; negative caching TTL
IN NS ns1.example.com.
IN NS ns2.example.com.
IN MX 10 mail.example.com.
ns1 IN A 192.0.2.10
ns2 IN A 192.0.2.11
www IN A 192.0.2.20
mail IN A 192.0.2.25Syntax rules that cause most errors
The trailing dot
A name that ends in a dot is absolute (fully qualified). A name without a trailing dot is relative, and the server appends the zone origin to it.
In example.com, www becomes www.example.com.. But ns1.example.com without the final dot becomes ns1.example.com.example.com.. This is the most common zone file mistake. The server loads the file without complaint and then returns wrong answers.
The origin and the @ symbol
The origin is the name appended to relative names. BIND sets it from the zone statement in named.conf. You can also set it in the file with $ORIGIN example.com.. The @ symbol stands for the current origin, so @ at the start of a line means the zone apex.
Omitted owner names
A line that starts with whitespace reuses the owner name of the previous record. In the example, the NS and MX lines sit directly under the SOA record, so all three belong to @. Leading whitespace is meaningful. If you align records with spaces, check that every line meant to start with a name starts in column one.
Omitted TTL and class
$TTL 3600 sets the default TTL for records that do not state one. Without it, BIND refuses to load the zone, or applies the SOA minimum field on older software. Always set $TTL explicitly. You can override it per record, for example www 300 IN A 192.0.2.20. The class is almost always IN (Internet).
Comments and parentheses
A semicolon starts a comment that runs to the end of the line. Parentheses let one record span several lines, which the SOA record needs.
Record data rules
- The right side of an
NSorMXrecord must be a hostname, never an IP address. - A
CNAMEcannot coexist with any other record at the same name. That rules out a CNAME at the apex, where the SOA and NS records already live. - An
NSorMXtarget should not be a CNAME. - A hostname used as an
NStarget within its own zone needs an A record, asns1andns2have above. - Multiple A records at one name make a round-robin set. Resolvers receive all of them.
The SOA record
The Start of Authority record is required and must be the first record in the zone. It carries the zone's administrative and timing data.
| Field | Example | Meaning |
|---|---|---|
| MNAME | `ns1.example.com.` | Primary name server for the zone |
| RNAME | `hostmaster.example.com.` | Admin mailbox, with the first `.` standing for `@` |
| Serial | `2026112801` | Version number of the zone |
| Refresh | `7200` | How often a secondary checks the primary for a new serial |
| Retry | `3600` | How long a secondary waits before retrying a failed refresh |
| Expire | `1209600` | How long a secondary keeps serving without reaching the primary |
| Negative TTL | `3600` | How long resolvers cache "no such name" answers |
The RNAME replaces @ with a dot, so hostmaster.example.com. means [email protected]. If the mailbox local part contains a dot, escape it with a backslash.
The last field is the negative caching TTL (RFC 2308). Older documents call it "minimum". Keep it modest. A large value means a newly created name stays "not found" in resolver caches for that long after you add it.
Serial numbers
A secondary server transfers the zone only when the primary's serial is higher than its own. If you edit the zone and forget to raise the serial, secondaries keep serving the old data indefinitely. This is the most frequent cause of "the secondary has stale data."
The serial is a 32-bit unsigned number, compared using serial number arithmetic (RFC 1982). The common convention is YYYYMMDDnn, where nn is a two-digit revision counter for that day. In the example, 2026112801 is the first change on 28 November 2026. The next change that day is 2026112802.
Rules for serials:
- Always increase the serial. Never decrease it.
- A ten-digit date-based serial fits in 32 bits, since the maximum is 4294967295. The convention works until a date such as
2100…would overflow it. This is not a concern now. - If you do set a serial lower than the secondaries' value by mistake, the secondaries ignore the zone. Recovery means raising the serial carefully, or forcing a retransfer on the secondary with
rndc retransfer example.com. - A zone with dynamic updates or automatic DNSSEC signing can manage its own serial. Do not hand-edit those files while the server runs.
Verifying a zone file
Check the file before you load it. named-checkzone takes the zone name and the file path.
sudo named-checkzone example.com /etc/bind/db.example.comOn success it prints the loaded serial and OK:
zone example.com/IN: loaded serial 2026112801
OKIt reports problems such as a missing SOA, a CNAME next to other data, or an NS target with no address record. Treat every warning as an error. Run the check as part of every edit.
After you load the zone, query it directly. +norecurse makes sure you see what this server holds, not what it fetched elsewhere.
sudo rndc reload example.com
dig @127.0.0.1 example.com SOA +short
dig @127.0.0.1 www.example.com A +norecurseThe SOA output should show the serial you just set. If it shows the old one, the reload did not happen or the file you edited is not the one named in named.conf.
Common failure modes
| Symptom | Likely cause |
|---|---|
| Names resolve with the zone name doubled | Missing trailing dot on a fully qualified name |
| Zone fails to load, "no TTL specified" | `$TTL` missing |
| Edits have no effect on secondaries | Serial not increased |
| Zone fails to load, "CNAME and other data" | CNAME shares a name with another record |
| Query returns `SERVFAIL` for the zone | Zone failed to load; check `journalctl -u named` or the log for the parse error |
| Some records vanish | Lines meant to carry a name begin with spaces and attach to the previous owner |
Relationship to the BIND configuration
A zone file does nothing until named.conf points at it. The zone statement from the authoritative server example names the file and the zone's role:
zone "example.com" {
type primary;
file "/etc/bind/db.example.com";
allow-transfer { 192.0.2.11; };
};The next chapter covers serving this zone in full. For now, note that the zone name in named.conf becomes the origin for relative names in the file.
Exercise
A colleague adds the line www2 IN A 192.0.2.30 and a line mail IN MX 10 mx.example.com to the zone, raises the serial, and reloads. named-checkzone reports OK, but dig for the MX target returns mx.example.com.example.com.. What is wrong, and how do you fix it?
Show answer
The MX target mx.example.com has no trailing dot, so the server treats it as relative and appends the origin. Change it to mx.example.com. (with the final dot), raise the serial again, run named-checkzone, and reload. Note that the corrected record also needs an A record for mx in the zone, or the name must resolve elsewhere.
Exercise
A secondary still serves old data after you added a record to the primary. dig @primary example.com SOA +short shows serial 2026112801 and the secondary shows the same. What happened, and what is the fix?
Show answer
The record was added without raising the serial, so the secondary saw no change and did not transfer. Set a higher serial such as 2026112802, run named-checkzone, reload the zone on the primary, and confirm the secondary's SOA serial matches. If the primary sends NOTIFY, the secondary updates immediately. Otherwise it updates at the next refresh interval, or you can run rndc retransfer example.com on the secondary.