How to Setup Dynamic DNS

Published on: Fri, Sep 22, 2017 at 11:45 am EST

What is Dynamic DNS?

Let's take a common example of a DNS setup. Your domain is example.com, and you have "A" records for server1.example.com and server2.example.com. Server1 is hosted in Vultr, but server2 is hosted on a local network, like inside your business or as a development machine in your home. Typically, these types of local environments have public IP addresses are dynamically assigned from your Internet Service Provider (ISP), and they may change unexpectedly. This would result in server2 suddenly pointing to a wrong IP address which could severely affect service. This article will show you how to set up your own dynamic DNS service, which runs on a local machine and can update the record for server2 in Vultr DNS automatically after a public IP change.

While clients like ddclient and inadyn will work for some DNS providers, Vultr does not currently support these and instead uses its own custom API. This service will run locally on your machine and use GET and POST requests along with your access key.

Prerequisites

  • Access to your Vultr account to setup DNS and manage access keys.
  • Ability to connect to the local machine that has a dynamic IP address (or access to another machine on the same network).

Installation and Setup

Setting up your domain to use Vultr's DNS service

  • If you are not using Vultr's DNS service for your domain, follow this article to modify your nameservers for use with Vultr.
  • Once Vultr DNS has been setup, go to the DNS management page under "Servers -> DNS -> [your domain name]".
  • Here you will find all of the records set up for your domain. Write down which record(s) you would like to change, as you will need this later.
    • Only "A" records are supported by this utility. If you have multiple "A" records that you want updated to the same IP address, change them to "CNAME" records and point them to one "A" record. This will simplify things for you in the long run and is considered the proper way to configure DNS.

Installing the required tools

  • Connect to your local server. This can be directly or through SSH.
  • This tool requires Python 3. Most distributions of Linux include Python pre-installed. In the event that it is not installed in your environment, instructions are available on the official Python wiki here:
  • This process also requires Git, which you can install with the instructions below:

Downloading the Dynamic DNS client

Use Git to clone the repository containing the python DDNS client. You should do this in a safe directory for which you have full access. Your home folder should be sufficient. Run the following command:

git clone https://github.com/andyjsmith/Vultr-Dynamic-DNS.git vultrddns && cd vultrddns

Generating a personal access token

Generate a personal access token in Vultr. This can be done under "Account -> API -> Personal Access Token". On that same page, you will need to go under "Access Control" and click the "Allow All IPv4" button, since you will not know the IP of your local server every time, hence the point of dynamic DNS.

Configuring the tool for your server

  • Read through the config.json.example file for an example of what your configuration file should look like.
  • Open the config.json file using your favorite text editor and fill out the values based on your specific server setup.
  • The file should contain the following:

    {
        "api_key": "",
        "domain": "",
        "dynamic_records": [
        ""
        ]
    }
    
  • Enter your access token into the field api_key.

  • Enter your base domain into the domain field. For example, example.com.
  • Now input the "A" records you want to change under the dynamic_records field. Using the previous example, you would enter server2 to automatically change server2's IP address. You may enter multiple subdomains. If you want to change the base "A" record for your domain (commonly referenced as an @, the record that responds when you navigate straight to example.com), simply leave empty quotes.
  • A proper config file may look like this, where you want to dynamically change the IP for example.com and server2.example.com:

    {
        "api_key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "domain": "example.com",
        "dynamic_records": [
        "",
        "server2"
        ]
    }
    
  • Now save these changes and your file should be good to go!

Test your configuration

  • Test the script and configuration by running python3 ddns.py. If this does not return any errors, your configuration is nominal and you can start the automation process.

Automation

After completing the basic setup, it is important to set up a recurring task as the script does not do this by default. Connect to your local machine and follow the steps below based on your operating system.

Linux and Mac OSX

Linux and Mac have the cron utility already installed, which lets you specify scripts to run on set schedules.

  • Find the full path of the ddns.py file using realpath ddns.py while still in the vultrddns directory.
  • Run crontab -e to edit your crontab.
  • Add the following line to the end of the file, adding in the real path to the ddns.py file:

    */30 * * * * python3 [full path to ddns.py] > /dev/null 2>&1
    
  • This will run the script every 30 minutes and redirect all of its output to /dev/null.

  • Save and quit out of the text editor. The crontab file will automatically be installed and your IP will now automatically be updated.

Windows

Create a task in Task Scheduler to run every 30 minutes. Follow the Microsoft guide for basic task creation.

  • Open Task Scheduler and click "Create Task...".
  • Give it a name and create a new trigger.
  • Click "Daily". Under "Advanced Settings" click to repeat the task every 30 minutes and change "for a duration of" to "Indefinitely".
  • Add a new action to start a program and browse to your Python executable. Add the ddns.py script as an argument.