Dynamic DNS

To be able to access your home network devices remotely, you need to know the public IP of your network. This is given by your ISP and changes dynamically.

In this section, we will create a DNS alias that will map a given hostname to your up-to-date public IP. To this end, we will use NOW-DNS.

Create DNS for your public IP

  • Go to NOW-DNS and register.

  • Go to Manage Hostnames in the left navigation bar.

  • Create your hostname. First, you need to click on Check Availability, next it will ask you for your public IP. Note that NOW-DNS will auto-fill it, however, you easily check your IP here.

  • Once created, your hostname will appear on the DNS table.

Dynamically update the hostname

As aforementioned, public IPs are dynamic, which means that they change from time to time (e.g. router reboot). Getting track of these changes is unfeasible, even more when you are abroad (as you would need to access your home network which in turn requires you to previously know the current public IP).

To solve this, we simply create a script that updates the NOW-DNS hostname (using NOW-DNS API) and place it as a crone task on the raspberry.

Create file with credentials

First, create a file called /home/pi/crons/now-dns/credentials with your NOW-DNS details using the following structure:

/home/pi/crons/now-dns/credentials
machine now-dns.com login [YOUR_MAIL] password [YOUR_PASSWORD]

After creating, modify the permissions so only you have access

chmod 600 /home/pi/crons/now-dns/credentials

Automate hostname updates

To do this, we will create a local script, which runs periodically (using crontab). First, we create a script file.

/home/pi/crons/now-dns/script.sh
# Get current public IP
PUBLIC_IP=$(curl icanhazip.com)
NOW_DNS="lucas-example.2mydns.net"

# Update ip
curl --netrc-file /home/pi/crons/now-dns/credentials \
    https://now-dns.com/update?hostname=$NOW_DNS&myip=$PUBLIC_IP

Next, we add script execution to crontab. In our configuration, we set an every-5-minute execution.

$ crontab -e
*/5 * * * * bash /home/pi/crons/now-dns/script.sh

You can check the logs of crontab by running

tail -n 10 -f /var/log/syslog | grep CRON

Last updated