Ubuntu: Broadband Local DNS Cache
October 5, 2009 by: Allen SanfordEver wanted to run your local DNS cache in order to speed up your over all browsing speeds. Well with Linux especially with Ubuntu this is super easy to do. I am going to take a few moments here and explain how it is done. This should only taje about 5 to 10 minutes of painless command line work to get everything in tip to shape.
A brief explanation for those not in know. A DNS server resolves, or interprets domain names (ex. google.com, yahoo.com, webternals.com) into IP addresses (ex. 74.125.127.100, 209.131.36.159, 74.207.227.1). So when you request “google.com” for example, the DNS server finds out the address for the domain, in my case 74.125.127.100, and sends your browser to the right server so you can find the page you were looking for. You can think of those IP numbers as a street address and the names as names for a given web page.
Now lets talk about the benefit of running your own cache. You can run a DNS cache on your computer and speed up the process of looking up domain names while you are browsing. The difference is only about 30-60 ms, but that doesn’t sound like much you say, well multiply that difference by the number of websites you visit a day and you can begin to see the benefit. Just as a scenario, lets say you visit 20 sites that 20 * 30ms thats 600ms ~ 0.6 seconds.
OK. enough babel lets do this, we are going to install dnsmasq for caching are look ups. I am going to be showing instructions for Ubuntu users, so everybody else has to fend for themselves, sorry.
sudo apt-get install dnsmasq |
OK, as always we got to resort to editing some files, first up is /etc/dnsmasq.conf, so break out that favorite text editor and lets begin. Remember you are going to need to use root permissions: sudo, su, however you are doing on you system. Uncomment the following line.
...
listen-address=127.0.0.1
... |
Now on to /etc/dhcp3/dhclient.conf we are interested mostly in this section and most importantly the prepend domain-name-servers 127.0.0.1; part of it. 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 your 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 name servers listed below the “127.0.0.1″. Blah!, that was a mouth full, now make the change please and lets keep going here.
...
#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;
... |
Lastly we edit /etc/resolv.conf what we are after here is to add 127.0.0.1 to the list of name servers noting where I placed it here is crucial.
search *.lan
nameserver 127.0.0.1
nameserver 10.27.11.91
nameserver 10.27.11.30 |
Almost done all we need to do is restart dnsmasq and the changes we made to the configuration file take effect. You can do that using this command:
sudo /etc/init.d/dnsmasq restart |
Now you are running a local DNS cache. If you want to measure your speed improvement, type the command twice back to back and you will see the difference noting the line similar to “Query time: 22 msec”:
dig google.com |
The first time, since google.com’s details are not in our cache the query took 22 ms or whatever you get. The second time, you should get a much faster time like 2 msec or so.
Enjoy and Have a Good’n!












Broadband has helped in the improvement of the way in which we communicate. Unlike some years back, people today communicate from all corners of the world without any form of physical communication media like wires. This time round it is purely wireless
How long have you had this blog of yours? We totally like it.
I love your blog! did you create this yourself or did you outsource it? Im looking for a blog design thats similar so thats the only reason I’m asking. Either way keep up the nice work I was impressed with your content really..