Skip to content

Instantly share code, notes, and snippets.

@taichikuji
Last active July 25, 2024 08:36
Show Gist options
  • Save taichikuji/6f4183c0af1f4a29e345b60910666468 to your computer and use it in GitHub Desktop.
Save taichikuji/6f4183c0af1f4a29e345b60910666468 to your computer and use it in GitHub Desktop.
Get a DDNS up and running with DuckDNS - Step by Step

Get a DDNS up and running with DuckDNS - Step by Step

Understanding DDNS

In summary, DDNS stands for Dynamic DNS. DDNS updates a DNS name in real-time to point to a changing IP address. This is useful for devices without a static IP. For example, companies like Google use static IPs and IP ranges, which are more expensive than ephemeral IPs and IP ranges. DDNS provides a cost-effective alternative, linking a hostname to a dynamic IP address.

How Does DDNS Work?

To use DDNS, you need an account with a DDNS provider. While some services are paid, they are still cheaper than static public IPs from ISPs. A script or service on your device updates the DDNS server with your current IP at regular intervals to maintain the link between your hostname and IP address. Luckily for us, DuckDNS is free for everybody and relays on donations to keep their services running.

Please go ahead and donate to DuckDNS' patreon if you enjoy their services.

Setting Up DuckDNS

As mentioned, we'll use DuckDNS for this guide.

  1. Create an Account and Login: Visit DuckDNS and log in or create an account.

img

  1. Create a Domain: Once logged in, create a domain and link it to your current public IP.

img

Automating IP Updates with a Script

DuckDNS provides a simple script to keep your domain updated with your dynamic IP.

img

You can go ahead and follow their guide, or continue reading;

  1. Ensure crontab is Running:

    ps -ef | grep cr[o]n
  2. Check if curl is Installed:

    curl --version

    If not, install it on Ubuntu/Debian systems:

    sudo apt install curl -y
  3. Create a DuckDNS Script:

    • Navigate to your home directory and create a duckdns folder:
      cd ~
      mkdir duckdns
      cd duckdns
      touch duck.sh
  4. Edit the Script:

    • Open the script with nano or your preferred text editor and add the following line:
      #!/bin/bash
      DOMAIN="EXAMPLE"
      TOKEN="TOKEN"
      echo url="https://www.duckdns.org/update?domains=$DOMAIN&token=$TOKEN&ip=" | curl -k -o ~/duckdns/duck.log -K -
    • Replace EXAMPLE with your DuckDNS domain and TOKEN with your unique token found on the DuckDNS dashboard at the top of the script. On line 2-3.
  5. Set File Permissions and Schedule the Script:

    • Make the script executable:
      chmod +x ~/duckdns/duck.sh
    • Open crontab to schedule the script:
      crontab -e
    • Add the following line to run the script every 5 minutes:
      */5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1
  6. Run the Script Manually for the First Time:

    • Navigate to the script directory and execute it:
      cd ~/duckdns/
      ./duck.sh
    • Check for the creation of duck.log. If it contains "OK", the setup is successful. If it displays "KO", there was an issue. You may want to revise that everything is correctly configured before proceeding further.

Next Steps

Just as an example, but you may want to set up port forwarding on your router to utilize your new DDNS hostname effectively.

@ansie3045
Copy link

ansie3045 commented Oct 16, 2022

If you use this shell script instead, you can also update your ipv6
#!/bin/bash domain=mydomain token=aaaabbbb-cccc-4444-8888-ddeeff001122 ipv6addr=$(curl -s https://api6.ipify.org) ipv4addr=$(curl -s https://api.ipify.org) echo "https://www.duckdns.org/update?domains=$domain&token=$token&ip=$ipv4addr&ipv6=$ipv6addr" curl -s "https://www.duckdns.org/update?domains=$domain&token=$token&ip=$ipv4addr&ipv6=$ipv6addr" -o ~/duckdns/duckdns.log
(source: https://superuser.com/questions/1634497/how-to-publish-ipv6-on-duckdns)

@MatinBoss5
Copy link

I installed but my IP has not changed :/

Why?

@taichikuji
Copy link
Author

I installed but my IP has not changed :/

Why?

Hello! This was an explanation of the official guide located within the duckdns website. Do you have an account on duckdns? If so, have you checked if a file is created with "KO" inside? If this is the case, this means an error occured but the API was successfully contacted so the error is related to the account ( token ) or domain.

I will check if the guide has changed in any way and update this gist in consequence. Thank you for your comment!

@taichikuji
Copy link
Author

If you use this shell script instead, you can also update your ipv6 #!/bin/bash domain=mydomain token=aaaabbbb-cccc-4444-8888-ddeeff001122 ipv6addr=$(curl -s https://api6.ipify.org) ipv4addr=$(curl -s https://api.ipify.org) echo "https://www.duckdns.org/update?domains=$domain&token=$token&ip=$ipv4addr&ipv6=$ipv6addr" curl -s "https://www.duckdns.org/update?domains=$domain&token=$token&ip=$ipv4addr&ipv6=$ipv6addr" -o ~/duckdns/duckdns.log (source: https://superuser.com/questions/1634497/how-to-publish-ipv6-on-duckdns)

Hello! First of all thank you very much for your suggestion!

I will update the gist to reflect on this as IPv6 is becoming increasingly important as time goes on, however, I will need to do some testing to see how does that snippet works. Thank you!

@Enissay
Copy link

Enissay commented Dec 4, 2022

A docker solution would be cleaner :-)

@taichikuji
Copy link
Author

A docker solution would be cleaner :-)

You're more than welcome to paste any solutions that involve docker to me and I will gladly note them to implement them!

@MatinBoss5
Copy link

Your guide is correct, when I ping my subdomain in Windows CMD, it says that my vps IP does not change, and the IP has not changed, and it shows OK in the Linux terminal.

@nateandmo731
Copy link

How do we check if a new file is created with "OK" inside? Coding noob here.

@taichikuji
Copy link
Author

How do we check if a new file is created with "OK" inside? Coding noob here.

In order to check it, you can run the cat ./duck.log to show the logging output through terminal. You can also manually open it with any text editor!

@Chris-Scot
Copy link

For anyone that is not a genuine git-hub user, the only authentication method available is google.
Persona & reddit support has been removed & Twitter does not work.
I had created a free machine account on github as this would be an automated process however, the account was suspended with the following explanation. "GitHub is meant to be used as a collaborating and hosting tool for software developers. We're unable to see activity on your account that indicates that it's being used for the intended purpose." This does not seem unreasonable.

@dotbanana
Copy link

A docker solution would be cleaner :-)

Docker-abstraction-layer for a curl-command.
Yeah sounds absolutely reasonable.

@n8jadams
Copy link

n8jadams commented Aug 18, 2023

No need to abstract this out into its own .sh script. Just run

crontab -e

And then add this line to your crontab file

*/5 * * * * curl "https://www.duckdns.org/update?domains=YOUR_DOMAIN&token=YOUR_TOKEN&ip=" >/dev/null 2>&1

Save it and you're done!

@andraekaaa
Copy link

im sorry my englis is bad, and im also new in linux.
should i setup mydomain.duckdns on my router, or just inside my linux server?
Thank You.

@taichikuji
Copy link
Author

im sorry my englis is bad, and im also new in linux. should i setup mydomain.duckdns on my router, or just inside my linux server? Thank You.

I am not entirely sure what you mean by 'setting up your duckdns on your router', but the option to do both are in the duckdns website itself, as long as you have a compatible router.

Personally? I prefer keeping it at the server level. Makes it easier to set up as well.

@Leela-Nayan
Copy link

Hi, this is the error i'm facing @taichikuji
WhatsApp Image 2024-04-09 at 09 52 04

@taichikuji
Copy link
Author

Hi, this is the error i'm facing @taichikuji

There are so many things that could be wrong with the information you have shared that my first thought was if you even have internet connection in your server.

Have you tried pinging duckdns.com?

@Leela-Nayan
Copy link

No, I actually don't know how to do that. Could you help me out?

@taichikuji
Copy link
Author

No, I actually don't know how to do that. Could you help me out?

Apologies but I do not have enough free time for that.

You could do some basic connectivity troubleshooting on your machine and confirm if you indeed have both connectivity to the internet and towards duckdns.com .

If that works, the guide should work for the most part.

@mrx23dot
Copy link

Could you add full domain name xx..duckdns.org on status page, not just xx
It would make copying easier.

@taichikuji
Copy link
Author

Could you add full domain name xx..duckdns.org on status page, not just xx It would make copying easier.

Can you tell me in which part of the guide I do that?

Also, please do keep in mind that the domain itself should be modified always for your specific use case.

@mrx23dot
Copy link

On this page
2024-06-30_150517_km

@taichikuji
Copy link
Author

On this page 2024-06-30_150517_km

There may be some confusion. I feel like, for changes on the DuckDNS service itself you should reach out to the DuckDNS team directly, not to me.

They seem to have the following email available for support, so you may want to try your luck over there; support@duckdns.org

Hope that helps! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment