Local DNS for development environment on Mac

At times we need to setup local development domains for our test environment. I use it to maintain a local copy of my hosted website. It is very convenient to make changes to local and then export to hosted website when online.

We can though setup local domain names pointing to 127.0.0.1 (localhost) using /etc/hosts file, but I find it a little dumb as a smarter way exist that will not only allow you to specify wild carded domain names but will also allow you to enable a local cache to make DNS resolutions faster.

Steps V2 (2017/08/10)

# bash <(curl -s https://gist.github.com/drye/5387341/raw/ec72cddfe43ec3d39c91a3c118cb68ab14a049f8/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 dev domain
# ----------------------
[ -d /etc/resolver ] || sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'
# ----------------------
# configuring dnsmasq
# ----------------------
sudo mkdir -p $(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

Steps V1

brew install dnsmasq
mkdir -pv $(brew --prefix)/etc/
echo 'address=/.dev/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf
sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'

 

Test

  1. scutil –dns
  2. ping thereisnowaythisisarealdomain.dev

 

References

  1. http://davebaker.me/articles/setting-up-a-local-dns-server-on-osx
  2. https://echo.co/blog/never-touch-your-local-etchosts-file-os-x-again
  3. https://gist.github.com/drye/5387341

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *