A Python script that automatically updates your Cloudflare DNS records with your current public IP address. This is useful if you have a dynamic IP address but want to maintain a consistent domain name that points to your home server, NAS, or other services.
- Automatically detects your current public IP address
- Updates your Cloudflare DNS record only when your IP changes
- Configurable check intervals
- Detailed logging with automatic log rotation
- Runs as a background process
- Supports both proxied and unproxied DNS records
- Can automatically discover your DNS record ID based on domain name
- Secure authentication using Cloudflare API tokens
- Python 3.6+
- A Cloudflare account with:
- A domain name
- API access (API Token)
- An existing DNS record to update
-
Clone this repository:
git clone https://github.com/yourusername/cf-dyndns.git cd cf-dyndns
-
Install the required dependencies:
pip install -r requirements.txt
-
Create your
.env
file by copying the example:copy .env.example .env
-
Edit the
.env
file with your Cloudflare credentials and configuration.
Edit the .env
file with your specific details:
# Cloudflare API credentials
CLOUDFLARE_API_TOKEN=your-api-token
# Cloudflare zone and record IDs
ZONE_ID=your-zone-id
# DNS_RECORD_ID is optional if you provide DOMAIN_NAME
# DNS_RECORD_ID=your-dns-record-id
# Domain configuration
DOMAIN_NAME=your-domain.com
RECORD_TYPE=A
TTL=3600
PROXIED=true
# Script settings
CHECK_INTERVAL=300 # Check every 5 minutes (in seconds)
# Log rotation settings
LOG_MAX_SIZE=1048576 # Maximum log file size in bytes (1 MB default)
LOG_BACKUP_COUNT=3 # Number of backup log files to keep
The script implements automatic log rotation to prevent log files from becoming too large:
LOG_MAX_SIZE
: Maximum size of each log file in bytes before rotation occurs (default: 1MB)LOG_BACKUP_COUNT
: Number of backup log files to keep (default: 3)
When a log file reaches the maximum size, it's renamed to cf_dyndns.log.1
, and a new log file is created. Older backup files are shifted (e.g., .1
becomes .2
), and the oldest ones are deleted if they exceed the backup count.
This script uses Cloudflare's API Token authentication method, which is more secure than the older Global API Key method. To create a token:
- Log in to your Cloudflare dashboard
- Navigate to "Profile" > "API Tokens"
- Click "Create Token"
- Use the "Edit Zone DNS" template or create a custom token with the following permissions:
- Zone > DNS > Edit permissions
- Include the specific zone you want to update
Zone ID: Log into your Cloudflare dashboard, select your domain, and the Zone ID is displayed in the "Overview" tab's API section.
There are two ways to specify which DNS record should be updated:
-
Automatic Discovery: Simply provide your
DOMAIN_NAME
andRECORD_TYPE
in the.env
file, and the script will automatically find the corresponding DNS record ID for you. -
Manual Specification: If you have multiple records with the same name and type, or if you prefer to explicitly specify which record to update, you can set the
DNS_RECORD_ID
in your.env
file. You can find this ID in your Cloudflare dashboard by going to the DNS tab and clicking on the record you want to update.
Run the script:
python cf_dyndns.py
The script will:
- Check your current public IP address
- Compare it with your Cloudflare DNS record
- Update the record if they differ
- Wait for the specified interval before checking again
- Log all activities to both console and a rotating log file
To run this script as a background service on Windows:
-
Create a batch file
start_cf_dyndns.bat
:@echo off cd /d %~dp0 python cf_dyndns.py
-
Create a scheduled task:
- Open Task Scheduler
- Create a new task
- Set it to run with highest privileges
- Configure it to run on startup
- Add a new action to start the batch file
- Set it to restart if it fails
Check the cf_dyndns.log
file for detailed error messages.
Common issues:
- Incorrect API token or insufficient permissions
- Incorrect Zone ID or DNS Record ID
- Network connectivity problems
- Multiple DNS records with the same name (in this case, specify DNS_RECORD_ID manually)
This project is licensed under the MIT License.