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.

Thanks… after three other how to’s this one worked fof my setup. I was going crazy trying to figure out why my internal ip’s wouldn’t resolve.
Todd, I’m glad you found it useful.
Awesome HowTo, exactly what I was looking for. Thanks…
Hey Ulyssesr!
Nice work man!
That was simple, clear and fast!
Thank you for writing this!
I just study Linux. And I want to know how to set up DNs, Mail Sever and Web Server in my Linux(Only one pc or labtop).
Thanks for reply,..
Great work.
Liked it loads, easy to understand.
I have a question though:
I have about 10 windows work station and two linux boxes. i want to be able to refer to each PC or System with its name. Say, i want to be able to ping them by their names e.g instead of pinging a system with it’s IP address, i want to ping it with it’s name; like thing : ping PC-ubuntu-desktop instead of ping 192.168.0.20. How can i achieve this please.
Hi Kay,
1. First, you will need to assign a static ip addresses to all your workstations. The last thing you need is a host with a different ip address each time you are connected to the network.
2. Second, you need to enter several A records to your zone file, e.g. /etc/bind/zones/mydomain.com.db in this example.
ubuntudesktop-1 IN A 192.168.0.11
ubuntudesktop-2 IN A 192.168.0.12
ubuntudesktop-3 IN A 192.168.0.13
ubuntudesktop-4 IN A 192.168.0.14
and so forth….
3. For reverse lookup, make the appropriate entries to your reverse DNS file, rev.0.168.192.in-addr.arpa in this example. Here, we are making a reverse DNS entry for ubuntudesktop-1 at ip 192.168.0.11.
11 IN PTR ubuntudesktop-1.mydomain.com.
Don’t forget that period in the end of the domain.
Hope that helps.
Dear Ulysses,
What do I need to do if I need to use the same DNS server for several subnets, such as:
192.168.100.0 255.255.255.0
192.168.110.0 255.255.255.0
192.168.120.0 255.255.255.0
Would it work if I made a separate reverse dns entry in named.conf.local for each subnet and create a separate file for each entry?
Yes, creating 3 reverse DNS entries in named.conf.local and making 3 separate reverse files in /etc/bind/zones/ should do the trick. Let me know how it goes.
I haven’t gotten it to work. The actual subnets I am using are:
192.168.1.252 255.255.255.252
192.168.20.0 255.255.255.0
I have the Ubuntu Server 7.10 named GutsyServer at:
192.168.1.254 255.255.255.252
I have the Windows XP client named Client-B3 at:
192.168.20.11 255.255.255.0
I wonder if I named the reverse-dns files correctly. If I want to use just one reverse-dns file for a 192.168.0.0 255.255.0.0 network, could I just create a file named rev.168.192.in-addr.arpa. In the file I made reference to Client-B3 such:
11.20 IN PTR Client-B3.Gutsy.org
Is that the correct way to do it?
I have tried using two separate reverse-dns files, one for 192.168.1.252 and one for 192.168.20.11, but I could not get it to work. In each file I stated the SOA. Is that correct? I wonder if that is the source of my error.
I am able to do a tracert from Client-B3 to GutsyServer, but for some reason the browser will not work with the internet? It seems like internally the DNS server works, but will not work with the ISP’s DNS server? I made sure the forwarders are correct. Is there something else that needs to be configured?
Daniel, I thought about this for a little bit. I haven’t use multiple multiple networks locally, much more use DNS across 2 or multiple networks. The rule is to always get your network working first. Make sure you can ping the DNS server from all the networks.
I have already gotten the network working. When I use Windows 2003 Server everything works smoothly. When I use Ubuntu as a DNS Server I am able to resolve dns names from the server itself, including http://www.microsoft.com. I checked it with nslookup. When I try to use nslookup from one of the clients that I’ve assigned to the DNS Server with the A command and the PTR command, I am successful. But when I do nslookup for http://www.microsoft.com from the client, I get a message saying that the query was denied. It seems that the ISP’s DNS servers are rejecting my DNS server’s request for a query when I do the request from a client. This is odd to me. I’ve looked at about a dozen tutorials. None of them address this issue. I wonder if there is something else involved rather than DNS, like perhaps ldap.
In any case, thanks for the reply.
Daniel
sir dns server is exalent but i want nis server
I have a dns cache defined as “home”
—————————————————
$TTL 3D
@ IN SOA ns1.home. admin.home. (
2006081401
28800
3600
604800
38400
);
home. IN NS ns1.home.
server1 IN A 192.168.1.3
www IN CNAME server1
————————————————–
and my reverse lookup is
————————————————–
$TTL 3D
@ IN SOA ns1.home. admin.home. (
2006081401;
28800;
604800;
604800;
86400
);
IN NS ns1.home.
3 IN PTR server1.home.
———————————————————
I have just installed apache and havent touched its default config yet. When I type
server1.home –> it works (apache default page)
but, when I type
home –> Works (apache default page)
or
server2.home — > Works (apache default page)
Why?
When I type any other canonical, like server3.home or server100.home, it doesnt work.
I have cleared cache in firefox, but still result is the same. From where is it finding server2 or just home?
I am a bit confused, any tip would help. Thanks.
I read the stuff about the ibuntu server and i think you did an excellent job.I have been for sometime now been working how to configure an ubuntu dns server.The stuff is very simple and i wish to say CONGRATULATIONS.I have learnt lots more from you.
I want to know if you also do anything on configratioon of an ubuntu proxy server.
Best wishes to you.You are just too wonderful.
I found your blog on google and read a few of your other posts. Look forward to reading more from you in the future.
Hi Ulysses,
I liked your tutorial about setting up DNS for a local network and followed directions to try and set up mine. Apparently I overlooked that it was for a local network so after a day of trying (and not getting it to work) I woke up the next day and realized I may need a tutorial for a web server rather than a local network. Duh! So, do you have a tutorial to set up DNS for a web server? If not can you point me one? I am trying to set up Apache on ubuntu on a server at home and want to handle my own DNS. I have a few towers and am wondering if I should setup DNS on the same tower I have my www files/Apache on, or set DNS up on a seperate tower? I have 5 static IP addresses I can use.
Thanks Ulysses!!
thanks you..
Ya hurre
But can you tell me that how to add the ubuntu clients in the dns and how to add the windows machines in this domain e.g. mydomain.com
hey
I was wondering if i set up a dns server from home do i not need to register my domain name? or if can it work as a redirect service for people trying to access my site externally?
dude, this is wonderfull im so glad and thankful that u wrote this. it worked perfectly.
thanx a lot once again,
take care
nslookup mydomain.com
doesn’t provide an IP address with this setup.
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.
Hello there,
I have a question that maybe someone can help me. I try to setup a server (Ubuntu 8.04) to be mail, ftp, www, dns, dhcp server. The Linux Box has 2Networks card, one for ISP with public STatic IP :
ETH1 : (ISP)
eth1 IP : 216.12.37.88
Subnet : 255.255.255.237
gateway : 216.12.37.80
DNS : 206.181.2.8
206.181.2.9
ETH0 : (Internal)
eth0 IP : 10.10.8.1
subnet : 255.255.255.0
gateway : 216.12.37.88
DNS : 10.10.8.1
Any idea to setup DNS and DHCP server, i don’t know nothing for now about Linux, i’m studying now.
Thank’s
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.
so awesome!
[...] [...]
excellent! I’ll have to read up more to see how this all works, but I got a dns configured in 15 minutes.
I’m glad it worked for you. DNS was a total mystery to me until I started digging for information. I’m glad I was able to help.
Thanks a lot man!!
Instructions are clear and easy to follow. I set this up relatively quickly and am able to resolve by using the dns name setup in the config files. I dropped the firewall and added the linux IP to my Windows system dns entry under the network config. I’m trying to use the dns server on my windows box now but cant resolve unless I use the entire domain name. On my linux box I can ping gw but on my Windows box I have to ping gw.example.com in order for it to resolve. If I ping gw from my windows box it does not resolve. Does anyone know of an additional step needed to use the dns on all local computers without having to type in the entire domain name? Thanks in advance.
Dennis, The only thing I can recommend is to make sure your Windows clients are pointing to your internal DNS. The other thing I did was added this: ‘domain.com. IN A xxx.xxx.xxx.xxx’ in the zone file. Just replace the xxx with your ip address.
Nice job on your website here. I too am attempting to build a site for my small home construction business. Its a long story, but I’m not very good at this techy stuff. I like the site theme you have used here and was wondering what it was named? I bought Brian Gardner’s theme but just gave up. I want to supplement my bills in the winter time and thought I could do it. Its a long road ahead. Anyway, I just stumbled across your article here and am glad to have met you. virtually of course
.
It’s the Lust theme from the 7 Deadly Series of Themes of wpdesigner.
Super site. Thx!
Hey! Thanks for not obeying standard practices, posting any warnings, or stating which systems versions you’re using.
My computer, ubuntu 9.10, no longer goes online after following these instructions. It’s offline totally. Thanks, bud.
TO ALL USERS: Use Webmin. I’ve been able to ‘mostly’ repair my system and set things back to defaults using webmin. I should have used webmin in the first place before taking advice from self-declared ‘guru’s.
Hi,
Do you know how to make sure there’s nothing wrong with the DNS settings on an Ubuntu client? I have Optimum Online as my ISP and I keep getting, what looks to me like, DNS errors:
http://domainnotfound.optimum.net/cablevassistctxt/dnsassist/main.iscx?ycmredirected=true&domain=google.com
I’ve never gotten a ‘domain not found’ like that on any operating system, when trying to resolve major domains. (its happening with a lot of sites, too.)
Jzaksh,
It sounds like your DNS server is not forwarding to your ISP DNS servers. Check your resolv.conf configuration file.
Hey, thanks for the reply
My resolv.conf is (as expected) built automatically:
$ sudo less /etc/resolv.conf
# Generated by NetworkManager
domain 532
search 532
nameserver 192.168.0.1
^ that nameserver 0.1 is the address of the router. 532 is the LAN’s SSID. Any pointers? I would think that’s right (not only because its automated) because the request should just be passed on to the nearest authority (the router), right?
SSID? Did you mean your internal domain name? A typical entry would be:
domain yourdomain.com
search yourdomain.com
Hey, I’m going to start a thread on unix.com and stop abusing your comments area – sorry
To answer your question, this is on a laptop (not a server) in a home network (not a business), so there’s no domain (no name servers or anything in this house). The whole network is setup fairly close to the defaults of our Dlink router. If you have any suggestions what I could look up to give me a lead, that’d be awesome! (ps, great article, I’m going to set up a domain just for kicks pretty soon)
Abuse is never good. : )