This script updates a DNS record on Cloudflare with your current public IP address. It is useful for dynamic DNS setups where your public IP address may change frequently.
Ensure you have the following installed on your system:
curl
jq
(for processing JSON)
sudo apt-get update
sudo apt-get install curl jq
Before using the script, you need to set up some configurations:
-
API Token: Generate an API token from your Cloudflare account with permissions to edit DNS records.
- You can create the API token here: Cloudflare API Tokens.
-
Zone ID: Find your zone ID on the Cloudflare dashboard for your domain.
- You can find a tutorial on how to find your Zone ID here: Finding Account and Zone IDs.
-
DNS Record Name: Specify the subdomain you want to update (e.g.,
ssh.example.com
).
-
Clone the repository
git clone https://github.com/o-Oby/cloudflare-ddns-updater.git cd cloudflare-ddns-updater
-
Create the
config.json
fileCreate a
config.json
file with the following content and update it with your configurations:{ "API_TOKEN": "your_cloudflare_api_token", "ZONE_ID": "your_zone_id", "DNS_RECORD_NAME": "your_subdomain.example.com", "CF_API_URL": "https://api.cloudflare.com/client/v4" }
-
Set Permissions on
config.json
Ensure that the
config.json
file has the correct read permissions:chmod 644 config.json
-
Create the script file
Create a shell script file (e.g.,
cloudfare-ddns-updater.sh
) and copy the provided script into the file. -
Make the script executable
chmod +x cloudfare-ddns-updater.sh
-
Run the script
./cloudfare-ddns-updater.sh
This script does the following:
- Fetches your current public IP address using
https://api.ipify.org
. - Retrieves the DNS record details from Cloudflare using the API token and zone ID.
- Checks if the current public IP address is different from the one in the DNS record.
- Updates the DNS record on Cloudflare if the IP address has changed.
Fetching current public IP address...
Current public IP: 123.456.789.012
Retrieving DNS record details from Cloudflare...
IP address has changed from 123.456.789.000 to 123.456.789.012. Updating DNS record...
DNS record updated successfully.
To ensure the script runs every hour and at reboot, add the following entries to your crontab:
-
Open the Crontab File:
crontab -e
-
Add the Reboot and Hourly Jobs:
@reboot /path/to/your/script/cloudfare-ddns-updater.sh >> /path/to/your/script/cloudfare-ddns-updater.log 2>&1 0 * * * * /path/to/your/script/cloudfare-ddns-updater.sh >> /path/to/your/script/cloudfare-ddns-updater.log 2>&1
Replace
/path/to/your/script/cloudfare-ddns-updater.sh
with the actual path to your script if it is different. -
Save and Exit.
This project is licensed under the MIT License - see the LICENSE file for details.
This project is designed to keep sensitive data in a separate configuration file (config.json
). This helps to maintain security and manageability by avoiding hardcoded sensitive data within the script. Ensure that you handle and store your config.json
file securely.