Register Linux (Ubuntu) server with Windows DNS

If you are looking for a script to help register your linux box with a windows DNS sever than you have come to the right place.

Note: For this to work you need to have enabled Windows DNS to allow unsecure updates

Note: This script ASSUMES that your /etc/network/interfaces is setup for static IP and has dns-nameservers and dns-search setup

Example:

# The primary network interface
auto eth0
iface eth0 inet static
        address 192.168.1.22
        netmask 255.255.255.0
        network 192.168.1.0
        broadcast 192.168.1.255
        gateway 192.168.1.1
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 192.168.1.10 #my Windows DNS server
        dns-search corp.local # my DNS zone that I want to update

Step 1: create a folder calls /var/scripts

sudo mkdir /var/scripts

Step 2: create a script file called dns-update.sh

sudo vi /var/scripts/dns-update.sh

Step 3: paste the following into the script file

#!/bin/sh
ADDR=`/sbin/ifconfig eth0 | grep 'inet ' | awk '{print $2}' | sed -e 's/.*://'`
HOST=`hostname -f`
DNSSERVER=`systemd-resolve --status | grep 'DNS Servers: ' -m 1 | sed -e 's/.*: //'`
DNSZONE=`systemd-resolve --status | grep 'DNS Domain: ' -m 1 | sed -e 's/.*: //'`

echo "server $DNSSERVER" > ./nsupdate.txt
echo "zone $DNSZONE" >> ./nsupdate.txt
echo "update delete $HOST A" >> ./nsupdate.txt
echo "update add $HOST 600 A $ADDR" >> ./nsupdate.txt
echo "show" >> ./nsupdate.txt
echo "send" >> ./nsupdate.txt

#cat ./nsupdate.txt
nsupdate ./nsupdate.txt

Step 4: Enable the script for execution

sudo chmod +x /var/scripts/dns-update.sh

Step 5: Run the script (or schedule it via cron)

sudo /var/scripts/dns-update.sh