~/nodetwelve.com$
34 notes · 6 topics
updated 2026-11-27

Updating DigitalOcean DNS records with a dynamic IP

· 3 min read

If you host services on a home server with a dynamic IP, you need a way to point your domain to your new public IP when it changes. Here is how I use Bash and Crontab to automatically update my DigitalOcean DNS records.

Requirements

Collect record IDs for each domain

DigitalOcean uses specific IDs for every record. You need to grab the record ID for every domain and subdomain you want to update (e.g., example.com, www.example.com).

Create a bash file to fetch record IDs for the domains

nano get_record_ids.sh
#!/bin/bash

[[ -z "$1" ]] && { echo "Usage: sudo ./get_record_ids example.com"; exit 1; }

curl GET -H "Content-Type: application/json" \
    -H "Authorization: Bearer INSERT_YOUR_TOKEN_HERE" \
    https://api.digitalocean.com/v2/domains/$1/records | json_pp | grep -E "id|name|type"

Make it executable and run it

chmod u+x get_record_ids.sh
./get_record_ids.sh example1.com
./get_record_ids.sh example2.com
./get_record_ids.sh example3.com
Caution

Do not update NS (nameserver) or SOA (Start Of Authority) records.

Here is a sample of what the output looks like:

# ./get_record_ids.sh example1.com

        "id" : 117631553,
        "name" : "@",
        "type" : "SOA",

        "id" : 117499523,
        "name" : "@",
        "type" : "NS",

        "id" : 117636234,
        "name" : "@",
        "type" : "NS",

        "id" : 117634432,
        "name" : "@",
        "type" : "NS",

        "id" : 117634123,
        "name" : "@",
        "type" : "A",

        "id" : 117634634,
        "name" : "www",
        "type" : "A",

        "id" : 117634433,
        "name" : "blog",
        "type" : "A",

        "id" : 117612111,
        "name" : "store",
        "type" : "A",

        "id" : 117634674,
        "name" : "www.blog",
        "type" : "A",

Create the update_digital_ocean.sh script

Now we have the DigitalOcean API token, the domain names, and all the "A" record IDs. It is time to create the bash script that actually updates them.

Create a bash file to update the IPs

nano update_digital_ocean.sh
#!/bin/bash

TOKEN="insert_your_TOKEN_here"
LOG_FILE="/home/YOUR_USERNAME/digital_ocean_update_logs.txt"

DOMAIN1="example1.com"
RECORD_A1="112176990"
RECORD_A2="112177021"
RECORD_A3="113300718"
RECORD_A4="127939656"
RECORD_A5="117087411"

DETECTED="$(dig +short myip.opendns.com @resolver1.opendns.com)"
PREVIOUS="$(tail -1 $LOG_FILE | awk -F, '{print $2}')"

if [ "$DETECTED" = "$PREVIOUS" ]
then
    echo "Previous IP -> ($PREVIOUS)."
    echo "Detected IP -> ($DETECTED)."
    echo "No change detected, exiting."
else
    echo "Previous IP -> ($PREVIOUS)."
    echo "Detected IP -> ($DETECTED)."
    echo "New public IP detected, updating DigitalOcean records…" && echo
    echo "$(date),$DETECTED" >> "$LOG_FILE"

    curl -X PUT -H "Content-Type: application/json" -H \
            "Authorization: Bearer $TOKEN" -d '{"data":"'"$DETECTED"'"}' \
            "https://api.digitalocean.com/v2/domains/$DOMAIN1/records/$RECORD_A1"
fi
Info

Script above only updates $RECORD_A1. It needs a copy and paste for the rest of the required records.

Make it executable and run it

chmod u+x update_digital_ocean.sh
./update_digital_ocean.sh
./update_digital_ocean.sh

This time it will exit normally and complete the task.

Automate the process with crontab

To fully automate this, set up a job in crontab. This runs the script in the background on a schedule.

crontab -e
*/15 * * * * /path/to/script/update_digital_ocean.sh
Info

You can change the timer to whatever you want, but I wouldn't recommend going lower than 5 minutes to avoid spamming the DigitalOcean backend.