Skip to content

Instantly share code, notes, and snippets.

@tobiasstrebitzer
Created September 27, 2012 08:20
Show Gist options
  • Save tobiasstrebitzer/3792859 to your computer and use it in GitHub Desktop.
Save tobiasstrebitzer/3792859 to your computer and use it in GitHub Desktop.
Set up a local DNS server to catch all requests to a specific tld to your local machine (osx lion)
#! /bin/bash
# USAGE: sudo ./osxdns.sh tld
# Templates
namedconf='include "/etc/rndc.key";
controls {
inet 127.0.0.1 port 54 allow {any;}
keys { "rndc-key"; };
};
options {
directory "/var/named";
};
zone "." IN {
type hint;
file "named.ca";
};
zone "localhost" IN {
type master;
file "localhost.zone";
allow-update { none; };
};
zone "0.0.127.in-addr.arpa" IN {
type master;
file "named.local";
allow-update { none; };
};
zone "{TLD}" IN {
type master;
file "{TLD}.zone";
allow-update { none; };
};
logging {
category default {
_default_log;
};
channel _default_log {
file "/Library/Logs/named.log";
severity info;
print-time yes;
};
};'
zoneconf='$TTL 60
$ORIGIN {TLD}.
@ 1D IN SOA localhost. root.localhost. (
45 ; serial (d. adams)
3H ; refresh
15M ; retry
1W ; expiry
1D ) ; minimum
1D IN NS localhost.
1D IN A 127.0.0.1
*.{TLD}. 60 IN A 127.0.0.1
'
# Script
echo "Creating configuration files for $1":
echo "$namedconf" |sed "s/{TLD}/$1/g" > /etc/named.conf
echo "$zoneconf" |sed "s/{TLD}/$1/g" > /var/named/$1.zone
rndc-confgen > /etc/rndc.conf
head -n5 /etc/rndc.conf |tail -n4 > /etc/rndc.key
echo "OK"
echo "Checking configuration:"
named-checkconf /etc/named.conf
named-checkzone $1 /var/named/$1.zone
echo "Setting up autostart:"
launchctl load -w /System/Library/LaunchDaemons/org.isc.named.plist
/usr/sbin/named
echo "OK"
echo "Setup complete, now just add '127.0.0.1'"
echo "as a DNS server to your network adapter."
echo "Have fun!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment