Skip to content

Instantly share code, notes, and snippets.

@vitalk
Created March 31, 2014 14:14
Show Gist options
  • Save vitalk/9893205 to your computer and use it in GitHub Desktop.
Save vitalk/9893205 to your computer and use it in GitHub Desktop.

How To Use Dnsmasq on Local Development

Rationale

Access your local development servers by human-readable domain name instead of numerical IP without touching your /etc/hosts.

How To

  • Install dnsmasq package via homebrew
brew install dnsmasq
  • Copy config example into desired location
sudo cp $(brew list dnsmasq | grep dnsmasq.conf) /usr/local/etc/dnsmasq.conf
  • Place other upstream DNS in /usr/local/etc/resolv.dnsmasq.conf.
sudo echo 'nameserver 8.8.8.8' > /usr/local/etc/resolv.dnsmasq.conf
  • Use whatever as TLD for localhost development. Use any TLD, except .local. Add to the confuguration file /usr/local/etc/dnsmasq.conf.
address=/whatever/127.0.0.1
resolv-file=/usr/local/etc/resolv.dnsmasq.conf
  • Launch dnsmasq at startup.
sudo cp -fv $(brew list dnsmasq | grep dnsmasq.plist) /Library/LaunchDaemons/

To force reload dnsmasq use

sudo launchctl stop homebrew.mxcl.dnsmasq && \
sudo launchctl start homebrew.mxcl.dnsmasq

Testing

Now any domain on TLD whatever resolves to your localhost.

dig example.whatever @127.0.0.1 | grep -B1 example.whatever.

; <<>> DiG 9.8.5-P1 <<>> example.whatever @127.0.0.1
--
;; QUESTION SECTION:
;example.whatever.                   IN      A
--
;; ANSWER SECTION:
example.whatever.            0       IN      A       127.0.0.1
ping -c1 example.whatever

PING example.whatever (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.045 ms

--- example.whatever ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stdwhatever = 0.045/0.045/0.045/0.000 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment