Joeri Verdeyen bio photo

Joeri Verdeyen

Web-engineer, cyclist, Nespresso lover, Strava pusher.

Twitter LinkedIn Instagram Github Stackoverflow Last.fm Strava

Dynamic DNS with the Digital Ocean API

Digital Ocean

DigitalOcean is an American virtual private server provider based in New York City. The company leases capacity from existing datacenters, including sites in New York, Amsterdam, San Francisco, London and Singapore.

Dynamic DNS script

I’ve created a bash script to update an already created A record with the IP of a (home) server. You can e.g. add server.yourdomain.be as an A record.

 1 #!/bin/bash
 2 
 3 # Your domain name
 4 domain="yourdomain.be"
 5 
 6 # record id to update
 7 record=""
 8 
 9 # api key
10 api_key=""
11 
12 # lookup ip
13 ip="$(curl http://ipecho.net/plain)"
14 
15 echo content="$(curl \
16         -k \
17         -H "Authorization: Bearer $api_key" \
18         -H "Content-Type: application/json" \
19         -d '{"data": "'"$ip"'"}' \
20         -X PUT "https://api.digitalocean.com/v2/domains/$domain/records/$record")"

Create a file /usr/bin/dyndns.sh and add the script with your settings. To lake sure you are able to execute the script, add execute permissions.

chmod +x /usr/bin/dyndns.sh

Cron

Add an entry in /etc/crontab or in the respective cron file.

0 4 * * * /usr/bin/dyndns.sh

Every night at 4am the script will update the A record with your ip address.

Edit: Where can I find the record id?

Login to your DigitalOcean control panel. Navigate to the given dns domain record. Find the A record you want to be able to change dynamic. Right click on it and choose inspect element. The id is located in the following input field:

<input id="domain_record_id" name="domain_record[id]" type="hidden" value="THISISYOURID">

Enjoy your free Dynamic DNS setup!

Thanks for reading

Feel free to leave a comment if you have remarks or like this post