How to Setup DuckDNS on a Raspberry Pi!

Today we are going to look at how to setup DuckDNS on a Raspberry Pi.

DDNS is a way to automatically update a name server (in this case, DuckDNS) in real-time with your external IP address. There are many reasons why you might need a DDNS hostname, with one of the most common being creating a VPN Server. In this tutorial, we will run through the commands to setup DDNS on a Raspberry Pi.

Instructions – DuckDNS Raspberry Pi

Before you start the setup tutorial, ensure that you have navigated to https://www.duckdns.org and setup a user account. We will be using the domain and token that exist on that page

1. SSH into your Raspberry Pi. If you aren’t sure how, you can find our tutorial here.

2. After you SSH in, we need to make a directory and then create a file in that directory that will run at a scheduled time (created in later steps).

mkdir duckdns
cd duckdns
nano duck.sh
making duckdns folder and creating a new file

3. We’re now going to place a script in this file that will run at a scheduled interval. The script needs to have the domains you are using (from DuckDNS) added to it.

adding the file to automatically update the ddns hostname
echo url="https://www.duckdns.org/update?domains=[YOUR_DOMAIN]&token=[YOUR_TOKEN]&ip=" | curl -k -o ~/duckdns/duck.log -K -

4. We now need to change the permissions of these files and access crontab to schedule this script to run every five minutes. Crontab is a task schedular that automates certain commands.

chmod 700 duck.sh
crontab -e
changing the permissions of the script created and editing the crontab

5. Paste the contents of the file below in the crontab file.

*/5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1
adding folder to the cron to automatically run

8. The job will now run every five minutes. We will test it, and assuming it works, start the cron service. If you receive OK after running this command, the process worked properly. If you receive KO, you have an error somewhere in your script file or you did not give the file the proper permissions.

./duck.sh
cat duck.log
sudo service cron start
starting the cron service

Conclusion

This is a very simple and straight forward process, but we will be building on top of this in future tutorials. Ensuring that you are connecting to your external IP address is very important when you use services that require your external IP address.

If you’re interested in using your new DDNS to setup a WireGuard VPN, check out our tutorial here!

Frank Joseph (WunderTech)

WunderTech is maintained by Frank, an IT professional with 15+ years of experience in corporate IT and application management. Frank holds a Bachelor of Science in Computer Information Systems and a Master of Business Administration (MBA). He focuses on sharing his experience with others on his website and YouTube channel.