17.9 DNS

Contributed by Chern Lee.

17.9.1 Overview

FreeBSD utilizes, by default, a version of BIND (Berkeley Internet Name Domain), which is the most common implementation of the DNS protocol. DNS is the protocol through which names are mapped to IP addresses, and vice versa. For example, a query for www.FreeBSD.org will receive a reply with the IP address of The FreeBSD Project's web server, whereas, a query for ftp.FreeBSD.org will return the IP address of the corresponding FTP machine. Likewise, the opposite can happen. A query for an IP address can resolve its hostname. It is not necessary to run a name server to perform DNS lookups on a system.

DNS is coordinated across the Internet through a somewhat complex system of authoritative root name servers, and other smaller-scale name servers who host and cache individual domain information.

This document refers to BIND 8.x, as it is the stable version used in FreeBSD. BIND 9.x in FreeBSD can be installed through the net/bind9 port.

RFC1034 and RFC1035 dictates the DNS protocol.

Currently, BIND is maintained by the Internet Software Consortium (www.isc.org)

17.9.2 Terminology

To understand this document, some terms related to DNS must be understood.

Term Definition
forward DNS mapping of hostnames to IP addresses
origin refers to the domain covered for the particular zone file
named, bind, name server common names for the BIND name server package within FreeBSD
resolver a system process through which a machine queries a name server for zone information
reverse DNS the opposite of forward DNS, mapping of IP addresses to hostnames
root zone literally, a ``.'', refers to the root, or beginning zone. All zones fall under this, as do all files in fall under the root directory. It is the beginning of the Internet zone hierarchy.
zone Each individual domain, subdomain, or area dictated by DNS

Examples of zones:

As one can see, the more specific part of a hostname appears to its left. For example, example.org. is more specific than org., as org. is more specific than the root zone. The layout of each part of a hostname is much like a filesystem: the /dev directory falls within the root, and so on.

17.9.3 Reasons to Run a Name Server

Name servers usually come in two forms: an authoritative name server, and a caching name server.

An authoritative name server is needed when:

A caching name server is needed when:

When one queries for www.FreeBSD.org, the resolver usually queries the uplink ISP's name server, and retrieves the reply. With a local, caching DNS server, the query only has to be made once to the outside world by the caching DNS server. Every additional query will not have to look to the outside of the local network, since the information is cached locally.

17.9.4 How It Works

In FreeBSD, the BIND daemon is called named for obvious reasons.

File Description
named the BIND daemon
ndc name daemon control program
/etc/namedb directory where BIND zone information resides
/etc/namedb/named.conf daemon configuration file

Zone files are usually contained within the /etc/namedb directory, and contain the DNS zone information served by the name server.

17.9.5 Starting BIND

Since BIND is installed by default, configuring it all is relatively simple.

To ensure the named daemon is started at boot, put the following modifications in /etc/rc.conf:

    named_enable="YES"

To start the daemon manually (after configuring it)

    # ndc start

17.9.6 Configuration Files

17.9.6.1 make-localhost

Be sure to:

    # cd /etc/namedb
    # sh make-localhost

to properly create the local reverse DNS zone file in /etc/namedb/localhost.rev.

17.9.6.2 /etc/namedb/named.conf

    // $FreeBSD$
    //
    // Refer to the named(8) manual page for details.  If you are ever going
    // to setup a primary server, make sure you've understood the hairy
    // details of how DNS is working.  Even with simple mistakes, you can
    // break connectivity for affected parties, or cause huge amount of
    // useless Internet traffic.
    
    options {
            directory "/etc/namedb";
    
    // In addition to the "forwarders" clause, you can force your name
    // server to never initiate queries of its own, but always ask its
    // forwarders only, by enabling the following line:
    //
    //      forward only;
    
    // If you've got a DNS server around at your upstream provider, enter
    // its IP address here, and enable the line below.  This will make you
    // benefit from its cache, thus reduce overall DNS traffic in the
    Internet.
    /*
            forwarders {
                    127.0.0.1;
            };
    */

Just as the comment says, to benefit from an uplink's cache, forwarders can be enabled here. Under normal circumstances, a name server will recursively query the Internet looking at certain name servers until it finds the answer it is looking for. Having this enabled will have it query the uplink's name server (or name server provided) first, taking advantage of its cache. If the uplink name server in question is a heavily trafficked, fast name server, enabling this may be worthwhile.

Warning: 127.0.0.1 will not work here. Change this IP address to a name server at your uplink.

            /*
             * If there is a firewall between you and name servers you want
             * to talk to, you might need to uncomment the query-source
             * directive below.  Previous versions of BIND always asked
             * questions using port 53, but BIND 8.1 uses an unprivileged
             * port by default.
             */
            // query-source address * port 53;
    
            /*
             * If running in a sandbox, you may have to specify a different
             * location for the dumpfile.
             */
            // dump-file "s/named_dump.db";
    };
    
    // Note: the following will be supported in a future release.
    /*
    host { any; } {
            topology {
                    127.0.0.0/8;
            };
    };
    */
    
    // Setting up secondaries is way easier and the rough picture for this
    // is explained below.
    //
    // If you enable a local name server, don't forget to enter 127.0.0.1
    // into your /etc/resolv.conf so this server will be queried first.
    // Also, make sure to enable it in /etc/rc.conf.
    
    zone "." {
            type hint;
            file "named.root";
    };
    
    zone "0.0.127.IN-ADDR.ARPA" {
            type master;
            file "localhost.rev";
    };
    
    zone
    "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.INT" {
            type master;
            file "localhost.rev";
    };
    
    // NB: Do not use the IP addresses below, they are faked, and only
    // serve demonstration/documentation purposes!
    //
    // Example secondary config entries.  It can be convenient to become
    // a secondary at least for the zone where your own domain is in.  Ask
    // your network administrator for the IP address of the responsible
    // primary.
    //
    // Never forget to include the reverse lookup (IN-ADDR.ARPA) zone!
    // (This is the first bytes of the respective IP address, in reverse
    // order, with ".IN-ADDR.ARPA" appended.)
    //
    // Before starting to setup a primary zone, better make sure you fully
    // understand how DNS and BIND works, however.  There are sometimes
    // unobvious pitfalls.  Setting up a secondary is comparably simpler.
    //
    // NB: Don't blindly enable the examples below. :-)  Use actual names
    // and addresses instead.
    //
    // NOTE!!! FreeBSD runs bind in a sandbox (see named_flags in rc.conf).
    // The directory containing the secondary zones must be write accessible
    // to bind.  The following sequence is suggested:
    //
    //      mkdir /etc/namedb/s
    //      chown bind:bind /etc/namedb/s
    //      chmod 750 /etc/namedb/s

For more information on running BIND in a sandbox, see Running named in a sandbox.

    /*
    zone "domain.com" {
            type slave;
            file "s/domain.com.bak";
            masters {
                    192.168.1.1;
            };
    };
    
    zone "0.168.192.in-addr.arpa" {
            type slave;
            file "s/0.168.192.in-addr.arpa.bak";
            masters {
                    192.168.1.1;
            };
    };
    */

In named.conf, these are examples of slave entries for a forward and reverse zone.

For each new zone served, a new zone entry must be added to named.conf

For example, the simplest zone entry for example.org can look like:

    zone "example.org" {
        type master;
        file "example.org";
    };

The zone is a master, as indicated by the type statement, holding its zone information in /etc/namedb/example.org indicated by the file statement.

    zone "example.org" {
        type slave;
        file "example.org";
    };

In the slave case, the zone information is transferred from the master name server for the particular zone, and saved in the file specified. If and when the master server dies or is unreachable, the slave name server will have the transferred zone information and will be able to serve it.

17.9.6.3 Zone Files

An example master zone file for example.org (existing within /etc/namedb/example.org) is as follows:

    $TTL 3600
    
    example.org. IN SOA ns1.example.org. admin.example.org. (
                            5               ; Serial
                            10800           ; Refresh
                            3600            ; Retry
                            604800          ; Expire
                            86400 )         ; Minimum TTL
    
    ; DNS Servers
    @       IN NS           ns1.example.org.
    @       IN NS           ns2.example.org.
    
    ; Machine Names
    localhost       IN A    127.0.0.1
    ns1             IN A    3.2.1.2
    ns2             IN A    3.2.1.3
    mail            IN A    3.2.1.10
    @               IN A    3.2.1.30
    
    ; Aliases
    www             IN CNAME        @
    
    ; MX Record
    @               IN MX   10      mail.example.org.

Note that every hostname ending in a ``.'' is an exact hostname, whereas everything without a trailing ``.'' is referenced to the origin. For example, www is translated into www + origin. In our fictitious zone file, our origin is example.org., so www would translate to www.example.org.

The format of a zone file follows:

    recordname      IN recordtype   value

The most commonly used DNS records:

SOA

start of zone authority

NS

an authoritative name server

A

A host address

CNAME

the canonical name for an alias

MX

mail exchange

PTR

a domain name pointer (used in reverse DNS)

    example.org. IN SOA ns1.example.org. admin.example.org. (
                            5               ; Serial
                            10800           ; Refresh after 3 hours
                            3600            ; Retry after 1 hour
                            604800          ; Expire after 1 week
                            86400 )         ; Minimum TTL of 1 day
example.org.

the domain name, also the origin for this zone file.

ns1.example.org.

the primary/authoritative name server for this zone

admin.example.org.

the responsible person for this zone, email address with @ replaced. ( becomes admin.example.org)

5

the serial number of the file. this must be incremented each time the zone file is modified. Nowadays, many admins prefer a yyyymmddrr format for the serial number. 2001041002 would mean last modified 04/10/2001, the latter 02 being the second time the zone file has been modified this day. The serial number is important as it alerts slave name servers for a zone when it is updated.

    @       IN NS           ns1.example.org.

This is an NS entry. Every name server that is going to reply authoritatively for the zone must have one of these entries. The @ as seen here could have been example.org. The @ translates to the origin.

    localhost       IN A    127.0.0.1
    ns1             IN A    3.2.1.2
    ns2             IN A    3.2.1.3
    mail            IN A    3.2.1.10
    @               IN A    3.2.1.30

The A record indicates machine names. As seen above, ns1.example.org would resolve to 3.2.1.2. Again, the origin symbol, @, is used here, thus meaning example.org would resolve to 3.2.1.30.

    www             IN CNAME        @

The canonical name record is usually used for giving aliases to a machine. In the example, www is aliased to the machine addressed to the origin, or example.org (3.2.1.30). CNAMEs can be used to provide alias hostnames, or round robin one hostname among multiple machines.

    @               IN MX   10      mail.example.org.

The MX record indicates which mail servers are responsible for handling incoming mail for the zone. mail.example.org is the hostname of the mail server, and 10 being the priority of that mail server.

One can have several mail servers, with priorities of 3, 2, 1. A mail server attempting to deliver to example.org would first try the highest priority MX, then the second highest, etc, until the mail can be properly delivered.

For in-addr.arpa zone files (reverse DNS), the same format is used, except with PTR entries instead of A or CNAME.

    $TTL 3600
    
    1.2.3.in-addr.arpa. IN SOA ns1.example.org. admin.example.org. (
                            5               ; Serial
                            10800           ; Refresh
                            3600            ; Retry
                            604800          ; Expire
                            3600 )          ; Minimum
    
    @       IN NS   ns1.example.org.
    @       IN NS   ns2.example.org.
    
    2       IN PTR  ns1.example.org.
    3       IN PTR  ns2.example.org.
    10      IN PTR  mail.example.org.
    30      IN PTR  example.org.

This file gives the proper IP address to hostname mappings of our above fictitious domain.

17.9.7 Caching Name Server

A caching name server is a name server that is not authoritative for any zones. It simply asks queries of its own, and remembers them for later use. To set one up, just configure the name server as usual, omitting any inclusions of zones.

17.9.8 Running named in a Sandbox

Contributed by Mike Makonnen.

For added security you may want to run named(8) in a sandbox. This will reduce the potential damage should it be compromised. If you include a sandbox directory in its command line, named will chroot(8) into that directory immediately upon finishing processing its command line. It is also a good idea to have named run as a non-privileged user in the sandbox. The default FreeBSD install contains a user bind with group bind. If we wanted the sandbox in the /etc/namedb/sandbox directory the command line for named would look like this:

     # /usr/sbin/named -u bind -g bind -t /etc/namedb/sandbox <path_to_named.conf>

The following steps should be taken in order to successfully run named in a sandbox. Throughout the following discussion we will assume the path to your sandbox is /etc/namedb/sandbox

There are some issues you need to be aware of when running named in a sandbox.

named(8) can be started in a sandbox properly, if the following is in /etc/rc.conf:

    named_flags="-u bind -g bind -t /etc/namedb/sandbox <path_to_named.conf>"

17.9.9 How to Use the Name Server

If setup properly, the name server should be accessible through the network and locally. /etc/resolv.conf must contain a name server entry with the local IP address so it will query the local name server first.

To access it over the network, the machine must have the name server's IP address set properly in its own name server configuration options.

17.9.10 Security

Although BIND is the most common implementation of DNS, there is always the issue of security. Possible and exploitable security holes are sometimes found.

It is a good idea to subscribe to CERT and freebsd-announce to stay up to date with the current Internet and FreeBSD security issues.

Tip: If a problem arises, keeping sources up to date and having a fresh build of named would not hurt.

17.9.11 Further Reading

BIND/named manual pages: ndc(8) named(8) named.conf(5)

This, and other documents, can be downloaded from ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/.

For questions about FreeBSD, read the documentation before contacting <questions@FreeBSD.org>.
For questions about this documentation, e-mail <doc@FreeBSD.org>.