How to Setup DNS (Bind) Server on CentOS/RHEL

The Domain name system use to translate domain names to IP address and vice versa.For example when visitors go to your domain name like,“http://cpanelplesk.com”, Our computer sends a request to DNS and get an IP address of domain.

In This article  we will help you to setup DNS server on CentOS and RedHat systems.

Install Bind Packages

To install package simple execute below command:

yum install bind bind-chroot

Edit Main Configuration File

Now we have to edit the main configuration file, Bind default configuration file is located under /etc directory. But in chroot environment this file is located at /var/named/chroot/etc directory. Now edit the file and update content as below.

Content for the named.conf file is as follows:

// /var/named/chroot/etc/named.conf
options {
        listen-on port 53 { 127.0.0.1; 192.168.1.0/24; 0.0.0.0/0; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { localhost; 192.168.1.0/24; 0.0.0.0/0; };
        recursion yes;

        dnssec-enable yes;
        dnssec-validation yes;
        dnssec-lookaside auto;

        /* Path to ISC DLV key */
        bindkeys-file "/etc/named.iscdlv.key";

        managed-keys-directory "/var/named/dynamic";
};

logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};

zone "." IN {
        type hint;
        file "named.ca";
};

zone "cpanelplesk.com" IN {
        type master;
        file "/var/named/cpanelplesk.com.db";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

Create Zone File for Your Domain

After creating bind configuration file, Now we have to create a zone file for your domain as per configuration, for example we are using cpanelplesk.com.db in this article.

nano /var/named/chroot/var/named/cpanelplesk.com.db

File content is as follows:

Zone file for cpanelplesk.com
$TTL 14400
@      86400    IN      SOA     ns1.cpanelplesk.com. webmaster.cpanelplesk.com (
                3013040200      ; serial, todays date+todays
                86400           ; refresh, seconds
                7200            ; retry, seconds
                3600000         ; expire, seconds
                86400          ; minimum, seconds
      )
demotecadmin.net. 86400 IN NS ns1.cpanelplesk.com.
demotecadmin.net. 86400 IN NS ns2.cpanelplesk.com.
demotecadmin.net. IN A 192.168.1.100
demotecadmin.net. IN MX 0 mail.cpanelplesk.com.
mail 			  IN CNAME cpanelplesk.com.
www 			  IN CNAME cpanelplesk.com.

If you have more domains,You can create zone file for those domains individually.

Add More Domains

To add more domains in DNS, create zone files individually for all domain after that add an entry for all zones in named.conf like as above.

Start Bind Service

Start named service using following command:

service named restart

Now enable auto start on system boot:

chkconfig named on

Test Your DNS Setup

Now test your DNS server using below command:

nslookup cpanelplesk.com 5.9.105.171
Server:  5.9.105.171
Address:  5.9.105.171
Name:    cpanelplesk.com
Address:  5.9.105.171

So we have successfully set up a DNS server.