Dynamic DNS

From DreamHost

Jump to: navigation, search

Dynamic DNS is the practice of automatically updating DNS records in response to a frequently-changing, or "dynamic" IP address.

Typically, DNS records point to servers with an infrequently-changing, or "static" IP, or one which changes under the control of the server's administrator. In this situation the DNS records and IP addresses are easily synchronized. However, there are many situations where an administrator does not have control of the IP address of their server. Such is the case when a residential subscriber to a ISP wishes to run a server at home. In this situation, a script may be used to automatically update DNS records. This scripted update process is called "Dynamic DNS."

In practice, a Dynamic DNS script allows a Dreamhost user to use http://mydomain.com to access their Dreamhost-hosted website, and use a subdomain like http://home.mydomain.com to access the webserver running on their cable modem-connected computer. Dynamic DNS makes it easy to "ssh home" when away, or to tell your friends the address of your network video game, without having to remember an IP address.

Contents

Dreamhost DNS Update Scripts

The instructions provided in this article or section are considered advanced.

You are expected to be knowledgeable in the UNIX shell.
Support for these instructions is not available from DreamHost tech support.


For Unix/Linux

The following script can be used on a UNIX-like system (such as Linux) to update the DNS records for your Dreamhost account:

#!/bin/sh
 
# Dreamhost Dynamic DNS script
#  by Rodrigo Damazio <rodrigo@damazio.org>
 
USER=rdamazio
PASS=xxx
DOMAIN=yourdomain.com
HOST=yourhost
RECTYPE=A
IFACE="eth0"
 
# Shouldn't have to edit anything beyond this point
RECVAL=`env LANG=C /sbin/ifconfig $IFACE | grep "inet addr" | sed 's/.*inet addr:\([0-9.]*\).*/\1/'`
DELA="--delete-after"
COOKIEFILE=`mktemp`
LOADSAVECOOKIES="--save-cookies $COOKIEFILE --load-cookies $COOKIEFILE"
OUTFILE=`mktemp`
 
# Get login screen
wget --save-cookies $COOKIEFILE $DELA "https://panel.dreamhost.com/"
 
# Login
wget $LOADSAVECOOKIES $DELA --post-data="Nscmd=Nlogin&username=$USER&password=$PASS&" https://panel.dreamhost.com/index.cgi
 
# go to domain listing
wget $LOADSAVECOOKIES -O $OUTFILE "https://panel.dreamhost.com/index.cgi?tree=domain.manage&current_step=Index&next_step=ShowZone&domain=$DOMAIN"
 
# Get URL with current configured IP
URL=`grep "next_step=ShowRecord&editname=$HOST&editzone=$DOMAIN&edittype=$RECTYPE" $OUTFILE | sed 's/.*a href=.\(.*\).>edit.*/\1/'`
rm -f $OUTFILE
 
# Load record edit page
wget $LOADSAVECOOKIES $DELA $URL
 
# Send new record information
wget $LOADSAVECOOKIES $DELA --post-data="tree=domain.manage&current_step=ShowRecord&next_step=EditRecord&newname=$HOST&newtype=$RECTYPE&newvalue=$RECVAL&newcomment=" "https://panel.dreamhost.com/index.cgi"
 
rm -f $COOKIEFILE

For BSD/Darwin/MacOS X

Here's an alternate version of the script which has been adapted for use with BSD/Darwin/MacOS X:

#!/bin/sh

# Dreamhost Dynamic DNS script
#  by Rodrigo Damazio <rodrigo@damazio.org>

USER=futurephase
PASS=xxxx
DOMAIN=yourdomain.net
HOST=yoursubdomain
RECTYPE=A
IFACE="en1" 
# You may want to verify the interface above by going to the terminal and running ifconfig to see which interface is using your primary IP address.

# Shouldn't have to edit anything beyond this point
RECVAL=`env LANG=C /sbin/ifconfig $IFACE | grep "inet " | sed 's/.*inet \([0-9.]*\).*/\1/'`
echo $RECVAL

DELA="--delete-after"
COOKIEFILE=`mktemp tmp.XXXXX`
LOADSAVECOOKIES="--save-cookies $COOKIEFILE --load-cookies $COOKIEFILE"
OUTFILE=`mktemp tmp.XXXXX`

# Get login screen
wget/wget --save-cookies $COOKIEFILE $DELA "https://panel.dreamhost.com/"

# Login
wget/wget $LOADSAVECOOKIES $DELA --post-data="Nscmd=Nlogin&username=$USER&password=$PASS&" https://panel.dreamhost.com/index.cgi

# go to domain listing
wget/wget $LOADSAVECOOKIES -O $OUTFILE "https://panel.dreamhost.com/index.cgi?tree=domain.manage&current_step=Index&next_step=ShowZone&domain=$DOMAIN"

# Get URL with current configured IP
URL=`grep "next_step=ShowRecord&editname=$HOST&editzone=$DOMAIN&edittype=$RECTYPE" $OUTFILE | sed 's/.*a href=.\(.*\).>edit.*/\1/'`
rm -f $OUTFILE

# Load record edit page
wget/wget $LOADSAVECOOKIES $DELA $URL

echo $RECVAL
sleep 10

# Send new record information
wget/wget -v $LOADSAVECOOKIES $DELA --post-data="tree=domain.manage&current_step=ShowRecord&next_step=EditRecord&newname=$HOST&newtype=$RECTYPE&newvalue=$RECVAL&newcomment=" https://panel.dreamhost.com/index.cgi"

rm -f $COOKIEFILE

The script can be added to your crontab to check regularly for IP changes, or to your DHCP client.

The above scripts only work under the assumption that you are using this computer as your primary means of connecting to the internet. That is, if you are on a local home network and going through a router using NAT to get to the internet, your computer's IP will reflect your locally assigned address (something like 192.168.x.x or 10.x.x.x) as opposed to the actual Internet IP assigned by your ISP. In this case you have two solutions : Use the script above (added May 30 2006) or use a service like No-Ip.com or DynDns.com(see below)

For UNIX users behind a NAT router

Dynamic Update Script for users behind NAT:

#!/bin/sh
 
# Dreamhost Dynamic DNS script
#  by Rodrigo Damazio <rodrigo@damazio.org>
#  External IP Update by Olivier Patry <olivier@technologiksolutions.com>
 
USER=username
PASS=password
DOMAIN=yourdomain.com
HOST=hostname
RECTYPE=A
 
# Shouldn't have to edit anything beyond this point
RECVAL=$(curl "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi")
DELA="--delete-after"
COOKIEFILE=`mktemp`
LOADSAVECOOKIES="--save-cookies $COOKIEFILE --load-cookies $COOKIEFILE"
OUTFILE=`mktemp`
 
# Get login screen
wget --save-cookies $COOKIEFILE $DELA "https://panel.dreamhost.com/"
 
# Login
wget $LOADSAVECOOKIES $DELA --post-data="Nscmd=Nlogin&username=$USER&password=$PASS&" https://panel.dreamhost.com/index.cgi
 
# go to domain listing
wget $LOADSAVECOOKIES -O $OUTFILE "https://panel.dreamhost.com/index.cgi?tree=domain.manage&current_step=Index&next_step=ShowZone&domain=$DOMAIN"
 
# Get URL with current configured IP
URL=`grep "next_step=ShowRecord&editname=$HOST&editzone=$DOMAIN&edittype=$RECTYPE" $OUTFILE | sed 's/.*a href=.\([^"]*next_step=ShowRecord[^\"]*\)".*/\1/'`
rm -f $OUTFILE
 
# Load record edit page
wget $LOADSAVECOOKIES $DELA $URL
 
# Send new record information
wget $LOADSAVECOOKIES $DELA --post-data="tree=domain.manage&current_step=ShowRecord&next_step=EditRecord&newname=$HOST&newtype=$RECTYPE&newvalue=$RECVAL&newcomment=" "https://panel.dreamhost.com/index.cgi"
 
rm -f $COOKIEFILE

For SSH + Dreamhost UNIX Shell

If you can't run those scripts locally, you can use the following script on your usual Dreamhost server to perform the update. You must connect via ssh to run this script. You must do so from your server, or from a machine within your LAN, whenever your external IP address changes. Adding this to your Dreamhost crontab will not work.

Advantages of this method over the others is that it does not rely on any external servers like networksecuritytoolkit.org to determine your external IP address, and will work whether or not you are behind a NAT.

#!/bin/sh
 
# Dreamhost Dynamic DNS script
#  by Rodrigo Damazio <rodrigo@damazio.org>
#  External IP Update by Olivier Patry <olivier@technologiksolutions.com>
#  Dreamhost servers version by Michael Lamb http://datagrok.org 2007-02-05
#  update for panel html change by S Popejoy http://popejoy.org 2007-03-27

 
USER="username"
PASS="password"
DOMAIN="yourdomain.com"
HOST="hostname"
RECTYPE="A"
 
# Shouldn't have to edit anything beyond this point
RECVAL=$(echo $SSH_CLIENT | awk '{print $1}')
DELA="--delete-after"
COOKIEFILE=`mktemp`
LOADSAVECOOKIES="--save-cookies $COOKIEFILE --load-cookies $COOKIEFILE"
IPCACHE="$HOME/.dyndns-ip-cache"

# No need to update if ip already set.
OLDRECVAL=$([ -e $IPCACHE ] && cat $IPCACHE || echo '(no cache)' )
if [ "$OLDRECVAL" == "$RECVAL" ]; then
        echo "No change to IP address; not updating."
        exit 0
else
        echo "IP address changed from $OLDRECVAL to $RECVAL; updating."
fi

echo "Loading Dreamhost Web Admin Panel..."
wget -q --save-cookies $COOKIEFILE $DELA "https://panel.dreamhost.com/"
 
echo "Logging in to Dreamhost Web Admin Panel..."
wget -q $LOADSAVECOOKIES $DELA --post-data="Nscmd=Nlogin&username=$USER&password=$PASS&" "https://panel.dreamhost.com/index.cgi"
 
echo "Retrieving domain management page..."
URL=$(
        wget -q $LOADSAVECOOKIES -O - "https://panel.dreamhost.com/index.cgi?tree=domain.manage&current_step=Index&next_step=ShowZone&domain=$DOMAIN" |\
        sed -n -e "/next_step=ShowRecord&editname=$HOST&editzone=$DOMAIN&edittype=$RECTYPE/ s/.*a href=\"\(.*ShowRecord[^\"]*\)\".*/\1/p"
     )

echo "Retrieving domain management page for $HOST..."
wget -q $LOADSAVECOOKIES $DELA $URL

echo "Updating $HOST.$DOMAIN to point to $RECVAL..."
wget -q $LOADSAVECOOKIES $DELA --post-data="tree=domain.manage&current_step=ShowRecord&next_step=EditRecord&newname=$HOST&newtype=$RECTYPE&newvalue=$RECVAL&newcomment=Dynamic%20IP" "https://panel.dreamhost.com/index.cgi"

echo "Cleaning up..."
rm -f $COOKIEFILE
echo $RECVAL > $IPCACHE

echo "Done."

Turnaround time for updates

It may take up to 4 hours for computers on the Internet to notice that your DNS information has changed. If this is unsatisfactory, another way to enable Dynamic DNS on your Dreamhost-controlled subdomain is to point it at a Dynamic DNS service.

The default TTL for records at Dreamhost's DNS servers is 14400 seconds (4 hours). Records cached by DNS servers that already have the entry cached would take up to 4 hours to get updated. If your IP changes faster than this, you can create a fixed CNAME record for your domain at Dreamhost, pointing to a free dynamic dns domain. For example, suppose that you have a free account at No-Ip.com and you have the dynamic hostname yourname.no-ip.org (that has a TTL of 60 seconds). At Dreamhost's webpanel, you create a static CNAME record yourname.yourdomain-at-dreamhost.com ("static" because it's not frequently updated) that points to yourname.no-ip.org (dynamic, TTL 60s). Then you update just yourname.no-ip.org, using their dynamic DNS update client.

Subdomains

If you have a domain like example.net, and want to have a subdomain like home.example.net, the DNS of which must be controlled by a server which is not Dreamhost's, you can simply delegate that subdomain to a different server by creating a NS record. The NS record must use a hostname (not an IP address) of the target DNS server, and if that server is not officially registered as such, you can add an auxiliar type A record with its FQDN and IP.

This can be used as an alternative for the above dynamic DNS script - one can simply run a DNS server at home, and make home.example.net use that server instead of updating dreamhost's all the time.


External Links

Personal tools