# 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.&#x20;

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](https://now-dns.com/).

## **Create DNS for your public IP**

* Go to [NOW-DNS](https://now-dns.com/) 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](https://www.whatismyip.com/).

![NowDNS configurayion panel](https://cdn-images-1.medium.com/max/1600/1*jEfSCgHj66C2a95MsRNPKA.png)

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

![Registered DNS](https://cdn-images-1.medium.com/max/1600/1*baDiiGcxcIYmISz8F4DwoA.png)

## **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).&#x20;

To solve this, we simply **create a script that updates the NOW-DNS hostname** (using [NOW-DNS API](https://now-dns.com/?m=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:

{% code title="/home/pi/crons/now-dns/credentials" %}

```
machine now-dns.com login [YOUR_MAIL] password [YOUR_PASSWORD]
```

{% endcode %}

After creating, modify the permissions so only you have access

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

{% hint style="info" %}

* [Is "curl -u username:password http://example.com" secure?, StackExchange](https://superuser.com/questions/919859/is-curl-u-usernamepassword-http-example-com-secure)
* [How to pass credentials to curk?, StackOverflow](https://stackoverflow.com/questions/32802667/how-to-pass-credentials-to-curl)
  {% endhint %}

### Automate hostname updates

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

{% code title="/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
```

{% endcode %}

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

{% code title="$ crontab -e" %}

```
*/5 * * * * bash /home/pi/crons/now-dns/script.sh
```

{% endcode %}

You can check the logs of crontab by running

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