Better Local Dev Hostnames with Dnsmasq

by
Annika Backstrom
in Uncategorized on 18 October 2011. #Web#development#dns#dnsmasq#Mac OS X

I use ghost to configure development environment hostnames for all my local test sites. Some DTrace tomfoolery showed me how ghost handles this config: each new hostname is saved to its own plist file.

Update: for zero-configuration wildcard DNS, check out sslip.io.

ambackstrom@fsck:~:1$ sudo cat /var/db/dslocal/nodes/Default/hosts/example.com.plist
Password:




    ip_address
    
        127.0.0.1
    
    name
    
        example.com
    

There's filesystem caching going on behind the scenes and I expect the net impact is negligible, but I wondered if there wasn't a more robust solution that supported wildcards. Having just configured dnsmasq on my router, I started with a brewinstall dnsmasq and was pleasantly surprised to get a progress bar rather than "no available formula." Homebrew recommended a couple post-install actions for setting up a default config file and configuring launchd to keep dnsmasq alive, which I dutifully ran. After that, I enabled my fake TLD in dnsmasq.conf:

# respond to *.zomg with 127.0.0.1
address=/zomg/127.0.0.1

By sheer luck I ended up on this great serverfault.com post explaining Mac OS X's resolver(5) and the /etc/resolver directory. Forcing dnsmasq lookup on my zomg TLD is as easy as:

echo 'nameserver 127.0.0.1' >/etc/resolver/zomg

These settings will work on any network we connect to, no need to modify the DNS servers via System Preferences and put 127.0.0.1 (dnsmasq) in front. Simple, clean, flexible.