This script provides a way to implement a self-hosted Dynamic DNS (DynDNS) service that automatically updates your Cloudflare DNS record with your current public IP address whenever it changes.
-
Create a Subdomain: Set up a subdomain like
dyndns.your-domain.comin your Cloudflare account. All other DNS records should beCNAMErecords that resolve todyndns.your-domain.com. -
Retrieve Cloudflare Details: In your Cloudflare dashboard, find the following details for the subdomain you created:
- Zone ID
- DNS Record Name
- DNS Record ID
Set these values in the script configuration.
-
How It Works: The script checks your current public IP address. If it has changed, it updates the DNS record for
dyndns.your-domain.comto reflect the new IP address.
python3 -m venv /opt/cloudflare-python-dyndns/venv
/opt/cloudflare-python-dyndns/venv/bin/activateClone the repository and install required Python packages:
pip install -r /path/to/requirements.txtTo run the script periodically, open your crontab and add the following line to check for IP address changes every 15 minutes:
crontab -eThen add this line:
*/15 * * * * /opt/cloudflare-python-dyndns/venv/bin/python /opt/cloudflare-python-dyndns/cloudflare-python-dyndns/main.py >/dev/null 2>&1Make sure to create a .env file in the project directory and add the following environment variables:
CLOUDFLARE_API_TOKEN: Your Cloudflare API token with permission to edit DNS records.`
CLOUDFLARE_ZONE_ID: The Zone ID of your domain in Cloudflare.
CLOUDFLARE_DNS_RECORD_NAME: The DNS record name (e.g., dyndns.your-domain.com).
CLOUDFLARE_DNS_RECORD_ID: The DNS record ID for the dyndns record.
Support for IPv6 addresses.
Automate the process of fetching the DNS Record Name and ID.