How to install and configure DNS on Ubuntu

DNS makes it easy remembering the IP. Name servers are the computers which run the DNS. Let's setup one.

DNS is a short abbreviation for Domain Name Service which maps the IP and FQDN (Fully Qualified Domain Names) to one another. And by that, the DNS makes it easy to remember the IP. Name servers are the computers that run the DNS.

So in this tutorial, we are going to install and configure DNS on Ubuntu. Through this tutorial, we will use one of the most common programs used for handling the name server on Ubuntu that is BIND (which is an abbreviation for Berkley Internet Naming Daemon).

Install and configure DNS on Ubuntu

Before starting the installation process, please ensure that your system is updated by executing the next three commands.

Step 1- Update System

sudo apt-get update 

sudo apt-get upgrade 

sudo apt-get dist-upgrade

Step 2 – Install DNS package

Use the following command:

sudo apt-get install bind9

Once you execute the previous command it will suggest some other packages to be installed, press y to confirm downloading and installing those packages.

Install DNS
Install DNS

Step 3 – Install DNS Utilities

Another useful package that will help you a lot in troubleshooting and testing the DNS issues is the dnsutils package that can be installed using the next command.

sudo apt-get install dnsutils

Note that you may find it installed already.

Step 4 – DNS Configuration

Usually, you can find the DNS configuration files stored in /etc/bind directory. /etc/bind/named.conf is the master configuration file that contains the DNS options and it’s highly recommended that you should be careful while editing it.

Step 5 – Configuring NameServer

The most used and default configuration is using your server as a caching server. This means that the DNS will get the answer to name queries, cache it and use the answer again when the domain is queried for another time. So, to use your server as a caching nameserver you can follow the next few steps.

Open and edit the /etc/bind/named.conf.options with your favorite editor.

sudo vi /etc/bind/named.conf.options

Add the following block to it, here we have used Google’s DNS.
forwarders {
8.8.8.8;
};

The file should look like this:

To enable the new configurations you should restart the DNS service.

sudo systemctl restart bind9

To test your query time we can use the dig command which is installed by the dnsutils package.

dig google.com

Execute the previous command twice and check for the query time, the output should look like that:

Test Query Time for Google
Test Query Time for Google

Test Query Time for Google (2nd Time)
Test Query Time for Google (2nd Time)

You will notice that the query time for the second time you had executed the command is nearly zero.

Step 6 – Primary Master

For a primary master server configuration, the DNS gets the data for a zone from a file stored on its host. Also, the DNS has control for that zone. Now let’s say we have a domain called “example.com” we are going to configure the DNS to be the primary master for that domain.

Forward Zone File

Here in the forward zone, the name will map to the IP.

Step 1. Open and edit the /etc/bind/named.conf file.

sudo vi /etc/bind/named.conf

Ensure that it contains the following lines and NOT commented:

include “/etc/bind/named.conf.options”;
include “/etc/bind/named.conf.local”;
include “/etc/bind/named.conf.default-zones”;

The file should look like that:

DNS Configuration File
DNS Configuration File

Step 2. Open and edit the /etc/bind/named.conf.local file to add a DNS zone.

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

Add the following block to it:
zone “example.com” {
type master;
file “/etc/bind/db.example.com”;
};

The file should look like this:

Edit local conf file
Edit local conf file

Step 3. Create a zone file from the template one.

sudo cp /etc/bind/db.local /etc/bind/db.example.com

Step 4. Now open the new example zone file.

sudo vi /etc/bind/db.example.com

And change it to look like this:

Zone File
Zone File

Please note that you have to increase the Serial Number every time you make changes to the zone files.

Step 5. Restart DNS Service to apply changes.

sudo systemctl restart bind9

Reverse Zone File

Now to map an IP to a name you have to configure the reverse zone file.

Step 1. Edit the /etc/bind/named.conf.local file.

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

Add the following block:
zone “10.0.2.in-addr.arpa” {
type master;
file “/etc/bind/db.10”;
};

Where the 10.0.2 is the first three octets of your network.

Step 2. Create the  /etc/bind/db.10 file from template one.

sudo cp /etc/bind/db.127 /etc/bind/db.10

Step 3. Edit the /etc/bind/db.10 file.

sudo vi /etc/bind/db.10

And it should be like this:

Reserve Zone File
Reserve Zone File

Step 4. Restart DNS Service to apply changes.

Step 7 – Configuration Files Verification

Now and after performing all the previous configurations we need to verify all the configurations are correct.

Step 1. Execute the following commands to check if it will return any errors.

named-checkzone example.com /etc/bind/db.example.com 

named-checkzone 192.168.0.0/32 /etc/bind/db.10 

named-checkconf  /etc/bind/named.conf.local 

named-checkconf  /etc/bind/named.conf

The output of the previous commands should look like this:

Note that you may have a different serial number, so do not panic.

Finally, we have installed and configured the DNS server on Ubuntu successfully. I hope you enjoyed it and for any questions just leave a comment and we will be glad to help you.

Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

4 COMMENTS

  1. I have 2 name servers

    ns1.example.com
    ns2.example.com
    they are both in same IP addresses

    is there any differences on db.example.com and db.10 files? or just do all like your example?

  2. db.example.com – this is db.<your domainname of forward zone(watch this manual in step6 a step2).domain(as .com)>
    BUT db.10 this is a IP of FORWARD ZONE . (here in step 6 a step 2 reverse zone config)

  3. hello,
    how are you?..
    Should I register somewhere else for DNS service?

    i have one problem. when I add in DNS Management
    ns1.example.com on goddady.com , there show me this error “Duplicate nameserver provided.” why?

    thanks
    siva

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,533FansLike
366FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Top 10 Reasons to use Xfce as your Desktop Environment

There are many choices for desktop environments for Linux based operating systems. Mainly, you can install any DE of your choice on most of the Linux based distributions, even if they are not offered as a package officially. In our recent articles, we discussed the best of KDE and Cinnamon. In this article, we wish to present to you the top reasons why you should consider Xfce as your desktop environment.

The 10 Best Linux Network Monitoring Tools

Having total control over your network is essential to prevent programs from overusing your network resources and slowing down the overall performance. This is why you should install a network monitoring tool on your system, giving you a visual overview of everything that's happening on your network. Networking Tools are like swiss-knife for the system administrators for troubleshooting system issues.

How to install CouchPotato on Ubuntu

Downloading movies and copying them over to your home server can get frustrating, especially if you are doing it daily! What if you have an option to download videos automatically, quickly, and above all with excellent quality. CouchPotato allows you to download movies easily once they are available and released automatically.

Top 20 Rsync Examples in Linux

The Rsync (remote sync) command is a Linux/Unix utility used to synchronize and copy files and directories either locally or remotely. Most Linux geeks use Rsync to mirror, backup or migrate data across folders, across disks and networks. One notable feature with the Rsync command is that it uses the "delta transfer algorithm."

Setting up NFS Server on Ubuntu Server

We have put together a detailed step-by-step tutorial that will guide you on how to install and set up NFS Server on Ubuntu so you too can start sharing data with other client systems. It is divided into sections, the first part discussing how to set up the host server and the second part showing you how to set up the client-side. For this tutorial, we will be using Ubuntu 20.04 LTS.

Crontab in Linux Explained With Examples

Crontab is the Linux equivalent of the Window's Task Scheduler. It can help you set up a task to run automatically at a regular...

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

CopyQ – Advanced clipboard manager for Linux

We have all had that moment when we copied a text, but we first needed another one, and in the process, lost the first one. If you're lucky, you can get it back quickly with a bit of work.

5 Things to do when your Linux system GUI freezes

All operating systems freeze at some point and, Linux distributions are no exceptions. In this post, we are going to look at five methods you can apply to your frozen system before thinking of plugging out the power cable or holding down the power button to power off the PC.

5 Best Notepad++ Alternatives for Linux

Notepad++ has been the de facto standard for source code editors for nearly 16 years, almost since its creation in 2003.  For Windows users, that is.  For years, Linux users had no source code editor that compared to Notepad++ with all its bells and whistles, such as code folding, scripting, markup languages, syntax highlighting, auto-completion for programming (limited).

How to install Android Studio on Ubuntu

Android Studio, like the name suggests, is a full-featured cross-platform IDE that enables you to develop Android applications to run on any android device. Based on JetBrains' IntelliJ IDEA, Android Studio comes installed with all the necessary features needed for Android development.