CloudFlare Dynamic DNS
So I have an extra computer back home that runs some VMs that I like to have access to remotely. Easy enough, just some basic DDNS. However, I use CloudFlare whom doesn’t directly offer dynamic DNS. Worry not though, for CloudFlare does have an awesome API for updating DNS records.
So let’s go over how I got here. First things first, you’ll need your API key which you can get from your account settings page. You’ll also need to know the DNS Record ID of the record you which to modify.That’s where we’ll start
Getting your DNS Record ID:
If you need help, see here. To keep it short, run this command:
If you’re confused about what some of this stuff is then let me explain quickly. tkn should be set to your CloudFlare API key. email is the e-mail address of your CloudFlare account (same one that issued the API key). z is the TLD of the domain you want to manage on CloudFlare.
Dealing with the return can be interesting. Your response will be in JSON so you’ll have to scan through there for the record you want to mange and pull out its ID. If you need it formatted nicer, try using this online JSON parser.
Editing the Record
Similiar, the official documentation is here. This time though, there’s a little bit more involved:
A lot of the information is overlap from last time but there are a few new additions.
- The DNS Record ID you got in the previous step
- The type of the DNS record (A, CNAME, AAAA, MX, SRV, etc.)
- The name of the subdomain (you can give the full domain name)
- The content of the record. For A records it should be the IP address
Putting it together
To finalize I grab the current WAN IP from the plaintext service ifconfig.io/ip. With a little work I get the final product at the top of this post. I also create a gist of this if you prefer to make some edits.
To automate the process I simply wrote a cron to:
Right now I have it store the current WAN IP in /var/. You can change it to your preferred location or you can just get rid of the storing of it and the comparison. An issue that could arise that if the script fails to set the IP on CloudFlare, it will not try again until the WAN IP changes.