Setup Automatic Local Domains with Dnsmasq on MacOS Ventura
We will set up Dnsmasq as a local DNS server to resolve all development urls. For development purposes we will use the .test
since both .local
and .dev
domains are now publicly available domains. When the setup is completed all subdomains on the .test
domain will resolve to localhost (127.0.0.1
).
This setup will work with any development environment either local web server or docker since all subdomains resolve to the localhost. Edit of /etc/hosts
file for each project will no long be required.
Prerequisites
- Setup command line tools (Xcode) on macOS
- You should have brew setup and working
- Optional development environment; docker or full local server environment.
Step 1: Install and configure Dnsmasq
Use brew to install dnsmasq by opening your preferred terminal application and run the following command.
brew install dnsmasq
Create a config directory for dnsmasq
mkdir -pv $(brew --prefix)/etc/
Setup domain configurations for our*.test
domain. If you prefer a different domain than the suggested one, replace .test
in the command below with your preferred domain.
echo 'address=/.test/127.0.0.1' >> $(brew --prefix)/etc/dnsmasq.conf
The configuration is complete, now use the service management option of brew to manage the dnsmasq service. This will autostart the service even after reboot.
sudo brew services start dnsmasq
To test if the configuration is successful, perform a dig to query your local dnsmasq instance.
dig arctic-jill.test @127.0.0.1
Step 2: Create a dns resolver and test setup
Next create a dns resolver for the selected domain, which is .test
in the example given. Create a resolver directory if it doesn’t already exist.
sudo mkdir -v /etc/resolver
Add dnsmasq namserver to resolvers
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'
Concluding test if external links resolve successfully using the ping command below.
ping -c 1 apple.com
It still works if a reply comes from the apple.com server.
Now check if dnsmasq handles all request on the .test
domain.
ping -c 1 another-sub-domain.test
This should return a reply from 127.0.0.1
.
Conclusion
That is a wrap, but this setup can be extended with a virtual host set for local server development environments (apache or nginx webserver). The following guides can help.