Getting Wildcard DNS running on OSX Snow Leopard

Note: it took me several tries to get this working. If you are new to DNS be patient with yourself - you'll get it, but may take a few tries.

Why run a wildcard DNS server when its such a pain to set up?

A wildcard DNS setup lets you automatically have subdomains for a given domain. For example, say you own the domain widgets.com and you want to setup an unlimited number of subdomains like dev.widgets.com, test.widgets.com, customers.widgets.com, etc.... Typically you would have to set these all up individually. A wildcard DNS can let you bypass a lot of configuration. In a development environment it can let you setup any number of test/development sites very quickly and easily. Drupal developers in particular can leverage Drupal's multisite installation feature to setup lots of sites for development or production very quickly.

In this example I will concentrate on setting a development environment with OSX using wildcard DNS

Overview of Steps

I. Edit /etc/named.conf to add a zone.
II. Add a zone file at /var/named/
III. Check the syntax of named.conf and your zone files for errors
IV. Edit /etc/resolv.conf
V. Set your computers network settings to use 127.0.0.1 as a name server
VI. Start up Bind
VII. Check setup with dig
VIII. Reboot if needed

Before we start a few more notes.

I tend to use the nano text editor to edit Unix configuration files you could use Emacs, VI, Textmate, BBEdit or the editor of your choice.

Backup all these files we are editing so you can start over if you mess up. I didn't do this and it added more time to the project.
For example to backup /etc/named.conf do:

$ sudo cp /etc/named.conf /etc/named.conf.bck

Last, most of the files we need to edit are owned by root so you will need to use sudo to edit these files. If you get tired of typing sudo you can become root by doing this:

$ sudo -s

Be careful when working as root or using sudo. You can mess up your system so make sure to backup. All example here are run as root.

I. Edit /etc/named.conf to add a zone

We need to edit named.conf to add our zone.

$ nano /etc/named.conf

I called my zone vmdev so I added this to named.conf

zone "vmdev" IN {
        type master;
        file "db.vmdev";
};

I added this right before the zone 0.0.127.inaddr.apra and saved the file. So we told Bind to look in /var/named/db.vmdev for this zone.

II. Add a zone file at /var/named/

$ nano /var/named/db.vmdev
vmdev. 7200    IN       SOA     vmdev. root.vmdev. (
              2008031801 ;    Serial
              15      ; Refresh every 15 minutes
               3600    ; Retry every hour
               3000000 ; Expire after a month+
               86400 ) ; Minimum ttl of 1 day
                IN      NS      vmdev.
                IN      MX      10 vmdev.


                IN      A       192.168.0.199
*.vmdev.        IN      A       192.168.0.199

You can just copy this but be sure to change 192.168.0.199 to you Mac's IP address

III. Check the syntax of named.conf and your zone files for errors

Run this to check your named.conf file:

$ named-checkconf /etc/named.conf 

If it returns nothing, your named.conf file is at least syntactically correct. If there is an error, then well you have to diagnose and fix the error.

Now run this to check your zone file:

$ named-checkzone vmdev /var/named/db.vmdev

It should return something like this:

zone vmdev/IN: loaded serial 2008031801
OK

If there are errors then diagnose and fix them.

IV. Edit /etc/resolv.conf

$ nano /etc/resolv.conf
#
# Mac OS X Notice
#
# This file is not used by the host name and address resolution
# or the DNS query routing mechanisms used by most processes on
# this Mac OS X system.
#
# This file is automatically generated.
#
domain vmdev.
nameserver 127.0.0.1
nameserver 192.168.0.1

V. Set your computers network settings to use 127.0.0.1 as a name server

Do this at System Preferences -> Network.
You may want to use your ISPs Name server as the second name server

VI. Start up Bind

$ launchctl load -w /System/Library/LaunchDaemons/org.isc.named.plist

The -w option tells OSX to enable Bind at startup

VII. Check setup with dig

$ dig faker.vmdev 

Should return something like this:

; <<>> DiG 9.6.0-APPLE-P2 <<>> faker.vmdev
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45640
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;faker.vmdev.                   IN      A

;; ANSWER SECTION:
faker.vmdev.            7200    IN      A       192.168.0.199

;; AUTHORITY SECTION:
vmdev.                  7200    IN      NS      vmdev.

;; ADDITIONAL SECTION:
vmdev.                  7200    IN      A       192.168.0.199

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Oct 14 19:28:56 2009
;; MSG SIZE  rcvd: 75

The key here is status NOERROR; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45640
If you get an error then check previous steps.

Now try:

$ ping faker.vmdev

should return:

PING test.vmdev (192.168.0.199): 56 data bytes
64 bytes from 192.168.0.199: icmp_seq=0 ttl=64 time=0.059 ms
64 bytes from 192.168.0.199: icmp_seq=1 ttl=64 time=0.087 ms

but if it does not got to VIII.

VIII. Reboot if needed.

I needed to reboot to get everything to take. Whoila! Have a cookie or something.

Fix for key not found problem

Hey man,

Thanks for this guide. It's awesome!

A quick note, in case named-checkzone complained about missing rndc.key (like in my machine), then this command could create one

rndc-confgen -a

Cheers!
Arzumy

Really helpful! i couldnt

Really helpful! i couldnt have found all these info by my self.
btw, i made it to work on mac osx 10.5.8
thanks again

rndc.key and no-reboot-needed

cool!

thanks for this great walktrough. just what i needed!
to generate /etc/rndc.key i used this:
$ sudo rndc-confgen -a -c /etc/rndc.key
and instead of a reboot, the following should also do the trick:$ dscacheutil -flushcache

Network Settings and adding 127.0.0.1 for different wireless

Thanks for this post, it was really helpful! I was curious if you know of a way to automatically append the 127.0.0.1 to the DNS of every connection? I am jumping from wireless connection to wireless in different cities and it would be nice if I can append this to save a setup step every new place I go.

Happy New Year!