Wildcard subdomains hosts in ubuntu


The classic hosts file does not support wildcard domains. In fact, you have to specify everything manually for every subdomain. During development you sometimes need arbitrary subdomains to resolve to a certain development machine. So this is a bit annoying. Luckily it's not that difficult to set things up. That's what this page might help you with.

I'm basically following the instructions at https://groups.drupal.org/node/16862#debian, with the main exception that the config file works and I had to edit some other files. If not worky, check that link instead.

We will add a test domain called example.net

1. Install bind9 if it isn't there already
sudo apt-get install bind9

2. Add a "zone", this will be your target domain
sudo nano /etc/bind/named.conf.local

Somewhere in this (probably empty) file, and otherwise the end, add this:

zone "example.net" {
type master;
file "/etc/bind/db.example.net";

(Note that db. does not refer to a specific subdomain, I think it's just a name.)

3. You can check and confirm the syntax of this file, to make sure it's not crashing. If ok, it prints nothing.

named-checkconf /etc/bind/named.conf.local

4. Create the zone file (adds your development domain and wildcard subdomains)

sudo nano /etc/bind/db.example.net

Then paste this, replacing example.net with your domain (this may be just a "tld", or a subdomain). Make sure to keep trailing dots. They are important.
@ 86400 IN SOA example.net. root.example.net. (
20091028 ; serial yyyy-mm-dd
10800 ; refresh every 15 min
3600 ; retry every hour
3600000 ; expire after 1 month +
86400 ); min ttl of 1 day
@ 86400 IN NS example.net.
@ 86400 IN MX 10 example.net.
@ 86400 IN A
*.example.net. IN A

(Replace with the IP of your development machine!)

5. You can check and confirm the syntax of this file too. If ok, it will print OK

named-checkzone mydev /etc/bind/db.example.net

6. Update dhcp to use this dns first. If you're not on DHCP, check the original tutorial for instructions. Those instructions point you towards /etc/dhcp3/dhclient.conf, but I had to modify /etc/dhcp/dhclient.conf. If neither, do a locate dhclient.conf ...

nano /etc/dhcp/dhclient.conf

Find the line #prepend domain-name-servers; and remove the hash (#). If the line is not there I don't know what you should do. Make sure you're in the right file and add that line (without the hash) or maybe just bail.

7. Restart the dhcp client (I think you can use service for that, but this worked just the same for me)

sudo dhclient

8. (Re-)start bind9

sudo service restart bind9

9. Profit! Test by pinging your domains

ping example.net
ping foo.example.net

They should resolve to your development machine, provided it accepts pings ;)

Hope it helps you