Auto updates dynamic IP via Cloudflare API (DDNS)

Dynamic IP vs Fixed IP

Dynamic IP

A dynamic IP address is a temporary address for devices connected to a network that continually changes over time. An Internet Protocol (IP) address is a number used by computers to identify host and network interfaces, as well as different locations on a network.

Fixed IP

A static IP address is also known as a fixed IP address or dedicated IP address, and is the opposite of a dynamic IP address. A computer with an assigned static IP address uses the same IP address when connecting to the Internet

What is DDNS (Dynamic DNS)

Each device that connects to the internet has an Internet Protocol (IP) address, and websites on web servers are no different. However, when you navigate to a website, you do not type in a series of numbers, which would be its IP address. Instead, you type in the name of the site, such as “google.com” The mechanism that associates webpages with their IP addresses is called the Domain Name System or DNS.

However, as more and more devices connect to the internet, a system is needed to automatically assign IP addresses to domain names. In this way, IP addresses that were no longer in use could be recycled and given to new devices. A dynamic DNS (DDNS) service does this for you, mapping the name of your website to your IP address.

So in this tutorial, we will use Cloudflare as our DDNS provider. We will write a script which auto check machine external IP every 5 minutes and update it through Cloudflare CLI.

How can we keep IP up to date

Download script below and assign executable permission

current directory: /root
View raw code at: https://gist.githubusercontent.com/ookangzheng/321a02aba98c5863b3f95ae3beeb0c45/raw/update-cloudflare.sh

wget https://gist.githubusercontent.com/ookangzheng/321a02aba98c5863b3f95ae3beeb0c45/raw/update-cloudflare.sh && chmod +x update-cloudflare.sh

Configure domain properties

config name: /root/update-cloudflare-dns.conf

## Filename: update-cloudflare-dns.conf

## Which IP should be used for the record: internal/external
## Internal interface will be chosen automaticly as a primary default interface
what_ip="external"

## DNS A record to be updated
dns_record="hello.world.mom"

## Cloudflare's Zone ID
zoneid="asdfasdfasdf"

## Api token can be generated from here
## https://dash.cloudflare.com/profile/api-tokens
## Cloudflare Zone API Token
cloudflare_zone_api_token="asdf1234567890"

## Use Cloudflare proxy on dns record true/false
proxied="false"

## 60-300 in seconds or 1 for Aut
ttl=60

## Telegram Notifications yes/no (only sent if DNS is updated)
notify_me_telegram="no"

## Telegram Chat ID
telegram_chat_id="ChangeMe"

## Telegram Bot API Key
telegram_bot_API_Token="ChangeMe"

Setup cron job auto check and update every 5 minutes

nano /etc/crontab

*/5 * * * *  root /root/update-cloudflare.sh

Save it and restart crontab service

service cron restart

Final Result

./update-cloudflare.sh
==> 2022-06-14 13:51:52
==> External IP is: 100.100.100.100
==> DNS record of hello.mom is: . Trying to update...
==> Success!
==> DNS Record Updated To: 100.100.100.100, ttl: 60, proxied: false

Source code credit

Credit from: https://3os.org/automation/ddns-cloudflare-bash
License: MIT Copyright@ 3os.org