Setting up dnsmasq on MacOS with Homebrew

So earlier I posted about my decision to Dockerize my entire development environment. In that post I mentioned that you can either manually edit your /etc/hosts file or install a dns server to point domains to your local web server.

Disclaimer: this nothing to do with Docker, this steps are can be used even if you are running your own instance of web server directly on your machine.

dnsmasq is a simple DNS server that does a lot but it is not as complicated and full featured as stuff like BIND. In this article I will be using dnsmasq to spoof all the DNS requests to “.test” domain to my own local machine. Having my web server running (nginx) and listening for domains set on the virtualhost configurations I will then be able to serve my apps locally.

I’ll be using homebrew on my machine, lets get it installed on our machine:

$ brew install dnsmasq

If you install dnsmasq manually the configuration file would be at /etc/dnsmasq but if you used homebrew to install it then it is located at /usr/local/etc/dnsmasq.conf

In my case I want to redirect all the DNS requests to .test domain to my machine, so we need to add the following to the dnsmasq.conf

address=/test/127.0.0.1

Now we start the dnsmasq service using brew services. make sure you run this with sudo.

$ sudo brew services start dnsmasq

running dnsmasq via brew services will also make sure dnsmasq service runs on the start up.

Now lets add a specific resolver for .test domain.

$ sudo mkdir -v /etc/resolver
$ sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'

that should be it, lets test it out.

➜  ~ ping hazaveh.test
PING hazaveh.test (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.029 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.086 ms
^C
--- hazaveh.test ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.029/0.057/0.086/0.029 ms
➜  ~

Leave a Reply