DNS Cache with dnsmasq
Improve the browsing speed is something I’m always looking for. Some time ago I’ve written about DNS cache, with
pdnsd this time I will write about dnsmasq, another DNS cache, that also works as DHCP server.
How it works?
A DNS cache works by saving locally all queries to DNS servers, and then any future request for the same query is served immediately, as there is no network delay, the idea is install dnsmasq in your own PC, or in a PC in your local network.
Depending on how far or near you are from the DNS server, the latency can add substantial time to the server response.
Here is how a normal request for a web page works.
- The user type the url in the browser and hit enter
- The user’s computer looks in its nameserver list and send the request to the first one, asking for the IP number of the web server the user is trying to access
- The DNS server send the IP back to the user’s computer
- The user’s computer now, contacts the web server and ask for the web page
- The web server sends the user’s computer the web page
- The user can start watching at the requested web page.
dnsmasq can improve is the time between 2 and 3, this will only work for the second and so on requests, as the first one, needs to go to the Internet DNS server to query the IP.
Debian / Ubuntu
sudo aptitude install dnsmasq
Fedora / CentOS
sudo yum install dnsmasq
sudo pacman -Sy dnsmasq
The main configuration file, should be at
/etc/dnsmasq.conf, it is a good idea, to keep the original file for any future reference, so I always copy it before changing anything.
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.original
Now that we have saved the original file, we can start doing changes, usually the file itself is self explanatory, and reading it will let you learn how it works.
I will put here a basic configuration file, and explain the options I am adding.
This tells dnsmasq to respond only to the local machine
The port where dnsmasq should listen
This is going to tell dnsmasq, to not pass to the upstream servers, names without dots, or domain part, thus avoiding unnecessary charge over root DNS servers.
This will avoid sending to the parent DNS server forward addresses in the non-routed address spaces.
The default for this value is 150, which means 150 hosts addresses.
This tells dnsmasq where to look for parent servers, the syntax of this file, is the same as in
/etc/resolv.conf, here is mine.
nameserver 184.108.40.206 nameserver 220.127.116.11
Here is the full configuration file:
listen-address=127.0.0.1 port=53 domain-needed bogus-priv cache-size=1000 resolv-file=/etc/dnsmasq-parent-servers
Configuring the client
We now need to tell our computer to use pdnsmasq as DNS server instead of the normal ones.
If you are using
dhcp which is DHCP client daemon, you can edit the file
/etc/resolv.conf.head and add there:
If using dhclient, you need to edit the file
/etc/dhcp3/dhclient.conf and uncomment the following line, or add it if it doesn’t exists.
prepend domain-name-servers 127.0.0.1;
Then restart the server, and you are ready to go.
To test, run:
Two times, the second time the query time should be cero.
dnsmasq is a light and powerful dns server, that lets you run it on your PC or Laptop, and cache all DNS responses to your queries, reducing waiting times for the response of the DNS server.
One thing that is better in pdnsd, for me is that is stores the cache in a file, and then, it can re-use it the next time you boot your PC.