DYNDNS WITH CLOUDFLARE
To update an A record from a DNS zone hosted in CloudFlare dynamically, you'll need:
- A Linux machine to run the following script via cronjob, with curl and jq packages installed
- Access to your CloudFlare dashboard
That said, the only requirements regarding CloudFlare itself are:
- API key: Or API token. Generate it under your CloudFlare's account settings
- DNS Zone ID: Check this under Cloudflare > domain.tld > Information tab
- DNS Record ID: Retrieve this by running the following command and locating the record you'll be setting up for dynamic updates:
curl -sX GET "https://api.cloudflare.com/client/v4/zones/${DNS_ZONE_ID}/dns_records/ \
-H "Content-Type:application/json" -H "X-Auth-Key:${API_KEY}" -H "X-Auth-Email:${EMAIL_ADDRESS}
Once you get it, just save this snippet to a file:
DNS_ZONE='# yours here'
DNS_RECORD='# yours here'
API_KEY='# yours here'
EMAIL_ADDRESS='# yours here'
DNS_RECORD_NAME="\"domain.tld\""
CURRENT_IP_ADDRESS="\"$(curl -s ip.me)\""
CURRENT_DNS_VALUE=$(
curl -sX GET "https://api.cloudflare.com/client/v4/zones/${DNS_ZONE}/dns_records/${DNS_RECORD}" \
-H "Content-Type:application/json" \
-H "X-Auth-Key:${API_KEY}" \
-H "X-Auth-Email:${EMAIL_ADDRESS}" | jq '.result["content"]'
)
if [ ${CURRENT_DNS_VALUE} != ${CURRENT_IP_ADDRESS} ]; then
curl -sX PUT "https://api.cloudflare.com/client/v4/zones/${DNS_ZONE}/dns_records/${DNS_RECORD}" \
-H "X-Auth-Email:${EMAIL_ADDRESS}" \
-H "X-Auth-Key:${API_KEY}" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":'${DNS_RECORD_NAME}',"content":'${CURRENT_IP_ADDRESS}'}' > /dev/null
fi
And then add the following cron job:
*/5 * * * * /path/to/your/file.sh