configure dns server in centos/rhel

How to Configure DNS Server on CentOS/RHEL

DNS (Domain Name System) is a dictionary that maps domain names to IP addresses. It allows you to lookup domain names to IP addresses, as well as do reverse lookups of IP addresses to get domain names, in some cases. DNS server contains database of public IP addresses and domain names for lookups and domain name resolution. It reduces domain lookup time and improves browsing speed. In this article, we will learn how to configure DNS server on CentOS/RHEL.


How to Configure DNS Server on CentOS/RHEL

Here are the steps to configure DNS server on CentOS/RHEL. Our server name is ns1.example.com and IP address is 192.168.0.1


1. Install BIND

BIND (Berkeley Internet Name Domain) is a software that allows you to get IP address from domain names. Open terminal and run the following command to install it.

# yum -y install bind bind-utils


2. Configure BIND

By default BIND listens to localhost. We will change it to listen to an IP address so that other systems can communicate with it. Open its configuration file in a text editor.

# vi /etc/named.conf

Listen to All IP Addresses

Comment the following lines by adding // at their beginning as shown, to listen to all IP addresses.

// listen-on port 53 { 127.0.0.1; };
// listen-on-v6 port 53 { ::1; };

Listen to IP Address

If you want BIND to listen to specific IP address, add the following line instead.

listen-on port 53 { 127.0.0.1; 192.168.0.1; };

Next add your network in the configuration file so that BIND will allow queries from your network. Replace 192.168.0.0/24 with your network’s CIDR.

allow-query     { localhost; 192.168.0.0/24; };


3. Create Zones

A DNS zone is a specific part of the DNS namespace managed by administrator. We will define forward and backward zones for DNS Server.

Open DNS configuration file in a text editor.

# vi /etc/named.conf

Add the following lines to configure forward zones. Replace example.com with your domain name.

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

         allow-update { none; };
};

example.com – Domain name
master – Primary DNS
example.com.db – Forward lookup file
allow-update – Since this is the primary DNS, it should be none

Next add the following lines to configure reverse zone.

zone "0.168.192.in-addr.arpa" IN {
          
          type master;
          
          file "/var/named/192.168.0.db";
         
          allow-update { none; };
};

0.168.192.in-addr.arpa – Reverse lookup name
master – Primary DNS
192.168.0.db – Reverse lookup file
allow-update – Since this is the primary DNS, it should be none


4. Create Zone Files

By default, zone files are present at /var/named folder. We will create a file for forward lookup at /var/named/example.com.db

Please note, the file name & path should be the same as that used in your forward zone configuration in previous step.

Add the following lines to it.

@   IN  SOA     ns1.example.com root.example.com. (
                                                1001    ;Serial
                                                3H      ;Refresh
                                                15M     ;Retry
                                                1W      ;Expire
                                                1D      ;Minimum TTL
                                                )

;Name Server Information
@      IN  NS      ns1.example.com.

;IP address of Name Server
ns1 IN  A       192.168.0.1

;Mail exchanger
example.com. IN  MX 10   mail.example.com.

;A - Record HostName To IP Address
www     IN  A       192.168.0.100
mail    IN  A       192.168.0.150

;CNAME record
ftp     IN CNAME        www.example.com.

A – A record
NS – Name Server
MX – Mail for Exchange
CNAME – Canonical Name

Next, create a zone file for reverse zone at /var/named/192.168.0.db

# vi /var/named/192.168.0.db

Add the following lines to it.

@   IN  SOA     ns1.example.com. root.example.com. (
                                                1001    ;Serial
                                                3H      ;Refresh
                                                15M     ;Retry
                                                1W      ;Expire
                                                1D      ;Minimum TTL
                                                )

;Name Server Information
@ IN  NS      ns1.example.com.

;Reverse lookup for Name Server
10        IN  PTR     ns1.example.com.

;PTR Record IP address to HostName
100      IN  PTR     www.example.com.
150      IN  PTR     mail.example.com.

PTR – Pointer
SOA – Start of Authority

Once the zone files are created, restart the bind service.

# systemctl restart named

Also, enable it to autostart on system reboot.

# systemctl enable named


5. Update Firewall

Add the following firewall rules so that clients can connect to your DNS server.

# firewall-cmd --permanent --add-port=53/udp

# firewall-cmd --reload


6. Verify DNS Server

Go to any client machine, and open the following file in text editor.

# sudo vi /etc/resolv.conf

Add the following line to it.

nameserver 192.168.0.1

Save and close the file.

If you are using a Network Manager, open the following file in text editor.

# sudo vi /etc/sysconfig/network-scripts/ifcfg-eXX

Add the following line.

DNS1=192.168.0.10

Restart Network Manager.

# systemctl restart NetworkManager

Install dig utility with the following command.

# yum install -y bind-utils

You can check the DNS lookup with the following command.

# dig www.example.com

and check the reverse lookup with the following command.

# dig -x 192.168.0.100

In this article, we have learnt how to configure DNS server. You can use these steps in SUSE/Fedora Linux also.

Also read:

Python Delete Dictionary Key While Iterating
Python Remove Item from List While Iterating
How to Rename File in Python
How to Uninstall Java in Ubuntu
How to Format USB Drives in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *