How to set up Dynamic DNS with Cloudflare
Intro
As someone too poor to pay for a static IP, I struggled a little when I wanted to set up a VPN tunnel into my home network so I could access my self-hosted applications. Fortunately, getting DDNS working is actually quite easy and it only takes a few minutes.
I’ll be using ZenDNS for this article because I wrote it myself to be simple and easy to use. As the title suggests, I’ll only cover the configuration for Cloudflare but ZenDNS also has support for other providers.
This guide assumes you’re using Linux, but you should be able follow on MacOS and Windows as well.
Requirements
- Working knowledge of the commandline
- ZenDNS
- A Cloudflare API Token with
Zone:Zone:Read
andZone:DNS:Write
permissions
Installing ZenDNS
Navigate over to install instructions and choose the installation option that works best for you.
For me, it was as simple as:
# Download the source
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/dotzenith/zendns/releases/latest/download/zendns-installer.sh | sh
Getting the Cloudflare API key
- Login in to your Cloudflare account and on
My Profile
at the top right - Click on the
API Tokens
tab in the sidebar - Click
Create Token
and then use theEdit zone DNS
template - In the permissions section, add
Zone:Zone:Read
andZone:DNS:Edit
- You can leave the Client IP Address Filtering section as-is
- Select how long you’d like the token to be active for in the TTL section
- Click
Continue to summary
- Verify everything and click
Create Token
- Copy the token and keep it saved, you will only see this once
Set up ZenDNS
Now that we have the pre-requisites taken care of, we’re finally ready to go
Open ~/.config/zendns/config.yaml
in the text-editor of your choice and paste the following:
Note: this file can be anywhere
cloudflare:
- key: "your-api-token"
zone: "yourdomain.com"
hostname: "hostname.yourdomain.com"
ttl: 1
proxied: false
Note: Make sure you have a corresponding A record for the
hostname
in your Cloudflare dashboard
Once the config file is taken care of, we can run ZenDNS like so:
zendns --config ~/.config/zendns/config.yaml
Update DNS entries automatically
We very obviously don’t want to manually run ZenDNS every time our IP changes, so we can automate it using cron
We can use cron by running crontab -e
and then adding this entry:
*/5 * * * * /full/path/to/zendns --config ~/.config/zendns/config.yaml --log /var/log/zendns.log
Note: You might need to pass in the full path to ZenDNS because it might no be in the $PATH for cron
This will run ZenDNS every 5 minutes and update the entries if they have changed
Note: ZenDNS will cache your IP and only make changes if they have changed since last usage, you can override this by passing the
--force
flag
Conclusion
I hope this serves as an easy and straightforward guide to setting up DDNS using Cloudflare. Next up, we’ll set up a wireguard tunnel to easily access our homelab services when we’re away.