Gih's Blog

只言片语

Setup a DNS cache server using dnsmasq

2014-08-08 by gihnius, tagged as internet

Dnsmasq is a lightweight, easy to configure, DNS forwarder and DHCP server. It is designed to provide DNS and optionally, DHCP, to a small network. It can serve the names of local machines which are not in the global DNS. 

apt-get update
apt-get install dnsmasq

if installed successful, test it by:

dig google.com @localhost

Setup local caching

Edit /etc/dnsmasq.conf (default location in debian linux).

#listen-address=0.0.0.0 ## default 
port=53

no-poll

no-resolv

#forwarding google dns and opendns
server=8.8.8.8
server=8.8.4.4
server=208.67.222.222
server=208.67.220.220

cache-size=1024

neg-ttl=7200

max-ttl=86400

interface=eth0

interface=lo0

In order to configure dnsmasq to act as cache for the host on which it is running, put  nameserver 127.0.0.1 in /etc/resolv.conf to force local processes to send queries to dnsmasq. 

in /etc/resolv.conf:

nameserver 127.0.0.1

then restart dnsmasq. /etc/init.d/dnsmasq restart.