Skip to content

Setting Up a Dynamic DNS Updater for Cloudflare

Setting-Up-a-Dynamic-DNS-Updater-for-Cloudflare-930x620[1]

Setting Up a Dynamic DNS Updater for Cloudflare

Have you ever felt the frustration of your dynamic IP address constantly throwing a wrench in the works of your perfectly functioning homelab? I know I have! That moment when you realize your remote access coding project just failed because your IP changed without warning—ugh, it’s maddening! But fear not, my friends! Today, I’m here to guide you through a simple yet effective solution: setting up a Dynamic DNS updater using Cloudflare. Grab a cup of coffee, cozy up with your favorite chair, and let’s dive into it together.

1. Why Use Cloudflare for DynDNS?

Cloudflare isn’t just a buzzword—it’s a game-changer. Here’s why I love using it for Dynamic DNS:

  • DNS Management: Cloudflare provides incredibly fast and secure DNS services with features like caching and load balancing that keep my applications running smoothly.
  • Free Plan: Let’s be real; who doesn’t love free? Cloudflare’s free DNS management service means I can enjoy DynDNS perks without the strain on my wallet.
  • API Access: This is one of the best parts! With Cloudflare’s API, we can update DNS records programmatically, which is perfect for my self-hosted projects. No more manual updates for me!

2. Prerequisites

Before we get our hands dirty, let’s gather a few things we need:

  • Cloudflare Account: Sign up and add your domain on Cloudflare.
  • API Token: We need to generate a token for DNS updates—don’t worry, I’ll walk you through this!
  • Domain/Subdomain: Decide on the domain or subdomain—like `home.example.com`—that you want to keep updated with your current IP.

3. Generate a Cloudflare API Token

Let’s get started on the exciting part—creating that API token!

  • Log In: Head over to Cloudflare and log in.
  • Navigate to API Tokens: Click on My Profile > API Tokens.
  • Create Token: Hit the Create Token button and select Create Custom Token.
  • Set Permissions: For our DynDNS needs, we’ll set the token to have Zone: Read and DNS: Edit permissions for the domain we’re focusing on.
  • Save the Token: Once you generate it, make sure to copy and save it somewhere safe. You’ll need it for making API calls.

4. Find Your Zone ID

Now, let’s find that elusive Zone ID that we’ll need for our API requests.

  • Navigate to the Dashboard: Look for the DNS section in Cloudflare for your domain.
  • Copy Zone ID: The Zone ID is located at the bottom of the DNS page—save it for later!

5. Using a Script to Update DNS

Now we’re getting into the nitty-gritty! Here’s a simple Bash script that will check and update your Cloudflare DNS record if your IP has changed.

First, install “curl” and “jq” if you haven’t got them yet (and yes, this is a necessary step):

bash
sudo apt install curl jq

– Next, create a new Bash script with the following content:

bash
#!/bin/bash

# Configuration
CF_ZONE_ID=”your_zone_id”
CF_API_TOKEN=”your_api_token”
RECORD_NAME=”home.example.com”

# Get current public IP
CURRENT_IP=$(curl -s http://checkip.amazonaws.com)

# Get Cloudflare DNS record ID
RECORD_ID=$(curl -s -X GET “https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?type=A&name=$RECORD_NAME” \
-H “Authorization: Bearer $CF_API_TOKEN” \
-H “Content-Type: application/json” | jq -r ‘.result[0].id’)

# Get the current IP recorded in Cloudflare
CLOUDFLARE_IP=$(curl -s -X GET “https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$RECORD_ID” \
-H “Authorization: Bearer $CF_API_TOKEN” \
-H “Content-Type: application/json” | jq -r ‘.result.content’)

# Update DNS record if IP has changed
if [[ “$CURRENT_IP” != “$CLOUDFLARE_IP” ]]; then
curl -s -X PUT “https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$RECORD_ID” \
-H “Authorization: Bearer $CF_API_TOKEN” \
-H “Content-Type: application/json” \
–data “{\”type\”:\”A\”,\”name\”:\”$RECORD_NAME\”,\”content\”:\”$CURRENT_IP\”,\”ttl\”:1,\”proxied\”:false}”
echo “DNS record updated to $CURRENT_IP”
else
echo “No update needed. Current IP is $CURRENT_IP”
fi

This friendly script gently checks your current public IP against what’s stored in Cloudflare and will only update if things don’t match. How cool is that?

6. Automate with Cron

Just when you thought we were done, we can make this even better! Automate the script using `cron`. Open your crontab:

bash
crontab -e

And add the following line at the end to run the script every 5 minutes:

*/5 * * * * /path/to/your/script.sh

Believe me—it’s genuinely satisfying to know your DNS will always be up-to-date without you lifting a finger.

7. Using a Third-Party DynDNS Updater Tool

If all this scripting seems overwhelming, there’s an alternative: using `DDClient`. This popular tool makes it easy to manage your DynDNS updates with Cloudflare.

  • Install DDClient:

    sudo apt install ddclient

Configure DDClient: Edit the config file found at `/etc/ddclient.conf`:

daemon=300
syslog=yes
ssl=yes
protocol=cloudflare
use=web, web=checkip.amazonaws.com
server=api.cloudflare.com/client/v4
login=
password=
zone=example.com
home.example.com

8. Testing and Verification

Once you’ve set everything up, it’s time for some testing! Run your script manually, or check the DDClient status to ensure everything updates correctly. You can also:

– Use tools like `dig` or online services such as DNS Checker to verify that your domain’s IP is accurate.

9. Best Practices

Before we wrap up, here are a few best practices I’ve discovered on my journey:

  • Restrict API Token Permissions: For security reasons, limit the token to only the necessary permissions to minimize vulnerabilities.
  • Limit TTL: A low TTL of just 1 minute ensures that your updates happen quickly. It’s all about speed!
  • Monitor Logs: Keep an eye on your script or DDClient logs for any connection issues or failed updates.

The beauty of this setup is that it not only keeps your services available but also grants you peace of mind. Just think about it—you can now host your projects, access your media library, or even run a game server with a dependable domain name, regardless of your pesky ISP changes!

I really hope you found this guide helpful. If you have any questions, suggestions, or your own experiences with DynDNS and Cloudflare, I would love to hear from you! Please don’t hesitate to drop a comment below. Happy homelabbing!

Leave a Reply

Your email address will not be published. Required fields are marked *