This script automatically updates your Cloudflare DNS record to the current IP, supporting both IPv4 (A) and IPv6 (AAAA) records.
中文 | English
To download and make the script executable:
curl https://raw.githubusercontent.com/SphenHe/cloudflare-api-ddns/dev/cf-ddns.sh > /usr/local/bin/cf-ddns.sh && chmod +x /usr/local/bin/cf-ddns.shcf-ddns.sh
-4|-6 \ # specify IPv4 or IPv6
-a cloudflare-api-token \ # specify Cloudflare API token
-h host.example.com \ # FQDN of the record you want to update
-z example.com \ # your domain (zone) -u user@example.com \ # Cloudflare email, optional for API tokens
-f false(default)|true \ # force DNS update even if the record is unchanged
-t 120 \ # DNS TTL in secondsYou can also provide common values through environment variables:
export CLOUDFLARE_API_TOKEN=your-token
export CLOUDFLARE_EMAIL=user@example.com
export CFZONE_NAME=example.com
export CFRECORD_NAME=host.example.com
cf-ddns.sh -4The script reads the first available config file from:
$CFDDNS_CONFIG_FILE
$XDG_CONFIG_HOME/cf-ddns/config
$HOME/.config/cf-ddns/config
/config/cf-ddns.conf
/etc/cf-ddns.conf
The config file uses simple sh variable assignments. See cf-ddns.conf.example.
Runtime files are stored outside the current working directory by default:
cache: $CFDDNS_CACHE_DIR, or $XDG_CACHE_HOME/cf-ddns, or $HOME/.cache/cf-ddns
log: $CFDDNS_LOG_FILE, or $XDG_STATE_HOME/cf-ddns/cf-ddns.log, or $HOME/.local/state/cf-ddns/cf-ddns.log
For a system-wide cron job, a practical setup is:
install -m 600 cf-ddns.conf.example /etc/cf-ddns.conf
CFDDNS_CONFIG_FILE=/etc/cf-ddns.conf cf-ddns.sh -4Build the lightweight Alpine-based image:
docker build -t cf-ddns .Create a config file and run it once:
mkdir -p ./docker-config ./docker-cache ./docker-state
install -m 644 cf-ddns.conf.example ./docker-config/cf-ddns.conf
docker run --rm \
-v "$PWD/docker-config:/config:ro" \
-v "$PWD/docker-cache:/cache" \
-v "$PWD/docker-state:/state" \
cf-ddns -4For cron, run the same docker run command on your schedule. The container runs as a non-root user. Config is read from /config/cf-ddns.conf, cache is stored in /cache, and the file log is written to /state/cf-ddns.log; logs are also printed to stdout for docker logs.
If you want stricter config file permissions on Linux, keep the file mode at 600 and set its owner to UID/GID 65532:65532 before mounting it.
This project is licensed under the MIT License - see the LICENSE file for details.