Local DNS Cache for Faster Browsing on Ubuntu Machine
Posted by admin on January 1st, 2007
Email This Post
You can run a DNS cache on your computer. This will speed up the process of looking up domain names when browsing. The difference is about 30-60 ms. Multiply that difference by the number of websites you visit a day for an approximate estimate of the speed improvement.
The following instructions are for someone with a broadband internet connection, where the computer gets it’s local IP address using DHCP from the router in your home or office.
Install dnsmasq in Ubuntu
Dnsmasq is a lightweight, easy to configure, DNS forwarder and DHCP server. It is designed to provide DNS and optionally, DHCP, to a small network. It can serve the names of local machines which are not in the global DNS. The DHCP server integrates with the DNS server and allows machines with DHCP-allocated addresses to appear in the DNS with names configured either in each host or in a central configuration file. Dnsmasq supports static and dynamic DHCP leases and BOOTP for network booting of diskless machines.
First you need to make sure that Universe repository is enabled in your sources.list file
Install dnsmasq Using the following command
sudo apt-get install dnsmasq
uncomment the following line (remove “#” in the beginning) in the file /etc/dnsmasq.conf
listen-address=127.0.0.1
Now edit
/etc/dhcp3/dhclient.conf
and make sure the section below exactly like this, especially the line that says “prepend domain-name-servers 127.0.0.1;”
#supersede domain-name “fugue.com home.vix.com”;
prepend domain-name-servers 127.0.0.1;
request subnet-mask, broadcast-address, time-offset, routers,
domain-name, domain-name-servers, host-name,
netbios-name-servers, netbios-scope;
In the normal case, when you get a new dhcp lease, the dhcp3 client (tool) on your computer gets a new lease, and updates the
/etc/resolv.conf
file on your computer with the right values for the DNS servers to use (usually some machine in the network of your hosting provider). Adding the “prepend” option as we did above ensures that “127.0.0.1″ will appear on the top of the list of DNS servers. That magic number refers to your own computer. So in the future, whenever your computer needs to resolve a domain name, it will forward that request to dnsmasq (which is running at 127.0.0.1 - your computer). If the details for the domain name are already in you cache, well and good, dnsmasq will serve it up and make the process real fast. If it is not in the cache, then dnsmasq will look at the /etc/resolv.conf file and use the nameservers listed below the “127.0.0.1″. I hope that explains things.
Now open the file
/etc/resolv.conf
in your text editor. It probably looks like:
search yourisp.com
nameserver 217.54.170.023
nameserver 217.54.170.024
nameserver 217.54.170.026
The 127.0.0.1 is missing right now since you haven’t renewed your lease after you edited the /etc/dhcp3/dhclient.conf file. So, let us add that in manually this one time. After you do, your /etc/resolv.conf file will look like the following:
search yourisp.com
nameserver 127.0.0.1
nameserver 217.54.170.023
nameserver 217.54.170.024
nameserver 217.54.170.026
Now you need to restart the dnsmasq using the following command
sudo /etc/init.d/dnsmasq restart
Now you are running a local DNS cache.
Testing Your Local DNS Cache
If you want to measure your speed improvement, type the command
dig yahoo.com
You will see something like “;; Query time: 38 msec” there.
Now type the command again, and you should see something like:”;; Query time: 2 msec”
If you want to be notified the next time we write something please subscribe to our RSS feed.Thanks for Visiting!


January 4th, 2007 at 8:49 pm
really now, a 36 ms savings ..
from the wikipedia ( http://en.wikipedia.org/wiki/1_E-2_s )
100 to 150 milliseconds; typical time for a human blink
you saved yourself a 1/3 of a blink
April 23rd, 2007 at 2:30 pm
Thanks for this tutorial. Easy to follow and I now have DNS caching enabled.
One /tiny/ correction though: there is an extra period at the end of
”
sudo /etc/init.d/dnsmasq restart.
”
Thanks again.
April 23rd, 2007 at 5:27 pm
@Michael,
I have corrected now thanks for your correction
June 26th, 2007 at 12:06 am
How can I see the cache file contents? I realize this may save some time when visiting a web site, but most websites refer to ad networks and other addresses on the same page, so this cache may speed up a single website many times more than 36ms. By looking at the cache file I may see how many various places I have already been.
BTW, I just did this and my speed went from 15ms to 0ms.
September 8th, 2007 at 11:54 pm
I would suggest you modify your tutorial as follows (especially for newbies like myself)
Change
“uncomment the following line (remove “#” in the beginning) in the file /etc/dnsmasq.conf”
To
Open a text editor and type
sudo gedit /etc/dnsmasq.conf
“Add after “listen-address=” 127.0.0.1
At least in my case I couldn’t find the referenced entry. All I found was “listen-address=” on line 73 of gedit of /etc/dnsmasq.conf
Same with all after “Now edit”
sudo gedit /etc/dhcp3/dhclient.conf
Thank you for writing the tutorial I saw a dramatic improvement also.
December 16th, 2007 at 6:21 am
Thanks this worked great.. Some sites you visit will often call anywhere from 2 or 3 to 50 or 60 other sites. Ever bit helps.
First time - dig yahoo.com - Query time: 114 msec
Second Time - Same address - Query time: 4 msec
January 12th, 2008 at 8:14 pm
It makes an enormous difference over satellite, where each request is a two second round trip.
January 28th, 2008 at 12:46 am
Firefox was taking several seconds to resolve domain names on me in ubuntu 7.10, disabling IPV6 didn’t help but this method made a huge different. I’m not sure if using a local DNS server isn’t just kludging around something else that’s misconfigured for the ISP DNS, but hey, it works!
I read a lot of complaints about poky web-browing with Firefox in ubuntu, they should really have this as a standard inclusion on all installations. New users especially will be put out by slow web-browsing when they try the system on a livecd or experimental installation.
April 23rd, 2008 at 10:45 pm
This is great - thanks.
I can’t disagree more strongly with Dan’s cynicism (comment 1 above). He clearly doesn’t understand the aggregating problems of network latency nor that DNS sometimes hiccups and goes slowly for the odd second (or so it seems to me). And that’s for wired people - not putting up with satellites etc (comment 7 above).
The local dnsmasq cache solves these problems. I agree with the suggestion that this should be standard on Ubuntu desktop.
April 25th, 2008 at 3:30 am
it’s work - thank you very very much
Yes this should be standard on Ubuntu desktop
June 15th, 2008 at 8:48 pm
Yeah, in NORMAL circumstances, using a DNS cache saves you about 1/3 of an eyeblink. Yeah, big deal.
BUT - people like myself who live in outback nowhere, and rely on unreliable ISP´s, with unreliable DNS servers can realize a tremendous saving in time. It was not unusual for me to wait several seconds, just to read a message that an address couldn´t be found. Preposterous, that I can open maybe 6 tabs on a given site, and the seventh address can´t be found!!
Running your own DNS cache and/or DNS server can make the difference between enjoying your time online, or just giving up, out of frustration.
June 27th, 2008 at 10:01 pm
Thanks for the post - just what I was looking for as my ISP’s DNS is slower than a slow thing
Cheers!
July 13th, 2008 at 10:02 pm
I combined this with OpenDNS for an even better boost.
August 12th, 2008 at 6:44 am
Great info, thanks for the post!
August 26th, 2008 at 11:51 pm
Excellent tips ! I’m living in new Caledonia and like
Shipwright Says:
It makes an enormous difference over satellite, where each request is a two second round trip
Thanks a lot