Sunday, November 8, 2009

Set up a free Dynamic DNS for your home Ubuntu server.

This tutorial assumes you are using Debian based linux, such as ubuntu, and you have a server set up with root privileges on a home connection.

Update: Your router might already have Dynamic DNS functionality built it, specifically for the DynDNS service. If you can use that instead it will save yourself a lot of trouble! If not, read on and good luck :-)

DNS stands for Domain Name System, it is the service that allows you to go to google.com instead of having to remember google's IP address. A Dynamic DNS service will accept a change in IP address for a domain name. So instead of remembering your latest IP address assigned from your ISP to connect to your home server we will only have to remember one name, for example myhomeserver.dyndns.org. The server will then report any IP changes to the Dynamic DNS service.

There are a number of free Dynamic DNS services out there, for this tutorial we will be using DynDNS.

Head over to http://www.dyndns.com/ and enter in a name for your server.



You will then need to make an account with Dynamic DNS. Once you are finished it should set an IP address from where you logged in from by default. Now we are going to get your Ubuntu server to check if there is a change in IP address and if so log into your DynDNS account and report the change.

To accomplish this we are going to use the ddclient program.

First lets get ddclient from synaptic; execute this command:
sudo apt-get install ddclient

It will then run through a command line based installer and ask you a few questions to set up a basic config file. Answer all the questions it asks you, the DynDNS hostname you set, your DynDNS username and password, etc.

The tricky part is when it asks, "Enter the interface which is used for using dynamic DNS service."

If you are not behind a router or a firewall you can simply enter: eth0

But in my case I am behind a router so I cannot see what my global IP is, so I am going to use another server that will check the IP for me.

So if you are behind a router just skip this question and we will fix it manually in the config file later.

After synaptic is done installing ddclient we are going to have to manually edit the config file and add a few things. For this tutorial I am going to use emacs to edit the files.

Run the command: sudo emacs /etc/ddclient.conf

Your config file should look something like this:


# Configuration file for ddclient generated by debconf
#
# /etc/ddclient.conf

pid=/var/run/ddclient.pid
protocol=dyndns2
use=if, if=
server=members.dyndns.org
login=[your dyndns username]
password=[your dyndns password]
yoursite.dyndns.org


If your server is not behind a router then the use line should be set to "use=if, if=eth0".

However if your server is behind a router change this line,

use=if, if=

to this,

use=web, web=checkip.dyndns.com/, web-skip='IP Address'

And lastly put a new line that says,

daemon=600

This will tell the script to check your IP address every 10 minutes (600 seconds) using checkip.dyndns.com as a reference. If the IP has changed it will send an update to DynDNS else it will wait another 10 min and check again. The smallest value you are allowed to set for the update interval is every 60 seconds.

If you are using the emacs editor hold down the CTRL key and hit "X", then "S" to save your file. Then hold down CTRL again and hit "Z" to get back to the command line.


So your final configuration file should look something like this,


# Configuration file for ddclient generated by debconf
#
# /etc/ddclient.conf

daemon=600 #reports IP every 600 seconds

pid=/var/run/ddclient.pid
protocol=dyndns2
use=web, web=checkip.dyndns.com/, web-skip='IP Address'
server=members.dyndns.org
login=[your dyndns username]
password=[your dyndns password]
yoursite.dyndns.org



Now that we are all configured it is time to restart the ddclient program.

Cross your fingers and execute this command,

sudo /etc/init.d/ddclient restart

Assuming everything is set up properly ddclient should report any IP address changes. Now there is still one problem, DynDNS expects an update at LEAST once a month or else it will set your account as inactive. This is an issue if you keep the same IP for more than a month.

To avoid this we are going to make a cron job that will force ddclient to update every month.

Execute the command "crontab -e". You will be greeted with a simple editor. Add this on a new line,


00 00 28 * * ddclient -host yoursite.dyndns.org -force


Then hold down CTRL and hit X, it will ask you if you want to save, type "y" to say yes and hit enter.

Everything should work beautifully now!

Cheers.

No comments: