How To Configure DNS Server On Ubuntu 18.04 / Ubuntu 16.04

Domain Name System (DNS) is the root of the internet that translates the domain name to IP Address and vice versa. BIND9 (Berkeley Internet Name Domain) package provides the functionality of the name to IP conversion.
This post will guide you to configure DNS server on Ubuntu 18.04 / Ubuntu 16.04.
Environment
Domain Name: itzgeek.local | ||
---|---|---|
ns1.itzgeek.local | 192.168.0.10 | Master DNS Server |
Setup DNS Server on Ubuntu 18.04 / Ubuntu 16.04
Prerequisites
Update the repository index.
sudo apt update
Make sure the DNS server has a static IP address.
READ: How to configure static IP address in Ubuntu 18.04 / Ubuntu 16.04 using ifupdown
If you are using Netplan – a new network tool for configuring networking in Ubuntu 18.04, then.
READ: How To Configure Static IP Address in Ubuntu 18.04 using Netplan
Install DNS Server
The package name for the DNS server on Ubuntu is bind9 and is available in the base repository. Use the apt command to install the bind9 package.
sudo apt install -y bind9 bind9utils bind9-doc dnsutils
Configure DNS Server
The /etc/bind/ directory is the main configuration directory of the DNS server, and it holds configuration files and zone lookup files.
Global configuration file is /etc/bind/named.conf. You should not use this file for your local DNS zone rather you can use /etc/bind/named.conf.local file.
Create Zones
Let us begin by creating a forward zone for your domain.
sudo nano /etc/bind/named.conf.local
Forward Zone
The following is the forward zone entry for the itzgeek.local domain in the named.conf.local file.
zone "itzgeek.local" IN { // Domain name type master; // Primary DNS file "/etc/bind/forward.itzgeek.local.db"; // Forward lookup file allow-update { none; }; // Since this is the primary DNS, it should be none. };
Reverse Zone
The following entries are for the reverse zone in the named.conf.local file.
zone "0.168.192.in-addr.arpa" IN { //Reverse lookup name, should match your network in reverse order type master; // Primary DNS file "/etc/bind/reverse.itzgeek.local.db"; //Reverse lookup file allow-update { none; }; //Since this is the primary DNS, it should be none. };
Create Zone lookup file
Once you create zones, you can go ahead and create zone data files that hold DNS records for the forward zone and reverse zone.
Forward Zone lookup file
Copy the sample entries to zone file called forward.itzgeek.local.db for the forward zone under /etc/bind directory.
Record types in the zone file,
SOA – Start of Authority
NS – Name Server
A – A record
MX – Mail for Exchange
CN – Canonical Name
Domain names should end with a dot (.).
sudo cp /etc/bind/db.local /etc/bind/forward.itzgeek.local.db
Edit the zone.
sudo nano /etc/bind/forward.itzgeek.local.db
Update the content shown below.
$TTL 604800 @ IN SOA ns1.itzgeek.local. root.itzgeek.local. ( 3 ; Serial 604800 ; Refresh 86400 ; Retry 2419200 ; Expire 604800 ) ; Negative Cache TTL ; ;@ IN NS localhost. ;@ IN A 127.0.0.1 ;@ IN AAAA ::1 ;Name Server Information @ IN NS ns1.itzgeek.local. ;IP address of Name Server ns1 IN A 192.168.0.10 ;Mail Exchanger itzgeek.local. IN MX 10 mail.itzgeek.local. ;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.itgeek.local.
Reverse Zone lookup file
Copy the sample entries to the zone file called reverse.itzgeek.local.db for the reverse zone under /etc/bind directory and create reverse pointers for the above forward zone records.
PTR – Pointer
SOA – Start of Authority
sudo cp /etc/bind/db.127 /etc/bind/reverse.itzgeek.local.db
Edit the reverse zone file.
sudo nano /etc/bind/reverse.itzgeek.local.db
Update the content shown below.
$TTL 604800 @ IN SOA itzgeek.local. root.itzgeek.local. ( 3 ; Serial 604800 ; Refresh 86400 ; Retry 2419200 ; Expire 604800 ) ; Negative Cache TTL ; ;@ IN NS localhost. ;1.0.0 IN PTR localhost. ;Name Server Information @ IN NS ns1.itzgeek.local. ;Reverse lookup for Name Server 10 IN PTR ns1.itzgeek.local. ;PTR Record IP address to HostName 100 IN PTR www.itzgeek.local. 150 IN PTR mail.itzgeek.local.
Check BIND Configuration Syntax
Use named-checkconf command to check the syntax and named.conf* files for any errors.
sudo named-checkconf
Command will return to the shell if there are no errors.
Also, you can use named-checkzone to check the syntax errors in zone files.
Forward zone
sudo named-checkzone itzgeek.local /etc/bind/forward.itzgeek.local.db
Output:
zone itzgeek.local/IN: loaded serial 3 OK
Reverse zone
named-checkzone 0.168.192.in-addr.arpa /etc/bind/reverse.itzgeek.local.db
Output:
zone 0.168.192.in-addr.arpa/IN: loaded serial 3 OK
Restart bind service.
sudo systemctl restart bind9
Enable it on system startup.
sudo systemctl enable bind9
Check the status of the bind9 service.
sudo systemctl status bind9
DNS Record Update
Whenever you change a DNS record, do not forget to change the serial number in the zone file and reload the zone.
Change itzgeek.local & 0.168.192.in-addr.arpa with your zone names.
### Forward Zone ### sudo rndc reload itzgeek.local ### Reverse Zone ### sudo rndc reload 0.168.192.in-addr.arpa
Verify DNS Server
Go to any client machine and add our new DNS server IP Address in /etc/resolv.conf file.
sudo nano /etc/resolv.conf
Make an entry like below.
nameserver 192.168.0.10
OR
Read the below tutorial to set DNS server IP in Linux.
READ: How to Set DNS IP address in CentOS / Fedora
READ: How to Set DNS IP address in Ubuntu / Debian – ifupdown
READ: How To Set DNS IP Address in Ubuntu 18.04 – Netplan
Use the dig command to check the forward zone.
dig www.itzgeek.local
If you get command not found, then install the bind-utils package.
Output:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> www.itzgeek.local
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18022
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.itzgeek.local. IN A
;; ANSWER SECTION:
www.itzgeek.local. 604800 IN A 192.168.0.100
;; AUTHORITY SECTION:
itzgeek.local. 604800 IN NS ns1.itzgeek.local.
;; ADDITIONAL SECTION:
ns1.itzgeek.local. 604800 IN A 192.168.0.10
;; Query time: 0 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Mon Dec 30 12:42:18 EST 2019
;; MSG SIZE rcvd: 96
The DNS server’s answer for the forward lookup: 192.168.0.100 as IP address for www.itzgeek.local.
Confirm the reverse lookup with dig command.
dig -x 192.168.0.100
Output:
; <<>> DiG 9.10.3-P4-Ubuntu <<>> -x 192.168.0.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37122
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;100.0.168.192.in-addr.arpa. IN PTR
;; ANSWER SECTION:
100.0.168.192.in-addr.arpa. 604800 IN PTR www.itzgeek.local.
;; AUTHORITY SECTION:
0.168.192.in-addr.arpa. 604800 IN NS ns1.itzgeek.local.
;; ADDITIONAL SECTION:
ns1.itzgeek.local. 604800 IN A 192.168.0.10
;; Query time: 0 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Mon Dec 30 12:43:20 EST 2019
;; MSG SIZE rcvd: 120
The DNS server’s answer for reverse lookup: www.itzgeek.local as a name for 192.168.0.100.
This result confirms that both zone lookups are working fine.
Conclusion
That’s All. You now have successfully configured DNS server on Ubuntu 18.04 / Ubuntu 16.04 as the master server. In our next post, we will configure a slave DNS server on Ubuntu 18.04 / Ubuntu 16.04.