Setup Cloudflare Dynamic DNS (DDNS) with CLI on Windows, Mac, Linux
Cloudflare API v4 enables users to update DNS records programmatically, which makes Dynamic DNS (DDNS) implementation possible. I recently wrote a CLI tool called cf-ddns-cli
in Node.js to update DNS A
(IPv4) and AAAA
(IPv6) records with your current IP address, and it's available in Windows, Mac and Linux.
Prerequisites
Since the CLI tool is written in Node.js with TypeScript, you'll need to install Node.js first so you can install cf-ddns-cli
with NPM (Node Package Manager).
Install Node.js
Install Node.js if you haven't. To check if it's already installed in your system:
$ node -v
v17.0.1
Output
Windows & Mac
Download the installer from the official download page and install it:
Linux
-
Using package manager
Follow the instruction on official site to install Node.js via the package manager of your Linux distros.
-
Using NVM (recommended)
Installing Node.js with NVM allows you to install multiple versions of Node.js and switch between them with ease. Full instruction on Install & Manage Multiple Versions of Node.js with NVM on Linux.
Install Cloudflare DDNS CLI
Install Cloudflare DDNS CLI from NPM:
$ npm i -g cf-ddns-cli
-g
will install the package globally.
Usage
You'll need to generate an API token in order to modify your DNS records, and also get the identification of the zone where you wish to set up DDNS.
Generate API Token
Create a new API token on Cloudflare if you don't have one yet:
Choose Edit zone DNS template:
Select the Zone which you wish to grant access, then Continue:
Confirm to create API token:
Copy the generated API token:
Get Zone Identification
In order to modify the DNS records in the zone which we specified on the API token, we'll need to get the zone identification on the Overview panel of your website on Cloudflare:
Update DNS
Finally, use cf-ddns
to update/create your Cloudflare DNS records with your current IP address:
$ cf-ddns -k API_KEY -z ZONE_ID -n ddns.example.com
Createing new record...
Result: {
type: 'AAAA',
name: 'ddns.example.com',
content: '0000:0000:0000:0000:0000:0000:0000:0000',
ttl: 1,
proxied: false
}
Complete in 2840 ms.
Output
If the connection is made via IPv6, then an
AAAA
record is created/updated. On the contrary, anA
record will be created/updated for an IPv4 connection.
Proxied DNS
Add -p
to the arguments so the DNS record is proxied by Cloudflare:
$ cf-ddns -k API_KEY -z ZONE_ID -n ddns.example.com -p
Updateing exist record...
Result: {
type: 'AAAA',
name: 'ddns.example.com',
content: '0000:0000:0000:0000:0000:0000:0000:0000',
ttl: 1,
proxied: true
}
Complete in 1857 ms.
Output