A simple Python script that automatically updates Cloudflare DNS records with your current public IP address, providing Dynamic DNS (DDNS) functionality.
- Support for multiple domain configurations
- Automatic public IP address detection (using multiple IP lookup services for reliability)
- Automatic Cloudflare DNS record updates
- Configurable update intervals
- IP address caching to minimize API calls
- Detailed logging
- Docker support
- Copy the configuration template:
cp config.ini.example config.ini
-
Edit
config.ini
file -
Start the container (either way):
docker-compose up -d
docker run -d \
--name cloudflare-ddns \
-v $(pwd)/config.ini:/config/config.ini:ro \
-e UPDATE_INTERVAL=300 \
--restart unless-stopped \
connermo/cloudflare-ddns-py:latest
- Python 3.6+
- pip (Python package manager)
pip install requests
- Copy the configuration template:
cp config.ini.example config.ini
-
Edit
config.ini
file (see Configuration section) -
Run the script:
python cloudflare_ddns.py
[cloudflare]
# Authentication Method 1: Using API Token (Recommended)
# Create from Cloudflare Dashboard:
# My Profile > API Tokens > Create Token
# Requires "Zone.DNS" edit permissions
api_token = your_api_token_here
# First domain configuration
[domain:ddns.example.com]
# Zone ID (Required)
zone_id = your_zone_id_here
record_type = A
ttl = 1
proxied = false
# Second domain configuration
[domain:home.example.com]
zone_id = your_zone_id_here
record_type = A
ttl = 1
proxied = true
[cloudflare]
api_token = your_api_token_here
zone_id = your_zone_id_here
domain = your.domain.here
record_type = A
ttl = 1
proxied = false
-
Multiple Domain Configuration:
- Use
[domain:domain_name]
format for each domain section - Each domain can have its own zone ID, record type, and proxy settings
- Configure any number of domains
- All domains share the same API token
- Use
-
Configuration Parameters:
api_token
: Cloudflare API tokenzone_id
: Zone ID for the domain (Required)record_type
: DNS record type, typically A (IPv4) or AAAA (IPv6)ttl
: Time To Live in seconds, 1 for automaticproxied
: Enable/disable Cloudflare proxy (CDN)
- Log in to Cloudflare Dashboard
- Click the profile icon in the top right
- Navigate to "My Profile" > "API Tokens"
- Click "Create Token"
- Use "Edit zone DNS" template or create a custom token with DNS edit permissions
- Select applicable zones (domains)
- Create token and copy it to the config file's api_token field
- Log in to Cloudflare Dashboard
- Select your domain
- Find the Zone ID in the "API" section on the right side of the overview page
# Basic usage
python cloudflare_ddns.py
# Set update interval (in seconds)
python cloudflare_ddns.py -i 600 # Update every 10 minutes
python cloudflare_ddns.py --interval 3600 # Update every hour
# Specify config file path
python cloudflare_ddns.py -c /path/to/your/config.ini
- Create service file:
sudo nano /etc/systemd/system/cloudflare-ddns.service
- Add the following content (modify paths):
[Unit]
Description=Cloudflare DDNS Client
After=network.target
[Service]
Type=simple
User=your_username
WorkingDirectory=/path/to/cloudflare-ddns
ExecStart=/usr/bin/python3 /path/to/cloudflare-ddns/cloudflare_ddns.py -i 3600
Restart=on-failure
RestartSec=60
[Install]
WantedBy=multi-user.target
- Enable and start service:
sudo systemctl enable cloudflare-ddns.service
sudo systemctl start cloudflare-ddns.service
- Create plist file:
nano ~/Library/LaunchAgents/com.user.cloudflare-ddns.plist
- Add the following content (modify paths):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.cloudflare-ddns</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/python3</string>
<string>/path/to/cloudflare-ddns/cloudflare_ddns.py</string>
<string>-i</string>
<string>3600</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardErrorPath</key>
<string>/path/to/cloudflare-ddns/error.log</string>
<key>StandardOutPath</key>
<string>/path/to/cloudflare-ddns/output.log</string>
</dict>
</plist>
- Load service:
launchctl load ~/Library/LaunchAgents/com.user.cloudflare-ddns.plist
Two options for running:
- Use the provided
start_ddns.bat
script - Use Task Scheduler:
- Open Task Scheduler
- Create Basic Task
- Set Trigger (e.g., At System Startup)
- Choose Start a Program
- Set program path to
python
and arguments to script path
- Log file:
cloudflare_ddns.log
- IP cache file:
ip_cache.json
MIT License