Skip to content

Instantly share code, notes, and snippets.

@mbyczkowski
Last active August 20, 2018 11:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbyczkowski/e96d17f4526c57fbb8602d31cad84f50 to your computer and use it in GitHub Desktop.
Save mbyczkowski/e96d17f4526c57fbb8602d31cad84f50 to your computer and use it in GitHub Desktop.
Dnsmasq config for OS X
#!/usr/bin/env bash
# Resolve dev domains locally and passthrough other DNS requests
set -e
dnsmasq_installed=`brew list | grep dnsmasq`
if [ -n "$dnsmasq_installed"]; then
echo "Installing Dnsmasq..."
brew install dnsmasq
else
echo "Dnsmasq is already installed. Skipping."
fi
# .dev is now a legit domain, so this resolver need higher priority
echo "Adding resolver for .dev domain..."
sudo mkdir -p /etc/resolver
sudo tee /etc/resolver/dev >/dev/null <<EOF
nameserver 127.0.0.1
search_order 1
EOF
echo "Clearing local DNS cache..."
sudo dscacheutil -flushcache
echo "Listing list of DNS resolvers..."
scutil --dns
echo "Configuring Dnsmasq..."
tee /usr/local/etc/dnsmasq.conf >/dev/null <<EOF
strict-order
listen-address=127.0.0.1
address=/.dev/127.0.0.1
EOF
running_dnsmasq=`brew services list | grep 'dnsmasq.*started'`
if [ -n "$running_dnsmasq" ]; then
echo "Stopping Dnsmasq service..."
sudo brew services stop dnsmasq
fi
dns_list=(127.0.0.1)
dns_list+=($(sudo networksetup -getdnsservers Wi-Fi | grep -v 127.0.0.1))
echo "Updating DNS order in Network Preferences: ${dns_list[@]}"
echo "Starting Dnsmasq service..."
sudo brew services start dnsmasq
dig_cmd="dig some.random.domain.dev +short"
dig_res=`$dig_cmd`
if [ "$dig_res" != "127.0.0.1" ]; then
echo "Expected '$dig_cmd' to resolve to 127.0.0.1, but got '$dig_res' instead"
exit 1
else
echo ".dev domain resolves without specifying the resolver"
fi
dig_cmd="dig some.random.domain.dev +short @127.0.0.1"
dig_res=`$dig_cmd`
if [ "$dig_res" != "127.0.0.1" ]; then
echo "Expected '$dig_cmd' to resolve to 127.0.0.1, but got '$dig_res' instead"
exit 1
else
echo ".dev domain resolves with resolver set to 127.0.0.1"
fi
dig_cmd="dig google.com +short"
dig_res=`$dig_cmd`
if [ "$dig_res" = "127.0.0.1" ]; then
echo "Expected '$dig_cmd' to resolve to non-local IP, but got '$dig_res' instead"
exit 1
else
echo "google.com domain resolves without specifying the resolver"
fi
dig_cmd="dig google.com +short @127.0.0.1"
dig_res=`$dig_cmd`
if [ "$dig_res" = "127.0.0.1" ]; then
echo "Expected '$dig_cmd' to resolve to non-local IP, but got '$dig_res' instead"
exit 1
else
echo "google.com domain resolves with resolver set to 127.0.0.1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment