28 Jan 2009

Faster internet usage with a local dns cache

One of the things you can do to speed up browsing and general internet activity is to create a local DNS cache. A DNS is a Domain Name Server and its purpose is to convert domain names to IP addresses. Every time you type in a domain name your computer has to ask a DNS to convert it to an IP address. Occasionally a website’s IP address will change so you can’t store an IP address forever, but it is worth cacheing it locally, even just for 24 hours or so considering the number of times we hit some sites during the day.

In Ubuntu (and debian, and other flavours of linux, though this tutorial is written for ubuntu) we can install a little helper application to cache the DNS results for a time and thus speed up our internet experience in general! It is called dnsmasq and the first step is to install it:

1
sudo apt-get install dnsmasq

Then at the terminal we need to edit the config file:

1
gksudo gedit /etc/dnsmasq.conf

Then find the line that says “#listen-address” and remove the leading # (if there is one) and make it say:

1
listen-address=127.0.0.1

Save and exit. Now edit /etc/dhcp3/dhclient.conf:

1
gksudo gedit /etc/dhcp3/dhclient.conf

Edit the line that says “prepend domain-name-servers” to say:

1
prepend domain-name-servers 127.0.0.1;

or if you want to use OpenDNS.org’s DNS servers (which I recommend) then change that line to:

1
prepend domain-name-servers 127.0.0.1, 208.67.222.222, 208.67.220.220;

Now at this point if you were to get a new IP lease it would all work, but that’s a bit of hassle so lets make it work now without rebooting the hardware. Edit /etc/resolv.conf:

1
gksudo gedit /etc/resolv.conf

Insert the following line at the top of the nameserver list:

1
nameserver 127.0.0.1

Finally we need to restart our little utility to take into account the new settings:

1
sudo /etc/init.d/dnsmasq restart

Now it is time to test it! At the terminal type:

1
dig news.bbc.co.uk

…and take note of the query time. Then run the same command again and note the improvement. You should be looking at around 2ms for the 2nd time whereas the first one will be dependant upon your internet connection - for me it was about 50ms - so for me this makes the DNS query portion of browsing about 25 times quicker. Enjoy.

comments powered by Disqus