Zone Files (Master Files)

DNS zones are defined in the zone files that are hosted in the DNS servers. These files must follow the format defined in the RFC 1035 and extended in latter RFCs. These files are called Master Files.

Master files are text files and they are line orientated but parenthesis can be used to extend a certain record along several lines. Master files can include comments that start with a ; and they extend to the end of the line. Master files contain RR and directives.

There are three different directives (Bind allows a forth one, $GENERATE):

$ORIGIN
Defines the base used to complete non qualified names, non qualified names are the names that don't end in a dot (.). The value of $ORIGIN must be a qualified name (that is, it ends in a dot) and hence it converts non qualified names into qualified names when it's appended to them. The value of $ORIGIN is used during the process of the file to either qualify non qualified names and to replace all occurrences of the character @ by its value.
$ORIGIN       example.org.
$INCLUDE
It allows the inclusion of an external file containing directives and RR into the master zone file. As a general rule one should not have $INCLUDE directives in included files.

The format of the directive is:

$INCLUDE      <file> [domain-name]

The domain name is optional and if specified it's used as a value for $ORIGIN directive inside the included file, additionally the file can have $ORIGIN directives.

$TTL
Time To Live, is the default value for use the RR TTL field, which indicates how long a caching server can cache the RR. The default TTL is used when the RR does not have an explicit TTL. TTLs are by default in seconds but most DNS servers allow the definition in other units:
  • Hours: 1h means one hour, or 3600 seconds
  • Days: 2d means two days, that is seconds
  • Weeks: 3w means three weeks, or 1814400 seconds

Except in cases where is expected that a domain will change in the near future the recommendation is to use high values for TTL, ranging from one day to a couple of weeks. The most common setting is to use TTLs of two days (default value if not explicitly defined for most DNS servers), which means that changes take two days to propagate, it is possible to reduce this value when changes in the near future are expected.

The format of the directive is:

$TTL          <time>
$GENERATE
This is a Bind DNS server extension that is used to generate repetitive sequences of RR, it is generally used to generate PTR RR.

The format of the directive is:

$GENERATE <lhs> <type> <rhs>

Example:

$ORIGIN 1.168.192.addr-in.arpa.
$GENERATE 1-127 $ PTR $.host.example.org.

Which genates:

1.1.168.192.addr-in.arpa.   IN PTR 1.host.example.org.
2.1.168.192.addr-in.arpa.   IN PTR 2.host.example.org.
3.1.168.192.addr-in.arpa.   IN PTR 3.host.example.org.
4.1.168.192.addr-in.arpa.   IN PTR 4.host.example.org.
...
127.1.168.192.addr-in.arpa. IN PTR 127.host.example.org.

The master files always start with the directives $ORIGIN and $TTL followed by a SOA (Start Of Authority) resource record, in this register the zone global parameters are defined and there must only be one SOA RR per zone. The format of the RR is:

;Zone-name      TTL      Class   RR  DNS-Server        E-mail-address
example.org.    172800   IN      SOA dns1.example.org. hostmaster.example.org. (
                                   1  ; Serial number
                                   1d ; Refresh
                                   1d ; Retry
                                   4w ; Expiry
                                   3h ; Minimum TTL
                                   )
Zone-name
This is the name of the zone that is contained in the file, it can either be a FQDN (Fully Qualified Domain Name which ends with a dot) or an @ which is replaced by the value of the $ORIGIN directive.
TTL
TTL of the RR, if not explicitly set the default value will be used.
Class
Protocol family, as long as we work with Internet addresses is IN.
RR
Register, SOA in this case.
DNS-Server
Master DNS server for the zone, it should be the server the administrator updates and contains the authoritative information for the zone. If the name is not qualified (ends with a dot) the contents of $ORIGIN will be appended to the name to get the FQDN.
E-mail-address
E-mail address of the domain administrator (generally the technical contact) of the zone replacing the @ by a dot (.). Usually the address hostmaster.example.org., that corresponds to hostmaster@example.org. If the e-mail address has a dot in the part of the user name (like host.master@example.org) that dot has to be escaped with a \ (host\.master@example.org).
Serial number
Integer number (32 bit unsigned integer) that must be incremented every time the zone is changed. The RFC 1912 recommends the format YYYYMMDDnn that means year, month day and version number, in this way we get a serial number that is increased on each change and at the same time it provides information about the last time the zone was changed.
Refresh
Amount of time (32 bit integer) after which a slave server must update the zone information from the master server.
Retry
Amount of time (32 bit integer) secondary servers must wait to retry a zone update after a failed attempt to update the zone from the master server.
Expiry
Amount of time (32 bit integer) after which a zone is no longer authoritative. This value is used by slave servers to know for how long they can use that zone information to answer queries before they consider it expired.
Minimum TTL
Minimum TTL (32 bit unsigned integer) that can be assigned to any zone RR. It is also the TTL recommended by the RFC 1912 for negative answers (NXDOMAIN).

Inside a zone there are another two essential elements: the records NS and MX:

NS records
They contain the names of the DNS servers that hold authoritative information for the zone, they are also used to delegate parts of the DNS tree, one of the DNS servers is the master DNS server, the server that holds the original information as introduced by the administrator, and the others are slave servers, that replicate the information from the master server. The master server also uses these RR as the default list of servers to send update notifications when the zone changes.
MX records
They hold the mail exchanger for the domain and they are essential for the correct behaviour of the mail protocol SMTP (Simple Mail Transfer Protocol). They have the particularity that the RR data is made of two fields: a 16 bit integer indicating the priority (lower numbers indicate higher priorities) and a text field that contains a domain name, the domain name must be an A record (or an AAAA in case of IPv6) and it must never be a CNAME.

This record is used by the SMTP protocol to determine the names of the mail servers, so when an SMTP server has to deliver a mail to the address user@example.org the SMTP server requests the MX records from the DNS and it sends the mail the server held in the higher priority MX record of the domain example.org. If that server is not available it tries to send the e-mail to the immediately lower priority MX record and so on until it either founds an MX record with a working SMTP server or exhausts all the zone MX records.

Example:

example.org.    172800 IN      MX      10 mx1.example.org.
                172800 IN      MX      20 mx2.example.org.

In this case the higher priority MX record would be mx1.example.org while mx2.example.org would be the secondary MX record.

Example of master zone file:

$ORIGIN example.org.    ; Base of the domain name of the zone, it must be qualified (end with a dot)
$TTL 172800             ; Default TTL for the zone (2 days)

@       IN      SOA   ns.example.org. hostmaster.example.org. (
                            2009101401      ; Serial number (YYYYMMDDnn)
                            86400           ; Refresh (1d)
                            86400           ; Retry (1d)
                            2419200         ; Expiry (4w)
                            7200            ; Minimum TTL  (2h)
                            )

        IN      NS      ns.example.org.     ; Master DNS, A record that must be defined inside the zone
        IN      NS      ns.example.net.     ; Slave DNS, it must be an A record and it's defined in the zone
                                            ; example.net

        IN      MX      10 mx.example.org.  ; Primary mail server for the zone, it must be an A record and it 
					    ; must be set inside the zone
        IN      MX      20 mx.example.net.  ; Secondarymail server for the zone, it must be an A record and it 
					    ; must be set in the zone example.net

ns              IN      A      10.0.1.1     ; Setting of ns.example.org, it the non qualified name is used (not 
					    ; ended with a dot (it's qualified appending the value of $ORIGIN)
					    ; The TTL is set to the default (172800 seconds, that is, 2 days).
mx      3600    IN      A      10.0.1.2     ; Setting of mx.example.org, it the non qualified name is used.
					    ; The TTL is set to 3600 seconds (one hour).