How to setup wildcard subdomains in Linux
Mon Jul 04 21:21:48 UTC 2011
This simple tutorial will show you how to setup an small localhost nameserver to support wildcard subdomains in your development environment.

The file /etc/hosts is very useful and commonly used when you have to map an IP to certain server name/domain. The format is as follow:
127.0.0.1 www.ponup.dev.ar
The problem appears when you want to map an unknown or high number of subdomains. It is still possible to use the /etc/hosts to define each of those subdomains:
127.0.0.1 autos.ponup.dev.ar casas.ponup.dev.ar empleos.ponup.dev.ar
But it has an important drawback. It won't resolve something out of that definition. blog.ponup.dev.ar is not reachable, m.ponup.dev.ar is not reachable, and user.nn.ponup.dev.ar is not reachable as well. (The list of possible subdomains is never ending)
How can we solve this? Install a local DNS server is the answer. What I use in my Linux boxes for its simplicity and flexibility is dnsmasq.
As with other .deb packages, the installation is as easy as sudo apt-get install dnsmasq, and only one line is required to modify if you want to configure the domain of this example.
1. Open the dnsmasq configuration:
sudo vim /etc/dnsmasq.conf
2. And add a line like this:
address=/ponup.dev.ar/127.0.0.1
3. After this, a restart of the dnsmasq service is necessary.
sudo service dnsmasq restart
4. Edit the file /etc/resolv.conf to put the new local nameserver at work.
nameserver 127.0.0.1
nameserver 8.8.8.8
nameserver 8.8.4.4
ping whatever-you-need.ponup.dev.ar and the resolution will work!
Keywords: wildcard,subdomain,domain,dns,dnsmasq,hosts,nameserver
