Set Up a Home DNS Server on MacBook
In this tutorial, I will walk you through the steps to set up a DNS server on a MacBook using DNSMASQ - a lightweight and easy-to-configure DNS tool.
Setting up your own DNS server grants greater control over your local network. Imagine hosting a home web server to share content within your private network—having a DNS server makes this easier. Additionally, it can improve browsing speed.
In my case, I use a local DNS server for web development. When testing websites on different devices, I can use my DNS server to point the domain back to my local computer, making the site accessible through the local network.
How DNSMASQ Works
DNSMASQ acts as an intermediary between your device and the internet, intercepting DNS requests. If the request matches a predefined local name, DNSMASQ resolves it; otherwise, it forwards the query to an upstream DNS (such as Google's 8.8.8.8) and caches the result for faster future lookups.
Installing DNSMASQ on Mac
To install DNSMASQ using Homebrew, run:
brew install dnsmasq
# Then check the installation location:
brew info dnsmasqConfig DNSMASQ
Three key configurations to consider:
Find the dnsmasq.conf file location using:
brew info dnsmasqThen update the configuration file (dnsmasq.conf) to set your Mac’s IP address for listening:
# Bind DNSMASQ to your local network interface
listen-address=your_mac_static_ipEnsure your Mac has a static IP by configuring your router.
Specify the upstream DNS servers:
server=8.8.8.8 # Google DNS
server=1.1.1.1 # Cloudflare DNS
server=9.9.9.9 # Quad9 DNSOptionally, prevent DNSMASQ from reading /etc/resolv.conf by adding:
no-resolvMap your custom domain to a local IP address:
address=/mydomain.local/your_local_ip_addressCreating a Launchd Service
Since DNSMASQ listens on port 53, root permissions are required. Homebrew services do not run as root by default, so the recommended method is to use sudo with launchd.
First, create a service plist file at /Library/LaunchDaemons/homebrew.dnsmasq.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.dnsmasq</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/dnsmasq/sbin/dnsmasq</string>
<string>--keep-in-foreground</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardErrorPath</key>
<string>/var/log/dnsmasq.err.log</string>
<key>StandardOutPath</key>
<string>/var/log/dnsmasq.out.log</string>
</dict>
</plist>Ensure the correct path to DNSMASQ in your system (/usr/local/opt/dnsmasq/sbin/dnsmasq in this example).
Then, set the appropriate permissions:
sudo chown root:wheel /Library/LaunchDaemons/homebrew.dnsmasq.plist
sudo chmod 644 /Library/LaunchDaemons/homebrew.dnsmasq.plistLoading the Service
Once configured, load the service so DNSMASQ starts with root privileges and runs automatically on reboot:
# Load service
sudo launchctl load -w /Library/LaunchDaemons/homebrew.dnsmasq.plist
# To stop the service:
sudo launchctl unload /Library/LaunchDaemons/homebrew.dnsmasq.plistTo verify if DNSMASQ is running on port 53, use:
sudo lsof -i :53
# Expected output example:
# dnsmasq PID ... UDP your_computer_IP_address:53Configuring the Mac Firewall
If you prefer not to configure the firewall manually, you can disable it entirely. However, it’s recommended to keep it enabled for security.
To check if the firewall is enabled:
/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate
# Expected output:
# Firewall is enabled. (State = 1)If the firewall is off, enable it:
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate onAllow DNSMASQ through the firewall:
# Ensure the path matches your installation
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /opt/homebrew/sbin/dnsmasq
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --unblockapp /opt/homebrew/sbin/dnsmasqFirewall stealth mode makes Mac ignores incoming connection attempts silently. To allow incoming DNS requests, disable stealth mode:
sudo defaults write /Library/Preferences/com.apple.alf stealthenabled -bool false
sudo pkill -HUP socketfilterfwVerify that DNSMASQ is on the firewall’s allow list:
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --listappsCheck that DNSMASQ is listening on port 53:
sudo lsof -nP -iUDP:53 -iTCP:53Configuring Other Devices to Use Your DNS Server
There are two ways to configure other devices:
Debug Checklist
sudo lsof -iUDP:53 -iTCP:53Expected output:
dnsmasq 1234 nobody ... UDP 192.168.1.100:53
dnsmasq 1234 nobody ... TCP 192.168.1.100:53If missing, verify your dnsmasq.conf contains:
listen-address=192.168.1.100Restart DNSMASQ:
sudo launchctl unload /Library/LaunchDaemons/homebrew.dnsmasq.plist
sudo launchctl load -w /Library/LaunchDaemons/homebrew.dnsmasq.plistping 192.168.4.100Make sure your dnsmasq.conf has a line:
address=/mylocal.test/192.168.1.30Then from another computer run:
dig mylocal.test @192.168.1.100
# Update 192.168.1.100 yours DNS serversudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate offsudo /usr/local/opt/dnsmasq/sbin/dnsmasq --no-daemon --log-queries
# ensure the path to your dnsmasq correctRecap
Here’s a summary of the steps to set up a DNS server on your Mac: