IP address autoupdate – Cloudflare

Create an API token in Cloudflare

Log in to your cloudflare’s dashboard and enter the domain you want to manage.

  • Copy your “Zone ID”
  • Click to “Get your API token”
    • Create two permissions:
    • Zone | DNS | Read
    • Zone | DNS | Edit
  • Edit Zone resources
    • Include | Specific zone | YOUR_DOMAIN

Save your API key token and save the number generated. Once you exit the page you won’t be able to get it again.


apt install git

Choose a good location to save the following git folder. It contains the script to update your ip.

git clone https://github.com/K0p1-Git/cloudflare-ddns-updater.git

Cd into folder, copy the template file, rename it and make a copy. Edit your new copy with your own details like so (I’m not using the global API):

auth_email="YOUR_EMAIL"     # The email used to login 'https://dash.cloudflare.com'
auth_method="token"         # Set to "global" for Global API Key or "token" for Scoped API Token
auth_key="YOUR_API_TOKEN"   # Your API Token or Global API Key
zone_identifier="YOUR_ZONE_ID"    # Can be found in the "Overview" tab of your domain
record_name="YOUR_ROOT_DOMAIN"    # Which record you want to be synced
ttl="3600"                        # Set the DNS TTL (seconds)
proxy="false"                     # Set the proxy to true or false
sitename="SITE_TITLE"          # Title of site "Example Site"
slackchannel=""                                     # Slack Channel #example
slackuri=""                                         # URI for Slack WebHook "https://hooks.slack.com/services/xxxxx"
discorduri=""                                       # URI for Discord WebHook "https://discordapp.com/api/webhooks/xxxxx"
sudo chmod +x [path_to_your_script]

Schedule automatic verification and update of your public IP

After that a cron job can be created to run the script periodically. Create it with sudo so it is run by root and not by your admin user. Reason if that if the system restarts and you are not around to log in, the cron job for your user won’t start until you log in. This is undesirable if your server is meant o run partially supervised.

sudo crontab -e

Example of adding a cron job to be executed every 15 mins everyday.

*/15 * * * * /home/user/cloudflare-ddns-updater/cloudflare-template.sh

Save and exit.

Update cron service.

systemctl restart cron.service

Using a systemd service file

Instead of a cron job you can create a service file in /etc/systemd/system and save it with the following directives. This might be more advantageous because if the service stops for whatever reason you can be easily be notified to solve the problem. Here is an example of a service file where is defined the path to the script and the time interval (every 12 hours):

[Unit]
Description=Public IP address verification and update for cloudflare dns records.
After=network-online.target

[Service]
Type=simple
ExecStart=/home/user/cloudflare-ddns-updater/cloudflare-template.sh
Restart=always
RestartSec=43200
StartLimitInterval=0

[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload

sudo systemctl restart [name_of_your_service_file]

Credits to Jason K. and collaborators
https://github.com/K0p1-Git/cloudflare-ddns-updater/tree/main


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *