This post assumes you are using a current version of Rasbian or other Linux distribution that includes both a cron manager (with crontab) and curl.
I will also be using “nslookup” this is not installed by default. To install nslookup (and other DNS tools) run the following command:
sudo apt-get install dnsutils
Dynamic DNS allows you to convert your dynamic internet address (givin to you by your internet provider) into something that seems static. It works by doing the following:
- Allowing you to register a DNS record (a name for your address). In our case we will use Duck DNS so the record will look like YourName.DuckDNS.org
- Running a script, that you will install on your server, that will tell your Dynamic DNS (DDNS) Provider (DuckDNS.org) what your current external internet address is. This script will update your dDNS provider at regular intervals.
- Schedule the script to run at 5 minute intervals.
- From outside of your home, use the the DNS record created in step one to be forwarded to your home address (external DNS).
Configuring DDNS is the first step to publishing services (like Home Assistant) to the internet so you can control your home devices while you are away.
Full installation instructions for many operating systems and routers can be found on the Duck DNS installation page. I am not endorsing one service over another but Duck DNS is free, reliable, and has a lot of community support.
Create A Dynamic DNS Account (Duck DNS for this article)
- Browse to http://www.duckdns.org
- Sign in using one of their connected services (Twitter, Google, Facebook, reddit, or Persona)
- Enter a name you would like to use for your domain (.duckdns.org will be appended to the name), and press add domain.
- Record your token (you will need this in later steps)
Congratulations you now have a domain name of your very own!
Create an Update Script
This can be done on any server on your internal network. You only have one external IP and that is what we will be updating.
Log on to your Raspbian server and ensure you are in your home directory (you will be by default, if you are not sure type
Make a directory to install Duck DNS into and change into that directory:
Create a new script file to store the Duck DNS commands into (this will be run to update Duck DNS with your current IP address):
copy the following line into duck.sh (change the bold items to your specific domain and token):
echo url="https://www.duckdns.org/update?domains=exampledomain&token=a7c4d0ad-114e-40ef-ba1d-d217904a50f2&ip=" | curl -k -o ~/duckdns/duck.log -K -
Save the file (ctrl+X, Y, Enter)
change the permissions on the file you just made so that it is executeable:
chmod 700 duck.sh
Create A Cron Job Using crontab
Create a cron Job (scheduled task) using crontab, that will run this script every 5 minutes
Open your crontab file (you may have to choose an editor your first time opening cron tab. I recommend nano):
append the following line to the end of the file:
*/5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1
Save the file (ctrl+X, Y, Enter)
You are done! The cron job you just created will update Duck DNS every 5 minutes. You can manually run the script by entering the command
if all goes well a log file called duck.log will be created. View it by typing
cat duck.log. If you get an OK response everything is good. If you get KO as a response, something is wrong.
If you want to be sure your cron job is running wait 5 minutes and check the date of the log file. It will update every time the file is updated
You should also now be able to do an NSLOOKUP command and get a response with your internet facing IP nslookup domainname.duckdns.org.