Hybrid Hacker

👾 Technical notes and hybrid geekeries

Wildcard DNS with Dnsmasq on Mac OS X

A wildcard domain is a domain name that will match requests for non-existent sub-domain names. This is fairly simple with a registered domain name and consist of adding a dns record like *.example.com. For more info about setting up a wildcard public domain name, refer to your domain registrar.

During last months I worked on a big project that required a wildcard dns domain and, as usual for every project I work on, I like to have a shadowed local environment. How to create a local wildcard domain name on Mac OS X the easy way? The answer is Dnsmasq.

First of all install Dnsmasq using Brew (if you didn't installed Brew yet, please do it now!)

brew install dnsmasq

Then you can follow these steps to configure a local wildecard dns on your Mac OS X ennvironment:

cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf
sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons
sudo chown root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

tee /usr/local/etc/dnsmasq.conf > /dev/null <<EOF
address=/mylocaldomain.dev/172.17.8.200
EOF

sudo launchctl stop homebrew.mxcl.dnsmasq
sudo launchctl start homebrew.mxcl.dnsmasq
sudo mkdir -p /etc/resolver

tee /etc/resolver/mylocaldomain.dev > /dev/null <<EOF
nameserver 127.0.0.1
EOF

Please be sure to replace mylocaldomain.dev with your local domain and 172.17.8.200 with the local ip of your project (i.e. a Vagrant/Virtualbox machine).

Now, if the installation was successful, if you ping let's say test.mylocaldomain.dev, you system should always resolve 172.17.8.200 (or the ip address you configured before)