Dynamic DNS with CloudFlare
-
So I have a dynamic DNS service for my ERL, but it's not in my actual domain. So I made a small script that updates my CloudFlare DNS from my ERL with their API. There is a cron job that just runs this script every 5 minutes.
#!/bin/bash key="your-api-key" zoneID="your-zone-id" email="[email protected]" recordID="record-id-to-update" recordName="newrecord.yourdomain.com" ip=$(ifconfig eth0 | grep "inet addr:" | cut -d: -f2 | awk '{ print $1 }') curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records/$recordID" \ -H "X-Auth-Email: $email" \ -H "X-Auth-Key: $key" \ -H "Content-Type: application/json" \ --data '{"type":"A","name":"'"$recordName"'","content":"'"$ip"'","ttl":120,"proxied":false}' -k
You do have to get the record id from the API. I haven't found a way to get it through the web interface. So that means you have to create the record initially, either through the API or the web interface (or use an existing one). Once you have the ID, just paste it in the variable.
-
You can do this with a device on your LAN also. Instead of getting the IP from the interface just use something like
ip=$(curl http://icanhazip.com)
-
@stacksofplates interesting that would work awesome. Gotta test it this week.
-
Awesome, thanks.
-
Are you guys still using this? Do you think it's the best method still?
-
This is interesting.
But what about jsut using a CNAME to your DynDNS name? That is how I handle it.I have
jared.bundystl.com
as a CNAME on CloudFlare forsorvani.mooo.com
from afraid.org. My router keeps afraid.org up to date. -
@jaredbusch good call, that sounds way easier than what’s above. Setting it up your way now. Thanks
-
@jaredbusch said in Dynamic DNS with CloudFlare:
This is interesting.
But what about jsut using a CNAME to your DynDNS name? That is how I handle it.I have
jared.bundystl.com
as a CNAME on CloudFlare forsorvani.mooo.com
from afraid.org. My router keeps afraid.org up to date.I just don’t have to do accounts with anything else, that’s all. This just makes it a one stop shop. I actually stopped running it on my ERL and run it on one of the servers here. It’s been running since I did this and have never had any issues.
-
@stacksofplates Where do you obtain your record ID? I easily have the record name but can't seem to locate a record ID for my A record. Or is "A" what you're referring to as record ID?
-
Interesting feature with Cloudflare that I just discovered is that they flatten a CNAME record to the root of the domain. So I can enter my DDNS domain as a CNAME record. It will query the IP of that domain and return the IP address to use for the root domain. So this script isn't really needed since it will do the lookup for you.
-
-
@nashbrydges said in Dynamic DNS with CloudFlare:
Where do you obtain your record ID?
#!/bin/bash key="Your_Global_API_Key" zoneID="Your_Zone_ID" email="[email protected]" recordName="example.com" curl -X GET "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records?type=A&name=$recordName" \ -H "X-Auth-Email: $email" \ -H "X-Auth-Key: $key" \ -H "Content-Type: application/json"
-
This post is deleted! -
@NashBrydges I would have bet money that the
$recordID
was just the short name of the record (ie. "sub" of "sub.example.com"). I swear I did it like that and it worked, but apparently not :flushed_face:.The script to get the real identifier is above.
-
@bnrstnr said in Dynamic DNS with CloudFlare:
@NashBrydges I would have bet money that the
$recordID
was just the short name of the record (ie. "sub" of "sub.example.com"). I swear I did it like that and it worked, but apparently not :flushed_face:.The script to get the real identifier is above.
Thanks. I'll give that a try later.
-
@nashbrydges said in Dynamic DNS with CloudFlare:
Interesting feature with Cloudflare that I just discovered is that they flatten a CNAME record to the root of the domain. So I can enter my DDNS domain as a CNAME record. It will query the IP of that domain and return the IP address to use for the root domain. So this script isn't really needed since it will do the lookup for you.
Ya I don’t have a DDNS name so that’s why I Serb it up this way.
-
Very useful. bookmarking this!
-
I've confirmed that Cloudflare's CNAME flattening feature works exactly as advertised.
Create a CNAME record with following values:
- Type = CNAME
- Name = Your root mydomain.com domain name
- Domain Name = Your DDNS domain subdomain.ddns.net (I use No-IP for example but change yours as needed)
I then created another CNAME record to handle the www subdomain and everything works as expected. Both root domain name and www route correctly.
-
@nashbrydges I thought somebody already said this, but I cant find it. The only bad part about the CNAME method if you're using a free account on no-ip, then you have to login to their site and confirm that you're still using the DDNS every 30 days, which is a PITA. Setup the script with a cron job and never have to worry about checking in on no-ip or afraid.org every n days.
-
@stacksofplates said in Dynamic DNS with CloudFlare:
ip=$(curl http://icanhazip.com)
While I was looking into the Cloudflare API a little further yesterday, I came across this. I don't know much about it, but here it is.
Consider replacing
curl -s http://icanhazip.com
withdig +short myip.opendns.com @resolver1.opendns.com
. It works exactly the same.Querying icanhazip.com or other similar sites requires a fairly expensive TCP connection, HTTP overhead, etc. This can be burdensome when queried regularly via cron jobs. A UDP-based DNS connection is considerably faster, lighter weight, and uses far less resources.