≡ Menu

How to setup dnsmasq DNS caching server on Linux?

Dnsmasq is a light weight DNS caching server which will cache all DNS request sent through it. Configuring and installing dnsmasq is quite simple. By caching DNS queries, the lookup speed of DNS queries will improve. If you have a few DNS queries that run several times, the speed of those queries will increase significantly.

1) Install dnsmasq

If you have a Debian / Ubuntu system, use the following command:

apt-get install dnsmasq

If you have a CentOS, Red Hat or Fedora Linux distribution:

yum install dnsmasq

On my CentOS box, the above command produces the following output:

[root@server ~]# yum install dnsmasq
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.trouble-free.net
 * extras: mirror.solarvps.com
 * rpmforge: mirror.yongbok.net
 * updates: mirror.itc.virginia.edu
base                                                     | 3.7 kB     00:00
extras                                                   | 3.5 kB     00:00
rpmforge                                                 | 1.9 kB     00:00
updates                                                  | 3.4 kB     00:00
vz-base                                                  |  951 B     00:00
vz-updates                                               |  951 B     00:00
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package dnsmasq.i686 0:2.48-13.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch           Version                  Repository      Size
================================================================================
Installing:
 dnsmasq           i686           2.48-13.el6              base           145 k

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 145 k
Installed size: 282 k
Is this ok [y/N]: y
Downloading Packages:
dnsmasq-2.48-13.el6.i686.rpm                             | 145 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : dnsmasq-2.48-13.el6.i686                                     1/1
  Verifying  : dnsmasq-2.48-13.el6.i686                                     1/1

Installed:
  dnsmasq.i686 0:2.48-13.el6

Complete!

2) Change /etc/resolv.conf

To make dnsmasq run as a caching DNS server, all DNS queries must go through it. To do this, change the following file:

/etc/resolv.conf

Add the following line at the top of the file:

nameserver 127.0.0.1

This will ensure that all DNS queries sent by your web browser or your email client will go through the DNS server run by dnsmasq! Dnsmasq ignores the first line and sends the request to the other name servers specified in /etc/resolv.conf.

3) Restart dnsmasq

Now you must restart the dnsmasq service:

[root@server ~]# service dnsmasq restart
Shutting down dnsmasq:                                     [  OK  ]
Starting dnsmasq:                                          [  OK  ]
[root@server ~]#

4) Check if dnsmasq is listening on port 53:

As dnsmasq is a DNS server which forwards requests to other upstream DNS servers, it must listen on port 53.

To check if dnsmasq is indeed running an listening on port 53, use the netstat command:

[root@server ~]# netstat -plant | grep :53
tcp        0      0 0.0.0.0:53                  0.0.0.0:*                   LISTEN      13739/dnsmasq
tcp        0      0 :::53                       :::*                        LISTEN      13739/dnsmasq
[root@server ~]#

As the above output shows, dnsmasq is listening on port 53!

Another way to check this is to use the lsof tool:

[root@server ~]# lsof -i | grep dnsmasq
dnsmasq  13739 nobody    4u  IPv4 1622196865      0t0  UDP *:domain
dnsmasq  13739 nobody    5u  IPv4 1622196866      0t0  TCP *:domain (LISTEN)
dnsmasq  13739 nobody    6u  IPv6 1622196867      0t0  UDP *:domain
dnsmasq  13739 nobody    7u  IPv6 1622196868      0t0  TCP *:domain (LISTEN)
[root@server ~]#

5) Check your DNS speed for repeated queries

Now run the same DNS query multiple times and you will see that the speed with which your DNS queries run improves considerably:

[root@server ~]# dig bing.com A

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> bing.com A
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54291
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;bing.com.                      IN      A

;; ANSWER SECTION:
bing.com.               1297    IN      A       204.79.197.200

;; Query time: 22 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Sep 22 13:20:49 2013
;; MSG SIZE  rcvd: 42

[root@server ~]# dig bing.com A

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> bing.com A
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26967
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;bing.com.                      IN      A

;; ANSWER SECTION:
bing.com.               1295    IN      A       204.79.197.200

;; Query time: 1 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Sep 22 13:20:51 2013
;; MSG SIZE  rcvd: 42

[root@server ~]#

So that was a quick tutorial on how to install the DNS caching server dnsmasq and how to run it on your Linux system. DNS caching is very useful and can speed up DNS requests by several orders of magnitude. By caching repeated DNS requests, you also cause less load on the upstream open recursive name servers (ISP, OpenDNS, Google Public DNS etc.). It will also save you a small amount of network traffic by caching the DNS answers locally on your Linux box. I hope you enjoy running dnsmasq and leave your comments with your findings!

Incoming search terms:

  • how to check dnsmasq is running (1)

{ 0 comments… add one }

Leave a Comment