DNSmasq is a lightweight tool to provide both DNS service and DHCP service for small-scale networks.
DNSmasq’s local DNS cache helps response time during internet browsing. Because the domain names get stored locally on the first visit to a site, the browser doesn’t have to go to a domain name server (DNS). It improves the browsing speed.
The dynamic host configuration protocol (DHCP) service provides a computer the ability to assign network addresses to other computers in the network. DNSmasq’s DHCP service can provide both static and dynamic DHCP leases.
When DNSmasq receives a query, it looks for the entry in the local cache. If the answer is not found, then it forwards it to a real DNS server. It also monitors /etc/hosts. It gives you the ability to control traffic using /etc/hosts file. If you want to retract access to a site called www.restricthissite.com, you can enter “0.0.0.0 www.restrictthissite.com” into your /etc/hosts file. You can use this strategy to locally block unwanted sites.
The “dnsmasq” package can interfere with Network Manager which can provide DHCP service through the use of “dnsmasq-base” package. So, for Network Manager users, the recommendation is to install “dnsmasq-base” without “dnsmasq” package.
Installing DNSmasq
The “dnsmasq” is an Ubuntu standard package. So, you can install it using the following commands
$ sudo apt-get install dnsmasq
All the configuration information for DNSmasq resides in the following file:
/etc/dnsmasq.conf
Setting Up Local DNS Cache
The job of a DNS server is to translate human-readable domain names to IP addresses. When a browser requests a site like www.google.com, this domain name needs to be translated into an IP address like 172.217.5.196. DNS servers have the domain-name-to-IP-address tables. When you set up a local DNS cache, your browser requests don’t have to go to these remote DNS servers to get the IP addresses every time. The DNS cache stores the domain-name-to-IP-address entry. It can shave off hundreds of milliseconds from your browsers query time. The result is faster responses.
In order to activate DNS cache, open the following file in an editor like gedit or vim in administration mode:
/etc/dnsmasq.conf
And change the following line:
#listen-address= To listen-address=127.0.0.1
Next open the following file in an editor:
/etc/dhcp3/dhclient.conf
Change the following line:
#prepend domain-name-servers 127.0.0.1; To prepend domain-name-servers 127.0.0.1;
The prepend option is telling the computer to check locally (127.0.0.1) for the DNS server. So, the request for resolving domain names will first go to the dnsmasq.
Next, open the following file in an editor:
/etc/resolv.conf
The file should look like this:
# Generated by dhcpcd from eth0.dhcp # /etc/resolv.conf.head can replace this line nameserver 192.168.65.1 # /etc/resolv.conf.tail can replace this line
Add the “nameserver 127.0.0.1” to the resolve.conf file as the first active line:
# Generated by dhcpcd from eth0.dhcp # /etc/resolv.conf.head can replace this line nameserver 127.0.0.1 nameserver 192.168.65.1 # /etc/resolv.conf.tail can replace this line
Restart the DNSmasq using the following command:
$ sudo /etc/init.d/dnsmasq restart
Now you have a DNS cache set up on your computer. The domain name and IP address of any new site