Skip to content

SphenHe/cloudflare-api-ddns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloudflare API Dynamic DNS Update Script

This script automatically updates your Cloudflare DNS record to the current IP, supporting both IPv4 (A) and IPv6 (AAAA) records.

中文 | English

Usage

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.sh

Command

cf-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)

Optional flags

    -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 seconds

You 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 -4

Config, cache, and logs

The 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 -4

Docker

Build 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 -4

For 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.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Cloudflare API v4&v6 Dynamic DNS Update in Bash. This repo is forked from yulewang/cloudflare-api-v4-ddns

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors