Setting up DDNS with bash script Linux

Dynamic DNS, Y dis?

Setting up Dynamic DNS from eg your home or small business office can come in very handy if your ISP changes your IP often. Some ISPs either don’t offer static IPs, or they simply charge too much for it. You may also run a game server and if you already own a domain, you can have people connecting to myexamplegame.gg instead of an IP.

So here’s a quick guide on how to set up a script that will allow you to update your DNS records automatically from a local server. I’ll be basing this on UnoEuro’s setup, but I assume most domain providers are similar.

Example #1 – Use eth0 IP

According to Simply.com – former UnoEuro’s documentation, they allow you to update DNS every 15 minutes. Remember to check this at the domain registrar you’re using, as you don’t want to spam their service and risk getting a timeout/ban.

#!/bin/bash 
#Return your current IP (Leave the space after 'inet ', to make sure you don't get IPV6 as well. 
ifconfig eth0 | awk '/inet / {gsub("addr:", "", $2); print $2}' curl -s -L "https://api.unoeuro.com/ddns.php?apikey=YOURAPI&domain=YOURDOMAIN.COM&hostname=dev&myip=$2"

apikey – Find this in your control panel. domain – is your domain. hostname – is your subdomain, eg (dev.yourdomain.com), like shown above.

Example #2 – Use public IP

#!/bin/bash

ip=$(curl -s ifconfig.co)
curl -s -L "https://api.simply.com/ddns.php?apikey=YOURAPI&domain=YOURDOMAIN.COM&hostname=dev&myip=$ip"

Example #3 – The Unoeuro way for public IP

#!/bin/bash

curl -s -L "https://api.simply.com/ddns.php?apikey=YOURAPI&domain=YOURDOMAIN.COM&hostname=dev"

The IP field is optional, at least at Simply, which means that by leaving out the IP field, it will default to the public IP of the machine making the request. (This might also be the case with other DNS suppliers).

When you’ve made your bash script with the code above, make sure you run chmod +x scriptname.sh

Also note down where you save the file, as you’ll need the full path with filename when you run the script.

Cronjob

The easiest way to run the script, is probably using a cronjob, since it’s probably not super important if the task is skipped once or twice. Even though I’m allowed to update DNS every 15 minutes, I’ve set mine to 20 minutes, just to have 5 extra minutes to avoid spamming.

  1. Type the following on your Linux machine.
    crontab -e
  2. Now add the following line at the bottom of the crontab window.
    */20 * * * * /customscripts/scriptname.sh
  3. Save the cronjob and you’re good to go.

If you want to update every hour, once a day or some other time, I recommend generating the string you need, using this website: https://crontab.guru/

Good luck!

One Comment
  1. Avatar

Add a Comment

Your email address will not be published.