Dynamic DNS for Hostmonster

So I have this home file server running Ubuntu, connected to the internet for whatever reason, and I want to be able to access the files when I'm at work, or on the road, etc. I have Comcast (ugh, I know) so I have no guarantee that my IP address won't change every few days. So, how am I supposed to reliably access my server, or even keep track of the IP address I would need to do so? Dynamic DNS is the answer, for me at least.

If you are reading this I take it you already know what a DNS server does, so I won't even go there. A Dynamic DNS setup is one that will basically keep track of your IP address and update your DNS records (with Hostmonster in this case) so that your preferred domain name will always point to your varying IP address. There are free services out there, like these, that will do this for you, but you will generally only be able to use their selection of domain names, so you won't be able to use a URL you own.

In searching I found a couple of posts on the web on how to do this with Hostmonster and some local scripts on the file server. Fisrt was this post on DeathWarrior posted in March of 2012, then Jason's Computer Science Blog posted a few updates to it in December of the same year. These scripts basically send an update to the HostMonster DNS server with the current IP address of the server, so if you set it up as a cron job it will update on a regular basis.

I'm not by any means a pro at writing shell/ruby scripts, but I was able to adjust these scripts to work in my environment (neither of the solutions I linked above worked out of the box for me), so here is the solution I settled on:

Install the Ruby and JSON Libs

First you need to install the proper libraries from the Ubuntu 12.04 repositories:

Echo your servers IP address

Create a file that will echo the IP address that your server is accessing the internet from. You can host this PHP file on your file server if it's set up to host web pages as well, or in my case I've hosted this file on my Hostmonster account.

So go ahead and create a file named ip.php and put ONLY the fillowing single line in it, then put it somewhere you can access it via a browser on the World Wide Web:

Create and Install the DNS Update Script

You'll need to install the following script on your server which will check your current IP address using the previous file and send that IP address up to the Hostmonster DNS server. NOTE - this is only set up to update a SUB-domain, for anything else you'll have to adjust.

Go ahead and place the following code in a file named filename.sh, replacing filename with whatever you want, read the comments and replace the appropriate info.

Make this script executable

Automate it

Last step will be to make this script run however often you want using cron. Edit your cron file:

Add the following line to your crontab to have the script run every hour.

That's it!

Double check your work

If you wanted to check to make sure your script is successful you can navigate to the directory where your script is and run it with the following command:

You should see a success message if all goes well.