Setting up Dynamic DNS with Cloudflare on Raspberry Pi 4 – ddclient

Update 2024/06/25: Since writing this article, someone has written a script for this, which you can find here: https://github.com/K0p1-Git/cloudflare-ddns-updater . Thanks for the info to tesztszerverem.hu!
If you have a home server and want to access a service from the internet (e.g. OpenVPN), it’s worth setting up a dynamic DNS record. This article details the combined setup of Cloudflare – ddclient.
First, create an ‘A’ record with a dummy IP address (so we can later verify that the update was successful), and with a 5-minute TTL.
On the Raspberry Pi, download one of the forks of the program called „ddclient”, which is a background-running daemon that (by default) checks the public IP address every 5 minutes and pushes the changes to Cloudflare using its API.
Install the required packages:
apt-get install perl libdata-validate-ip-perl apt-get install libjson-any-perl
You can find the latest version of ddclient here. Download it, extract it and navigate into the folder:
wget https://github.com/ddclient/ddclient/archive/v3.9.0.tar.gz tar -xzvf v3.9.0.tar.gz cd ddclient-3.9.0
Then copy the executable to its usual location:
cp ddclient /usr/sbin/
Create the necessary directories, copy the configuration files and start editing:
mkdir /etc/ddclient mkdir /var/cache/ddclient cp sample-etc_ddclient.conf /etc/ddclient/ddclient.conf vim /etc/ddclient/ddclient.conf
In the configuration file, you can comment out the mail-related lines (mail= & mail-failure=), then add the following at the end and fill in your own details:
use=web ssl=yes protocol=cloudflare, server=api.cloudflare.com/client/v4, zone=teszt-elek.hu, login=tesztelek@cloudflarefiok.com password=CloudflareGlobalAPIkey dinamikusrekord.teszt-elek.hu,
You can find the API key on the Cloudflare API token page, no need to create a new token, we need the „Global API Key”. Then verify that the configuration is correct with the following command:
ddclient -daemon=0 -debug -verbose -noquiet
Then set up the ddclient daemon as a system service so it starts automatically and does its job in the background:
cp sample-etc_rc.d_init.d_ddclient.ubuntu /etc/init.d/ddclient update-rc.d ddclient defaults systemctl start ddclient systemctl status ddclient
Once everything is done, check whether the DNS record has been updated on the Cloudflare dashboard?