Linking a Domain to a Dynamic IP

Since your ISP assigns a dynamic IP address to your home connection, you cannot simply set a static DNS record. Instead, you must use Dynamic DNS (DDNS). This guide explains how to configure your Ubuntu server to automatically update Cloudflare whenever your IP address changes.

Prerequisites: You need a domain name, a free Cloudflare account, and access to your router's admin panel.

Step 1: Move DNS to Cloudflare

If you purchased your domain from a different registrar (e.g., Namecheap, GoDaddy), you must point it to Cloudflare.

  1. Log in to Cloudflare and click Add a Site.
  2. Select the Free Plan.
  3. Cloudflare will provide two nameservers (e.g., bob.ns.cloudflare.com).
  4. Go to your Domain Registrar's settings and replace their nameservers with the ones provided by Cloudflare.

Step 2: Create the Initial Record

Before automating the process, create a placeholder record in the Cloudflare Dashboard.

  1. Go to the DNS tab in Cloudflare.
  2. Click Add Record.
  3. Type: A
  4. Name: @ (or 'www')
  5. IPv4: 1.1.1.1 (This is a placeholder).
  6. Proxy Status: Proxied (Orange Cloud).
  7. Click Save.

Step 3: Generate an API Token

Your server needs permission to edit your DNS records.

  1. Go to My Profile > API Tokens.
  2. Click Create Token.
  3. Select the template: Edit zone DNS.
  4. Under Zone Resources, select Include > Specific zone > Your Domain.
  5. Create the token and copy it immediately.

Step 4: Configure DDClient on Ubuntu

We will use ddclient, a standard utility for updating Dynamic DNS.

1. Install the necessary packages

sudo apt update
sudo apt install ddclient libio-socket-ssl-perl

2. Edit the configuration file

Open the configuration file using nano:

sudo nano /etc/ddclient.conf

Delete the existing content and paste the following. Be sure to replace the placeholders with your actual data:

# Configuration for Cloudflare DDNS
ssl=yes
use=web, web=https://cloudflare.com/cdn-cgi/trace
protocol=cloudflare
zone=yourdomain.com
login=token
password=YOUR_API_TOKEN_HERE
yourdomain.com

3. Test the configuration

Run a debug test to ensure it can connect and update the IP:

sudo ddclient -daemon=0 -debug -verbose -noquiet

Look for a SUCCESS message in the output.

4. Restart the service

sudo systemctl restart ddclient
sudo systemctl enable ddclient

Step 5: Port Forwarding

Your domain now points to your home IP, but your router blocks incoming traffic by default. You must forward ports to your Ubuntu server.

  1. Log in to your router (usually 192.168.1.1).
  2. Find Port Forwarding or Virtual Ser...