Use Cloudflare as Dynamic DNS with Raspberry Pi

Use Cloudflare as Dynamic DNS with Raspberry Pi

Share on facebook
Share on twitter
Share on pinterest

You’ll learn how to update a domain on Cloudflare® after your dynamic IP changes. By utilizing Cloudflare as Dynamic DNS, you gain access to your home server from anywhere without worrying about your ever-changing IP. That machine could very well be a Raspberry Pi running a web server with WordPress among other things like VNC or a security camera. However, the process requires a domain that you manage on Cloudflare. Two technologies collide in this first step of a journey that leads to a development server at home.

The choice fell on Cloudflare as Dynamic DNS

Initially, I thought I’ll continue using free Dynamic DNS services such as DynDNS, FreeDNS by afraid.org, or No-IP. If any of these ring a bell, you are in the right place. What these services have in common, is that they give you a free subdomain and let you update to what IP it should point. They allow access to your home network without knowing your current IP. You might want to automatically update records because you have a dynamic IP that changes every day. But as soon as you leverage utilities or scripts to automate updating the IP, they’ll start to nag you every month with an upsell email. You need to log in sometimes to keep these services running (for free). I grew tired of this nag, only to discover that Cloudflare has an API. It sparked an idea that maybe it’s possible to use Cloudflare as Dynamic DNS.

The API means that machines and 3rd party apps can edit DNS records, without manual intervention. It turns out; a nag-free excellent service was hiding in plain sight. We use Cloudflare extensively anyway, on every site we operate. So why not pick something like home.example.com and keep it pointing to my home server all the time? With Cloudflare as Dynamic DNS, it’s finally possible, and I’m going to teach you how.

Pre-requisites

I’m going to assume you have all of these ready or know how to do these (hence the advanced label):

  • Own and manage a domain on Cloudflare
  • Have a home server that runs Linux. This example uses Raspberry Pi 3 Model B+ running Raspbian Stretch with a desktop.

Why the Raspberry Pi? Simply because it’s an inexpensive way to get your hands on a home server and to learn Linux. That’s the exact reason why I bought it.

What services you host on the server, how you forward ports to them are not the scope of this article. They could be anything you want. On a side note, a static local IP address doesn’t hurt, but in Raspbian you can do that in the GUI.

Creating an A record for your home server

Come up with a subdomain. It’s part of the address at which you’ll reach your home server. Eventually, that’ll be the host for your development WordPress site too.

  1. Go to Cloudflare Dashboard Home while you are logged in
  2. Choose your domain and go to its DNS tab
  3. The “A” record is the default to add, so enter your desired subdomain name like home to Name
  4. As the IPv4 address, enter 0.0.0.0 (not your real IP, so you can later verify the script works)

Home subdomain pointing to 0.0.0.0 when using Cloudflare as Dynamic DNS

Obtaining the necessary key from Cloudflare

There is a key (looks like random characters) that we need from Cloudflare. It ensures you have the rights to update your chosen domain.

Global API Key

  1. Log in to Cloudflare and go to your Profile
  2. Scroll down and View your Global API Key
  3. Complete the password challenge and note your key

Setting up the file on the Raspberry Pi

First, we’ll create a folder for the script and its files it needs. Open a terminal and run this:

mkdir cf

It’ll create a folder named cf in your home directory. For the next step, you can use the simple text editor from the accessories. If you are going to edit some source code on the Raspberry Pi, you might be missing Sublime Text. It’s not available, but another similar editor called Geany will surely help. For this step, it’s not necessary just recommended. So, the optional command is:

sudo apt-get install geany

Create a new file called lwp-cloudflare-dyndns.sh and put it in the folder you’ve created. The gist includes the complete script. You need to change the following part with real values:

auth_email="[email protected]"
auth_key="global_api_key_goes_here" 
zone_name="example.com"
record_name="home.example.com"

For the email, use the address you log in to Cloudflare with. The auth key is the Global API Key you noted above. Save, then change the permissions on this file to be executable:

chmod +x cf/lwp-cloudflare-dyndns.sh

You can run it now. If everything is fine, you should see three new files pop into existence. The cloudflare.ids file should have two lines in it. In case it doesn’t, read the optional part at the end of this article.

Setting up a cron job to use Cloudflare as Dynamic DNS

For now, you can manually run the file, and you should see the correct IP in Cloudflare. The only thing left is to automate this. Open a terminal and run this command:

crontab -e

If it offers you to choose an editor, select the one that says Nano (easiest), by its number then press Enter. Please note that this instruction is for Nano. Scroll down and add this as a new line without any # at the beginning of the line:

0 */6 * * * /bin/bash /home/pi/cf/lwp-cloudflare-dyndns.sh

FYI, the Ctrl+Shift+V is the paste. If you are wondering what those stars are, then crontab.guru can help. I set it to run every 6 hours. Even if your IP only changes every few months, this six hours is the longest amount of time you can be without a fresh IP sent to Cloudflare. If that’s unacceptable, feel free to increase the frequency. For example, this updates every minute, so you can at least test it by looking at the cloudflare.log file.

*/1 * * * * /bin/bash /home/pi/cf/lwp-cloudflare-dyndns.sh

To exit this editor, press Ctrl+X, then press Y to say Yes to save changes. It might prompt you another time indicating where to save; just press Enter there. The changes are live and imminent, but if you don’t trust the system you may reload the service, but it’s not necessary:

sudo service cron reload

From this point on, the cron job will operate in a set it and forget it manner. It’ll work until you remove your domain from the system or if the API changes.  This concludes setting up Cloudflare as Dynamic DNS, but if you run into any trouble don’t forget to ask. The rest of the article helps if the script cannot get your zone and record identifiers. There was a time when I needed to fetch them by hand.

The rest is optional! Manually get identifiers from Cloudflare

In case the script gives you an error and can’t populate the cloudflare.ids file automatically; you need to do its task by hand.

Zone identifier

This ID is unique for each domain, and represents on of them. The following will be used for the Record identifier too, so you hit two flies with one stone.

  1. Go to Cloudflare Dashboard Home while you are logged in
  2. Choose your domain and go to its DNS tab
  3. Open developer tools with F12 and go to Network tab and choose XHR subtab
  4. Click the cloud icon in the row that has your subdomain
  5. Right click on the newly appeared row and select Copy > Copy link address (Chrome) or Copy URL (Firefox)
  6. Note the zone identifier that you’ll find after the zones/ part.

The link will look like this:

https://dash.cloudflare.com/api/v4/zones/zone_id/dns_records/record_id

Record identifier

It identifies individual records. It’s imperative to get the identifier for the correct record (click on the right cloud). Fortunately, the steps are the same from above, but use the last part from the link.

Once you have both identifiers

  • Make sure the cloud is gray. It’ll disable the caching services and lets you use Cloudflare as Dynamic DNS only.
  • To the cloudflare.ids file, add the zone identified on the first line, and the record identifier on the second line.

This site is powered by Elementor

  • This site is powered by Elementor

Related Posts

Comments are closed.

Check out Justified Image Grid, my top-selling WordPress gallery that shows photos without cropping!

Show your photos with Justified Image Grid!