DigitalOcean dynamic DNS update script in bash (noip/dyndns alternative)

Visit site

A bash script that retrieves your public IP address and updates a domain record (like a subdomain) using DigitalOcean API v2.

This script is an alternative for dyndns and noip tools, with no dependencies, just a bash script that is less than 20 lines.

How it works:

  • The script retrieves your public IP address
  • checks if it has changed (using a log file)
  • updates a record on DigitalOcean using API v2
  • and writes the date and that new IP in the log file

Use it with a cron job for example and you are done.

I have written a detailed step by step tutorial on how to configure and use the script, with a section on how to open ports on your router too, but if you know what you are doing here is the script:

#!/bin/bash

#################### CHANGE THE FOLLOWING VARIABLES ####################
TOKEN="abcdef1234567890"
DOMAIN="yourdomain.info"
RECORD_ID="111222333"
LOG_FILE="/home/youruser/ips.txt"
########################################################################

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

if [ "$CURRENT_IPV4" = "$LAST_IPV4" ]; then
    echo "IP has not changed ($CURRENT_IPV4)"
else
    echo "IP has changed: $CURRENT_IPV4"
    echo "$(date),$CURRENT_IPV4" >> "$LOG_FILE"
    curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"data":"'"$CURRENT_IPV4"'"}' "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$RECORD_ID"
fi
Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This would be perfect for servers if it took the variables as inputs and had some error handling…

Sweet! You just saved me some time! Thanks!

Publish your Tool on Community

Have you created an Integration, API Wrapper, Service, or other Tool that helps developers build on DigitalOcean? Help users find it by listing it in Community Tools.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.