Jul
28
Speed Up Web Browsing by Caching DNS to Your Hard Drive in Ubuntu.
July 28, 2007 | By: UbuntuLinuxHelp | 19 Comments
Posted in How to...
I was sent a web link today that contained a great article about how to speed up web browsing by locally caching DNS name resolution results using dnsmasq. That article is great and it is here: Local DNS Cache for Faster Browsing. I like it because it is equally simple to do and works, but the results (of DNS lookups) are not saved after your PC or laptop "session" is finished.
To clarify, in my opinion, one of the drawbacks (someone correct me if I'm wrong), seems to be that the cache is lost after the laptop or PC is turned off.
To resolve that issue, I prefer to use pdnsd.
As a side note: if you're looking for a fast, reliable DNS server, I've been using OpenDNS for 2 years and the service has been reliable (and arguably provides faster responses than the DNS servers from my ISP). I will be using their DNS server IP's to configure pdnsd - But you can use any DNS server IP's you want.
To install pdnsd simply use apt-get by issuing the following command in an Ubuntu Linux terminal window:
sudo apt-get install pdnsd resolvconf

As I mentioned, this will cache the results of DNS lookups to your hard drive and therefore improve your web surfing speed. As a side not, don't omit the "resolveconf" part (above). If you omit it, then you may run into problems when different applications/programs are trying to access (or change) the configuration file (found at /etc/resolv.conf).
After the install is complete (it should be fast), edit that configuration file by issuing this terminal command:
sudo gedit /etc/pdnsd.conf
One of the significant change I made is:
label = "OpenDNS";
ip=208.67.222.222,208.67.220.220;To compare, here is what my complete edited pdnsd.conf file contains:
global {
perm_cache=512;
cache_dir="/var/cache/pdnsd";
max_ttl=604800;
run_as="pdnsd";
paranoid=on;
# next setting allows ppp/ip-up update the name servers -- ABa / 20040213
status_ctl=on;
server_port=53;
server_ip="127.0.0.1";
}#Edit these to be your own servers if wished:
server {
label = "OpenDNS";
ip=208.67.222.222,208.67.220.220;
proxy_only=on;
timeout=10;
}# if you installed resolvconf, and status_ctl=on
server {
label="resolvconf";
}source {
ttl=86400;
owner="localhost.";
# serve_aliases=on;
file="/etc/hosts";
After saving your changes, you'll need to restart the server by issuing this terminal command:
sudo /etc/init.d/pdnsd restart
Then of course, test that everything is working. I used the command:
dig ubuntu.com
And received resolution in 74ms as below

Then (as I knew this should be cached), I re-issued the above command. In my case name resolution using the cached results was 1ms - Wow!

A great way to help speed up your web surfing.
Not bad for 10 minutes work on a saturday afternoon!
Related posts:
- Speed Up Your Internet Connection in Ubuntu Linux – Part 1
- How to Install TTF and CTF Fonts in Ubuntu.
- Speed Up and Improve Web Surfing With an Ubuntu Squid Server.
- A Quick Way to Improve Ubuntu Linux Operating Speed Performance.
- Ubuntu Updates Versus Disk Space – Reader Questions

(1 votes, average: 4.00 out of 5)
Very nice. This seems to be yet another reason to hop over to Ubuntu. I’ve also been using OpenDNS for about two years and haven’t looked back.
What’s the disk cost of DNS caching? I would think that after several weeks or months of power-surfing that the cached DNS files would become so large that there would be no noticable gain to it.
@Jason
That’s a great question! Thanks.
I do know that you can control the amount of space used…
There is a configuration switch like this:
<strong><em>perm_cache=(number|off);</em></strong>
You can use it to switch the disk cache off (<em>perm_cache=off</em>) or supply a maximum cache size in kB. If the disk cache is switched off, 8 bytes will still be written to disk. The memory cache is always 10kB larger than the file cache. This value is 2048 (2 MB) by default.
So for example you can edit the configuration file by:
sudo gedit /etc/pdnsd.conf
and edit the perm_cache parameter to your needs.
Mine looks like this:
<blockquote>
global {
perm_cache=512;
cache_dir=”/var/cache/pdnsd”;
max_ttl=604800;
.
.
.</blockquote>
If you really want to be the resident guru <smiling here> I found all the settings here: http://www.phys.uu.nl/~rombout.....d/doc.html
There are such a vast array of setting, it’s pretty good too that they are properly documented.
(One of the reasons why I became an Ubuntu user was because of the community support!)
I hope this reply helps answer the question.
dnsmasq – A lightweight DHCP and caching DNS server
This is another solution that works for me.
For anyone interested in this too, here is the link for them: http://www.thekelleys.org.uk/d.....q/doc.html
I agree with Bolangi, I tried that and it worked nice! The only difference is that there is no disk caching.
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Fri Apr 4 00:20:33 2008
;; MSG SIZE rcvd: 156
I am using bind for this.
@tallman – That’s fast! :)
Does this still apply on 7.10 and 8.04?
@Vadim P. – I did this on 7.10 as well and it caused no problems. I’ve not tried on 8.04, yet. I’m waiting for the stable release to come out (I think it’s about 9 or 10 days before release).
I’m not sure why anyone (who isn’t a server admin) should cache DNS lookups locally?
For instance, when I query the DNS of my ISP for a record, it will (of course) dig up the address, walking down the “DNS tree” (or should you climb up a tree?).
A second address for the same record will then also be cached at my ISP’s DNS servers!
So, what’s the need then for a local cache when you can use the ISP’s cache?
Other discussion: do you really think that 70 or 150 millisecond of lookup delay will impact your websurfing experience dramatically?
cheers!
1st query:
;; Query time: 130 msec
2nd query:
;; Query time: 2 msec
@pistole – Thanks for the input. I think in some aspects it’s a case of ISP DNS reliability/performance (or of non-ISP based DNS servers for those who use them) and even network performance. I found in my personal experience that my surfing speed had considerably improved as I was making less and less DNS requests. As we surf through web pages, our browsers make a separate download request for every single element of that page (scripts, style sheets, images, etc.) and each of those requires a DNS lookup. Pages quite literally can include many, many numbers of elements. as such this is one thing that can impact surfing speeds. Now if I’m browsing through several sites and related pages, a local DNS cache has reduced the number of DNS requests, which in turn mitigated the browsing performance loss. As such, web browsing speed improved. :)
Also, I think you are quite right in that caching DNS is a must for servers as that has an even greater performance improvement. :) ;)
Well I guess (no, I’m sure) that I’m spoiled with my ISP that has good DNS servers.
On the other hand, I run my own (caching only) nameserver on my Fedora box @home so I never experience any delays from DNS lookups :)
@pistole – Do you have the details on how to do that properly. I tried it a couple years back and got an email from my ISP saying that I have to stop running a server on my local network or they would eventually cut of services. I thought the DNS was in caching mode only – Obviously I was wrong.
If you don’t have the install details, do you have a link?
Thanks again and have a good one! :)
I just tried this on Intrepid Ibex. Unfortunately, it doesn’t quite work – Network Manager is used by default now, and controls resolv.conf. So, you can’t use resolvconf, as it’ll break your entire DNS (leaving it blank).
However, what you *do* need to do is follow part of the first article:
—————-
“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;”
———————
and then kill network manager, and restart it. Then it works a charm!
@UbuntuLinuxHelp
On a Fedora box, simply do this:
# yum install caching-nameserver
It will automatically select other packages to satisfy dependancies.
When setup, make sure you don’t allow DNS queries from the internet to your box; when done properly your ISP won’t “see” that you have you’re own nameserver.
If you’d like bind to use your ISP’s nameservers instead of doing recursive queries, edit the named.conf (/etc/named.conf or /var/named/chroot/etc/named.conf), and put in your ISP’s nameservers as “forwarder”:
forward ( only );
forwarders { ip-address ; ip-address ; }
After install pdnsd resolvconf
check with the /etc/resolv.conf
i see only 127.0.0.1
when i try to ping outside like yahoo.com
it show unknown host..
please advise
@pistole – That’s sweet. Thanks. (It’s also about time I had another look at Fedora. – I dropped out at Fedora Core 2).
The binding of ISP servers tip was also pretty good – Cheers!
Thanks! :)
@Ray – Does your NIC have an IP address? Maybe it does not, is it local or public?
instead of digg ubuntu.com its dig ubuntu.com like in your terminal… took me a minute to work that out :P
@alex – Thanks for catching and correcting that for us! :) Between the two of us, I’m glad that at least you know what you’re doing. Hee hee hee… I’ve corrected that command in the post, thanks again and have a good one! – Cheers!