DNS saves its database in zone files. A zone file stores records of a particular domain in simple text format. For easy management and administration, it uses directives. In a zone file, a directive is variable. If you are familiar with any programming language, you can easily understand the purpose of a directive in the zone file. It does the same thing a variable does in the programming language. In simple words, a directive is a zone file's variable.
This tutorial is the second part of the tutorial series "DNS zone files Explained with Examples.". Each tutorial of this series explains a specific aspect of the zone file. Other tutorials of the series are the following.
DNS zone file Format, Configuration, and Directives
Types of resources records in zone files
The SOA Record Explained in the DNS zone file
The NS Record, Glue Record, and Lame Delegation
The MX record in a zone file explained
A directive starts with a dollar sign. It stores value. DNS replaces a directive with its value when it processes the zone file. There are two important directives $TTL and $ORIGIN.
$TTL
This directive is mandatory. It defines a default Time to Live (TTL) value for resource records. All resource records have the TTL field. If you want to define a specific TTL value for a record, you can configure its TTL field. If you want to use the default TTL value for a record, you need to leave the record's TTL field empty. When processing records, DNS uses the default TTL value in all blank TTL fields.
DNS service includes TTL value with each record it serves from this file. Other DNS servers and resolver systems that receive a record served from this file use the TTL value to cache the record. For example, if the TTL value of a record is 1 day, resolver systems and other DNS servers can cache the record for 1 day. After that, they must have to retrieve the same record again.
The following image shows how the DNS service uses the $TTL directive's value to configure resource records' TTL value.
By default, the TTL directive accepts time value in seconds. If you want to specify the time in other formats, you need to use related keywords. These keywords are m for minutes, h for hours, d for days, and w for weeks.
The following examples set this directive's value to 2 days in different formats. You can use whatever format you like.
$TTL 172800 ; 2 days in seconds 60 * 60 * 24 * 2 $TTL 2880m ; 2 days in minutes 60 * 24 * 2 $TTL 48h ; 2 days in hours 24 * 2 $TTL 2d ; 2 days
$ORIGIN
DNS uses a zone file to translate names into IP addresses. There are four ways to specify a name in the zone file. You can specify a fully qualified domain name, a relative name, a single @ character, and a blank space.
- If you use an FQDN name, DNS uses the name as it is.
- If you use a relative name, DNS uses the $ORIGIN directive's value to convert it into the FQDN name.
- If you use a single character @, DNS uses the value of the $ORIGIN directive.
- If you use a blank space or leave the field empty, DNS uses the value of the name field of the previous record. If the previous record is not available, it uses the value of the $ORIGIN directive.
The $ORIGIN directive defines the domain or zone name. You can use it anywhere in the zone file. DNS uses it to convert all relative names that come after it into FQDN names. In the zone file, all names that do not end with a dot are considered relative names. An FQDN (fully qualified domain name) ends with a dot and denotes the complete name.
The following image shows how the DNS service uses the $ORIGIN directive's value to convert relative names into FQDN names.
You can use this directive multiple times. In that case, DNS will use a directive's value until it finds the next directive.
The following examples show how DNS processes this directive.
$ORIGIN example.com. ; It defines the value of this directive to example.com. ; The following is a resource record. It uses a relative name to define the host www www IN A 192.168.2.2 ; DNS will read and process the above hostname name as www.example.com. $ORIGIN test.com; It defines the new value of this directive to test.com ; The following is another resource record. It uses a relative name to define the host ftp ftp IN A 192.168.2.5 ; DNS will read and process the above hostname name as ftp.test.com.
This $ORIGIN directive is optional. If you do not set its value, DNS uses the domain or zone name configured in the named.conf file as the default value of this directive to process all records. For example, if you use relative names in the file without configuring this directive, DNS uses the domain or zone name configured in the named.conf file to convert them into FQDN names.