This script updates the DNS record on Cloudflare if the current public IP address is different from the IP address registered in Cloudflare and the last recorded IP address. This is useful for keeping a dynamic IP address updated with a static DNS record.
- The script fetches the current public IP address.
- It reads the last recorded IP address from a local file (
last_ip.txt
). - If the current IP is different from the last recorded IP:
- It checks the current IP address registered with Cloudflare.
- If the current public IP is different from the Cloudflare IP, it updates the DNS record on Cloudflare.
- It logs the update in
logs.txt
with a timestamp.
- If the current IP is the same as the last recorded IP, no action is taken.
-
Clone this repository:
git clone https://github.com/jonaylton/cloudflare-ddns.git cd cloudflare-ddns
-
(Simplest way) Edit the script: Open
cloudflare-ddns.sh
and replace the following placeholders with your actual Cloudflare API key, Zone ID, and DNS record name:CF_API_KEY="YOUR_API_KEY" ZONE_ID="YOUR_ZONE_ID" RECORD_NAME="A_DNS_RECORD"
-
(Optional but recommended) Set up environment variables: Create a file named
.env
in the same directory as the script and add your Cloudflare credentials:CF_API_KEY=YOUR_API_KEY ZONE_ID=YOUR_API_KEY RECORD_NAME=YOUR_API_KEY
Modify the script to load the environment variables (add the following lines at the top):
#!/bin/bash set -a source .env set +a
Remove the following lines that are no more needed:
CF_API_KEY="YOUR_API_KEY" ZONE_ID="YOUR_ZONE_ID" RECORD_NAME="A_DNS_RECORD"
-
Make the script executable:
chmod +x cloudflare-ddns.sh
-
Set up a cron job to run the script periodically: Open your crontab configuration:
crontab -e
Add the following line to run the script every minute (adjust the frequency as needed):
* * * * * /path/to/cloudflare-ddns.sh >/dev/null 2>&1
- The script logs updates in
logs.txt
with timestamps. - The log includes the current IP, last recorded IP, and any updates made to the Cloudflare DNS record.
curl
: Used to fetch the current public IP and interact with the Cloudflare API.jq
: Used to parse JSON responses from the Cloudflare API.
You can install these dependencies using your package manager. For example, on Debian-based systems:
apt install curl jq
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to fork this repository and submit pull requests. Any improvements or bug fixes are welcome!