Authoritative name servers use the domain's zone file to translate names into IP addresses. The A and AAAA resource records map IP addresses with names. These records are also known as host records. You need to create host records for all resources available inside the domain.
If you don't add a host record for the resource available within the domain, you cannot access it. Let's understand why this would happen.
- To access a resource, you need the resource's IP address.
- You can get the resource's IP address only from the authoritative name server.
- The authoritative name server uses the zone file to tell the resource's IP address.
- In the zone file, a host record maps a name and the IP address.
- Since you did not add a host record for the resource, the authoritative name server fails to resolve the resource name into the IP address.
- Since only authoritative name servers can resolve names into IP addresses and a resource cannot be accessed without knowing its IP address, you cannot access a resource if you don't have a host record for it in the zone file.
Adding or creating the A and AAAA records
A host record uses the following syntax.
name TTL class resource-record-type IP address
The following image shows the zone file of the example.com domain. It contains four host records.
Let's understand the above records field-by-field.
This tutorial is the seventh 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
The TTL and ORIGIN directives in the zone file
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 name Field
This field defines the name of the host or service whose IP address you want to make publicly accessible. There are two methods to specify a name in this field. These methods are FQDN and relative. In the FQDN method, you type the complete name of the host or service. It includes the names of all parent domains. It always ends with a dot. In the relative method, you type only the hostname or the short name of the host or service. You don't need to add parent domain's name with it. It never ends with a dot.
Regardless of the method you use to specify the name in this file, DNS always processes the specified name in the FQDN format. If you specify an FQDN name, it uses the name as it is. If you specify a relative name, it uses the $ORIGIN directive's value to convert the relative name into the FQDN name.
The $ORIGIN directive is variable. Administrators use it to save the domain's name.
In our example zone file, all host records are defined using the relative name. To convert these names into FQDN names, DNS will use the $ORIGIN directive's value.
The TTL Field
This field defines how long other name servers and resolver systems can cache this record. After this time, they need to fetch this record again. If you leave this field empty, DNS uses the value of the $TTL directive. The $TTL directive defines the default TTL value. If you leave the TTL field empty in any record, it uses the $TTL directive's value in that field.
The class type Field
This field defines the class type of this record. There are three classes: IN, CH, and HS. Currently, only the IN class is used. The CH and HS classes were used when the Internet was an in-house project in the MiT lab. CH stands for CHAOS. It is an MIT LAN protocol. HS stands for Hesiod. It is an information service used at MIT. IN stands for the Internet that we use today.
The resource-record-type Field
This field defines the type of record. The A record maps an IPv4 address with the name of a host or a service. The AAAA record maps an IPv6 address with the name of a host or a service.
The IP address Field
The field defines the IP address we want to map with the name defined in the name field. There are two types of IP addresses: IPv4 and IPv6. To map an IPv4 address, the resource type A is used. To map an IPv6 address, the resource type AAAA is used.
In our example zone file, we mapped IPv4 addresses with names. If we map IPv6 addresses with names, the file would look like this.
Mapping multiple IP addresses with a name
You can map multiple IP addresses with the same name. This feature is used when you run the same service on multiple hosts. Normally, administrators run important services on multiple hosts for backup and redundancy purposes. For example, they can run a web service on two or more hosts. In such a situation, they can map their IP addresses with the web service.
Let's understand this feature through our example. Currently, in our example, the web service is available only on the host 192.168.0.3. Now suppose we configure the web service on two additional servers whose IP addresses are 192.168.0.9 and 192.168.0.10. In this situation, we need to add two more A records for these hosts.
If you map multiple IP addresses with the same name, DNS provides an IP address for the name in a round-robin or random order. You can define this order by configuration directives.
Mapping multiple names with a single IP address
You can also map multiple names with a single IP address. You can use this feature when you run more than one service on the same host. For example, if you have a small website and use only a few email addresses for management and communication purposes, you can run the mail, web, and ftp services on the same server. This setup allows you to save the cost of additional servers.
Let's understand this feature through our example. Currently, in our example, all services are available on individual servers. Now suppose we configure the mail, web (www), and ftp services on a single server whose IP address is 192.168.0.2. In this situation, we need to map these services with the IP address 192.168.0.2.
The CNAME resource record type
The CNAME resource record type assigns an alias name to the name of an existing host or a service. It uses the following syntax.
alias-name TTL class resource-record-type canonical-name
In the above syntax, the TTL and class fields have the same meanings as the TTL and class fields have in the host records.
The alias-name field defines the name you want to use for the existing name defined in the canonical-name field.
The resource-record-type field defines the record type. The value CNAME indicates this record defines an alias name for the existing name.
The canonical-name field defines the actual name which you want to access by an alias name defined in the alias-name field.
This record type is used when you want to access a service or a host from another name. You can also use this record to hide your internal network structure from external users.
Let's understand it through our example. Suppose we run the web service on a host called myWebServer. In this situation, we can map the name www (web service) with the name myWebServer. After this, DNS will translate the name www into the IP address of myWebServer. Users will access the web service running on the IP address of myWebServer by using the name www.
The following image shows how to add a CNAME for the web service in our example zone file.
Addressing a website with or without www
In general, administrators configure a web server to serve websites with and without the www. This setup needs a CNAME record in the zone file for the web service. To achieve this goal, first, you need to add a host (A) record for the domain name and then you need to configure the www as the alias name for the domain name.
Let's understand this through our example. In our example, the domain name is example.com. It hosts a website that we want to access by using both names: www.example.com and example.com.
In this case, first, we will create a host record for the domain name. Then we will configure www as the alias name for the domain name.
The following image shows how to add both records.
A zone file only translates names into IP addresses. It does not run or provide any other service. If you add an alias name for a service, you also need to configure that service to respond on both canonical (actual) and alias names. In the preceding example, you also need to configure the webserver to respond on both names www.example.com and example.com.