Periodically Update DigitalOcean Dynamic DNS from a Remote Machine Using Cron

Guide by Kristof Van Miegem

About: updating the DigitalOcean DNS records at regular time intervals (every 5 minutes) to deal with a changing dynamic IP address of a remote machine.

Usage example: our private server ("the box") is located behind a non-business internet connection with a dynamic IP. Since this IP address changes periodically, it is required to keep the DNS-records up-to-date.

Step-by-step

Follow the following instructions to update the DNS records automatically every 5 minutes.

  1. Install Python 3.4.

    sudo apt-get install python3.4

  2. Download the DigitalOcean update DNS script (see Github), written in Python. Download the script into the /opt/digitaloceanupdatedns directory (use mkdir to create it).

    sudo wget https://raw.githubusercontent.com/bensquire/Digital-Ocean-Dynamic-DNS-Updater/master/updater.py

  3. Give the file execute permissions.

    chmod +x updater.py

  4. Create a shell script which will update the DNS records using the DigitalOcean update DNS script (jira.confluence.be, box.confluence.be)

    nano updatedns.sh

  5. Insert the following lines. The Personal Access Token (hash) can be found/created on DigitalOcean.

    python3.4 /opt/dynamicdns/updater.py d7beb6931d6b05e61d02e7a3676092199ec1154cbfb956a998bbb98148199ea4 codifly.be bitbucket python3.4 /opt/dynamicdns/updater.py d7beb6931d6b05e61d02e7a3676092199ec1154cbfb956a998bbb98148199ea4 codifly.be jira python3.4 /opt/dynamicdns/updater.py d7beb6931d6b05e61d02e7a3676092199ec1154cbfb956a998bbb98148199ea4 codifly.be confluence python3.4 /opt/dynamicdns/updater.py d7beb6931d6b05e61d02e7a3676092199ec1154cbfb956a998bbb98148199ea4 codifly.be box python3.4 /opt/dynamicdns/updater.py d7beb6931d6b05e61d02e7a3676092199ec1154cbfb956a998bbb98148199ea4 codifly.be gitlab

  6. Create a cronjob that will execute the above script updatedns.sh. Edit the cronjob file for the labs user. Run sudo crontab -u labs -e and add

    */5 * * * * /opt/dynamicdns/updatedns.sh