How To Set Caching DNS Server

Q. How do I set caching dns server to speed up dns lookup for my LAN (500 PC)? The DNS lookups creating a lot of network activity. There are a few times when this causes performance issues. How do I configure caching dns under Debian Linux?

A. Often many users and running daemon can perform tons of dns lookup. This can create problems for slow network connections as well the latency of the lookup can slow down overall experince for end users. For large sites / LANs / WANs it is recommended that you set your own caching dns servers.


dnsmasq dns caching server

dnsmasq is a lightweight DNS, TFTP and DHCP server. It is intended to provide coupled DNS and DHCP service to a LAN. Dnsmasq accepts DNS queries and either answers them from a small, local, cache or forwards them to a real, recursive, DNS server.

Step # 1: Install dnsmasq

Simply, type the following command under Debian / Ubuntu Linux:
# apt-get install dnsmasq
Red Hat, Fedora / CentOS Linux user can grab dnsmasq rpm here or just enter:
# rpm -ivh

Step # 2: Configure dnsmasq

Open /etc/resolv.conf
# vi /etc/resolv.conf
Set up upstream DNS servername i.e add ISP name server:

Save and close the file. Start dnsmasq dns caching server:
# /etc/init.d/dnsmasq start
Sample output:

Starting DNS forwarder and DHCP server: dnsmasq

Now test your caching server:
$ dig
Sample output:

; <<>> DiG 9.4.1-P1 <<>>
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24426
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 2

;           IN      A

;; ANSWER SECTION:    86341   IN      A

;; AUTHORITY SECTION:            51538   IN      NS            51538   IN      NS

;; ADDITIONAL SECTION:      222764  IN      A      220797  IN      A

;; Query time: 26 msec
;; WHEN: Tue Jun 17 00:50:02 2008
;; MSG SIZE  rcvd: 134

Try it one more time to see caching server in action:
$ dig
; <<>> DiG 9.4.1-P1 <<>>
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46610 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ; IN A ;; ANSWER SECTION: 86272 IN A ;; Query time: 0 msec
;; WHEN: Tue Jun 17 00:51:11 2008
;; MSG SIZE rcvd: 53

Step # 3: Optional dnsmasq configuration

The default configuration file is located at /etc/dnsmasq.conf. By default it works out of box. However, configuration files offers more options. It is possible to use dnsmasq to block Web advertising by using a list of known banner-ad servers, all resolving to or It is also possible redirect local LAN mail traffic to central mail hub server. Please refer dnsmasq man pages for all advanced configuration options.
$ man dnsmasq

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

13 comments… add one
  • Hai Aug 28, 2014 @ 10:14

    I have installed bind9, I can use bind9 is caching DNS ??

    • Hai Aug 28, 2014 @ 10:15

      my LAN (20 PC)

  • Per Oct 13, 2014 @ 12:40

    Yes. It’s actually configured as a caching server out of the box.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.