If you are facing slow connections and slow browsing experience, then you can speed up your browsing by a small factor by caching your DNS queries locally and using it.

What is DNS?

But before you go ahead the first question that would surely pop up in your mind is what is this DNS? And what does it do? To answer the first question I would take a simple example of your mobile phone, when you want to call your friends, you first find their name in the contacts and click on dial. You could have directly entered their number and dialled, but remembering a few numbers is possible but not all, hence you store them as contacts in your phone.

In the Internet too we have the same concept each site has an IP address which is nothing but a set of numbers, but it would be very difficult to remember the numbers for each site, so we have a DNS server which basically takes the site name and gives back the IP address for the site.

Why cache DNS mapping?

But why cache it locally? The answer is simple, there are a set of sites you would visit regularly and it takes a few milliseconds to get the IP address, so why not store it the first time it is got and use that the next time you visit the same site again. A few milliseconds may not seem a long time, but when on a slow connection it does make a difference and ultimately result in faster browsing experience.

So in Linux we have a simple light weight program ‘dnsmasq’. The steps mention here are applicable to all Linux Distributions, the only exception being the first step to install the software for which you can use your distributions software installed to install the program. I am using Linux Mint, which is based on Ubuntu so the steps are specific to it.

Installing dnsmasq

Follow the following steps to install and get ‘dnsmasq’ running, please note that in the below ‘sudo gedit’ is entered in the terminal to open the gedit editor with root previliges, you can use any other editor such as vim. ‘sudo’ may not work in all distributions in which case you can just issue ‘su’ command and for each of the ‘sudo gedit’ you can just run ‘gedit’:

Steps:

  • In Ubunutu/Mint you could just give the below command to install ‘dnsmasq’: sudo apt-get install dnsmasq
  • The next step is editing the file ‘/etc/dnsmasq.conf’, you need root previliges to edit this file. In the terminal you can issue the following command: sudo gedit /etc/dnsmasq.conf
  • Find the following string in the file: #listen-address=
  • Now remove the ‘#’ at the starting, and change it to the below string: listen-address=127.0.0.1
  • Save and close ‘/etc/dnsasq.conf’
  • Next you need to edit the file ‘/etc/dhcp3/dhclient.conf’, issue the following command sudo gedit /etc/dhcp3/dhclient.conf
  • Search for ‘prepend domain-name-servers’ in the opened file, and change it to below and remove any ‘#’ character if present at the start of the line. prepend domain-name-servers 127.0.0.1;
  • Save and close ‘/etc/dhcp3/dhclient.conf’
  • Next you need to edit the file ‘/etc/resolv.conf’, issue the following command to open it: sudo gedit ‘/etc/resolv.conf’
  • Add the following to the start of the file: nameserver 127.0.0.1
  • Your other DNS servers should come after the above line, for example my file is as below (I use OpenDNS): nameserver 127.0.0.1 nameserver 208.67.222.222 nameserver 208.67.220.220

Thats it, your local DNS caching server is set up and ready for use, you can issue the following command to restart the ‘dnsmasq’ program: sudo /etc/init.d/dnsmasq restart

In case you want to test if your queries are actually hitting the local DNS cache, issue the below command a couple of time, you can yourself see the difference it the resolution time: dig kamaths.org

The output is as below, important things to note is the ‘Query time’ and ‘SERVER’, since I had already visited ‘kamaths.org’ it gives 0msec:

;; <<>> DiG 9.7.1-P2 <<>> kamaths.org
;; global options: +cmd ;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65232
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION: ;kamaths.org. IN A
;; ANSWER SECTION: kamaths.org. 937 IN A 118.139.186.1
;; Query time: 0 msec ;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sat Sep 17 22:28:03 2011 ;; MSG SIZE rcvd: 45