DNS & Domain Names

Zone Files and Name Servers Explained

Read a DNS zone file line by line and learn how primary and secondary name servers publish it and keep copies in step.

Key takeaways

  • A zone file lists records, each with a name, an optional TTL, a class, a type and data.
  • Every zone has exactly one SOA record and at least one NS record at its top.
  • A name without a trailing dot gets the zone's own name appended, a frequent source of mistakes.
  • Secondary servers copy the zone from a primary, using the serial number to decide when to update.

A zone file is a plain text list of the DNS records for one zone, and a name server is the program that reads that data and answers questions from it. If you manage a domain through a web dashboard, you are editing a zone whether or not you ever see the file.

A zone is a portion of the DNS tree managed as a single unit. example.com and the names under it usually form one zone. If part of it, such as lab.example.com, is handed to other name servers, that part becomes a zone of its own.

A complete small zone

Here is a working example using documentation addresses.

$ORIGIN example.com.
$TTL 3600

@       IN  SOA   ns1.example.com. hostmaster.example.com. (
                  2026120501 ; serial
                  7200       ; refresh
                  3600       ; retry
                  1209600    ; expire
                  300 )      ; negative caching TTL

@       IN  NS    ns1.example.com.
@       IN  NS    ns2.example.com.
ns1     IN  A     192.0.2.53
ns2     IN  A     198.51.100.53

@       IN  A     192.0.2.10
@       IN  AAAA  2001:db8::10
www     IN  CNAME example.com.

@       IN  MX    10 mail.example.com.
mail    IN  A     192.0.2.25
@       IN  TXT   "v=spf1 mx -all"

Semicolons start comments. Parentheses let one record span several lines.

The shorthand rules

Zone files save typing with a few conventions defined alongside DNS itself.

  • $ORIGIN sets the name that gets appended to unfinished names.
  • $TTL sets the default time to live for records that do not state one.
  • @ means the current origin.
  • A line that starts with white space reuses the previous record's name.
  • A name ending in a dot is complete. A name without one is relative.

That last rule causes real outages. In the example zone, mail means mail.example.com.. But writing mail.example.com without the final dot produces mail.example.com.example.com., which almost certainly does not exist.

One record line, field by field
www 1 3600 2 IN 3 A 4 192.0.2.10 5 Name Relative here, so the zone name is added to it. 1 TTL Seconds a cache may keep the answer. Optional if a default is set. 2 Class Internet. You will rarely see anything else. 3 Type What kind of data follows. 4 Data The value, in a format that depends on the type. 5 www 1 3600 2 IN 3 A 4 192.0.2.10 5 Name Relative here, so the zone name is added to it. 1 TTL Seconds a cache may keep the answer. Optional if a default is set. 2 Class Internet. You will rarely see anything else. 3 Type What kind of data follows. 4 Data The value, in a format that depends on the type. 5

The records every zone needs

SOA (start of authority) appears once, at the top of the zone. It names the primary server, gives a contact mailbox with the @ written as a dot, and carries the timers that govern copying and negative caching.

NS records name the authoritative servers. The same set should also be registered in the parent zone, normally through your registrar. When the two lists disagree, lookups behave inconsistently. Details are in NS and SOA records.

Address records for the name servers themselves are needed when those names sit inside the zone, as ns1.example.com does. The parent also needs a copy of these addresses, called glue.

Other records describe your services: A and AAAA for hosts, MX for mail, TXT for policies and proofs.

Name server roles

An authoritative server answers from zone data it holds. Most domains have at least two, preferably on separate networks, so one failure does not remove the domain from the internet.

In the traditional arrangement:

  • The primary holds the editable master copy.
  • Secondaries obtain their copy from the primary and serve it just as authoritatively.

Resolvers do not care which is which. They pick any server listed in the NS set, and often favor whichever answers fastest.

A hidden primary is a variation where the editable server is not listed publicly. It feeds the public secondaries and stays out of reach of outside queries.

How copies stay in step

A secondary picks up a change
Administrator Primary Secondary Edit a record, raise the serial 1 NOTIFY: the zone changed 2 What is your SOA serial? 3 A higher number than yours 4 Request a zone transfer 5 Full zone, or just the changes 6 AXFR is full, IXFR is incremental Administrator Primary Secondary Edit a record, raise the serial 1 NOTIFY: the zone changed 2 What is your SOA serial? 3 A higher number than yours 4 Request a zone transfer 5 Full zone, or just the changes 6 AXFR is full, IXFR is incremental

The serial number is the version of the zone. A secondary updates only when the primary's serial is newer than its own. A common convention is the date plus a two-digit counter, as in 2026120501, though a simple incrementing integer works too. If you edit a zone file by hand and forget to raise the serial, the secondaries keep serving the old data.

The other SOA timers cover the case where notifications are missed:

Field What it controls
Refresh How often a secondary checks the serial
Retry How soon it tries again after a failed check
Expire How long it keeps serving if it cannot reach the primary
Minimum Used to limit how long "no such record" answers are cached

Zone transfers should be restricted. An open transfer hands anyone a complete list of your hostnames. Limit them to your own secondaries by address, and preferably authenticate them with a shared key (a mechanism called TSIG).

Many hosted DNS services replicate zones internally instead of using these transfers. The serial may be generated for you, and some features in a dashboard, such as aliases at the top of a domain, have no equivalent in a standard zone file.

Editing safely

  1. Keep a copy of the current zone before changing it. Many dashboards offer an export.
  2. Make the change, and raise the serial if you manage it yourself.
  3. Check the syntax before loading. With BIND's tools:
named-checkzone example.com /etc/bind/db.example.com
  1. Reload the server, then query it directly to confirm:
dig @192.0.2.53 example.com SOA +short
dig @198.51.100.53 example.com SOA +short
  1. Both should report the same serial. If not, check transfer permissions and firewalls on TCP and UDP port 53.

File paths vary by system and distribution, so adjust the path to your installation.

Frequent mistakes

  • Missing trailing dots on targets in CNAME, MX and NS records.
  • A CNAME beside other records at the same name, which the rules forbid. See CNAME records.
  • NS or MX pointing at an alias instead of a name with its own address records.
  • Forgotten serial, leaving secondaries behind.
  • Name servers changed at the registrar before the new provider holds a complete copy of the zone.
  • Both name servers on one machine or network, which defeats the purpose of having two.

More traps are collected in common DNS mistakes.

How a server decides what to answer

It helps to know what a name server does with your file when a question arrives.

First, it picks the zone it holds that best matches the end of the requested name. If it holds both example.com and lab.example.com, a question about pc1.lab.example.com is answered from the second, because that is the closer match.

Next, it looks for the exact name. If the name exists and has the requested type, those records are returned. If the name has a CNAME, the server returns it, and follows the target too when that target is in a zone it also holds. If the name exists but has no record of that type, the reply is an empty success.

If the name is not there, the server checks for a delegation: NS records partway down the zone that hand a branch to other servers. In that case it returns a referral. It also checks for a wildcard, a record whose name starts with *, which can supply an answer for names that are otherwise absent. Only when none of these apply does it say that the name does not exist, attaching the SOA record so the resolver knows how long to remember that.

Delegating a branch looks like this in the parent zone file:

lab        IN  NS    ns1.lab.example.com.
ns1.lab    IN  A     192.0.2.80

From then on, anything you add for names under lab in the parent file is ignored in favor of the child's servers, apart from the glue address needed to find them.

Hosted DNS versus running your own

Most organizations use a hosted DNS service. It provides many locations, handles replication and saves you from patching a server that the whole internet can reach. Running your own makes sense for learning, for internal networks or where you have specific control requirements and the staff to keep it healthy.

If you do run your own, plan for more than the zone file:

  • Keep the software updated, since name servers are exposed by design.
  • Turn recursion off on servers that answer the public.
  • Place servers in separate networks and monitor each one.
  • Back up the zone and any signing keys, and practice restoring them.
  • If you sign the zone with DNSSEC, automate re-signing. Expired signatures take a domain offline for validating resolvers.

A mixed setup is also possible, with your hidden primary feeding a hosted secondary service. You keep control of the master data while the provider takes the query load.

Moving a zone between providers

When you change DNS hosts, recreate every record at the new provider before touching the registrar. A zone export saves time, but check records that depend on the old provider's special features. Then compare answers from old and new servers name by name, change the delegation and keep both running while caches expire. How to migrate DNS without downtime walks through that sequence.

One thing a zone file cannot tell you is what you forgot to include. Mail settings, verification records and old subdomains are the usual casualties, so build your list from the existing zone and from the services you actually use, not from memory.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in DNS & Domain Names →

Latest news on this topic