Use Descriptive Local Dev URLs with dnsmasq on macOS

· 2 min read ·
Use Descriptive Local Dev URLs with dnsmasq on macOS

I don’t want to point to localhost all the time. I would like to use *localdev.me instead. And, I also don’t want to modify my hosts file for every subdomain.

During local development I prefer URLs that actually mean something. Likecouchbase.localdev.me, rabbit.localdev. me, myproject.localdev.me. A local proxy routes traffic to the right service, and *.localdev.me resolves cleanly back to 127.0.0.1. Here’s how to set it up on macOS using dnsmasq.

1. Install dnsmasq

Terminal window
brew install dnsmasq

2. Configure dnsmasq

Edit the dnsmasq config file — one of these paths will exist depending on your setup:

  • /opt/homebrew/etc/dnsmasq.conf
  • $(brew --prefix)/etc/dnsmasq.conf

Add:

Terminal window
address=/localhost/127.0.0.1
address=/localdev.me/127.0.0.1

This tells dnsmasq to resolve all *.localdev.me subdomains to 127.0.0.1.

3. Create a macOS DNS resolver

macOS needs an explicit resolver entry so it knows to hand .localdev.me queries to dnsmasq rather than your upstream DNS.

Create the resolver directory (if it doesn’t already exist)

Terminal window
sudo mkdir -p /etc/resolver

Create the resolver file

Terminal window
sudo vi /etc/resolver/localdev.me

Add these contents

domain localdev.me
search localdev.me
nameserver 127.0.0.1

Reload macOS DNS

Terminal window
sudo killall -HUP mDNSResponder

4. Start dnsmasq

Terminal window
brew services start dnsmasq

5. Verify the setup

Check that the resolver is registered:

Terminal window
scutil --dns

You should see localdev.me listed under Resolver Configuration.

Test a subdomain:

Terminal window
ping subdomain.localdev.me

It should resolve to 127.0.0.1.

That’s it. Any *.localdev.me subdomain now resolves locally, so you can spin up as many named services as you need without touching /etc/hosts.

Cheers,
Anton

Originally published on Medium: https://medium.com/@antonkronaj/use-descriptive-local-dev-urls-with-dnsmasq-on-macos-f0d1a8c84842

Share this post