DIY RFC2136 dyndns with bind

ever since dyndns stopped to be completely free (including hassle-free) i was looking for alternatives. i recently stumbled across RFC2136 which can be used to provide dynamic dns services. since i have access to two nameservers running bind i decided to try it out.. it works pretty nicely :)

this is an improved version, now that i know more about bind, over my previous setup and it has been updated to work with bind 9.16 which no longer creates TSIG keys using dnssec-keygen but instead uses the much more comfortable tool tsig-keygen. The main disadvantage of my previous setup was, that any user holding one of the allowed keys could update any host entry, so the user was not limited to a single hostname which can be a seurity issue.

this following howto will explain how i did my setup so that i could have a little bash script that would allow me to add new hosts to my dyndns with a single command. all my hosts will end with .dyn.mydomain.ch.

i can run

/etc/bind/dyn/add_new_host.sh myhost

and it will add a new host called myhost.dyn.mydomain.ch to the configuration and return an authorization key which i can use on the client side.

so here is how i did it:

first of all i wanted to be able to have a simple script that would allow me to add new hosts with a minimum amount of work. so i split my config into different files, so i could later edit them automatically. also, you want to make sure the file where the keys are stored is not world readable..

script to remove hosts

optionally you can also create a little script to remove hosts just as easily. create a file called remove_hosts.sh with the following contents

remove_hosts.sh
#!/bin/bash
if [ -z "$1" -o "$1" == " " ]; then
        echo "usage: remove_host.sh <hostname>"
        echo "EXAMPLE: remove_host.sh myhost will remove myhost.dyn.mydomain.ch"
        exit 1
fi
cd /etc/bind/dyn/
hostname=${1}.dyn.mydomain.ch.
echo "old keys.conf entry: "
grep -E '[ "]'"${hostname}"'[."]\s' keys.conf
echo "remove key for ${hostname}"
sed -i '/[ "]'"${hostname}"'[."]\s/d' keys.conf
echo "reload bind";
/usr/sbin/rndc reload
echo "delete dns entry for ${hostname}"
echo -e "update delete ${hostname} a\nsend" | nsupdate -l -4
/usr/sbin/rndc sync -clean
echo "currently allowed hosts:"
grep "key " keys.conf | awk '{ print $2; }' | tr -d ";"

make it executable and run it to remove hotsts. warning make a backup of your keys.conf and your named.conf file before testing this :)

./remove_host.sh myhost