How to install DNS on CentOS


For the sole purpose of the “show and tell” we’ll be running through the DNS server installation process. What we’ll be doing here is installing the BIND 9 service on the CentOS machine and configuring it to serve as a domain resolver. Let’s check out how to install the DNS on CentOS!

Specification:

OS Version: CentOs 8.2 x64
  • 12.13.14.15: Example local network IP
  • 13.14.15.16: Example CentOS DNS server IP we are setting up with BIND9
  • 14.15.16.17: Example IP our test domain should resolve to.

First, make sure you have the static ip address setup on your CentOS box! Now, let’s start building this!

Update DNF package manager:

$ sudo dnf makecache

When DNf is updated we can proceed to installing the BIND 9 service:

$ sudo dnf install bind

BIND 9 service has been installed now! However, let’s not firing it up just yet. Let’s first get to know the BIND as an environment and then after we configure it we’ll start it and test it.

Configuration of the BIND 9 on CentOS

Configuration of the BIND9 service goes through three related steps:

  • Creating a DNS database for specific domains we want this DNS to resolve
  • Creating DNS zone file for each domain (similar to virtual hosts in Web servers)
  • Enable DNS zone file in the main configuration file

Creating DNS database

All DNS database files are located at /var/named/ directory. So, for each domain we want to resolve, this is where the database file should be placed. Let’s create a database file for the test domain bluegrid.awesome:

$ nano /var/named/db.bluegrid.awesome

and add the following configuration in it (replace your own ip addresses and domains accordingly):

$TTL 1d
$ORIGIN bluegrid.awesome.
@    IN   SOA  ns   root (
          2020031201 ; Serial
          12h        ; Refresh
          15m        ; Retry
          3w         ; Expire
          2h         ; Minimum
)
@    IN   A    14.15.16.17
@    IN   NS   ns
ns   IN   A    14.15.16.17
@    IN   MX   10   mail
mail IN   A    14.15.16.17
www  IN   A    14.15.16.17
ftp  IN   CNAME www

Let’s check the syntax for any errors we might have in the configuration above:

$ sudo named-checkzone bluegrid.awesome /var/named/db.bluegrid.awesome 
zone bluegrid.awesome/IN: loaded serial 2020031201
OK

Looks good! Now zone files!

Creating DNS zone file

DNS zone files (equivalent to web server virtual host files) are located at /etc/named/ directory. This is where wee hook to the DNS database file. For example, let’s open/create a zone file called bluegrid.awesome.zones

$ sudo nano /etc/named/bluegrid.awesome.zones

and add the following configuration in there (change to your own test domain accordingly):

zone "bluegrid.awesome" IN {
    type master;
    file "db.bluegrid.awesome";
};

Almost done!

Enable DNS zone file in main configuration file

Now, few steps to allow queries to this DNS server from your network or IP address you want to query this server. Open /etc/named.conf and add your network subnet under allow-query like this (we have used example IP 12.13.14.0/24, you should use your own):

options {
	listen-on port 53 { 127.0.0.1; };
        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";
        secroots-file   "/var/named/data/named.secroots";
        recursing-file  "/var/named/data/named.recursing";
        allow-query     { localhost; 12,13,14,15/24};

Now, add the IP address of your CentOS machine under listen-on like this (we have used the test IP 13.14.15.16, you should use your own):

options {
	listen-on port 53 { 127.0.0.1; 13.14.15.16; };
        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";
        secroots-file   "/var/named/data/named.secroots";
        recursing-file  "/var/named/data/named.recursing";
        allow-query     { localhost; 12.13.14.0/24; };

And almost there! Let’s load the zone file in the /etc/named.conf. Add the line include "/etc/named/bluegrid.awesome.zones"; (or however, you named it) to the very end of the /etc/named.conf file:

/etc/named.conf | include DNs zone file
/etc/named.conf | include DNs zone file

Save the change and exit. Now, let’s fire up the named service:

$ sudo systemctl start named

Enable auto start on boot:

$ sudo systemctl enable named

That’s it, fokks! Now, let’s test it 😁:

$ dig @13.14.15.16 bluegrid.awesome all

; <<>> DiG 9.11.13-RedHat-9.11.13-5.el8_2 <<>> @13.14.15.16 bluegrid.awesome all
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65127
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: b22de5f3b4d14680b0c9aa2e5f35afcec89e9a0395a3b9e2 (good)
;; QUESTION SECTION:
;bluegrid.awesome.		IN	A

;; ANSWER SECTION:
bluegrid.awesome.	86400	IN	A	14.15.16.17

;; AUTHORITY SECTION:
bluegrid.awesome.	86400	IN	NS	ns.bluegrid.awesome.

;; ADDITIONAL SECTION:
ns.bluegrid.awesome.	86400	IN	A	14.15.16.17

;; Query time: 0 msec
;; SERVER: 13.14.15.16#53(13.14.15.16)
;; WHEN: Thu Aug 13 21:25:34 UTC 2020
;; MSG SIZE  rcvd: 122

;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 34269
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: b22de5f3b4d14680cff3a94c5f35afce2aa522b7b750628d (good)
;; QUESTION SECTION:
;all.				IN	A

;; AUTHORITY SECTION:
.			10800	IN	SOA	a.root-servers.net. nstld.verisign-grs.com. 2020081302 1800 900 604800 86400

;; Query time: 30 msec
;; SERVER: 13.14.15.16#53(13.14.15.16)
;; WHEN: Thu Aug 13 21:25:34 UTC 2020
;; MSG SIZE  rcvd: 135

Looking good!

To see it in action we can simply define our newly created DNS server to be used by our local machine. I test this solution on a Linux machine so, to use custom DNS I will add this line “nameserver 13.14.15.16” (replace test IP 13.14.15.16) with above CentOS DNS server, you just created) in a file /etc/resolv.conf.

Now, opening the http://bluegrid.awesome in browser I can see my test website loading (you might have different ways of testing this solution though):

DNS resolution test for bluegrid.awesome
DNS resolution test for bluegrid.awesome

Voila! Thank you for your attention on this short journey 😁!

Share this post

Share this link via

Or copy link