Prerequisites

The following tools are only needed if you are using something like the UniFi Cloud Key

  • A SSH Client (for example Putty if you are using Windows)
  • A FTP Client (for example FileZilla if you are using Windows)

Getting Started

First you need to get a Global API Key from Cloudflare. For that you need to open this page and click on "Change" first to regenerate your API Key and afterwards on "View"

API_Tokens_1
API_Tokens_2
API_Tokens_3

Now you need to add a DNS A record to your domain that is pointing to 127.0.0.1

DNS_Record

After creating your DNS record you need to create a config.gateway.json file with the following content:

{
  "service": {
    "dns": {
      "dynamic": {
        "interface": {
          "<The network interface that is facing the internt. For me it's eth0, but could be different for you>": {
            "service": {
              "cloudflare": {
                "host-name": [
                  "<The name of the previous created A record. subdomain.example.org for example>"
                ],
                "login": "<Your Cloudflare E-Mail address>",
                "options": [
                  "zone=<Your domain. example.org for example>"
                ],
                "password": "<Your Cloudflare API Key>",
                "protocol": "cloudflare",
                "server": "api.cloudflare.com/client/v4"
              }
            },
            "web": "dyndns"
          }
        }
      }
    }
  }
}

You need to replace the placeholders with your own data. To get the interface that you are using you need to SSH into your router and enter ip addr to see the network interfaces of your router.
Now you ned to copy the file to the <unifi_base>data/sites/default directory. The <unifi_base> path is different for each platform:

  • UniFi Cloud Key: /usr/lib/unifi
  • Debian/Ubuntu Linux: /usr/lib/unifi
  • Windows: %userprofile%/Ubiquiti UniFi.
  • macOS: ~/Library/Application Support/UniFi

(Source)

Aftwards you need to update the old version of ddclient on your Unifi router:

  1. First you need to connect to the router via SSH. If you don't know how to connect to your router via SSH take a look at this article from Ubiquiti
  2. Now you need to make a copy of your existing ddclient:
sudo cp usr/sbin/ddclient /usr/sbin/ddclient.bak
  1. The only thing left to do is to update ddclient to the new version provided by Ubiquti:
curl -O ttps://dl.ubnt.com/firmwares/edgemax/afomins/ddlient-with-cloudflare-v4-api-support-000/ddclient && sudo chmod +x ddclient && sudo mv ddclient /usr/sbin/ddclient

(Source)
This is only a temporary workaround. You are going to need to upgrade the ddclient everytime you upgrade the firmware of your router.

The final step is to force provision your router with the new config. To force provision your router you first need to go to the clients list in your UniFi Network Controller

Force_Provision_1

and open the property panel with a click on your router. Now you need to open the settings of your router in the opened side panel

Force_Provision_2

go to the Manage Device section and click on Provision

Force_Provision_3

Checking the result

To check if your dynamic DNS is working open a command line and enter nslookup <DNS A Record> 8.8.8.8> to check the IP address that is being reported to Cloudflare by the Ubiquti router

nslookup

> nslookup <DNS A Record> 8.8.8.8
Server:  dns.google
Address:  8.8.8.8

Nicht autorisierende Antwort:
Name:    subdomain.example.org
Address:  XXX.XXX.XXX.XXX

Troubleshooting

If the external IP address does not match your external IP you need to SSH into your router again and enter update dns dynamic interface eth0 to force an update of your dynamic dns record and show dns dynamic status after waiting a few seconds to see the current status of your dynamic dns:

2020-07-03_23-07-25_2vXt5T8OuZ

> update dns dynamic interface eth0
> show dns dynamic status
interface    : eth0
ip address   : XXX.XXX.XXX.XXX
host-name    : subdomain.example.org
last update  : Fri Jul  1 12:00:00 2020
update-status: good