Cloudflare is a free service that provides DNS on the Internet. With dynamic DNS, you can access your home computer from any other device and still get to work or school without having to remember IP addresses or manage static IPs in a spreadsheet.
The “cloudflare dynamic dns client” is a simple, free tool that allows for the automatic updating of your domain name with Cloudflare’s servers.
Assume you established a DNS record that links to your router’s public IP address and published a website from your home network. When your public IP address changes, how do you keep your DNS record updated? Perhaps you should use Cloudflare’s dynamic DNS service.
Internet service providers (ISPs) commonly issue customers dynamic IP addresses that may change at any moment. As a consequence, your DNS record continues to refer to an IP address that no longer belongs to you, rendering your website unavailable.
Continue reading because this article will show you how to utilize Cloudflare’s application programming interface (API) and PowerShell to keep your DNS records updated. Let’s get this party started!
The prerequisites for following this session hands-on are listed below.
- The Cloudflare dynamic DNS update script will be performed on a machine using PowerShell 7.1. This training will be performed on a machine running Windows 10.
How to Guide Yourself Through a PowerShell 7 Upgrade
- Your preferred script editor. Visual Studio Code v1.59.1 will be used in this lesson (recommended).
A Tutorial on What You Need to Know About Visual Studio Code
- You already own the domain. This tutorial’s sample domain is lazyexchangeadmin.cyou.
Free top-level domains (TLDs) like.ml,.ga,.tk,.cf, and.gq are not supported by Cloudflare dynamic DNS automation and will not function with this tutorial.
DNS example A Cloudflare record
How to Host an Azure Static Website with Cloudflare Support (Set up Cloudflare DNS)
What is Cloudflare Dynamic DNS, and how does it work?
DNS records are essentially static, which makes them incompatible with dynamic IP addresses. You’ll need to set up dynamic DNS to remedy this issue. Fortunately, Cloudflare has an API that enables you to programmatically manage DNS records.
To set up Cloudflare dynamic DNS, you’ll need to run a process on a client within your network that does two things: gets your network’s current public IP address and updates the DNS record automatically.
The high-level figure below depicts how the Cloudflare dynamic DNS update pipeline works.
Update flow for Cloudflare Dynamic DNS
Many dynamic DNS clients are available for Linux systems to install, such as on an Ubuntu server or a Raspberry PI. There are also third-party dynamic DNS services available, which might be expensive or free. However, in this article, you’ll be using PowerShell 7.1 to deploy a DNS updater script.
How to Get a Cloudflare API Token
Your script must authenticate itself to the Cloudflare API before changing the Cloudflare dynamic DNS record programmatically. Cloudflare will only enable you to make modifications to your DNS records after that.
Cloudflare enables you to generate API tokens with sufficient permissions. You may then login with the Cloudflare API using your Cloudflare account username and the resultant API token.
Follow these instructions to create a Cloudflare API Token.
1. Go to https://dash.cloudflare.com/login/ in your browser and log in to your Cloudflare account.
2. After entering in to the Cloudflare dashboard, choose My Profile from the profile icon in the upper-right corner.
Getting to your Cloudflare profile
3. Finally, go to the API Tokens tab. Click the Create Token button in the API Tokens section. The following example assumes you haven’t yet generated any API tokens.
Create Token by clicking
4. To use the Edit zone DNS template, choose it from the list of API token templates. This template lets you establish an API token with edit access to all or a subset of your account’s DNS zones.
The Edit zone DNS API token template is selected.
5. On the Create Token page, under the Zone Resources section, pick the DNS zone to include in this API token’s access by clicking the right-most selection box. Continue to summary after Choosing a DNS Zone.
Fill in the Start Date and End Date under the TTL section to restrict the API token’s validity term. The API token will have no expiry date if it is left blank.
Choosing a DNS Zone
6. Look at the summary and double-check that the API has DNS: Edit permissions for the DNS zone you previously specified. Finally, to generate the API token, click Create Token.
Examining API token permissions
7. Once the API token has been created, copy the token value and save it safely. Treat the API token in the same way you would a password.
The API Token is copied.
8. Return to the API Tokens page and double-check that the API token you established is still active.
Authenticating the API Token
Cloudflare Dynamic DNS Update Script Configuration
You now have a Cloudflare account as well as a Cloudflare API Token that allows you to update DNS zones. What comes next? You’ll need to utilize the API token and combine it with a script that updates the Cloudflare dynamic DNS record programmatically.
This article includes a PowerShell script that can be used to update a DNS record in Cloudflare.
To store the Cloudflare dynamic DNS PowerShell script on your client PC, follow the instructions below.
1. Determine where the script will be saved and, if necessary, create that folder. The script is stored at C:CloudflareDDNS in this example.
2. Create a new PowerShell script file named Update-CloudflareDDNS in your code editor, such as VSCode. ps1. Save the script in the CloudflareDDNS folder at C:Cloudflare.
3. Copy and paste the script below into your code editor, then save the file. To learn what the script does, read the inline comments.
Version 7.1 is required. [cmdletbinding()] [parameter(Mandatory)] param [parameter(Mandatory)] $Email [parameter(Mandatory)] $Token [parameter(Mandatory)] $Domain $Record) # Construct the request headers just once. The script will utilize these headers throughout. @ “X-Auth-Email” = $($Email) $headers “Content-Type” = “application/json” “Authorization” = “Bearer $($Token)” Test of Region Tokens ## This section validates the validity of your API key. ## Otherwise, the script will exit. “https://api.cloudflare.com/client/v4/user/tokens/verify” $uri = “https://api.cloudflare.com/client/v4/ Invoke-RestMethod -Method GET -Uri $auth result -Headers $uri -SkipHttpError $headers Determine if (-not($auth result.result)) Writable-Output “Validation of API tokens failed. $($auth result.errors.message) $($auth result.errors.message) $($ Script is ending.” # Return from exit script Output-Write “[$($Token)] API token validation was successful. $($auth result.messages.message).” #Region #EndRegion Get Zone ID ## Returns the zone identifier for the domain based on the zone name. The script will exit if the identification is not found. “https://api.cloudflare.com/client/v4/zones?name=$($Domain)” $uri = “https://api.cloudflare.com/client/v4/zone Invoke-RestMethod -Method GET -Uri $DnsZone -Headers $uri Write-Output $headers -SkipHttpErrorCheck if (-not($DnsZone.result)) “There are no results for the DNS domain [$($Domain)]. Script is ending.” ## Save the DNS zone ID $zone id = $DnsZone.result.id # Exit script return #End Region #Region Write-Output “Domain zone [$($Domain)]: ID=$($zone id)” Get DNS Record ## Get the information of any existing DNS records from Cloudflare. “https://api.cloudflare.com/client/v4/zones/$($zone id)/dns records?name=$($Record)” $uri = “https://api.cloudflare.com/client/v4/zones/$ Invoke-RestMethod -Method GET -Uri $DnsRecord -Headers $uri Write-Output $headers -SkipHttpErrorCheck if (-not($DnsRecord.result)) “There are no results for the DNS record [$($Record)]. Script is ending.” # Exit script return ## Save the existing IP address in the DNS record $old ip = $DnsRecord.result.content ## Store the DNS record type value $record type = $DnsRecord.result.type ## Store the DNS record type value $record type = $DnsRecord.result.type $record id = $DnsRecord.result.id ## Save the DNS record id value $record ttl = $DnsRecord.result.ttl ## Save the DNS record ttl value ## Save the proxied DNS record value. $DnsRecord.result.proxied = $record proxied “DNS record [$($Record)]: Type=$($record type), IP=$($old ip)” Write-Output #Region #EndRegion $new ip = Invoke-RestMethod -Uri ‘https://v4.ident.me’ Get Current Public IP Address Write-Output “OLD=$($old ip), NEW=$($new ip) Public IP Address” ## Compare current IP address with DNS record ## #EndRegion #Region update Dynamic DNS Record Update the DNS record if the current IP address does not match the DNS record IP address. ($new ip -ne) if $old ip) Writable-Output “The current IP address does not match the IP address of the DNS record. Make an update attempt.” ## Replace the old IP address in the DNS record. “https://api.cloudflare.com/client/v4/zones/$($zone id)/dns records/$($record id)” $uri = “https://api.cloudflare.com/client/v4/zones/$( $body = @ ttl = $record ttl proxied = $record proxied type = $record type name = $Record content = $new ip ttl = $record ttl | JsonConverter Invoke-RestMethod -Method $Update PUT -Uri -Headers $uri $headers -Body -SkipHttpErrorCheck if (($Update.errors)) $body Writable-Output “The updating of DNS records failed. Error: ## Exit script return Write-Output “DNS record update succeeded. $($Update.errors.message)” “else Write-Output return ($Update.result) “The current IP address and the IP address of the DNS record are the same. There’s no reason to change anything.” } #EndRegion
Now that you’ve saved the Cloudflare dynamic DNS update script, what’s next? Before deploying the script, testing whether its functionality works is essential. Script Execution requires four details to be successful, and those are:
- The email address associated with your Cloudflare account.
- Token – the API token you established with your Cloudflare account before.
- The DNS domain name that includes the DNS record you wish to change is called domain. (For instance, lazyexchangeadmin.cyou).
- The DNS record you wish to change is called Record. Demo.lazyexchangeadmin.cyou, for example.
Follow the steps below to execute the script.
To begin, start a PowerShell session. Use the VSCode PowerShell terminal instead if you’re using VSCode.
2. Check the IP address of the current DNS record using a DNS record lookup. Run the Resolve-DnsName cmdlet to do so, as shown below. Make sure the -Name option has the fully qualified domain name (FQDN).
The command should return the IP address of the DNS record you looked up.
Before the Cloudflare dynamic DNS change, I looked up a DNS record.
Resolve-DnsName: Using PowerShell to Resolve DNS Records
3. Now, execute the Update-CloudflareDDNS.ps1 script to update the Cloudflare DNS record using the command below. Change the Email, Token, Domain, and Record if necessary. The following command assumes your script is located in C:CloudflareDDNSUpdate-CloudflareDDNS.ps1.
C:CloudflareDDNSUpdate-CloudflareDDNS.ps1 ‘ -Token ‘kGW8n……..eJl5a”-Domain ‘lazyexchangeadmin.cyou”-Record ‘demo.lazyexchangeadmin.cyou’
As you can see in the picture below, the script then takes the following steps.
- The API token is validated.
- Locate the domain that has the DNS record that needs to be updated.
- Locate the DNS record that needs to be updated.
- Compare the IP address of the DNS record to the external or public IP address.
- In Cloudflare, change the DNS record IP address.
- The outcome of the dynamic DNS update is shown.
The Cloudflare dynamic DNS update script is being run.
4. Return to your Cloudflare dashboard and double-check that the DNS record for the new IP address is correct. The IP address has changed to the external IP address value after executing the update script, as seen in the picture below.
Cloudflare DNS record verification
5. Finally, repeat step 2 to test if the new IP address has already been widely disseminated.
Propagation of DNS records might take many minutes, hours, or even days. Cloudflare propagation took less than a minute in this case.
After the Cloudflare dynamic DNS change, looking up a DNS record
Cloudflare Dynamic DNS Update Task Scheduling
At this point, you now have a working script that updates your Cloudflare dynamic DNS record. But Script Execution manually each time is not the goal. You must make sure that the dynamic DNS update runs on a schedule automatically as often as necessary.
The way you schedule the script to run differs depending on the operating system you’re using, such as a cron job in Linux. This guide will show you how to set up a Windows task to perform the Cloudflare dynamic DNS update script every five minutes.
How to Use PowerShell to Create and Manage Scheduled Tasks
Defining the Task Action Schedule
You must have an action for your scheduled job, which you can build using the New-ScheduledTaskAction cmdlet. The Update-CloudflareDDNS.ps1 script is launched using the PowerShell executable file pwsh.exe in this example.
Open a new PowerShell window as an administrator. Then, in PowerShell, copy and execute the code below. Make that the $scriptPath, $Email, $Token, $Domain, and $Record variables are all set to the correct values.
# Define the attributes of the scheduled task action ## Type the location of the PowerShell script. $scriptPath = ‘C:CloudflareDDNSUpdate-CloudflareDDNS.ps1’ ## Email address for Cloudflare account ## Cloudflare API Token $Email = ‘[email protected]’ $Domain = ‘lazyexchangeadmin.cyou’ ## DNS Domain Name $Token = ‘kGW8n……..eJl5a’ ## DNS Record to Update ‘demo.lazyexchangeadmin.cyou’ $Record = ‘demo.lazyexchangeadmin.cyou’ # Create an action object for the scheduled job. ‘ -Execute $taskAction = New-ScheduledTaskAction “pwsh.exe”‘-File “-Argument -Email $Email -Token $scriptPath -Domain $Domain -Record $Record $Token”
Creating the Trigger for a Scheduled Task
After you’ve established what the job will accomplish, use the New-ScheduledTaskTrigger cmdlet to construct a trigger or schedule for the task. To create a new task trigger that runs every five minutes for 10 years, copy the code below and execute it in PowerShell.
# Make a timetable for a bew scheduled job trigger # # Trigger = once every five minutes for ten years. New-ScheduledTaskTrigger = $taskTrigger ‘ -Once ‘ -At (Get-Date -Minute 0 -Second 0) ‘ -IntervalOfRepetition (New-TimeSpan -Minutes 5) ‘ -DurationOfRepetition (New-TimeSpan -Days 3650)
The System’s New Scheduled Task is Created
Finally, you may use the computer to register the new scheduled job. To do so, use the Register-ScheduledTask cmdlet, which is the same as the code below. To register the scheduled job, copy the code below and execute it in PowerShell.
The SYSTEM account will be used to perform the resultant scheduled job. If required, you may designate a separate user account to conduct the scheduled operation.
# Create a system task for the scheduled task. Name of the scheduled task $Description = ‘Update Cloudflare DDNS Entry every 5 minutes’ ## Scheduled Task Name $TaskName = ‘Update Cloudflare Dynamic DNS’ Make a scheduled task. ‘ -TaskName $TaskName’-Description Register-ScheduledTask -Action $taskAction’-Trigger $taskTrigger’-User ‘NT AUTHORITYSYSTEM’ $Description
The command established the scheduled task, and the status is ready, as shown below. The script will now execute every five minutes from this point forward.
New Scheduled Task Registration
It doesn’t have to be difficult to keep DNS records updated when dynamic IP addresses change. You also don’t have to pay for a dynamic DNS service. You can use Cloudflare’s API to change your DNS records programmatically if you manage your DNS zones in Cloudflare.
Using a mix of API requests, PowerShell 7, and scheduled activities, you learned how to change a Cloudflare dynamic DNS record in this article. All of this enables you to set up your Cloudflare dynamic DNS update method once and leave it running in the background.
Would you consider using Cloudflare to manage your DNS zones in order to take use of its API? Or do you know of a more convenient and accessible option?
The “unifi cloudflare dynamic dns” is a feature that allows users to setup their own Dynamic DNS. Cloudflare has been providing this service for quite some time now, and it is easy to use.
- cloudflare dynamic dns free
- cloudflare dynamic dns docker
- cloudflare dynamic dns pfsense
- cloudflare ddns-updater
- cloudflare dynamic dns windows