Dynamic DNS service

From WickyWiki
Revision as of 12:48, 20 March 2016 by Wilbert (talk | contribs)

Dynamic DNS allows you to have a DNS name while your IP address changes frequently. You will need to register for an account with one of the dynamic DNS providers. This service is often provided freely.

More information:

Popular dynamic DNS registration sites:

Installation ipUpdate (dnsexit.com)

The interface uses an (unsecure) http protocol and it is strongly recommended to configure a different password (token) for this service. Logon at http://www.dnsexit.com and go to My Account -> Account profile -> IP Update Password.

WAN IP update script using Linux Upstart:

sudo /etc/init/dyndns-ip-update.conf

Contents:


# upstart script
#
# Copy script:
#   sudo cp $HOME/Scripts/dyndns-ip-update.conf.sh /etc/init/dyndns-ip-update.conf

# Test with:
#   sudo stop dyndns-ip-update
#   sudo start dyndns-ip-update

# Check log:
#   sudo cat /var/log/upstart/dyndns-ip-update.log

author "Wilbert Volkers"
description "Update WAN IP address at DynDNS service www.dnsexit.com"

#start when network is up
#does not seem wait for the network so implemented the retries
start on runlevel [2345]

stop on runlevel [!023456]

# write stdout to log in /var/log/upstart/dyndns-ip-update.log:
console log

pre-start script
  logger "pre-start for dyndns-ip-update"
end script

post-start script
  logger "post-start for dyndns-ip-update"
end script

script
	echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : start ..."

	# settings
	host="wilbertvolkers.linkpc.net"
	login="LOGIN"
	passtoken="PASSTOKEN"
	storedipfile="/opt/dyndns-ip-update.ip"

	# get current IP using checkip.dyndns.com
	# ${retries} retries, every ${retrywait}
	retries=5
	retrywait=5s
	i=0
	while [ ${i} -lt ${retries} ]; do
	  currentIP=$( wget --quiet --output-document=- http://checkip.dyndns.com | sed -ne "s/.*[^0-9]\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)[^0-9].*/\1/p" )
	  if [ -n ${currentIP} ]; then break; fi;
	  i=$((i + 1))
	  echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : get IP, try ${i}/${retries} failed, waiting ${retrywait}"
	  sleep ${retrywait};
	done;

	echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : current  IP = '${currentIP}'"

	# get previous IP from file
	storedIP="unknown"
	forceafterdays=3
	if [ -f "${storedipfile}" ] ; then
	  #force update if stored IP older then ${forceafterdays} days
	  storedipfiledate=$( stat -c %y ${storedipfile} | cut -d ' ' -f1 )
	  storedipdays=$(( (`date +%s -d $(date '+%Y%m%d')` - `date +%s -d ${storedipfiledate}`)/86400 ))
	  if [ ${storedipdays} -gt ${forceafterdays} ] ; then
	    storedIP="old"
	  else
	    storedIP=$(cat ${storedipfile})
	  fi
	fi

	echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : previous IP = '${storedIP}' (${storedipfiledate})"

	# update the dyndns IP if needed
	if [ "${currentIP}" = "${storedIP}" ] ; then
	  echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : update not needed"
	else
	  updateurl="http://update.dnsexit.com/RemoteUpdate.sv?login=${login}&password=${passtoken}&host=${host}"
	  response=$( wget --quiet --output-document=- ${updateurl} | tail -1 )
	  echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : updated : ${response}"
	  echo ${currentIP} > ${storedipfile}
	fi

	echo "$(date '+%Y-%m-%d %H.%M.%S') dyndns : done."

end script

Test script with:

sudo start dyndns-ip-update

Check log:

sudo cat /var/log/upstart/dyndns-ip-update.log | tail -10

More information on Linux Upstart:

Installation ddclient (dyndns and others)

This client updates the DynDNS record automatically.

sudo apt-get install ssh libio-socket-ssl-perl ddclient

Follow the instructions. Additionally configure a longer update interval 'daemon=3600' and use SSL:

sudo gedit /etc/ddclient.conf
protocol=dyndns2
use=web, web=checkip.dyndns.com, web-skip='IP Address'
server=members.dyndns.org
login=**dyndns-login**
password='**dyndns-pwd**'
wilbertvolkers.dyndns.org
daemon=3600
ssl=yes

View ddclient logging:

cat /var/log/syslog* | grep 'ddclient'

Test/force refresh:

sudo ddclient -force

Reconfigure and restart, you can edit the config file instead:

sudo dpkg-reconfigure ddclient
sudo /etc/init.d/ddclient restart

Remarks

Note that when your IP address has changed, the ddcient needs to announce this to your dyndns service. Then it is still possible that the DNS you are using does not give you the new IP address, it may take some time before updates are routed through the DNS network.