How to Setup a DNS Server in Ubuntu
Overview
Would you like to setup a DNS Server in Ubuntu? How about setting up a private internal domain name at home? Well, you’ve come to the right place. There are number of tutorials on the internet showing you how to setup a DNS Server with Ubuntu using Bind 9. So, why another how-to document? That’s a good question. I’ve decided I needed to write a simple tutorial that anyone with a little bit of Linux knowledge would be able to follow. In the process, I hope readers are also able to learn how DNS works. Ok, let’s jump right to it!
What is DNS?
First of all, let’s cover the basics. What is DNS? DNS stands for Domain Name Server. It’s a service that runs on a server that translates humanly recognizable domain names such as www.yahoo.com or www.google.com into its assigned IP addresses. If the DNS server does not recognize the domain name being requested, it will forward the domain name request to another DNS server and so on until the name is resolved.
A typical DNS request is when someone is accessing a website. Let’s use the www.yahoo.com domain as an example. When a user clicks a Yahoo link or types the Yahoo URL on the address bar of the browser, the DNS server processes the domain request. If it doesn’t find www.yahoo.com on its DNS table, it will forward the request to another DNS server with a higher authority and so on until it finds a server with the URL entry. The IP address information is then sent back to the user’s browser. If the domain name is not found, a “server not found” message is displayed on the browser.
Assumptions
Enough with the DNS background. Let’s now start configuring our own DNS server. Let’s assume that we have the following: we want to create a private internal domain name called mydomain.com, our private internal network is 192.168.0.x and our router and gateway is set at 192.168.0.1. Let’s assume all devices are going to be configured with static IP addresses. Normally, most computer systems nowadays are configured to automatically obtain IP addresses from the DHCP server/router. In this example, we will use static IP addresses to show how DNS works. Finally, we have 3 computers connected to our network:
- Ubuntu Server, the DNS server – 192.168.0.9
- Ubuntu Desktop – 192.168.0.10
- PC – 192.168.0.11
Instructions
1. To install the DNS server, we need to install Bind 9.
sudo apt-get install bind9
2. Let’s configure Bind. We need to touch 5 files.
We will edit 3 files.
- /etc/bind/named.conf.local
- /etc/bind/named.conf.options
- /etc/resolv.conf
We will create 2 files.
- /etc/bind/zones/mydomain.com.db
- /etc/bind/zones/rev.0.168.192.in-addr.arpa
A. First step. Lets add our domain zone – mydomain.com.
sudo vi /etc/bind/named.conf.local
# Our domain zone zone "mydomain.com" { type master; file "/etc/bind/zones/mydomain.com.db"; }; # For reverse DNS zone "0.168.192.in-addr.arpa" { type master; file "/etc/bind/zones/rev.0.168.192.in-addr.arpa"; };
Save file. Exit.
We just created a new domain. Please note: later we will create two files named mydomain.com.db and rev.0.168.192.in-addr.arpa files. Also, notice the reverse IP address sequence in the reverse DNS section.
B. Let’s add the DNS servers from your ISP. In my case, I’m using Comcast DNS servers. You can place the primary and secondary DNS servers here separated by semicolons.
sudo vi /etc/bind/named.conf.options
forwarders { 68.87.76.178; };
Save file. Exit.
C. Now, let’s modify the resolv.conf file found in /etc and place the IP address of our DNS server which is set to 192.168.0.9.
$ sudo vi /etc/resolv.conf
search mydomain.com. nameserver 192.168.0.9
D. Now, let’s define the zones.
sudo mkdir /etc/bind/zones sudo vi /etc/bind/zones/mydomain.com.db
$TTL 3D @ IN SOA ns.mydomain.com. admin.mydomain.com. ( 2007062001 28800 3600 604800 38400 ); mydomain.com. IN NS ns.mydomain.com. ubuntudesktop IN A 192.168.0.10 www IN CNAME ubuntudesktop pc IN A 192.168.0.11 gw IN A 192.168.0.1 TXT "Network Gateway"
The TTL or time to live is set for 3 days
The ns.mydomain.com nameserver is defined
ubuntudesktop, pc and gateway are entered as an A record
An alias of www is assigned to ubuntudesktop using CNAME
E. Let’s create a “rev.0.168.192.in-addr.arpa” file for reverse lookup.
sudo vi /etc/bind/zones/rev.0.168.192.in-addr.arpa
$TTL 3D @ IN SOA ns.mydomain.com. admin.mydomain.com. ( 2007062001 28800 604800 604800 86400 ) IN NS ns.mydomain.com. 1 IN PTR gw.mydomain.com. 10 IN PTR ubuntudesktop.mydomain.com. 11 IN PTR pc.mydomain.com.
3. Let’s restart Bind to activate our latest changes.
sudo /etc/init.d/bind9 restart
4. Finally, let’s test our new domain and DNS entries.
Dig
$ dig mydomain.com
Nslookup
nslookup gw
5. That’s it.









If I was to have a purchased domain : thisdomain.com and had it point to my home static IP address, would this work as a real host. Assuming I have apache installed and the firewall configured properly.
that’s is interesting me, too… please help us
how to configure ubuntu as dns and webserver ???
[...] Como montar un servidor DNS en Ubuntu 8.04 LTS Este servidor se monto en una maquina virtal VMWare server, corriendo tanto como host y como guest Ubuntu 8.04 LTS, y se hizo siguiendo la siguiente pagina [...]
great tutorial, just in the point, clear and soft, thanks
hi ulysses.
I just want to make a simple thing. I’m really new to Linux.
I got a static ipaddress in my dormitory. Here is what I got from TCP/IP properties on my windows.
IP address 129.125.101.82
subnest mask 255.255.0.0
default gateway 129.125.101.251
preferred DNS server: 129.125.36.9
alternate DNS server: 129.125.4.13
I have changed the third one (IP address, sbnt mask and gatwy) through this command sudo vi /etc/network/interfaces
now, how can I put the 129.125.36.9 on my ubuntu? where should I put it?
Really thanks
The author doesn’t seem to followup any more.
floating away:
edit your /etc/resolv.conf file
nameserver 129.125.36.9
nameserver 129.125.4.13
hope that helps.
thanks
wonderful – very helpful – thanks!
Thanks for the post, very straightforward – to the point. I’m in the process of setting up name servers for the first time and this is a big help – setting up a private system to play with. It’s great.
thanks
I found this very useful. Now I want to take this one step further by setting up a load of forward aliases (for HTTP headers) to be used setup a number of IIS (web) sites on the same server which happens to be a KVM machine running win2003. so if my domain was “test.com” should I able to ping ssp or mysite or team and if so what would be their qualified names.. I have tried to ping
ssp.win2003-01.test.com and it times out where as win2003-01.test.com is ok
win2003-01 IN A 192.168.1.110
www IN CNAME win2003-01
ssp IN CNAME win2003-01
Mysite IN CNAME win2003-01
team IN CNAME win2003-01
Thanks in advance
Daniel,
Have you tried this format instead of an ip address?
`subdomain1 IN CNAME domain.com`
or
`ssp IN CNAME win2003-01.test.com`
It’s worth a try.
Thanks for your help. In the end I just changed the order of the DNS servers and this worked. I think this article could do with an example windows client TCP/IP configuration which ensures I can resolve my local machine/aliases addresses as well as all my other www adresses.
Also I guess we need to automatically start bind9 automatically on server power up. Have you done this?
Regards
Daniel
[...] http://idzole32onthenet.wordpress.com/2008/08/06/dns-server-pada-ubuntu-server-71/ http://ulyssesonline.com/2007/11/07/how-to-setup-a-dns-server-in-ubuntu/ http://slackerbox.com/node/334 [...]
Would have been great if the following had been explained instead of just saying here it is, use it:
“0.168.192.in-addr.arpa”
@ IN SOA ns.mydomain.com. admin.mydomain.com. (
2007062001
28800
3600
604800
38400
);
mydomain.com. IN NS ns.mydomain.com.
ubuntudesktop IN A 192.168.0.10
www IN CNAME ubuntudesktop
pc IN A 192.168.0.11
gw IN A 192.168.0.1
TXT “Network Gateway”
Unfortunately I didn’t learn anything from the tutorial except how to copy and paste
Check this tutorial it explains some details better,
http://www.ubuntugeek.com/dns-server-setup-using-bind-in-ubuntu.html
It can’t be told clearer, I’ve configured from scratch my DNS in 15 minutes.
Thank you very much!!!
I’m glad that worked for you.
Being on Linode, the service offers its own DNS server, and I know that many web hosts, VPS, and dedicated server companies also do the same. However there are real benefits in running your own DNS server, with editing speed and ease of use being one of them. Although for full disclosure I have decided to use Linode’s DNS service to reduce load on my own server. Nonetheless, this guide will go through the relatively simple process of setting up a DNS server in Ubuntu Linux.
The first thing one needs to do is to install Bind. Bind is a file based DNS server that is pretty simple to use once you understand it; however there are multiple files to edit. When installed using sudo apt-get install bind9 a default configuration file is created for you as well.
The second step is to update the /etc/bind/named.conf.local configuration file to add our zone. Our zone specifies what domains this DNS server is responsible for. For this tutorial I will use example.com as the sample domain. Therefore in name.conf.local you will add both the zone definition as well as the reverse DNS entry for your IP. They should be written as:
zone “example.com” in {
type master;
file “/etc/bind/zones/example.com.db”;
allow-transfer { any;};
};
zone “1.0.168.192.in-addr.arpa” {
type master;
file “/etc/bind/zones/1.0.168.192.db”;
};
Please remember to replace example.com with your real domain name and 192.168.0.1 (written in reverse) with your real IP address.
The third, and optional step, is to configure some default DNS server options. The file used to do this is /etc/bind/named.conf.options The main settings that ought to be of interest are: forwarders, notify, and directory. Forwarders specify which DNS server should be used when your DNS server is queried for a domain that it is not responsible for. Notify specifies whether slave DNS servers should be notified of changes when they are made on this server. Directory specifies where DNS configuration files should be looked for if a full file parameter is not used in our zone entries in step two. Samples of three options are:
forwarders { 208.67.222.222; 208.67.222.220; }
notify { yes; }
directory { “/dns/zones”; }
The fourth step in our Ubuntu DNS server setup is creating our zone file. I am assuming that you did not specify a custom zone directory like the options example above. Therefore you will want to create your zone files in the folder /etc/bind/zones by just creating example.com.db and filling it with entries such as:
// TTL = Time to live for records on slave (2 days)
// 2009030700 = Serial for Bind to check whether an update has occured
// 6H = Time between refresh requests
// 1H = Time between retry attempts
// 1W = Expiry time for the record on slave
// 1D = Amount of time an invalid response is stored on slave
$TTL 2D
@ IN SOA ns1.example.com. root.example.com. (
2009030700
6H
1H
1W
1D
)
// ns1.example.com. = Name server
// mail.example.com. = Mail server
// http://www.example.com. = HTTP server
// *.example.com. = Wildcard entry
example.com. IN NS ns1.example.com.
example.com. IN MX 10 mail.example.com.
ns1 IN A 192.168.0.1
www IN A 192.168.0.1
mail IN A 192.168.0.1
* IN A 192.168.0.1
The above zone definition file sets some basic servers and points them to the computer with the IP address 192.168.0.1. You can host each service on a different IP if they are on different servers. You can also point to other name servers by using CNAME instead of A records. Please note that all domain names end with a “.”.
While a reverse DNS zone file is optional, for things like mail servers if a reverse entry is not available it can be flagged as a possible spam server. So it is good practice to do it. For our example zone file the reverse would be in the file 1.0.168.192.db and look like:
// TTL = Time to live for records on slave (2 days)
// 2009030700 = Serial for Bind to check whether an update has occured
// 6H = Time between refresh requests
// 1H = Time between retry attempts
// 1W = Expiry time for the record on slave
// 1D = Amount of time an invalid response is stored on slave
$TTL 2D
@ IN SOA ns1.example.com. root.example.com. (
2009030700
6H
1H
1W
1D
)
IN NS ns1.example.com.
1 IN PTR example.com.
After the files have been created restart bind through the command /etc/init.d/bind9 restart and using the command dig @192.168.0.1 http://www.example.com to use your own DNS server to query the record http://www.example.com. If an answer is given (should look like your entry for www in the example.com.db file) then everything is set up correctly. You should now update your domain name registar’s DNS records to point to your server.