Last Updated:

Setting up dnsmasq for macOS to handle subdmoains for localhost

Step 1

Install Homebrew

$ /usr/bin/ruby -e "$(curl -fsSL"

Step 2

Install dnsmasq

brew install dnsmasq

 Step 3

Create config folder if it doesn’t already exist

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

 Step 4

Configure dnsmasq for *.localhost

echo 'address=/.localhost/' >> $(brew --prefix)/etc/dnsmasq.conf

 Step 5

Configure the port for macOS High Sierra

echo 'port=53' >> $(brew --prefix)/etc/dnsmasq.conf

 Step 6

Start dnsmasq as a service so it automatically starts at login

sudo brew services start dnsmasq

 Step 7

Create a dns resolver

sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver" > /etc/resolver/localhost'

Step 8

Verify that all .localhost requests are using

scutil --dns
resolver #8
domain : localhost
nameserver[0] :
flags : Request A records, Request AAAA records
reach : 0x00030002 (Reachable,Local Address,Directly Reachable Address)

 You can also test by using ping

ping foo.localhost