Local DNS Cache for Faster Internet Browsing on your Linux Mint

genothomas
  13 years ago
  13

 

A DNS server resolves domain names into IP addresses. So, when you request "www.google.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 for me. Multiply that difference by the number of websites you visit a day for an approximate estimate of the speed improvement. Of course, all this would be worth it if it weren't for the fact that setting this up is way too easy.
 
The following instructions are for someone with a cable (broadband) internet connection, where the computer gets it's local IP address using DHCP from the router in your house/office:
 
The package we will be using for caching nameserver lookups is called dnsmasq. So first, install it using:
 
sudo apt-get install dnsmasq
 
(If you can't find then, then you probably haven't added the Universe repository to your list of repositories.)
 
No uncomment the following line (that is edit the line to NOT have a "#" in the beginning) in the file /etc/dnsmasq.conf:
 
Run in terminal:
 
sudo gedit /etc/dnsmasq.conf
 
Find listen-address= and change to 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;"
 
sudo gedit /etc/dhcp3/dhclient.conf
 
#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, domain-search, host-name,
netbios-name-servers, netbios-scope, interface-mtu,
rfc3442-classless-static-routes, ntp-servers;
require subnet-mask, domain-name-servers;
timeout 60;
retry 60;
reboot 10;
select-timeout 5;
initial-interval 2;
 
Explanation for the above change: 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 in your text editor. It probably looks like:
 
# Generated by NetworkManager
nameserver 218.248.241.5
nameserver 8.8.8.8
 
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:
 
# Generated by NetworkManager
nameserver 127.0.0.1
nameserver 218.248.241.5
nameserver 8.8.8.8
 
"Don't worry if the numbers are different"
 
Okay. We are almost done here. All we have to do now is to restart dnsmasq so that the changes we made to the configuration file take effect. You can do that using the command:
 
sudo /etc/init.d/dnsmasq restart
 
Now you are running a local DNS cache. If you want to measure your speed improvement, type the command:
 
dig google.com
 
You will see something like ";; Query time: 42 msec" there.
Now type the command again, and you should see something like ";; Query time: 0 msec"
 
See, the first time, since www.google.com's details were not in your cache (you are using it for the first time), the query took 42 ms. The second time, the cache speeds up the lookup. I have been using this for over a month now, and haven't had any problem.
 
A DNS server resolves domain names into IP addresses. So, when you request "www.google.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 for me. Multiply that difference by the number of websites you visit a day for an approximate estimate of the speed improvement. Of course, all this would be worth it if it weren't for the fact that setting this up is way too easy.
 
The following instructions are for someone with a cable (broadband) internet connection, where the computer gets it's local IP address using DHCP from the router in your house/office:
 
The package we will be using for caching nameserver lookups is called dnsmasq. So first, install it using:
 
sudo apt-get install dnsmasq
 
(If you can't find then, then you probably haven't added the Universe repository to your list of repositories.)
 
Now uncomment the following line (that is edit the line to NOT have a "#" in the beginning) in the file /etc/dnsmasq.conf:
 
Run in terminal:
 
sudo gedit /etc/dnsmasq.conf
 
Find listen-address= and change it to 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;"
 
sudo gedit /etc/dhcp3/dhclient.conf
 
#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, domain-search, host-name,
netbios-name-servers, netbios-scope, interface-mtu,
rfc3442-classless-static-routes, ntp-servers;
require subnet-mask, domain-name-servers;
timeout 60;
retry 60;
reboot 10;
select-timeout 5;
initial-interval 2;
 
Explanation for the above change: 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 in your text editor. It probably looks like:
 
# Generated by NetworkManager
nameserver 218.248.241.5
nameserver 8.8.8.8
 
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:
 
# Generated by NetworkManager
nameserver 127.0.0.1
nameserver 218.248.241.5
nameserver 8.8.8.8
 
"Don't worry if the numbers are different"
 
Okay. We are almost done here. All we have to do now is to restart dnsmasq so that the changes we made to the configuration file take effect. You can do that using the command:
 
sudo /etc/init.d/dnsmasq restart
 
Now you are running a local DNS cache. If you want to measure your speed improvement, type the command:
 
dig google.com
 
You will see something like ";; Query time: 42 msec" there.
 
Now type the command again, and you should see something like ";; Query time: 0 msec"
 
See, the first time, since www.google.com's details were not in your cache (you are using it for the first time), the query took 42 ms. The second time, the cache speeds up the lookup.
I have been using this for over a year now, and haven't had any problem.
 
Thanks. smiley
Comments
aplatypus 9 years ago

Very helpful but probably needs an update. The dhcp3 is now in directory

/etc/dhcp/

I have Mint 17 -- And a few of us have had trouble with intermintent DNS drop-outs on domains like google.com, yahoo.com, linkedin.com, youtube.com.

The most obvious thing I found is that there was NO

/etc/dnsmasq.conf

on the system (fresh install). What's up with that anyway?

Thankfully this tutorial seems to be OK so far. Only time will tell when I've had a few weeks of good DNS-ing and been able to read my email at home. It is looking good.

dig mail.google.com

Is down from 243 ms to 73ms!

many (hopeful) thanks.


wyrdoak 13 years ago

It seems like 127.0.0.1 drops out on reboot and it goes with the one my router assigns. but the google ping went from around 600msec to 100msec
thanks for the tip.


blueXrider 13 years ago

Need to go HERE for support questions.


TK14 13 years ago

my hosts file?
could you be more precise
/me newbie :)


TK14 13 years ago

thx for the tuto
but i got a probleme
every time i restart my computer
idont find nameserver 127.0.0.1 in my resolv.conf file
i wonder whats wrong?


trollboy 13 years ago

Nice!

If you are using static IP addresses, you don't need to worry about the dhclient.conf changes so it's even easier :)