Networking

How to speed up DNS caching on Linux machines with dnsmasq

If you're looking to speed up DNS on your Linux machines, Jack Wallen shows you how with the simple to use dnsmasq.

networkhero.jpg
Image: Jack Wallen

Chances are, you're looking to squeeze out as much performance from your Linux servers and desktops as possible. This is especially so with regards to networking. With Linux, there are so many tweaks you can undertake to optimize a machine, it's mind blowing. One such tweak that can significantly decrease your DNS lookups is by adding dnsmasq into the mix.

Dnsmasq is a lightweight DHCP and DNS caching nameserver. Unlike BIND, dnsmasq is incredibly simple to get up and running. The benefits of adding this will be quite obvious (like your DNS queries getting dramatically faster).

I want to walk you through the steps of installing and configuring dnsmasq. I'll demonstrate on a Ubuntu 16.10 machine, but the software is available in the standard repositories of most distributions (which means that if you're using a different flavor of Linux the steps will only require slight alteration).

Installation

The first thing you must do is install dnsmasq. To do this, open up a terminal window and issue the following command:

sudo apt install dnsmasq

Once that command completes, the software is installed and ready to be configured.

Configuration

There are three files that must be configured. The first is /etc/dnsmasq.conf. Open that file in your text editor of choice and look for the line:

#listen-address=

Change the above line to:

listen-address=127.0.0.1

Optionally, you could increase the cache size for dnsmasq. Look for the line #cache-size=150. Remove the # and change the 150 to 1000 or whatever size you might need (there is a hard-limit of 10000). Increasing this will allow dnsmasq to cache more names (which is especially important if the machine you're working on would benefit from caching more than the default.

Save and close the file.

Next open up the file /etc/dhcp/dhclient.conf. Look for the line #supersede domain-name "fugue.com home.vix.com"; and make sure the following section looks exactly like this:

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;

NOTE: From request to netbios-scope is all one line.

Save and close that file.

Finally, open up your /etc/resolv.conf file and make sure that it includes (at least) the line:

nameserver 127.0.0.1

Restart the dnsmasq service with the command:

sudo service dnsmasq restart

Testing the improvements

Testing dnsmasq is quite simple. Go back to your terminal window and issue a command like dig techrepublic.com. The first time you do this, you should see fairly normal results (Figure A).

Figure A

Figure A

Our first dnsmasq run.

Notice the query time of 48 msec.

Run the same command again, and you should see a considerable improvement over the query times (Figure B).

Figure B

Figure B

Our configuration is working.

We now see a 0 or near 0 query time for the same command. When a machine is having to query a significant amount of addresses, that time savings adds up.

A much-needed improvement

I've used dnsmasq on a number of Linux machines and always found it added a much-needed improvement to networking speeds. Give this easy DHCP and DNS caching nameserver a go and see if it doesn't find your Linux machines screaming on the network.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox