Home / Archive

DNS Server in Ubuntu / Debian

Local DNS Server in Ubuntu Debian

What is a DNS

DNS stands for Domain Name System and its a service that associates domain names with ip addresses. Let me try to explain this a bit more.Since you are reading this article it means that you have typed my domain name (www.allaboutlinux.eu) or you found that link on another website. but when you are typing www.allaboutlinux.eu, your computer is not really able to know where this website is hosted and of course  computers are only good with numbers. So your computer will ask the DNS server about a domain name and the DNS server will reply with an ip address. Then your computer will contact that ip and hopefully the server behind that ip will reply. Enough with that lets start with the setup of the DNS server.

What you will need:

bind9 will be used in this tutorial and this one is the most widely used DNS in the world. BIND stands for Berkeley Internet Name Domain and was initially a project of 4 graduate students at the Computer Research Group at the University of California, Berkeley. Lets say now now that i have my DNS server (the one that we create now) at 172.16.10.1, my sql server at “172.16.10.12”, my Apache server at “172.16.10.15”, my file server at “172.16.10.17”, my router at “172.16.10.254” and my computer at 172.16.0.101 all of them are at /24. so my network is working but i have to remember all the ips in order to connect to each server. and as my company growth i will have more servers and more workstations and that is making things more complicated. it would be easier if I had a way to connect to my sql server by just providing the name and not the ip and that is exactly what we will do here. Open a terminal and type the following commands.

sudo apt-get update

sudo apt-get install bind9

Now we need to define a name for our local zone. I will name this one “allaboutlinux.local” but you can choose whatever you want. more zones are also possible. All files that you will need to configure are in “/etc/bind/”. lets define the new zone. Open the /etc/bind/named.conf.default-zones

sudo gedit /etc/bind/named.conf.default-zones

and add the following lines at the end of the document:

zone “allaboutlinux.local” {
type master;
file “/etc/bind/db.allaboutlinux.local”;
};

Lets create the db that will have the information for our zone. as we defined that in the previous step that will be /etc/bind/db.allaboutlinux.local

sudo gedit /etc/bind/db.allaboutlinux.local

Add the following lines but configure the ips and names to fit your needs.

;
; BIND data file for allaboutlinux.local
; Filename: /etc/bind/db.allaboutlinux.local
;
$TTL    604800
@       IN      SOA     ns.allaboutlinux.local   root.allaboutlinux.local. (
                              1         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      ns.allaboutlinux.local.
ns      IN      A       172.16.10.1

;also list other computers
DNS     IN      A       172.16.10.1
sql     IN      A       172.16.10.12
Apache  IN      A       172.16.10.15
filesrv IN      A       172.16.10.17
router  IN      A       172.16.10.254

save, exit and restart bind9:

sudo service bind9 restart

That was it. The DNS is up and running for you local network and you can add the ip of your dns in all computers in your network. There is one last thing that we need to do since your dns is able to convert names into ips in your network but not outside of that. So if you want to visit allaboutlinux.eu most likely you will get an error that the name cannot be resolved. To fix that we need to setup some forwarders so our dns server will check his own database and if he cannot resolve the provided name it will forward this request to another dns. open the following file:

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

Add the following lines at the end of the document save and close:

forwarders {
8.8.8.8;
8.8.4.4;
172.16.10.254;
};

In this example I’m using the public dns of google and as 3rd the ip of my router that points to the dns of my ISP. On very change that you are doing in the config files of your dns dont forget to restart the service.

Now lets test our dns server. open a terminal and type:

nslookup apache.allaboutlinux.local 172.16.10.254

Actually i’m asking here the ip of apache.allaboutlinux.local by using the dns 172.16.10.254. and that should reply something like:

nslookup apache.allaboutlinux.local
Server:         172.16.10.254
Address:        172.16.10.2544#53

Name:   apache.allaboutlinux.local
Address: 172.16.10.15