Duckdns (part 2)

With the knowledge we have gained in the previous lessons, we’ll now setup a script to make sure the IP address of your internet connections is automatically updated to DuckDNS so you can also access your Raspberry Pi when your provider decides to change your IP address.

For this we found a nice article during the third online lesson, which can be found here.

The short version is as follows:

mkdir duckdns
cd duckdns
nano duck.sh

enter the following line and edit both the domain (the part you choose before .duckdns.org) and the token (which you can find when you log into DuckDNS at the top.)

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

Than change the access rights to the file and edit crontab, which is a job scheduler that in this case run the script we just made every 5 minutes.

chmod 700 duck.sh
crontab -e

Add the following line at the bottom

*/5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1

Than test the script, check the log and start cron so from now on you will not have to remember to update your IP address.

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

How to get connected

Cool, so we have a base install of Raspberry Pi OS and we can connect to the Pi on the local network. To be able to connect to it from anywhere, let’s set this up. We kind of lay the Pi to the side and focus on your home router. We are going to do some port forwarding and create a dynamic DNS name.

As about every internet provider comes up with their own router, it is a bit tricky to create up a general manual. Luckily we have the internet to help out here. On https://portforward.com/router.htm you should be able to find your router and find the correct way to setup a port forward.

You want to forward port 22 from the internet to port 22 on your Raspberry Pi’s IP address.

Also a lot of Internet providers do not provide you with a static IP address, we want to do another step to be able to simply reach your Raspberry Pi from anywhere in the world. We setup Dynamic DNS. For this we use the free after registration service of DuckDNS

  • Go to DuckDNS and login with one of the login options
  • Create a DuckDNS subdomain
  • Add the domain to your router

DuckDNS ducky logo