DDNS With FreeDNS Part 2

Published on February 10, 2022 at 1:11 pm by LEW

Introduction

In the last post, we setup a subdomain.domain address and manually tied it to our public IP address using freedns.afraid.org. In this post we will automate the process. The reason we are doing this is so we can access a local server from outside of our local network.

What was done in the last post has a specific issue, where if our service goes down or disconnect for any reason, there is a chance we could get a different IP address (assuming we did not setup a static IP address with our ISP).

If you have access to your local network when this happens, you can easily fix the issue. However if you do not have access, it is likely to be a problem until you get access. So we are going to look at automating the update process when your IP address changes.

Using curl to Update

In the last post, our process was to find our local IP address, then log into freedns.afraid.org and update it there. This time around we will do it from the command line on our server. To do this you will need to install a program called curl.

The curl program is available for all major operating systems, the only difference being how it gets installed. For Unix/Linux check your package manager. Otherwise, for all other cases, download from the website.

Finding the Identifier

If you select the home option from the left side lower menu of the freedns.afarid.org web page, you will see the curl command used in Dynamic update demonstration example (v2 interface). This is the command we will use to update our IP address. Lets dissect the example command. For reference it is given below.

curl https://sync.afraid.org/u/CyTXMbtq5cPnLjEg5vKHTPDE/

A note of interest, the example command is given as https:, but when I click on the v2 interface, it shows up as http:. My testing shows it works with either one, and I always recommend using https: wherever possible. Also if you generate the cron script, the -s option is added. This means silence and stops any terminal output.

The curl program is used to transfer data to and from a server. In this case the address is the site sync.afraid.org. Just like freedns is a subdomain of afraid.org, so to is sync. The u directory is a folder off of the root of the subdomain. That big long 24 character string that comes next is your subdomain domain identifier. Each combination of subdomains and domains will have a different unique identified.

Note you can find all your subdomain domain pair unique identifiers by going to the dynamic DNS page version 2 at freedns.afariad.org.

What Happens

I am not going to hazard a guess as to what the behind the scenes process is, I can think of about a half dozen ways to do it. The results are the only critical part.

The sync.afraid.org server obtains your public IP address from the current transaction, and compares it to its record. If they do not match, then the record is changed. If they do match then nothing happen. Then feedback is returned, letting you know what happened, even if it is nothing.

Get on a terminal on your server and type the command, remembering to substitute your unique identifier, and see what happens.

Automating the Process

If the above command can be made to run periodically or with specific triggers on your server, then when your IP address changes, it will be automatically updated. There are a variety of ways to accomplish this, but the simplest is to setup a cron job.

There is an option on the version 2 Dynamic DNS page to generate a cron script. And while this does work, the instructions can be a little hard to follow, plus you might want to make a few changes. To setup a cron job, type in the following.

crontab -e

If you have never established a cron job before, you will have to select a text editor. Chose your favorite.

The cron script has a “PATH=” line. Check your existing cron file, it might already have one. If so, then you may need to modify it to match. Otherwise paste it into your editor. The “PATH” environmental variable lists all the directories, separated by colons, that will be searched for commands. Here is the example.

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

The next command is one really long line that actually does the automation work for us. Below is an example, which we will work through one part at a time.

1,6,11,16,21,26,31,36,41,46,51,56 * * * * sleep 19 ; curl -s http://sync.afraid.org/u/CyTXMbtq5cPnLjEg5vKHTPDE/ >> /tmp/freedns_bovine_mooo_com.log.log 2>/dev/null

The first section sets up when and how often this will be run. In this case the command runs every five minutes starting at one minute after the hour. You should review the cron manual page to learn its ins and outs.

My server is not what I would call job critical, so I think once an hour is fine for my application. How often you run the command will depend on how critical your server is. Below is before and after line section. I run the job at ten minutes after the hour every hour.

1,6,11,16,21,26,31,36,41,46,51,56 * * * *

vs

10 * * * *

The next section is the sleep command. It is set for 19 seconds in the example. This is basically a time delay. I’m not clear on the necessity of having it here, but it does not do any harm, so I leave it. The semicolon at the end separates it from the next command to be executed.

sleep 19 ;

After this is our old friend from above. In the downloaded script it is a http: command, however I have found that https: also works.

curl -s http://sync.afraid.org/u/CyTXMbtq5cPnLjEg5vKHTPDE/

What follows is a continuation of the curl command. These next entries redirect the output. Lets take them one at a time. The first one using “>>” appends the command output to a file. I prefer logs to go to /var/log, so I rewrite it a little. You will want to check this log file every now and then.

>> /tmp/freedns_bovine_mooo_com.log

vs

>> /var/log/freedns_bovine_mooo_com.log

The last section uses “2>” to send the error output to null, making it disappear. I actually prefer to send error output to the log file, just in case, so I make a little change here also.

2>/dev/null

vs

2>/var/log/freedns_bovine_mooo_com.log

Once you are done, save your file and restart your server. Wait a day or so, then review the log file.

Conclusion

At this point your server should be setup to check its public IP address and update it if needed. If your IP changes, you will only need to wait a short amount of time for it to be corrected.

DDNS is great for setting up your own local server, and allowing you to connect to it form almost anywhere.

Add New Comment

Your email address will not be published. Required fields are marked *