You are here

DNS entries for local development using dnsmasq

Sometimes it's handy to have local DNS entries, for example when you're working on a copy of a site on your local machine - perhaps via a VM, vagrant, lxc or docker. A simple way of doing this is to add entries to your hosts file e.g.:

# local site
127.0.0.1   dev.mcdruid.co.uk

It can be a bit of pain though having to set one - or sometimes more - of these up for every new site you work on.

dnsmasq is a really useful utility for DNS and DHCP. It may well be what's providing DHCP on your router - it is on mine, which runs OpenWrt.

You can use dnsmasq to set up a wildcard DNS entry for a fake top level domain which you use for local development. For example:

# Wildcard DNS entries
address=/.devel/127.0.0.1

That would mean dnsmasq will return A records for all domains ending in .devel pointing them at localhost:

$ dig +noall +answer example.devel
example.devel.              0       IN      A       127.0.0.1
 
$dig +noall +answer foo.example.devel
foo.example.devel.          0       IN      A       127.0.0.1

...which can be very handy. You can use different IPs (perhaps for a VM's network interface), and it's also possible to configure CNAME and other types of records.

You could put these entries straight into the dnsmasq config file (typically in /etc/dnsmasq.conf) - for example on your router if dnsmasq is running there and you have access to that file. You can also run dnsmasq locally.

On debian / ubuntu desktops, you'll probably find that dnsmasq is already running - and there's a handy dnsmasq.d directory into which you can drop config files which will get picked up. For example on xenial / 16.04 you can see dnsmasq running:

$ ps aux | grep dnsmasq
 
nobody   25133  0.0  0.0  52880  4304 ?        S    Nov02   0:02 /usr/sbin/dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/var/run/NetworkManager/dnsmasq.pid --listen-address=127.0.1.1 --cache-size=0 --conf-file=/dev/null --proxy-dnssec --enable-dbus=org.freedesktop.NetworkManager.dnsmasq --conf-dir=/etc/NetworkManager/dnsmasq.d

...and that there's a /etc/NetworkManager/dnsmasq.d directory which dnsmasq will check for config files. You can put a file called e.g. custom.conf in there. You may need to restart NetworkManager for the new config to be picked up.

Other versions may have the directory in different places - e.g. on precise / 12.04 it seems to be just plain /etc/dnsmasq.d

No more littering your hosts file with entries for every local site!