January 1, 2007 · General · Email This Post
Share

If you're new here, you may want to subscribe to my RSS feed and if you have questions related to your ubuntu system post question to our forums. Thanks for visiting!

A DNS server resolves domain names into IP addresses. So when you request “yahoo.com” for example, the DNS server finds out the address for the domain, and sends your request the right way.

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”

Share

Related posts

18 Comments to “Local DNS Cache for Faster Browsing on Ubuntu Machine”

  1. Dan says:

    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

  2. michael reed says:

    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.

  3. admin says:

    @Michael,

    I have corrected now thanks for your correction

  4. John says:

    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.

  5. jj says:

    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.

  6. Ric says:

    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

  7. Shipwright says:

    It makes an enormous difference over satellite, where each request is a two second round trip.

  8. Josh says:

    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.

  9. Rick says:

    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.
    :-)

  10. aremania says:

    it’s work - thank you very very much

    Yes this should be standard on Ubuntu desktop

  11. Runaway1956 says:

    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.

  12. SilverWave says:

    Thanks for the post - just what I was looking for as my ISP’s DNS is slower than a slow thing ;)

    Cheers!

  13. Christopher L. Everett says:

    I combined this with OpenDNS for an even better boost.

  14. Dyssolution says:

    Great info, thanks for the post!

  15. Ar4miS says:

    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

  16. David Stark says:

    Hi mate,

    great little tutorial, however I think everyone with no firwall between them and the net should know this:

    Supposing you only have one interface open to the internet (IE eth0)

    If you state in your config “listen-adress=127.0.0.1″ you would kinda expect it to do exactly that.

    however running this reveals that it has opened a TCP port on your eth0:

    (Great if you wanna speed things up on your LAN for others, but not for me)

    To see what its doing on run:

    $ netstat -ant | grep :53
    tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN
    tcp 0 0 {YOUR ETH0 IP}:53 {YOUR ETH0 IP}:47167 TIME_WAIT

    I want that port DEAD, and i’m not gonna use a firewall to do what software should control.. so…

    I fiddled with some of the settings in dnsmasq.conf like:

    except-interface=eth0
    interface=lo

    But still this didn’t work.., nmap could still see something running :

    $ nmap {YOUR ETH0 IP} -p 53 -A

    Starting Nmap 4.20 ( http://insecure.org ) at 2008-11-17 03:51 GMT
    Interesting ports on ({YOUR ETH0 IP}):
    PORT STATE SERVICE VERSION
    53/tcp open tcpwrapped

    -----

    I’m sure its not a big thing but i’m a paranoid perfectionist…

    Anyway all it needed to actually completely ignore eth0 is:

    interface=lo
    bind-interfaces

    ========= Viola !!!========

    $ nmap {YOUR ETH0 IP} -p 53 -A | grep 53
    53/tcp closed domain

    $ netstat -ant | grep :53
    tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN

    $ sudo lsof | grep dnsmasq | grep :
    dnsmasq 27561 dnsmasq 5u IPv4 155191 TCP localhost:domain (LISTEN)
    dnsmasq 27561 dnsmasq 6u IPv4 155192 UDP localhost:domain
    dnsmasq 27561 dnsmasq 9u IPv4 155201 UDP *:37795

    Hope this helps :)

  17. Dude says:

    I just wanted to see current situation before installing dnsmasq and it seems that there is a cache in ubuntu. So can we say ubuntu 9.04 has it out-of-the-box ?
    mustafa@mustafa-desktop:~$ dig http://www.youtube.com

    ; <> DiG 9.5.1-P2 <> http://www.youtube.com
    ;; global options: printcmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48259
    ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0

    ;; QUESTION SECTION:
    ;www.youtube.com. IN A

    ;; ANSWER SECTION:
    http://www.youtube.com. 86400 IN A 88.255.41.21

    ;; AUTHORITY SECTION:
    youtube.com. 86400 IN NS ns.company.lan.

    ;; Query time: 2014 msec
    ;; SERVER: 192.168.2.1#53(192.168.2.1)
    ;; WHEN: Sat Aug 29 10:15:13 2009
    ;; MSG SIZE rcvd: 77

    mustafa@mustafa-desktop:~$ dig http://www.youtube.com

    ; <> DiG 9.5.1-P2 <> http://www.youtube.com
    ;; global options: printcmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22587
    ;; flags: qr rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
    ;; WARNING: recursion requested but not available

    ;; QUESTION SECTION:
    ;www.youtube.com. IN A

    ;; ANSWER SECTION:
    http://www.youtube.com. 10000 IN A 88.255.41.21

    ;; Query time: 2 msec
    ;; SERVER: 192.168.2.1#53(192.168.2.1)
    ;; WHEN: Sat Aug 29 10:15:15 2009
    ;; MSG SIZE rcvd: 49

  18. ThinkUbuntu says:

    Great tip. Saves time (several hops to ISP on broadband and 3g wireless dns is slow to)

    One question, why is this not enabled by default in Ubuntu, what is the downside?

    Can the DNS entries become corrupt, and how long are the tables maintained?

    Great tip though!

Leave a Reply