Connecting your Local Network to the Internet with DuckDNS

Kendrick
Bina Nusantara IT Division
4 min readDec 31, 2022
DuckDNS

As a software engineer, I often work on projects which require me to set up a development server mainly on the cloud. This is very convenient because it frees up the workload and space on my computer. What if we wanted to set up our own server and make it accessible for everyone on the internet?

This requires our IP Address to be static. Most of the time, Internet Service Providers rotate the IP Addresses between customers resulting in non-static/dynamic IP Addresses. Some ISP charges for a static IP address, and some do not provide it. It is not feasible to manually keep track of your IP Address by yourself.

After researching online for a solution, I found out about a service called Dynamic DNS (Domain Name Service). With Dynamic DNS, we can access our local network from anywhere simply by using a custom domain name.

NOTE: Unfortunately this solution does not work if your ISP assigned your router a private IP

Dynamic DNS works by storing our IP and then mapping it to a custom domain. Our local server will frequently update the IP to the dynamic DNS provider.

One of the most popular Dynamic DNS providers is DuckDNS. DuckDNS is completely free. You heard it right, 100% free service 😲.

DuckDNS Website

DuckDNS does not require registration. We can simply log in with OAuth. The supported platforms are Persona, Twitter, GitHub, and Google. Reddit is now unsupported.

After logging in, we can see our account and token. Make sure to keep the token private. Tokens are used to authorize ourselves when updating our local IP on the dynamic DNS.

Domains

Below the Account Information, We can view and create subdomains. DuckDNS allows up to 5 domains. You can create a new subdomain by inputting the name and clicking the “add domain” button. You cannot use a subdomain name which is already existing (created by ourselves and other users). I prefer to name the subdomains obscurely for privacy reasons.

Domain Created

After the domain is created, we can see that it is added to the domains list. We can manually update our IP by clicking the ”update ip” button. IPv6 is also supported. To delete a domain, we can click the “delete domain” button.

To make sure our local server is accessible, we need to assign our server an internal static IP and enable port forwarding on our router.

(NOTE: Not every router has the same configuration interface)

Open your router configuration, input the MAC Address for our server and assign it an IP.

Set up port-forwarding for the server. In this example, I wanted to port forward the SSH port of my raspberry pi.

After we configured internal static IP and port forwarding on our router. All we have left is to set up a script to update the IP on DuckDNS.

To update the IP on DuckDNS, we can use the following HTTPS request.

https://www.duckdns.org/update?domains={CUSTOM_DOMAIN}&token={ACCOUNT_TOKEN}&ip={IP}

CUSTOM_DOMAIN: The custom domain we created.
ACCOUNT_TOKEN: Unique token on the account information menu.
IP: IP Address. Leave it blank, DuckDNS can automatically detect it.

If success, the response is “OK”. Otherwise, the response is “KO”.

We can use curl (available on Windows & Linux) to send the request.

echo url="https://www.duckdns.org/update?domains={CUSTOMDOMAIN}&token={ACCOUNT_TOKEN}&ip=" | curl -k -o duck.log -K -

The above bash script will update the IP and output the status on file “duck.log”. You can save the file as “duck.sh” and store it in “duckdns” directory.

To automate the script, in Linux we can use crontab.

crontab -e

Add the following entry on the bottom of the crontab.

{CRON_EXPRESSION} {SCRIPT_PATH} >/dev/null 2>&1

For example, this will run the script which is in “~/duckdns/duck.sh” every 5 minutes.

*/5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1

Finally, we can connect to our local server anytime and anywhere with DuckDNS.

--

--