DEV Community

Perttu Ehn
Perttu Ehn

Posted on

DynDNS with OVH hosting

I use my own domain to access some specific services when I am not home or just feel lazy because I can do things over my mobile like this. Typing and remembering IP addresses isn't too much fun so I use my own domain to simplify my life.

I have opened some services to be accessed only from my home wifi network. Some services I have opened also are accessible from outside my home.

I did not want to pay for a fixed IP address and hence the internet service provider (ISP) may change my IP whenever they like. This would usually make reaching those services unreliable.

Since I have a few other things hosted at OVH Hosting and they offer DynDNS as well I went and set up a couple of subdomains with it. DynDNS is a dynamic domain name system service which allows some specified domain to be updated automatically. The update is initiated by some device behind the changing IP address (ie. your home).

Earlier I have had NASs' (tiny linux servers) which I used to update my home domains. All of my NASes have been turned off for a while now. Also I prefer handling the incoming traffic based on domain name rather than port so I can use nginx in my word facing Raspberry PI's to proxy the traffic where needed.

Proxying means I forward my incoming traffic coming to my hypothetical fridge domain with nginx to the fridge but the access to the fridge would happen via address and the outer most Raspberry PI would pass the request coming with that domain to the fridge. This way it is easy to handle incoming requests with one nginx in cheap Raspberry PI.

Updating DynDNS IP address

I wrote this small script I run for each of my home domains in the Raspberry PI. What the script does is it checks the currently configured IP address for the given domain, compares it with the public IP address it has and updates the OVH Hosting DNS with the new IP address if necessary.

Because the IP address comparison script can be run every few minutes with no worries querying the OVH side too often. Basically it sends update requests to OVH only when the IP address has been changed.


Let's put the script under path /path/to/ and the domain we configure is Let's assume also that our username is janedoe and the matching password is mysecret

The credentials file format is very strict since we pass it forward with the request to update domain - exactly one line and username and password separated with a single colon.

  1. Save the files

    $ cd /path/to
  2. You need to have credentials for your request (so only you can update your domains):

    echo "" >
  3. Make the script executable and owned by root and credentials files readable only by root:

    $ sudo chmod 700 # the main script
    $ sudo chown root:root
    $ sudo chmod 400
    $ sudo chown root:root
  4. Execute the script manually to see it works:

    $ sudo sh

    If the execution fails uncomment the DEBUG=1 line to get more output.

  5. When the script works, set up cronjob to run the script every few minutes.

    The longer time between the executions you have the longer time it takes for the domain IP address to get corrected.

    Edit your cronjob table with sudo crontab -e (or however the similar scheduled jobs configuration is done in your system).

    */10 * * * * sh /path/to/


This script works with OVH Hosting. You may need to adjust the curl command if your DynDNS host has a different API to update the domain IP addresses. Check with your hosting provider for the details.

#!/usr/bin/env bash
# /path/to/

# 1. Create a credentials file in the same folder with this script named 
# dyndns-update.sh_EXAMPLE.COM.creds.txt 
# with user credentials, such as 
# 2. Try this at least once. The output of curl commands tells if update is successful, not necessary or if something was wrong.
# sudo sh /path/to/
# 3. Run this as a cronjob
# $ sudo crontab -e
# and add something like this to your crontab (this checks IP every 10 mins):
# */10 * * * * sh /path/to/ EXAMPLE.COM

# Disable to get less output:

USER_ID=$(id -u)
if [ "$USER_ID" -ne "0" ]; then
  echo This script must be run as a root.
  exit 0

if [ -z "$DOMAIN" ]; then
  echo "Usage: "
  echo "${0}"
  exit 1

SCRIPT=`realpath $0`

DOMAIN_IP=$(host $DOMAIN |awk '{print $4}' | xargs)
PUBLIC_IP=$(curl --silent | xargs)

if [ "$DOMAIN_IP" != "$PUBLIC_IP" ]; then
  echo -n "Domain ${DOMAIN} public IP is ${DOMAIN_IP}. "
  echo "Your local IP is ${PUBLIC_IP}, so DynHost needs updating. "
  # Reads the 1st line of a file $FILE  into a PASS varible.
  read -r PASS < $FILE
  if [ -z "$PASS" ]; then
    echo "No password (tried $FILE), exiting!"
    exit 1;
  [ ! -z "$DEBUG" ] && [ "$DEBUG" -ne "0" ] && echo "Next (with creds):" "${DOMAIN}&myip=${PUBLIC_IP}"
  curl --silent --user "${PASS}"                       "${DOMAIN}&myip=${PUBLIC_IP}"
elif [ ! -z "$DEBUG" ] && [ "$DEBUG" -ne "0" ]; then
  echo  "Domain ${DOMAIN} public IP is ${DOMAIN_IP}."
  echo "Your local IP is ${PUBLIC_IP}."
  echo "Domain ${DOMAIN} IP still ok."
Enter fullscreen mode Exit fullscreen mode


Top comments (0)