Skip to content

Instantly share code, notes, and snippets.

@mshick
Last active July 27, 2020 18:05
Show Gist options
  • Save mshick/414612f1f5426242d4fa0744d509a82a to your computer and use it in GitHub Desktop.
Save mshick/414612f1f5426242d4fa0744d509a82a to your computer and use it in GitHub Desktop.
dnsmasq script

Running

$ bash <(curl -Ls https://gist.githubusercontent.com/mshick/414612f1f5426242d4fa0744d509a82a/raw/dnsmasq-setup.sh)

sudo permissions are required

What?

This will configure a homebrew installed dnsmasq service with .test and .localhost TLDs, and will add the LaunchDaemon to your system startup.

Why?

dnsmasq is a small utility which allows you (among other things) to create dynamic local hostnames and TLDs. This is useful for developers who need something other than http://localhost for local projects.

#!/bin/sh
echo "configuring dnsmasq"
brew_prefix="$(brew --prefix)"
dnsmasq_conf_file="${brew_prefix}/etc/dnsmasq.conf"
dnsmasq_plist="/Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist"
test_address="address=/.test/127.0.0.1"
localhost_address="address=/.localhost/127.0.0.1"
if ! [ -x "$(command -v dnsmasq)" ]; then
echo "dnsmasq not found, installing with homebrew"
brew install dnsmasq
fi
echo "sudo permissions will be required to add a LaunchDaemon and edit /etc/resolver"
# Get sudo permissions
sudo -v
if ! [ -f "${brew_prefix}/etc/dnsmasq.conf" ]; then
echo "creating dnsmasq.conf files and directories"
mkdir -p "${brew_prefix}/etc/"
touch "${dnsmasq_conf_file}"
sudo mkdir /etc/resolver
fi
if ! [ "$(grep -q "^${test_address}" ${dnsmasq_conf_file})" ]; then
echo "creating .test TLD"
printf "\n${test_address}\n" >> "${dnsmasq_conf_file}"
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'
else
echo ".test TLD already exists, skipping"
fi
if ! [ "$(grep -q "^${localhost_address}" ${dnsmasq_conf_file})" ]; then
echo "creating .localhost TLD"
printf "\n${localhost_address}\n" >> "${dnsmasq_conf_file}"
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/localhost'
else
echo ".localhost TLD already exists, skipping"
fi
if ! [ -f "${dnsmasq_plist}" ]; then
echo "installing LaunchDaemon"
sudo cp $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
fi
echo "dnsmasq successfully configured"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment