Monday, July 16, 2012

How To setup Caching DNS SERVER - PART II


As with our caching-only nameserver, we will see that BIND RPMS packaged by Red Hat® Enterprise Linux® and Fedora ease the process of configuring our master nameserver. Adding authoritative responsibility to the caching-only nameserver only requires us to add two more files and modify the existing named.conf file. For the purpose of this article we will assume the following:
  • The BIND 9.x RPMS discussed in Part 1 are installed on the machine that will serve as a nameserver.
  • Our internal network is in the subnet. You will need to substitute your subnet if different.
  • The master nameserver will only allow DNS queries from internal clients in the subnet.
  • The master nameserver will continue to forward any DNS requests it can't answer to your upstream ISP nameserver(s).
  • We will use the domain as our internal domain name.
You might notice that we selected a mock top-level domain (sometimes referred as a TLD) named lan. Our internal domain name can be as creative as we wish since the domain is only known inside our home network. The naming convention for a public nameserver is not as relaxed, since we would need to follow certain rules that would allow our nameserver to respond to other nameservers requesting host information from around the world.


Nameservers store information about a domain namespace in files called zone data files. A zone data file contains all the resource records that describe the domain represented in the zone. The resource records further describe all the hosts in the zone. We will need to modify our existing named.conf to reference two zone files for our domain name:
  • Forward zone definitions that map hostnames to IP addresses.
  • Reverse zone definitions that map IP addresses to hostnames.
Open /var/named/chroot/etc/named.conf and add the following forward and reverse zone file directives:
# Forward Zone for domain
zone "hughes.lan" IN {
        type master;
        file "";
# Reverse Zone for domain
zone "" IN {
        type master;
        file "";            
Both the forward and reverse zones contain the type master indicating that our nameserver is the master or primary nameserver for the domain. The file keyword indicates which zone file contains the resource records for the corresponding zone. Note that the reverse zone contains a special domain named in-addr-arpa. DNS uses this special domain to support IP to hostname mappings. Reverse lookups are backwards since the name is read from the leaf to the root (imagine a domain name as a tree structure) so the resultant domain name has the topmost element at the right-hand side. For a home network the reverse lookup zones can be considered optional but we will include them for completeness.
Included with the BIND RPMs is a root zone nameservers use when a query is unresolvable by any other configured zones. The root zone directive is named ".", has a type of hint and references a file named that contains a list of 13 root name servers located around the world. We will not directly use the root servers since we are forwarding any unresolvable queries to our ISP nameservers.
We need to modify the named.conf global options to allow our internal clients to query the nameserver. Modify the existing global options block to the following:

acl linuxhowto-in {; 127.0/8; };
options {
        directory "/var/named";
        allow-query { linuxhowto-lan; };
        forwarders {;; }; # ISP primary/secondary
        forward-only; # Rely completely on ISP for cache misses
The acl statement above sets up a range of IP addresses we can reference throughout the named.conf file. The allow-query specifies IP addresses of hosts that can query our nameserver. The forwarders statement tells our nameserver to forward any unresolvable queries to our upstream nameservers. The forward-only statement restricts our nameserver to only rely on our ISP nameservers and not contact other nameservers to find information that our ISP can not provide. It's very rare for a primary and secondary ISP nameserver to be down at the same time but you can comment forward-only if you want your nameserver to try the root nameservers when the upstream ISP nameservers cannot resolve a hostname.

Zone files

We are now ready to start defining our hostname mappings in the zone files we referenced in the named.conf configuration. Zone files need to be placed in the /var/named/chroot/var/named directory, have 644 permissions with an owner and group of named:

cd /var/named/chroot/var/named
chown named:named
chmod 644
Let's take a look at an example zone file for the forward zone and then dive into the various parts: 

$TTL 1D             IN      SOA (
                                200612060                 ; serial
                                2H                        ; refresh slaves
                                5M                        ; retry
                                1W                        ; expire
                                1M                        ; Negative TTL
@                       IN      NS       IN      A     ; RHEL server        IN      A      ; router      IN      A      ; upstairs WAP      IN      A      ; downstairs WAP  IN      A      ; Fedora desktop      IN      A      ; network printer
mysterymachine          IN      A      ; mail server
scrappy                IN      A      ; Windows box
                                                       ; aliases
www                    IN      CNAME ; WWW server   
virtual                IN      CNAME   cluster             ; virtual WWW tests
mail                    IN      CNAME   mysterymachine    ; sendmail host
                                                       ; DHCP Clients      IN      A      IN      A      IN      A      IN      A      IN      A
@                       IN      MX  10
The very first line in the contains the TTL (Time To Live) value and is set to one day. TTL is used by nameservers to know how long to cache nameserver responses. This value would have more meaning if our nameserver was public and had other external nameservers depending on our domain information. Notice the 'D' in the TTL value stands for Day. Bind also uses 'W' for weeks, 'H' for hours, and 'M' for minutes.

The first resource record is the SOA (Start Of Authority) Record which indicates that this nameserver is the best authoritative resource for the domain. The IN stands for Internet Class and is optional. The first hostname after the SOA is the name of our master or primary nameserver. The second name, "", is the email address for the person in charge of this zone. Notice the '@' is replaced with a '.' and also ends with a '.'. The third value is the serial number that indicates the current revision, typically in the YYYYMMDD format with a single digit at the end indicating the revision number for that day. The fourth, fifth, sixth, and seventh values can be ignored for the purposes of this article.

The NS record lists each authoritative nameserver for the current zone. Notice the first '@' character in this line. The '@' character is a short-hand way to reference the domain,, that was referenced in the named.conf configuration file for this zone.

The next block of A records contains our hostname to address mappings. The CNAME records act as aliases to previously defined A records. Notice how fully qualified domains end with a '.'. If the '.' is omitted then the domain,, is appended to the hostname. In our example the hostname, scrappy, will become

If you want to reference an internal mail server, then add a MX record that specifies a mail exchanger. The MX value "10" in our example indicates the preference value (number between 0 and 65535) for this mail exchanger's priority. Clients try the highest priorty exchanger first.

The reverse zone file,, is similar to our forward zone but contains PTR records instead of A records:
@       IN      SOA (
200612060       ; serial
2H              ; refresh slaves
5M              ; retry
1W              ; expire
1M              ; Negative TTL
        IN      NS
10      IN      PTR
1       IN      PTR
2       IN      PTR
3       IN      PTR
4       IN      PTR
5       IN      PTR
6       IN      PTR
7       IN      PTR
100     IN      PTR
101     IN      PTR
102     IN      PTR
103     IN      PTR
104     IN      PTR


Save your zone files, make sure you have the correct permissions and check the syntax using named-checkzone:

Correct any syntax errors reported by named-checkzone.
Restart the nameserver:
service named restart
Browse through the tail of the /var/log/messages file and confirm the domain loaded successfully.
Make the following DNS queries (substituting your domain):
dig -x
Your output should be similar to the following:
;             IN      A
;; ANSWER SECTION:      86400   IN      A
;; AUTHORITY SECTION:             86400   IN      NS
;; ADDITIONAL SECTION:       86400   IN      A
Continue to test each host you added to the zone file and then enjoy your new master nameserver.