/cloudflare-DDNS

Cloudflare DynamicDNS script

Primary LanguageShell

Cloudflare DDNS

What is Cloudflare Dynamic DNS?

DNS records are inherently static, and it does not play well with dynamic IP addresses. Now, to solve that problem, you’ll need to set up dynamic DNS. Fortunately, Cloudflare provides an API that allows you to manage DNS records programmatically.

To set up a Cloudflare dynamic DNS, you’ll need to run a process on a client inside your network that does two main actions: get your network’s current public IP address and automatically update the corresponding DNS record.

The image below shows the high-level diagram of how the Cloudflare dynamic DNS update flow happens.

it's now possible to update DNS records (A and/or AAAA) of a domain on Clouldflare via API Token.

To do so you need to:

  1. Go to the My Account -> API Tokens.

  1. Create an API with the template "Edit Zone DNS".

  1. Make sure that permissions are ZONE > DNS > Edit

  2. Select the zone (domain name), Sofiane.email in my case.

Use the API on Linux

  1. Download the bash script in a file, e.g. cloudflare.sh.
wget -O cloudflare.sh https://raw.githubusercontent.com/SofianeHamlaoui/cloudflare-DDNS/main/cloudflare.sh
  1. Make it executable.

chmod +x /path/to/cloudflare.sh.

  1. Run crontab -e and add a line like @weekly /path/to/cloudflare.sh to the end of the file to run the script periodically. Use tools like Crontab Guru to define the frequency properly.

Use the API on Windows

  1. Download and install Powershell 7.1+
  1. Download the cloudflare.ps1 script

  2. Run the script with the right information