Recently I’ve published an article about “DynDNS and ddclient: access your Linux from anywhere“, and some people commented that the services on DynDNS are not free anymore, so this is an alternative by Adam Buchanan first published on his interesting blog
This has nothing to do with search engine marketing, but everything to do with automation. If you’ve ever wanted to host a server at a location that doesn’t have a static IP address then you know just how much of a pain it can be.
Hopefully by the time you read this you’re familiar with what DNS is and what dynamic DNS is. If not, those links should catch you up to speed quick enough. I’ve tried a few different dynamic DNS providers but for the past few years I’ve been using freedns.afraid.org. I don’t host this site (of course), but I do host FTP and a few other services that allow me to get to my machines at home where ever I am.
I’m writing this post today because I just happened to have to update my scripts that keep my DNS up to date when my IP changes today.
The Dynamic DNS Bash Script
This is the basic script I use to keep DNS updated for my domain.
#!/bin/bash # paste in the info url from your account get_info_url='http://freedns.afraid.org/api/?action=getdyndns&sha=<paste_your_password_hash_here>' # get the current ip... # method 1 - they aren't providing it for scripts anymore... #ip=`curl -s http://whatismyip.org` # method 2 - google provides this as an error message for bots...I'll take it. ip=$(curl -s "https://www.google.com/search?q=what+is+my+ip+address" | grep "Client IP address:" | sed 's/.*Client IP address: //g;s/).*//g') # get the current dns settings... for each in `curl -s "$get_info_url"` do domain=`echo "$each" | cut -d"|" -f1` dns_ip=`echo "$each" | cut -d"|" -f2` update_url=`echo "$each" | cut -d"|" -f3` if [ "$ip" != "$dns_ip" ] then echo -e "$domain - "\\c >> log curl "$update_url" >> log fi done
Really the script is simple but the first thing you have to do is sign up for an account at freedns.afraid.org and choose your domain name. Once you’ve done that, you’re ready to get yourself setup with this script.
- Save the script above as
- Make sure that the script is set as executable. You can run
sudo chmod +x dns.shto make sure it is.
- You now need to get your API key. To do that, go to http://freedns.afraid.org/api/ and it will explain what you need to do. Basically, you need to hash your username and password, but you can find it on that page if you don’t want to hash it yourself.
- In the script, replace the string
<paste_your_password_hash_here>with the actual hash (don’t forget to save…).
- Now you can run the script when ever your IP address updates. Alternatively (my way), you can setup the script as a cron job and have the script check at regular intervals to make sure everything is up to date.
Setting up the Script as a Cron Job
Open a terminal and run
crontab -e. If you’ve never done this before, it will probably prompt you to choose an editor. If you don’t know how to use any of the command line editors then you’ll need to do some additional Googleing, I’m not going to explain it. Although, I prefer
nano because I don’t understand
Once you have your crontab open, paste the following line somewhere to run the script every 5 minutes.
*/5 * * * * /path/to/script/dns.sh
From now on your DNS information should be up to date. You may have noticed that I’m scraping Google to get your actual current IP address. That was the change I made to the script today. I used to use another site (commented out) but apparently the focus of the site has changed and they don’t really like scripts using it to get the current IP address. What a shame.
Anyway, I hope people find this handy.
- Reasons why 2015 will be year for Linux on the desktop
- Manage and read your ebooks on Linux with Calibre
- The Best Linux-Based Home Automation Systems for Under $300
Find me on Google+