This script (update_dns.sh
) automatically updates a specific DNS record in Cloudflare with the current external IP address of your server. It's useful for environments with dynamic IP addresses, ensuring your domain always points to the correct IP.
- A Cloudflare account
- A domain managed by Cloudflare
curl
andjq
installed on your server (for making API calls and parsing JSON responses, respectively)
-
Install Required Tools:
Ensure
curl
andjq
are installed on your server. If not, install them:sudo apt update && sudo apt install curl jq -y
-
Download the Script:
Download
update_dns.sh
.wget https://raw.githubusercontent.com/paulduca/cloudflare-simple-ddns/main/update_dns.sh
-
Make the Script Executable:
Change the script's permissions to make it executable.
chmod +x /path/to/update_dns.sh
-
API Token:
Generate an API token in Cloudflare with permissions to edit DNS settings:
- Go to My Profile → API Tokens → Create Token.
- Use the "Edit zone DNS" template and select the domain you want to update.
-
Zone ID:
Find your Zone ID in the Cloudflare dashboard under the "Overview" tab for your domain.
-
Edit the Script:
Open
update_dns.sh
in a text editor and replace the placeholders (<cloudflare_api_token>
and<zone_id>
) with your actual Cloudflare API token and Zone ID. Also, setRECORD_NAME
to the DNS record you wish to update (e.g.,server.domain.tld
).
-
Run the Script Manually:
To manually update your DNS record, execute the script:
/path/to/update_dns.sh
-
Automate DNS Updates:
To have the script run automatically at regular intervals, use
cron
. To edit the crontab:crontab -e
Add the following line to run the script every minute:
* * * * * /path/to/update_dns.sh >> /var/log/update_dns.log 2>&1
This will also log output and errors to /var/log/update_dns.log
.
-
Permission Issues: Ensure
update_dns.sh
is executable (chmod +x /path/to/update_dns.sh
). -
API Token Errors: Double-check your API token permissions in Cloudflare. The token should have
Zone.DNS
set toEdit
. -
Connectivity Issues: Ensure your server can reach Cloudflare's API and the external IP service.
-
Logging: Check
/var/log/update_dns.log
for any error messages or output from the script.
For additional help or information about Cloudflare's API, refer to the Cloudflare API documentation.