How to Set Up DuckDNS on Raspberry Pi

Most home broadband subscriptions do not include a static IP, but a DDNS (Dynamic Domain Name System) service lets you automatically update a domain name with your dynamic external IP address. There are many reasons why you might need a DDNS hostname, one of the most common being to set up a VPN server. In this guide I will explain how to configure DDNS on a Raspberry Pi using the free service provided by DuckDNS.

Before following the installation guide, make sure you have created an account at https://www.duckdns.org. Once registration is complete, you will be assigned a token and will be able to create one or more domain names.

Now connect to your Raspberry Pi via SSH and, once logged in, create a folder that will contain our script.
This script will tell DuckDNS what our current external IP address is.
So type:

mkdir script
cd script
mkdir log
nano DuckDNS_update.sh

and enter the following:

echo url="https://www.duckdns.org/update?domains=[IL_TUO_DOMINIO]&token=[IL_TUO_TOKEN]&ip=" | curl -k -o ~/script/log/DuckDNS.log -K -

Of course, replace [IL_TUO_DOMINIO] with the domain name you created on DuckDNS (for example, if the domain name assigned to you is http://johndoe.duckdns.org, simply enter johndoe) and [IL_TUO_TOKEN] with the token you were given.

Once you have finished editing the script with your details, close the file by pressing CTRL+X, then type Y (or S, depending on your system language) and finally press ENTER to confirm.

Now we need to change the permissions of the script we just created and then add it to crontab to schedule it to run every five minutes. Crontab is a task scheduler that automates specific commands.

chmod 700 DuckDNS_update.sh
crontab -e

and paste the following at the end of the file

*/5 * * * * ~/script/DuckDNS_update.sh >/dev/null 2>&1

Once you have pasted the command, close the file by pressing CTRL+X, then type Y (or S, depending on your system language) and finally press ENTER to confirm.

The script will now run every five minutes, but if you want to test it straight away, simply run the following commands. 

./DuckDNS_update.sh
cat log/DuckDNS.log

If the log file shows the message OK after the script has run, the script has worked correctly. If you see the message KO, there is an error in the script or it has not been given the appropriate permissions.

Enjoy!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top