thierry-b
4/20/2016 - 7:38 AM

enable_dnsmasq_on_osx.md

#!/usr/bin/env bash
# bash <(curl -s https://gist.githubusercontent.com/TBeeckmans/a6510f7fc3da48196b5d4743d15d75cc/raw/40b0169d84156f1cbed2dd4710953f95e3de4f0e/enable_dnsmasq_on_osx.sh)

# ----------------------
# installing dnsmasq and enable daemon
# ----------------------
brew install dnsmasq
sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
# ----------------------
# adding resolver for vbox domain
# ----------------------
[ -d /etc/resolver ] || sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/vbox'
# ----------------------
# configuring dnsmasq
# ----------------------
mkdir -pv $(brew --prefix)/etc/
cat > $(brew --prefix)/etc/dnsmasq.conf <<-EOF
listen-address=127.0.0.1
address=/.dev/127.0.0.1
# keep nameserver order of resolv.conf
strict-order
EOF
# ----------------------
# launching dnsmasq
# ----------------------
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Never touch your local /etc/hosts file in OS X again

To setup your computer to work with *.dev and/or *.local domains, e.g. project.dev, awesome.local and so on, without having to add to your hosts file each time.

Requirements

Install

brew install dnsmasq

Launch daemon on startup

sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons

Add to resolvers

Check if the resolver directory exists or create the resolver directory

[ -d /etc/resolver ] || sudo mkdir -v /etc/resolver

Add your nameserver to resolvers

sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'

Configure dnsmasq

Create config directory

mkdir -pv $(brew --prefix)/etc/

Setup *.dev and *.local

cat > $(brew --prefix)/etc/dnsmasq.conf <<-EOF
listen-address=127.0.0.1
address=/.dev/127.0.0.1
# keep nameserver order of resolv.conf
strict-order
EOF

Added strict-order to dnsmasq.conf to keep nameserver order of resolv.conf (see here).

Launch daemon

sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Add local DNS to search order in System Preferences

System Preferences > Network > Wi-Fi (or whatever you use) > Advanced... > DNS > add 127.0.0.1 to top of the list.

Finished

That's it! You can run scutil --dns to show all of your current resolvers, and you should see that all requests for a domain ending in .dev or .local will go to the DNS server at 127.0.0.1