hugopereira84
4/9/2015 - 4:23 PM

USING WILDCARDS IN THE HOSTS FILE (From: http://ihaveabackup.net/2012/06/28/using-wildcards-in-the-hosts-file/)

Common scenario: it is the beginning of a new project, you create a new virtualhost in your webserver, record newproject.local into your hosts file, so the address resolves to your local machine, restart webserver, test. Is there an easier way? Yes, it is possible to spare the part where you edit the hosts file.

The title of the post is partly a google bait, because wildcards are not possible in the hosts file, sorry about that. But, there is still way however, to get rid of editing that file, alltogether. What you need is a DNS server on the network somewhere, that you can configure, or, you could just install one on your own machine locally. Which one to choose depends on what you need. If you need to have some “in house” addresses, that resolve for everyone on the network, install it on a PC that everyone can access. If you just want to spare editing your own hosts file every time you start a new project, install it locally.

This post will cover installing dnsmasq locally, and setting up your linux PC to use it, but the theory is the same for every OS:

Set up a dns server on one of the machines on the network (or locally)
Configure it to return an IP for addresses matching *.local (or whatever ending you choose)
Configure the client PCs to use this server as primary DNS
INSTALLING AND CONFIGURING DNSMASQ
This obviously depends on your distribution, unless you want to compile for source. Use your package manager to install dnsmasq, it’s a pretty stable and well known program, you should find it. The default configuration for dnsmasq is about ~600 lines long, but fortunately for our purpose, we only need to edit a few lines of it. Open /etc/dnsmasq.conf, and search for this line:

Uncomment the #address line, and change it to:

address=/local/127.0.0.1
This will result in every domain ending with .local being resolved to 127.0.0.1, the machine that dnsmasq runs on. Obviously this is only good in the scenario when you are installing it for your local machine. If you want a shared setup, for example, you want every developer PC to have their own domain, and all of them should resolve for everyone, you specify them by repeating the address line:

It is also a good idea to enable a few more settings:

Save the file, and start dnsmasq. Again, this is distro specific, but the package should install an init script, so in Debian based systems, it will be something like /etc/init.d/dnsmasq start, under Arch it is rc.d start dnsmasq (run these as root). It should start without errors.

# Add domains which you want to force to an IP address here.
# The example below send any host in double-click.net to a local
# web-server.
#address=/double-click.net/127.0.0.1

address=/developera/192.168.0.101
address=/developerb/192.168.0.102
address=/developerc/192.168.0.103

# These will cause dnsmasq to change user and group after starting, so as to not run as root, which is always preferable
user=nobody
group=nobody
# This will disable the DHCP and TFTP capabilities. Dnsmasq has a lot of features, but we only want the DNS support
no-dhcp-interface=
# This will cause dnsmasq to only responnd to local requests, use this only if you are installing locally
listen-address=127.0.0.1