GeneralTutorials

Use Cloudflare as Dynamic DNS

I would like to share how I use Cloudflare as Dynamic DNS. I am using Dynamic DNS service from No-IP for three years until a colleague of mine mentioned about Cloudflare’s really fast global DNS. Initially, I thought that Cloudflare was just a CDN and an Internet company that protects websites from Distributed Denial-of-Service (DDoS) attack, upon further investigation I found out that Cloudflare also provides a DNS service with a REST-API for you to update your DNS records.

I migrated most of my domain to CloudFlare DNS and then uses their API to update my domain’s DNS records once my ISP changed the IP address.

To know my router’s IP address I use the service of ipinfo.io. You may use other provider or other technique to know your router’s public IP address.

Source code available at https://github.com/mrprintedwall/cloudflare-as-dynamic-dns

$nano updateip.sh
#!/bin/bash

IP=$(curl -s "https://ipinfo.io/ip")
echo "THIS IS IP $IP"

curlUpdate(){
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$1/dns_records/$2" \
-H "X-Auth-Email: [email protected]" -H "X-Auth-Key: YOUR_CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$4\",\"content\":\"$3\",\"proxied\":true}"
}

curlUpdate "ZONE_ID" "ID_OF_DNS_RECORD" "$IP" "johnpili.com"
curlUpdate "ZONE_ID" "ID_OF_DNS_RECORD" "$IP" "www.johnpili.com"
#curlUpdate "ZONE_ID" "ID_OF_DNS_RECORD" "$IP" "domain1.com"
#curlUpdate "ZONE_ID" "ID_OF_DNS_RECORD" "$IP" "www.domain1.com"
#curlUpdate "ZONE_ID" "ID_OF_DNS_RECORD" "$IP" "domain2.com"
#curlUpdate "ZONE_ID" "ID_OF_DNS_RECORD" "$IP" "www.domain2.com"

You can then setup your CRON to run the script on a specified interval or schedule.

Disclaimer

Cloudflare’s DNS REST API is owned by Cloudflare and subject to Cloudflare’s terms and conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *