How-to: Using Cloudflare for free dynamic DNS service


Recommended Posts

Like most of you, I'm on a dynamic IP from my ISP but my unRaid box is running a bunch of services I want to access remotely.  I could easily pay for a dynamic DNS service, but why pay for something when you can do it for free :)

 

Here's how I got it working for Cloudflare, and wanted to share how I did it in case others are interested.

 

Note: I'm not an engineer or super familiar with Linux or UnRaid.  If there's improvements you'd suggest, I'm open to them!

 

-------

 

Step 1: Setup a free Cloudflare account.

Beyond the scope of this how-to. I'll assume you can sign up and setup your zone records.  Make sure you add an A record for your domain or subdomain (E.g. tower.mydomain.com).

 

Step 2: Get your cloudflare API key

Go to https://www.cloudflare.com/a/account/my-account to get your API key.

 

Step 3: Create the base script that talks to cloudflare APIs

Using this script, copy it to /boot/scripts/cf-ddns.sh.

Don't bother adding your APi key and other values into the script itself, we'll just pass it in at run time in a later step to make things simpler.

 

Step 4: Create a second script to call into the base script

Create second script that will be ultimately called by your cron job.  This second file is used so we don't have to edit the base script.

Create /boot/scripts/cf-update-ip.sh:

 

#!/bin/bash

DOMAIN=yourdomain.com
[email protected]
CFKEY=<your API key from step 2>
SUBDOMAINS=(foo bar car)

for i in "${SUBDOMAINS[@]}"
do
  /boot/scripts/cf-ddns.sh -k $CFKEY -z $DOMAIN -u $USER -h ${i}.${DOMAIN} 2>&1 | /usr/bin/logger -t cf-update-ip.sh
done

 

The above code will pass your Cloudflare API key, domain, email to the base script and update the IPs for foo.yourdomain.com, bar.yourdomain.com and car.yourdomain.com.  It also suppresses your output and redirects everything to syslog (this is a personal preference, so if you prefer to not do that, omit the "2>&1 | /usr/bin/logger -t cf-update-ip.sh" part of the line).

 

Step 5: Setup Cron to regularly check your IP regularly

The value in this script is that it's run regularly. You can do it every minute if you want, but realistically your IP isn't changing that often. I do  at the 20 min mark every hour.

 

I keep all my custom cron files in /boot/config/plugins (another personal preference, put anywhere you want).  Assuming you're following my preferences, create /boot/config/plugins/cf-update-ip/cf-update-ip.cron:

 

#hourly update cloudflare IPs (run hourly, 20 mins after the hour)
20 * * * * /boot/scripts/cf-update-ip.sh

 

Step 6: Update your cron schedule

Make sure you run update_cron otherwise your new cron job won't be scheduled.

 

Step 7: Enjoy

You'll now see lines in your syslog when the job is run that either tells you the IP was updated, or usually it'll just spit out:

 

Apr 13 19:20:01 Tower cf-ddns.sh[21730]: WAN IP Unchanged, to update anyway use flag -f true

 

Note: If you have multiple subdomains you're updating, you'll see one syslog line for each.  If you want positive confirmation it's iterating through all of them, update line 124 in your /boot/scripts/cf-ddns.sh (step 3) to this:

 

echo "WAN IP Unchanged for ${CFHOST}, to update anyway use flag -f true"

 

Your syslog will then show the following lines, which I find much more helpful to know it's operating against all the subdomains I setup.

 

Apr 13 19:36:47 Tower cf-update-ip.sh: WAN IP Unchanged for foo.yourdomain.com, to update anyway use flag -f true
Apr 13 19:36:47 Tower cf-update-ip.sh: WAN IP Unchanged for bar.yourdomain.com, to update anyway use flag -f true
Apr 13 19:36:47 Tower cf-update-ip.sh: WAN IP Unchanged for car.yourdomain.com, to update anyway use flag -f true

 

Link to comment

There's a docker container that can do this for you.  I made a template for it I think but never released it as the underlying container wasn't my own.

 

Here it is if you want to make use of it..

 

https://github.com/CHBMB/docker-templates

 

I actually switched back to ddclient from cloudflare as I had some problems with it and ended up with all the features turned off other than the DynamicDNS service which seemed a bit of a waste. 

 

Sent from my LG-H815 using Tapatalk

 

 

Link to comment

There's a docker container that can do this for you.  I made a template for it I think but never released it as the underlying container wasn't my own.

 

Here it is if you want to make use of it..

 

https://github.com/CHBMB/docker-templates

 

I actually switched back to ddclient from cloudflare as I had some problems with it and ended up with all the features turned off other than the DynamicDNS service which seemed a bit of a waste. 

 

Sent from my LG-H815 using Tapatalk

 

Got the container setup but something seems wrong.. even though the refresh interval was setup as 300 seconds (the default), it's not updating or seemingly re-checking anything.  The logs show nothing after the first run when the container is started.

 

Link to comment
  • 3 months later...
  • 5 months later...

There is a bug in the script in step 3.  It works well but it will only update the first hostname due to the checking if the IP is the same as last run.  If you make a small change to the comparison txt file so that its unique for each record.  I changed line 115/116 to

 

if [ -f $HOME/.wan_ip-$CFHOST.txt ]; then
  OLD_WAN_IP=`cat $HOME/.wan_ip-$CFHOST.txt`

 

and line 179 to

 

  echo $WAN_IP > $HOME/.wan_ip-$CFHOST.txt

 

That way it creates a ip file unique for each hostname.  Just a suggestion.

Link to comment
  • 1 year later...
On 4/14/2016 at 4:55 AM, tmchow said:

but why pay for something when you can do it for free

 

So why are you using unRaid since OpenSource NAS like OMV or FreeNAS could do similar :P

BTW: I'm just pulling your leg

I use for too long OMV and FreeNAS then decide to give a try to unRAID yesterday and since then I'm like a kid with a new toys.

Link to comment
  • 11 months later...

Hi All.

Im not too technical here so sorry for the questions.

Can I put this cf-ddns.sh in a user share ?

Can I create the calling script in user scripts where I have all my other bits and pieces ? 

 

The reason I ask is that I did this all, made the cf-ddns.sh executable and edited the calling script to /mnt/user/Scripts/cf-ddns.sh -k $CFKEY -z $DOMAIN -u $USER -h ${i}.${DOMAIN} , and ran it via user scripts and I got this :

 

/usr/bin/env: 'bash\r': No such file or directory
/usr/bin/env: 'bash\r': No such file or directory
/usr/bin/env: 'bash\r': No such file or directory
Script Finished Mon, 25 Mar 2019 18:39:48 +1000

 

Sorry - likely a noob question.....

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.