Find Pi Everywhere: FreeDNS a free Dynamic DNS service

Journey to the Clouds series.
2. Configuring Dynamic DNS using FreeDNS.

tuxmemorygame
Dynamic DNS is what allows common mortals to have a domain name without paying for a static IP address. Why FreeDNS? Because it is free of charge, you only need an email address to register and to update the IP address it is as easy as it gets: execute a command. I will walk you through the process to create a domain and configure the Pi to update the IP address when needed.

Journey to the Clouds index:

  1. Install Raspbian on a headless Raspberry Pi.
  2. Configuring Dynamic DNS using FreeDNS.
  3. Encrypting an external USB drive using TrueCrypt.
  4. Setting up Nginx web server.
  5. Setting up MySQL database.
  6. Setting up PHP.
  7. Installing ownCloud.

As you know, Domain Name System (DNS) converts names into IP addresses. Every time you connect to a web server you type a name (i.e. google.com) but you actually connect to an IP address (i.e. 74.125.28.105) behind the scenes the names are converted to an IP address so you don’t need to remember any number but just a catchy name. In that sense, the IP address is like the phone number of your router, so a DNS will act as a telephone listing where you look up for a specific name and it will tell you the associated “phone number”. Usually residential connections do not have a static IP address, that is, every now and then your IP address will change, thus dynamic. If you want to have a domain name you need to update the IP associated to it every time in changes, so it can be located; that is the objective of the Dynamic DNS providers, they keep the association Domain Name↔IP address up to date.

Less chit-chat and more chop-chop, head to the FreeDNS site and at the bottom of the page click on “Sign Up!” you will be presented with the following screen:

freedns_subdomain_signup_1
Once you fill it up, wait for the activation email and then you are set to go. To create a domain name click on “Registry” in the left menu. You will be presented with a list of possible server names, as shown below, choose the one you like most, note that your domain will be a sub-domain of the chosen one, that is, if you choose “info.tm” your domain will be “something.info.tm“.

freedns_subdomain_new_1
Leave ‘Type‘ as it is, the IP address should be already there, you only need to come up with a good domain name. In the example above I wrote “raspberry.liar” because I am planing to have other XXX.liar domains, but you could simply write a name without any dots.

freedns_subdomain_new_2

OK, you created your domain and now what? How does FreeDNS work? How does it update your IP address? FreeDNS assigns a unique identifier, a hash key, to your domain, when you want to update the IP your send a HTTP request along with your hash key. When FreeDNS receives such a request, it retrieves the IP address of the system sending the request (your router) and it updates it in its database for the associated hash key. The simple HTTP request have the following structure:
http://freedns.afraid.org/dynamic/update.php?HASHKEY

Therefore in order to update the IP we only need to send that sort of request when needed, that can be accomplished in different ways but in my opinion the easiest is to use wget in a cron job, both standard programs in all Linux systems therefore no extra packages are needed.

How do you find out which is your hash key? Click on ‘Dynamic DNS‘, scroll down to the bottom of the page until you see the domain you created, right click on ‘Direct URL‘ and copy the link location.

freedns_subdomain_update_1

Paste the link in a text file and you will see something like:
http://freedns.afraid.org/dynamic/update.php?c29Q2s1Ml9df604bba2b1359ff62MTEyMT185e7=

The blue string is your hash key. Indeed if you (left) click on such link the IP will be updated, you can also execute wget to update the IP:
$ wget -q --read-timeout=0.0 --waitretry=5 --tries=400 http://freedns.afraid.org/dynamic/update.php?c29Q2s1Ml9df604bba2b1359ff62MTEyMT185e7=

Let’s have a command like that being executed by cron every five minutes. The following script will be a cron job that when started it will check the public IP address of the Pi, then check the IP address of your domain, if they don’t match it will send the HTTP request to FreeDNS to update it. All the actions are logged in /var/log/messages.

Script /etc/cron.d/freedns_update.sh replace DOMAIN and HASHKEY with your own values.

#!/bin/sh
# freedns_update.sh: Update the public IP on freedns.afraid.org only if it has changed.
## Place this script in the cron's job directory /etc/cron.d and assign the proper permissions
## and owner
## sudo chmod 500 /etc/cron.d/freedns_update.sh
## sudo chown root:root /etc/cron.d/freedns_update.sh
## Add to /etc/crontab to execute on reboot and every 5 minutes
## Edit /etc/crontab and append these two lines:
## @reboot root /etc/cron.d/freedns_update.sh >/dev/null
## */5 * * * * root /etc/cron.d/freedns_update.sh >/dev/null

#Use your own values
DOMAIN=raspberry.liar.info.tm
HASHKEY=c29Q2s1Ml9df604bba2b1359ff62MTEyMT185e7=

UPDATE_URL="http://freedns.afraid.org/dynamic/update.php?${HASHKEY}"
   
current_ip=$(wget -q --output-document - http://checkip.dyndns.org | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
registered_ip=$(ping -qn -c 1 $DOMAIN | head -n 1 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')

if [ "${current_ip}" != "${registered_ip}" ]; then   
   wget -q --read-timeout=0.0 --waitretry=5 --tries=400 --output-document /dev/null $UPDATE_URL
   if [ $? -eq 0 ]; then
     echo "$(date +"%b %_d %T") $(hostname) $0: IP address updated on freedns.afraid.org: new IP '${current_ip}', old IP '${registered_ip}'" >> /var/log/messages
   else     
     echo "$(date +"%b %_d %T") $(hostname) $0: ERROR IP address could not be  updated on freedns.afraid.org: current IP '${current_ip}', registered IP '${registered_ip}'" >> /var/log/messages
   fi
fi

As stated in the above script’s comments, create the script in /etc/cron.d, set the owner and permissions:
$ sudo chmod 500 /etc/cron.d/freedns_update.sh
$ sudo chown root:root /etc/cron.d/freedns_update.sh

In order to execute every time the Raspberry Pi reboots and every 5 minutes append these two lines to /etc/crontab

@reboot root /etc/cron.d/freedns_update.sh >/dev/null
*/5 * * * * root /etc/cron.d/freedns_update.sh >/dev/null

Finally you have to configure your router to forward the ports you want to the Raspberry Pi. At this time only the SSH server is active so add a rule to forward whichever public port you like to private port 22 of the Raspberry Pi IP address. Once we set up the Nginx web server then you will also have to redirect the ports 80 and 443 but for now it is not needed not recommended.

For instance if you have set the public SSH port to be 4563 then you can connect, and follow the rest of the steps, by
$ ssh pi@raspberry.liar.info.tm -p 4563

Remember you can always add the appropriate entries to ~/.ssh/config.

In the next chapter we will install TrueCrypt version 6 and encrypt a USB drive, see you around.

Advertisements
This entry was posted in Journey to the Clouds, Linux, network, Raspberry Pi, Raspbian and tagged , , , , . Bookmark the permalink.

2 Responses to Find Pi Everywhere: FreeDNS a free Dynamic DNS service

  1. monkey_holding_two_guns says:

    Definitively, freedns and hash is my preferred option, especially after discovering that dyndns.org either bought the other competitors or agreed some kind of exclusivity with some of the router manufacturers.
    Morover, I think that freedns is quicker than dyndns to update the IP address.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s