Skip to main content

A local development server with dnsmasq (Gentoo)

When you build websites on a regular basis, it is nice to have a certain Top Level Domain (TLD) to point at your development machine. For example, every domain ending in ".dev" or ".loc" should automatically point to your machine.

The lame way to do this is to add all these domains to your host file. This sucks because you have to edit your host file for every new website. Not only is this way too much effort for us lazy developers, but it also pollutes your host file with lots of unnecessary entries. So we are gooing to do this the smart way with DNS-masq.

dnsmasq is a simple DNS/DHCP-server with a very small footprint and easy configuration. So let's emerge that first:

emerge --ask --verbose dnsmasq

When ready, we want to add one rule to the config file that makes every .loc domain point to our machine. So edit the config file /etc/dnsmasq.conf and add the following line:

address=/.loc/127.0.0.1

Now start dnsmasq and add it to the default runlevel:

rc-update add dnsmasq default
/etc/init.d/dnsmasq start

Now we need to add or new local DNS-server to /etc-resolv.conf so that resolving actually takes place. /etc/resolv.conf is a generated file, and you should not edit it directly. Instead, we edit /etc/resolv.conf.head which is automatically included by NetworkManager. Make sure that /etc/resolv.conf is a symlink to /run/NetworkManager/resolv.conf. Now add the following line to /etc/resolv.conf.head

nameserver 127.0.0.1

Now all you have to do is restart your network by turning it off and on again with NetworkManager. Test if it's working by pinging devsite.loc in your terminal. It should point to 127.0.0.1