NoCache

Table of Contents

Setup Cloudflare Dynamic DNS (DDNS) with CLI on Windows, Mac, Linux

Cyrus Kao
Last modified on .

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:

Download page
Download page of Node.js

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:

API tokens
API tokens on Cloudflare

Choose Edit zone DNS template:

Creating API token
Creating API token

Select the Zone which you wish to grant access, then Continue:

Configuring API token
Configuring API token

Confirm to create API token:

Confirming to create API token
Confirming to create API token

Copy the generated API token:

API token generated
API token generated

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:

Zone ID
Zone ID on Overview panel

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, an A 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

Comments

Sign in to leave a comment.