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:

bash
# 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

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:

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

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

# 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

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.

Configuring the client PCs to use the new DNS server

Under linux based systems, you have to edit /etc/resolv.conf. On my system the file looks like this by default:

bash
# /etc/resolv.conf.head can replace this line
domain home.local
nameserver 192.168.1.1
# /etc/resolv.conf.tail can replace this line

What we have to do is add the new nameserver, as the first one. Depending on where you installed the DNS server (locally, or another PC), record it into the /etc/resolv.conf:

bash
# /etc/resolv.conf.head can replace this line
nameserver 127.0.0.1
domain home.local
nameserver 192.168.1.1
# /etc/resolv.conf.tail can replace this line

I used 127.0.0.1, because this example covers installing it locally, but obviously you have to change the IP if you installed it to another PC. It is important that the new one should be the first one! Make sure it's the first nameserver line, and you are good.

If you have Windows, it's a little more convoluted process as usual, open the Network and Sharing center, click on Change adapter settings in the left side, right click on your connection (usually Local Area Connection), click properties, click on Internet Protocol Version 4, click Properties, check "Use the following DNs server", and enter the IP of the PC where dnsmasq is running into "Preferred DNS server".

Now, try pinging, or opening an address in your browser, that ends with what you configured in the first step. If you did everything correctly, it should resolve successfully.

One last step

If you are using Linux, remember the advice in /etc/resolv.conf, that /etc/resolv.conf.head/tail can replace the beginning/ending. This is important, because depending on what you use as a DHCP client, the resolv.conf file will get overwritten on every restart, erasing your changes. So, open up /etc/resolv.conf.head, and move the nameserver line there, and verify after restarting that everything is fine.

An alternative solution: xip.io

The guys at 37signals (the company most notable for Campfire, and Basecamp) came up with a pretty nifty solution. They wrote a modified DNS server, that will always return the same IP, that was supplied, for hostnames ending with *.xip.io. So, if your internal IP is for example 10.0.0.1, and you make a DNS request to 10.0.0.1.xip.io, it will return 10.0.0.1! Zero-configuration DNS names! You just have to make virtualhosts that end with .xip.io. I'm not sure if it would actually work with 127.0.0.1.xip.io, but a quick test with nslookup seems to confirm it's working just fine, though I have heard, that firewalls and routers don't really like when a DNS resolves to some internal IP (or even more, localhost), and they might just filter it out, keep this in mind.

It has one very serious advantage, when you are testing from mobile devices, where there is no easy way to either edit the hosts file, or change the DNS server. Just set up a virtualhost alias with a xip.io ending, and you can test away.