Random Tech Thoughts

The title above is not random

Use Dnsmasq to Setup a Local DNS Server

In a previous article, I introduced how to use PowerDNS to setup a local DNS server for my lab. But it turns out that Dnsmasq is a better choice for the task. It’s much much easier to setup since it’s designed for this, the web page of Dnsmasq states that:

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 … Dnsmasq is targeted at home networks using NAT … but would be a good choice for any smallish network (up to 1000 clients is known to work) where low resource use and ease of configuration are important.

Installation and configuration

On debian, use aptitude install dnsmasq to install Dnsmasq. The configuration file is located at /etc/dnsmasq.conf, which contains very useful comments and you even don’t need to refer to the man page. In my lab, I just use the DNS service provided by Dnsmasq, so I’m going to talk only about the DNS service.

By default, Dnsmasq read /etc/resolv.conf to find the upstream DNS server, and read /etc/hosts for local host name, after dnsmasq server is up, you already have a working local DNS server! It’s just that simple! To add name for a machine, just add that in /etc/hosts and then send SIGHUP to dnsmasq.

There’s only a little configuration options I want explain more about.

  • expand-host: by default, simple host names in /etc/hosts are treated as top level domains. If this option is set, simple names in the hosts file will also be expanded with the value of domain option. E.g. If domain=lab, then you can use either “serv”, “serv.” to lookup the “serv” machine. If “expand-host” is set, you can also use “serv.lab”.
  • local: tells dnsmasq to answer queries from hosts file or DHCP, not from the upstream DNS server. E.g. local=/lab/
  • address: we can use this to force a name to an IP address. This can override upstream DNS server’s DNS record. We can also do this by adding names in the hosts file.