DDNS Script for Cloudflare

Cloudflare DDNS Setup

Step 1: Create DNS Record

Make an A record pointing to 8.8.8.8 with proxy off

Step 2: Get Your Cloudflare API Tokens

Global Key here: https://dash.cloudflare.com/profile/api-tokens

Step 3: Query Your DNS Records

curl -s -X GET "https://api.cloudflare.com/client/v4/zones/Zone ID/dns_records?name=domain.tld" \
  -H "X-Auth-Email: email@example.com" \
  -H "X-Auth-Key: Global Key" \
  -H "Content-Type: application/json"

Your result should start with something like below, let's call this THE ID

{"result":{"id":"he832434238hb327g" Put this in THE ID in the (and obviously fill in the other information we gathered)

Step 4: Find Your Zone ID

Scroll down on the "Overview" tab and you will find your Zone ID

Your Zone ID: https://dash.cloudflare.com/somethingsomething/domain.tld

Step 5: Prepare Your Data

Now fill your Email, Global key, Zone ID & THE ID

Step 6: Update Your DNS Record

curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/Zone ID/dns_records/THE ID" \
  -H "X-Auth-Email:email@example.com" \
  -H "X-Auth-Key:Global Key" \
  -H "Content-Type: application/json" \
  --data "{\"id\":\"Zone ID\",\"type\":\"A\",\"name\":\"domain.tld\",\"content\":\"$(curl https://ifconfig.co)\",\"proxied\":true}"

After running this command in your terminal you should see the A record has changed to your ip with the proxy enabled.

Step 7: Automate the Script

Marking the script run automatically

  1. mkdir /path/to/ddns.sh

  2. chmod 700 /path/to/ddns.sh

  3. Put the command we just made in here

  4. nano /path/to/ddns.sh

  5. crontab -e

  6. */5 * * * * /path/to/ddns.sh >/dev/null 2>&1

This will check your IP every 5 minutes and update your DNS record if it has changed.