Cloudflare Dynamic DNS updater
What does it do?
The Cloudflare DDNS (Dynamic Domain Name System) script automates the process of updating DNS records on Cloudflare to reflect changes in your external IP address. This is particularly useful for scenarios where you have a dynamic IP address assigned by your internet service provider (ISP) and need to ensure that your domain always points to the correct IP address. With this script, you can seamlessly manage your DNS records on Cloudflare without manual intervention, ensuring reliable access to your hosted services.
Requirements
- curl
- Linux VM (I run this on my Proxmox Host)
- Cloudflare API token with ZONE-DNS-EDIT Permissions
Getting the Cloudflare API Token
Go to Cloudflare API Tokens and follow these steps:
- Create Token
- Choose Edit zone DNS template
- Set the zone resources to:
- Include - Specific Zone - example.com
- Continue to summary
Copy the token and save it somewhere temporary. Do NOT share this token!
Go to the overview page of your domain in Cloudflare and copy the Zone ID in the right bottom under API. Save this also temporarily since we will use it later.
Run the following commands (source: DDNS-Cloudflare-Bash):
wget https://raw.githubusercontent.com/fire1ce/DDNS-Cloudflare-Bash/main/update-cloudflare-dns.sh
sudo chmod +x update-cloudflare-dns.sh
sudo mv update-cloudflare-dns.sh /usr/local/bin/update-cloudflare-dns
wget https://raw.githubusercontent.com/fire1ce/DDNS-Cloudflare-Bash/main/update-cloudflare-dns.conf
sudo mv update-cloudflare-dns.conf /usr/local/bin/update-cloudflare-dns.conf
Go to the folder where we moved both files to:
cd /usr/local/bin/
Edit the ‘update-cloudflare-dns.conf’ file using the following command:
sudo nano update-cloudflare-dns.conf
Fill in the information:
what_ip="external"
## Fill in your A dns record of which you want it to update
dns_record="example.com, blog.example.com"
## Paste the Zone ID
zoneid="Paste ZoneID"
## Paste the Cloudflare Zone API Token
cloudflare_zone_api_token="Paste Cloudflare Zone API Token"
## Use Cloudflare proxy on dns record true/false
proxied="false"
## 120-7200 in seconds or 1 for Auto
ttl=120
If you want you can set up the telegram notifications, but I will not.
Press ‘Ctrl + ‘S’ and ‘Ctrl’ + ‘x’
Now run the following command to test if it is working:
update-cloudflare-dns
You should receive a response similar to this:
==> 2024-03-02 13:35:04
==> External IP is: x.x.x.x
==> DNS record IP of example.com is x.x.x.x, no changes needed.
==> DNS record IP of blog.example.com is x.x.x.x, no changes needed.
Automatic updater with Crontab
Run:
crontab -e
And add the following code:
*/2 * * * * /usr/local/bin/update-cloudflare-dns
@reboot sleep 60 && /usr/local/bin/update-cloudflare-dns
This will run ‘update-cloudflare-dns every’ 2 minutes and 1 minute after rebooting.
You can check if it is running every 2 minutes by checking the log folder and checking the date:
nano /usr/local/bin/update-cloudflare-dns.log
That’s it, now you have set up a Dynamic DNS updater!