How to Set up DNS Server on Ubuntu 22.04 / Ubuntu 20.04

A domain Name System is an internet service used to resolve a domain name to IP Address and vice versa. This DNS service relieves us from remembering IP addresses to reach any services over the internet or intranet.

Here, we will see how to set up DNS server on Ubuntu 22.04 / Ubuntu 20.04.

Set up Environment

Let us create a DNS server for the itzgeek.local domain. You may also change it as per your requirement.

Domain Name: itzgeek.local

DNS Server Name: ns.itzgeek.local

IP Address: 192.168.0.10

Additionally, you need to ensure sure the DNS server has a static IP address.

READ: How to configure static ip address in Ubuntu 22.04/20.04

Install DNS Server

First, update the repository index.

sudo apt update

The package name of the DNS server in the Ubuntu operating system is bind9. It is available in the base OS repository. So, you can use the apt command to install the bind9 package along with other utility packages.

sudo apt install -y bind9 bind9-utils

Creating DNS Zones and Zone Files

Creating DNS Zones

The /etc/bind/ is the configuration directory for the DNS server that holds configuration files and zone files. The global configuration file for DNS server is /etc/bind/named.conf.

For the zone creations, use the /etc/bind/named.conf.local file instead of the global configuration file.

Let us begin by creating DNS zones for your domain.

sudo nano /etc/bind/named.conf.local

Creating Forward Zone

The following is the forward zone entry for the itzgeek.local domain in the named.conf.local file. This forward zone translates a fully qualified domain name (FQDN) into an IP address.

zone "itzgeek.local" IN { // Domain name

     type master; // Primary DNS

     file "/etc/bind/itzgeek.local.db"; // Forward Zone file

     allow-update { none; }; // Since this is the primary DNS, it should be none.

};

Creating Reverse Zone

The following is for the reverse name resolution zone in the named.conf.local file. This forward zone translates an IP address into a fully qualified domain name (FQDN).

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/r.itzgeek.local.db"; // Reverse lookup file

     allow-update { none; }; // Since this is the primary DNS, it should be none.

};

Creating Zone Files

Once you have created DNS zones, create DNS zone files for the forward and reverse zones.

Creating Forward Zone File

Create a  zone file /etc/bind/itzgeek.local.db for the forward zone.

sudo nano /etc/bind/itzgeek.local.db

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 (.).

Whenever you change any records in the zone file, update the serial number +1 with the current number.
$TTL    86400
@       IN      SOA     ns.itzgeek.local. root.itzgeek.local. (
                         200101         ; Serial
                         21600          ; Refresh
                         3600           ; Retry
                         604800         ; Expire
                         86400 )        ; Negative Cache TTL
;
;Name Server Information

@        IN      NS      ns.itzgeek.local.

;IP address of Name Server

ns      IN      A       192.168.0.10  

;Mail Exchanger

@       IN     MX   10   mail.itzgeek.local.

;A – Record HostName To Ip Address

www     IN       A      192.168.0.101
mail    IN       A      192.168.0.102

;CNAME record

ftp     IN      CNAME   www.itgeek.local.

Creating Reverse Zone File

Create a zone file /etc/bind/r.itzgeek.local.db for the reverse zone.

sudo nano /etc/bind/r.itzgeek.local.db

Update the content as shown below.

PTR – Pointer
SOA – Start of Authority

Whenever you change any records in the lookup file, update the serial number +1 with the current number.
$TTL    86400
@       IN      SOA     ns.itzgeek.local. root.itzgeek.local. (
                         200101         ; Serial
                         21600          ; Refresh
                         3600           ; Retry
                         604800         ; Expire
                         86400 )        ; Negative Cache TTL
;
;Name Server Information

@       IN      NS     ns.itzgeek.local.

;Reverse lookup for Name Server

10      IN      PTR    ns.itzgeek.local.

;PTR Record IP address to HostName

101     IN      PTR    www.itzgeek.local.
102     IN      PTR    mail.itzgeek.local.

Validating DNS Syntax

Use the named-checkconf command to check the syntax of DNS configuration 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.

Validating Forward Zone

sudo named-checkzone itzgeek.local /etc/bind/itzgeek.local.db

Output:

zone itzgeek.local/IN: loaded serial 200101
OK

Validating Reverse Zone

sudo named-checkzone 0.168.192.in-addr.arpa /etc/bind/r.itzgeek.local.db

Output:

zone 0.168.192.in-addr.arpa/IN: loaded serial 200101
OK

Finally, reload both the configuration file and zones. You may also use the same command whenever you change zone and zone files.

sudo rndc reload

Verify DNS Server

Use the dig command to verify the DNS server by looking up records.

dig www.itzgeek.local @192.168.0.10

Output:

; <<>> DiG 9.18.1-1ubuntu1-Ubuntu <<>> www.itzgeek.local @192.168.0.10
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39950
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 4cd7099044f151b6010000006294e2b3015aa1aa44435255 (good)
;; QUESTION SECTION:
;www.itzgeek.local.             IN      A

;; ANSWER SECTION:
www.itzgeek.local.      86400   IN      A       192.168.0.101

;; Query time: 0 msec
;; SERVER: 192.168.0.10#53(192.168.0.10) (UDP)
;; WHEN: Mon May 30 11:28:51 EDT 2022
;; MSG SIZE  rcvd: 90

Confirm the reverse lookup with the dig command.

dig -x 192.168.0.101 @192.168.0.10

Output:

; <<>> DiG 9.18.1-1ubuntu1-Ubuntu <<>> -x 192.168.0.101 @192.168.0.10
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39359
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 0c88a96d72425621010000006294e3c4c0eccc894ea351b7 (good)
;; QUESTION SECTION:
;101.0.168.192.in-addr.arpa.    IN      PTR

;; ANSWER SECTION:
101.0.168.192.in-addr.arpa. 86400 IN    PTR     www.itzgeek.local.

;; Query time: 0 msec
;; SERVER: 192.168.0.10#53(192.168.0.10) (UDP)
;; WHEN: Mon May 30 11:33:24 EDT 2022
;; MSG SIZE  rcvd: 114

This result confirms that both the forward and reverse zone are working fine.

Conclusion

That’s All. I hope you have learned how to set up DNS server on Ubuntu 22.04 / Ubuntu 20.04. In the next article, we will configure the slave DNS server on Ubuntu.

dnsdns serverubuntu 20.04ubuntu 22.04
Comments (0)
Add Comment