Dynamic DNS Using Gandi

Published Tuesday, Nov 12, 2019 by Tony Davis

Dynamic DNS

Dynamic DNS is a way to associate a changing Dynamic IP address (usually residential xDSL connections) to a static domain name (DNS record)

This allows you to connect to home.example.com -> DNS Lookup & Resolution -> 203.0.113.78. The DNS entry for home.example.com is updated automatically at set intervals or when an IP address change is detected.

Dynamic DNS Providers

There are a number of Dynamic DNS providers that can be used, a well known provider is https://dyn.com/, but unfortunately some of these services come with a cost.

Gandi Live DNS

https://gandi.net provides a https://doc.livedns.gandi.net/

LiveDNS is Gandi’s upcoming DNS platform, a completely new service that offers its own API and its own nameservers.

The new platform offers powerful features to manage DNS Zone templates that you can integrate into your own workflow. Features include bulk record management, association with multiple domains, versioning and rollback.

Implementation

The below instructions will show you how to create a Dynamic DNS system using a single script and Gandi’s LiveDNS.

Prerequisites

Make sure you have the following applications installed:

Bash Script

Create a bash script and put it under “/usr/local/bin/dyndns_update.sh”. (This can of course be kept wherever you want)

Add the API key you got from the Gandi Account Panel.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# This script gets the external IP of your systems then connects to the Gandi
# LiveDNS API and updates your dns record with the IP.

# Gandi LiveDNS API KEY
API_KEY="............"

# Domain hosted with Gandi
DOMAIN="example.com"

# Subdomain to update DNS
SUBDOMAIN="dynamic"

# Get external IP address
EXT_IP=$(curl -s ifconfig.me)  


#Get the current Zone for the provided domain
CURRENT_ZONE_HREF=$(curl -s -H "X-Api-Key: $APIKEY" https://dns.api.gandi.net/api/v5/domains/$DOMAIN | jq -r '.zone_records_href')

# Update the A Record of the subdomain using PUT
curl -D- -X PUT -H "Content-Type: application/json"
        -H "X-Api-Key: $APIKEY"
        -d "{"rrset_name": "$SUBDOMAIN",
             "rrset_type": "A",
             "rrset_ttl": 1800,
             "rrset_values": ["$EXT_IP"]}"
        $CURRENT_ZONE_HREF/$SUBDOMAIN/A

Run The Script

I would set this script to run via crontab every 30 minutes. This ensures with an IP change the Dynamic DNS would only be out of date for a maximum of 30 minutes.

Edit crontab with the following command

[root@server ~]# crontab -e

Add the following lines to run the script every 30 minutes.

*/30 * * * * /bin/bash /usr/local/bin/dyndns_update.sh

Once the script runs it should update the dynamic.example.com dns entry with the external IP that was found by the script.

comments powered by Disqus