A simple script that works as a self-hosted Dynamic DNS (DDNS) by checking the checking the network's current public IP address, comparing it with the content of a DNS record on Cloudflare, then then updating the DNS record if necessary.
Important
As it is now, this script only works with Cloudflare as a DNS provider/registrar.
- Python 3.9+
- cloudflare (cloudflare-python)
- requests
- python-dotenv
-
Clone this repository and navigate into it
git clone https://github.com/mzalal/my_ddns cd my_ddns -
Create a virtual environment and activate it
python -m venv .venv source .venv/bin/activate
Tip
Depending on your system, you may need to use python3 instead of python.
-
Install dependencies
pip install -r requirements.txt
-
Create a
.envfile with the following environment variables# The API token should have DNS edit permissions CLOUDFLARE_API_TOKEN=API_TOKEN_HERE # The DNS record name (domain/subdomain) DNS_RECORD_NAME=example.com # The ID of the DNS record CLOUDFLARE_DNS_RECORD_ID=DNS_RECORD_ID_HERE # The ID of the zone the DNS record belongs to CLOUDFLARE_ZONE_ID=ZONE_ID_HERE
-
Run the script
python my_ddns.py
That should be it. But if any error occurs, you should check the log file (
my_ddns.log) that is generated in the same directory as the script itself.
To get the most of myDDNS, you can run it as a system service through systemd or as a cron job. And to automatically run myDDNS on 1 minute time intervals using systemd, create the following two files in /etc/systemd/system:
-
my-ddns.service:[Unit] Description=myDDNS After=network-online.target Wants=network-online.target [Service] Type=oneshot # Change depending on where you placed .venv and my_ddns.py ExecStart=/path/to/python/executable /path/to/my_ddns.py [Install] WantedBy=multi-user.target
-
my-ddns.timer:[Unit] Description=Run my-ddns.service 10s after boot and every 1m after that [Timer] # Run my-ddns.service 10 seconds after the system boots OnBootSec=10s # Then run it every minute after that OnUnitActiveSec=1m Unit=my-ddns.service [Install] WantedBy=timers.target
Then start and enable my-ddns.timer:
sudo systemctl start my-ddns.timer
sudo systemctl enable my-ddns.timermyDDNS is licensed under the MIT license.