Setting up a wildcard DNS domain on Mac OS X

By Bilpp Team

There are many ways to develop on a Mac, and many stack to choose from. One common and recurring need however is to access your local websites through a named domain, ie using example.dev. Thankfully, there is a really simple way to do this using Dnsmasq as a local resolver.

Install Dnsmasq

This is straightforward with Homebrew:

brew install dnsmasq

# Create the etc dir if needed

mkdir -p /usr/local/etc

# Create a simple configuration

# This forces the .dev domain to respond with 127.0.0.1

# You can find more information in the default config file:

# /usr/local/opt/dnsmasq/dnsmasq.conf.example

echo “address=/.dev/127.0.0.1” > /usr/local/etc/dnsmasq.conf

# Install the daemon startup file

sudo cp -fv /usr/local/opt/dnsmasq/*.plist \

/Library/LaunchDaemons

# Start the daemon

sudo launchctl load \

/Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Configure Mac OS X

All we need to do is tell the resolver to use Dnsmasq for .dev domains:

# man 5 resolver

sudo mkdir -p /etc/resolver

sudo sh -c ‘echo “nameserver 127.0.0.1” > /etc/resolver/dev’

You can now use any .dev domain and it will always resolve to 127.0.0.1:

ping google.com # this still works

# PING google.com (74.125.239.38): 56 data bytes

ping foo.dev # you can use any domain

# PING foo.dev (127.0.0.1): 56 data bytes

ping bar.baz.dev # or subdomain

# PING foo.dev (127.0.0.1): 56 data bytes

Conclusion

This is very useful in particular for applications that use subdomains as account identifier: you can easily create new accounts on the fly, and never have to worry about your /etc/hosts file again.

Finally, you may also want to look at Pow (and powder). Pow will automatically start your Rails/Rack applications, and provides a port proxying feature for apps written in other languages. Definitely a great alternative.

That’s it for today.