UPDATE: I wrote a new article on using dnsmasq on my new blog here.
So you’re developing networked applications on your own computer and now you want to test using different host names or URLs that route to localhost (because obviously that’s where your running everything). Surely you could go on and edit your hosts file every time you need a new host name, but that becomes tedious and annoying quickly. This article will describe how to use Dnsmasq on Ubuntu 11.04 to setup a (top level) domain for which all host names will route to localhost (your mileage will vary on other distros).
Besides being a DNS forwarder and DHCP server Dnsmasq also offers you the capability to force domains of your choosing to route to specific IP addresses through it’s configuration file. Simply adding a TLD (top level domain) in there will give you a limitless pool of host names to use that will all go to the specified IP address. Note though that if you override existing domains that your configuration will take precedence… so be very conscious of what you setup or you may end up being very confused.
To get started install the packages dnsmasq and resolvconf. resolvconf ensures that Dnsmasq works with your preexisting DNS settings (if you wouldn’t install this package then your system would essentially ignore Dnsmasq). Once both packages are installed you’ll want to edit Dnsmasq’s configuration file: /etc/dnsmasq.conf
To get setup find and edit the below settings:
Using the above settings on top of the default does the following things (in the same order as the list above):
- Setup the top level domain dev to route to localhost (like Pow sets up for Mac OS X does, so any host name that ends in .dev will be resolved to 127.0.0.1 aka localhost). If you want more (top level) domains to route to specific IP addresses you can simply add addiontal address=… lines.
- Tell Dnsmasq to only work with the loopback interface (the virtual localhost network card, this prevents other systems from querying your Dnsmasq instance for DNS information)
- Disable the DHCP service on the loopback interface (since that’s the only active network interface for Dnsmasq we’ve disabled the DHCP service alltogether).
To make this all work properly you’ll have to reboot, once you have you can test your settings by accessing a terminal and using the nslookup command:
mkremer@devlin:~$ nslookup test.me.dev Server: 127.0.0.1 Address: 127.0.0.1#53 Name: test.me.dev Address: 127.0.0.1 mkremer@devlin:~$ nslookup www.without-brains.net Server: 127.0.0.1 Address: 127.0.0.1#53 Non-authoritative answer: www.without-brains.net canonical name = without-brains.net. Name: without-brains.net Address: 18.104.22.168 mkremer@devlin:~$
As you can see in the above output nslookup queries the DNS server running on localhost, where test.me.dev returns 127.0.0.1 and without-brains.net returns its matching IP adres.