Introductions –
DNS server resolves domain names into IP addresses. So when you request “yahoo.com” for example, the DNS server finds out the address for the domain, and sends your request the right way.
You can run a DNS cache on your system This will speed up the process of looking up domain names while browsing. The difference is about 35-55ms. dnsmasq is the tool, we will be using for caching nameserver lookups
** How to install dnsmasq –
root@arunbagul:~# apt-get install dnsmasq
Reading package lists… Done
Building dependency tree
…
….
root@arunbagul:~#
** How to configure dnsmasq –
The configuration file for dnsmasq is /etc/dnsmasq.conf. you can specify customized setting in this file…
root@arunbagul:~# vi /etc/dnsmasq.conf
root@arunbagul:~#
Now uncomment the following line in /etc/dnsmasq.conf file. So that dnsmasq will listen on local loop ie 127.0.0.1 address.
listen-address=127.0.0.1
Now edit /etc/dhcp3/dhclient.conf and make sure that it looks as shown below…
root@arunbagul:~# cat /etc/dhcp3/dhclient.conf
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;
…
….
root@arunbagul:~#
How it dnsmasq works? –
If you are using DHCP connection ,then whenever you get a new dhcp lease, the dhcp3 client will updates the /etc/resolv.conf file on your system with the right values for the DNS servers to use. Adding the “prepend” option as we did above ensures that “nameserver 127.0.0.1” will appear on the top of the list of DNS servers. So in the future, whenever your system want to resolve a domain name, it will forward that request to local dnsmasq. dnsmasq maintains it’s own cache. If the details for the domain name are already in you cache, well and good, dnsmasq will serve it up and make the process real fast. If it is not in the cache, then dnsmasq will look at the /etc/resolv.conf file and use the nameservers listed below the line “nameserver 127.0.0.1″…
Check your /etc/resolv.conf file –
root@arunbagul:~# cat /etc/resolv.conf
search indiangnu.org
nameserver 192.168.1.10
nameserver 192.168.1.11
root@arunbagul:~#
where is “nameserver 127.0.0.1” entry? This is because. Since dhcp haven’t renewed your lease after editing the /etc/dhcp3/dhclient.conf file. Let us add that in manually. see below
root@arunbagul:~# cat /etc/resolv.conf
search indiangnu.org
nameserver 127.0.0.1
nameserver 192.168.1.10
nameserver 192.168.1.11
root@arunbagul:~#
** Now is to start dnsmasq so that the changes we made to the configuration file take effect-
root@arunbagul:~# /etc/init.d/dnsmasq
Usage: /etc/init.d/dnsmasq {start|stop|restart|force-reload}
root@arunbagul:~#
root@arunbagul:~# /etc/init.d/dnsmasq start
Starting DNS forwarder and DHCP server: dnsmasq.
root@arunbagul:~#
Yes!! we are running our local DNS cache. If you want to measure your speed improvement then dig to some domain and find out how fast is Local DNS cache!
root@arunbagul:~# dig yahoo.com
root@arunbagul:~#
command(1) dnsmasq – is a lightweight DHCP and caching DNS server. Dnsmasq accepts DNS queries and either answers them from a small, local, cache or forwards them to a real, recursive, DNS server. It loads the contents of /etc/hosts so that local hostnames which do not appear in the global DNS can be resolved and also answers DNS queries for DHCP configured hosts. The dnsmasq DHCP server supports static address assignments, multiple networks & DHCP-relay.
Options
-h, –no-hosts
Don’t read the hostnames in /etc/hosts.
-H, –addn-hosts=<file>
Additional hosts file. Read the specified file as well as /etc/hosts.
-E, –expand-hosts
Add the domain to simple names (without a period) in /etc/hosts in the same way as for DHCP-derived names.
-T, –local-ttl=<time>
When replying with information from /etc/hosts or the DHCP leases file dnsmasq by default sets the time-to-live field to zero, meaning that
the requestor should not itself cache the information. This is the correct thing to do in almost all situations.
-k, –keep-in-foreground
Do not go into the background at startup but otherwise run as normal. This is intended for use when dnsmasq is run under daemontools or
launchd.
-d, –no-daemon
Debug mode: don’t fork to the background, don’t write a pid file, don’t change user id, generate a complete cache dump on receipt on
SIGUSR1, log to stderr as well as syslog, don’t fork new processes to handle TCP queries.
-q, –log-queries
Log the results of DNS queries handled by dnsmasq. Enable a full cache dump on receipt of SIGUSR1.
-x, –pid-file=<path>
Specify an alternate path for dnsmasq to record its process-id in. Normally /var/run/dnsmasq.pid.
-u, –user=<username>
Specify the userid to which dnsmasq will change after startup. Dnsmasq must normally be started as root, but it will drop root privileges
after startup by changing id to another user. Normally this user is “nobody” but that can be over-ridden with this switch.
-g, –group=<groupname>
Specify the group which dnsmasq will run as. The defaults to “dip”, if available, to facilitate access to /etc/ppp/resolv.conf which is not
normally world readable.
-v, –version
Print the version number.
-p, –port=<port>
Listen on <port> instead of the standard DNS port (53). Useful mainly for debugging.
Cheers,
Arun Bagul