Local DNS for Faster Browsing
July 18, 2007
Using a local DNS cache can decrease look up times for domain name resolutions. Making browsing response times and other Internet actions slightly faster.
What is a Domain Name Server?
When Internet site addresses are typed into the browsers location bar, for example, the computer queries another computer called a Domain Name Server. DNS’s either know or can discover where addresses server is. Domain name servers use IP’s (Internet Protocol) or numbered addresses to discover how to find the server. So first the query is converted to a number (e.g. 204.74.99.100 - that’s Gentoo’s) then the query is routed to that server.
By using a Local DNS, previously looked up domain name resolutions are saved on the local computer and therefore don’t require a query to a remote server. Using a local DNS can and does improve lookup times - expecially for busy DNS’s.
Local DNS are lightweight and don’t consume alot of the computer’s resources. There are several different options that provide a DNS this post covers two of them: dnsmasq in conjunction with net-misc/dhcp (dhclient) and dnscache. Both work just fine in Gentoo and for that matter, should for any other Linux.
Creating a local DNS cache with dnsmasq/dhclient
This method uses dhclient but several DHCP clients exist and should be able to be used with dnsmasq - for Gentoo users see “Using dhcpcd” below. Whatever dhcp client is used each dhcp client configuration will have to be configured to query the local loopback interface (127.0.0.1).
Install dnsmasq and dhclient (part of net-misc/dhcp) and add them to the runtime environment.
Install for Gentoo:
sudo emerge dnsmasq dhcp
Setup the dnsmasq configuration file to allow a local DNS in /etc/dnsmasq.conf.
listen-address=127.0.0.1
The name servers list file (/etc/resolv.conf) gets respawned everytime a DHCP client connects to a DHCP server. So every time dhclient runs the local loopback needs to be defined. In /etc/dhcp/dhclient.conf uncomment or add:
# /etc/dhcp/dhclient.conf prepend domain-name-servers 127.0.0.1;
Now DNSmasq (or local DNS cacher) and the dhclient need to be started and added to the default runlevels:
(Gentoo uses baselayout to manage it initialization scripts, several other initialization systems exist.)
sudo /etc/init.d/dnsmasq start sudo /etc/init.d/dhcpd start sudo rc-update add dnsmasq default sudo rc-update add dhcpd default
In Gentoo, specify which DHCP client baselayout should use in /etc/conf.d/net:
modules=( "dhclient" )
Before initializing the new network backup /etc/resolv.conf.
sudo cp /etc/resolv.conf /etc/resolv.conf.backup
Now with that set up, reconnecting to the internet will get the DHCP client to run again and set the /etc/resolv.conf to use the local loopback. Gentoo users can restart baselayouts network script by:
sudo /etc/init.d/net.yourdevice restart
/etc/resolv.conf should now look like this:
# /etc/resolv.conf # local loop for dnsmasq nameserver 127.0.0.1 # Charter DNS #search mad.wi.charter.com #nameserver 24.196.64.53 #nameserver 68.115.71.53 #nameserver 24.159.193.40search yourisp.com
To test if dnsmasq is correctly caching and using a local DNS, see “Test Test” below.
Using Gentoo’s dhcpcd instead of dhclient/dhcp
The instructions are the same as above to above but obviously with net-misc/dhcpcd instead of net-misc/dhcp. So set dnsmasq.conf as above, and manually edit /etc/resolv.conf to include the local loopback. Set dhcpcd not to overwrite /etc/resolv.conf in /etc/conf.d/net:
dhcpcd_eth0="-R"
(Obviously changing eth0 for your network device) and test whether the local DNS is working below.
Using Dnscache
Configuration of the dhcp client with dnscache is minimal.
emerge djbdns sudo cp /etc/resolv.conf /etc/resolv.conf.backup sudo /etc/init.d/svscan start sudo rc-update add svscan default
Dnscache has a setup utility, or it can be manually done. The setup utility works just fine for most people.
sudo dnscache-setup
Restart the network and check /etc/recolv.conf and make sure the local loopback is set.
DHCP clients that overwrite the local loopback entry
The dhcp client by default writes a new /etc/resolv.conf everytime it loads on boot.
Some dhcp clients can be told not to overwrite the /etc/resolv.conf file. In Gentoo it can be specified for dhcpcd not to overwrite /etc/resolv.conf in the /etc/conf.d/net file:
dhcpcd_eth0="-R"
Not all dhcp clients have this option though and /etc/resolv.conf file will need to restored. This is easy enough to do in the /etc/conf.d/local.start file.
First create a /etc/resolv.conf with a local loopback and back it up:
cp /etc/resolv.conf /etc/resolv.conf.loopback
And add to /etc/conf.d/local.start to restore it on boot:
cp /etc/resolv.conf.loopback /etc/resolv.conf
Test Test
To test install bind-tools. Test with the “dig” command on a website that hasn’t been visited before.
dig google.com
Do it again to see the lookup difference time.
Use of OpenDNS servers to speed up queries?
If the service provider DNS servers become jammed (some ISP’s are notorious for this) is have been suggested that OpenDNS can be useful.
NetworkManager
NetworkManager uses dhclient. So set up /etc/dhcp/dhclient.conf as listed above.
Thanks 2
This tidbit is began from Carthik Sharma’s original Local DNS for Faster Browsing, and built up from there. Thanks for the tip, Carthik .
2 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed




1.
Reto Bachmann-GMür | October 14, 2007 at 10:47 am
Thanks for this tidbit. I had to emerge dhcp as there was no package dhclient, the rest worked as you describe it. I still have to find out how to make it work when I connect with a ppp connection (using wvdial).
2.
dirkrgently | December 23, 2007 at 2:22 pm
Appreicate the reply Reto, I’ve updated the page. Unfortunately, I haven’t a ppp to test this, though I don’t imagine it would be different.