How to Set Up DuckDNS on Raspberry Pi for Easy Remote Access

Ben
Ben
@benjislab

If you're running services on your Raspberry Pi that you want to access remotely, you've likely encountered the challenge of dynamic IP addresses. Most residential internet connections don't provide a static IP address, meaning your external IP address changes periodically. This makes it difficult to reliably connect to your home network from the outside world.

That's where DuckDNS comes in - a free dynamic DNS service that allows you to create a custom subdomain that always points to your current IP address. In this guide, we'll walk through setting up DuckDNS on your Raspberry Pi, allowing you to access your Pi's services from anywhere using a simple, memorable domain name.

What is DuckDNS?

DuckDNS is a free dynamic DNS service that maps a subdomain of duckdns.org to your home's ever-changing IP address. The service automatically updates your DNS record whenever your IP address changes, ensuring that your chosen subdomain (like yourname.duckdns.org) always points to the correct location.

Why Use DuckDNS with Raspberry Pi?

  • Free forever - Unlike many dynamic DNS services, DuckDNS is completely free with no hidden fees
  • Simple to set up - The configuration process is straightforward
  • Reliable - DuckDNS has excellent uptime and performance records
  • Multiple domains - You can create up to 5 subdomains with one account
  • Privacy-focused - Minimal personal information required to sign up
  • Perfect for Pi projects - Ideal for self-hosted websites, game servers, home automation dashboards, or remote SSH access

Prerequisites

Before we begin, you'll need:

  • A Raspberry Pi (any model) with Raspberry Pi OS installed
  • Internet connection for your Pi
  • Basic familiarity with the command line
  • Access to your router's port forwarding settings (for remote access)
  • A free account at duckdns.org

Step 1: Create a DuckDNS Account and Domain

  1. Visit duckdns.org and sign in using your Google, GitHub, Twitter, or Reddit account
  2. Once logged in, enter your desired subdomain name in the "add domain" field (e.g., mypi would give you mypi.duckdns.org)
  3. Click "add domain" to create your subdomain
  4. Make note of your token - it's a long string of letters and numbers displayed on your account page. You'll need this for the next steps

Step 2: Set Up the DuckDNS Update Script

Now, let's create a script on your Raspberry Pi that will update your IP address with DuckDNS:

  1. Connect to your Raspberry Pi via SSH or open a terminal window if you're using the desktop environment
  2. Create a directory for the DuckDNS script:
mkdir ~/duckdns
  1. Create a new script file:
nano ~/duckdns/duck.sh
  1. Add the following content to the file, replacing YOUR-DOMAIN with your subdomain name and YOUR-TOKEN with the token from your DuckDNS account:
#!/bin/bash
echo url="https://www.duckdns.org/update?domains=YOUR-DOMAIN&token=YOUR-TOKEN&ip=" | curl -k -o ~/duckdns/duck.log -K -
  1. Make the script executable:
chmod 700 ~/duckdns/duck.sh
  1. Test your script to ensure it works:
~/duckdns/duck.sh
  1. Check the log file to confirm successful update:
cat ~/duckdns/duck.log

If you see "OK" in the log file, your update was successful!

Step 3: Automate Updates with Cron

To ensure your DuckDNS record stays current, we'll configure the script to run automatically every 5 minutes:

  1. Open your crontab file:
crontab -e
  1. If prompted to select an editor, choose nano (option 1) if you're not familiar with vim

  2. Add the following line to the file:

*/5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1
  1. Save and exit (in nano, press Ctrl+X, then Y, then Enter)

Your Raspberry Pi will now update your DuckDNS record every 5 minutes, ensuring it always points to your current IP address.

Step 4: Set Up Port Forwarding (For Remote Access)

For your DuckDNS domain to work for remote access, you need to configure port forwarding on your router:

  1. Log in to your router's administration panel (typically accessible at 192.168.0.1 or 192.168.1.1)
  2. Find the "Port Forwarding" section (may be under Advanced Settings, NAT, or similar)
  3. Add a new port forwarding rule:
    • External port: The port you want to use from outside (e.g., 80 for HTTP, 22 for SSH)
    • Internal IP: Your Raspberry Pi's local IP address (e.g., 192.168.1.100)
    • Internal port: The port your service is running on (often the same as external port)
    • Protocol: TCP or UDP or both (depends on your service)

Note: Each router has a different interface for setting up port forwarding. Check your router's documentation if you're unsure.

Step 5: Testing Your Setup

To verify everything is working:

  1. On a device outside your home network (like your phone with Wi-Fi turned off), try accessing your service using your DuckDNS domain
  2. For example, if you set up a web server on port 80, you would visit http://yoursubdomain.duckdns.org
  3. For SSH access, you would use: ssh [email protected]

Advanced: Using DuckDNS with HTTPS (Let's Encrypt)

If you're hosting a web service, securing it with HTTPS is highly recommended. Here's how to use Let's Encrypt with your DuckDNS domain:

  1. Install certbot:
sudo apt update
sudo apt install certbot -y
  1. Obtain a certificate:
sudo certbot certonly --standalone --preferred-challenges http --agree-tos --email [email protected] -d yoursubdomain.duckdns.org
  1. Configure your web server to use the generated certificates (usually found in /etc/letsencrypt/live/yoursubdomain.duckdns.org/)

Advanced: Setting Up a Systemd Service Instead of Cron

For a more robust setup, you can create a systemd service and timer instead of using cron:

  1. Create a service file:
sudo nano /etc/systemd/system/duckdns.service
  1. Add the following content:
[Unit]
Description=DuckDNS update service
After=network.target

[Service]
Type=oneshot
ExecStart=/home/pi/duckdns/duck.sh
  1. Create a timer file:
sudo nano /etc/systemd/system/duckdns.timer
  1. Add the following content:
[Unit]
Description=Run DuckDNS update every 5 minutes

[Timer]
OnBootSec=1min
OnUnitActiveSec=5min
Persistent=true

[Install]
WantedBy=timers.target
  1. Enable and start the timer:
sudo systemctl enable duckdns.timer
sudo systemctl start duckdns.timer

Troubleshooting Common Issues

"NO CHANGE" in Duck.log

This isn't an error - it means your IP address hasn't changed since the last update.

Connection Refused Errors

  1. Check that port forwarding is set up correctly
  2. Verify your service is running and listening on the expected port
  3. Check if your ISP is blocking the port you're trying to use

DuckDNS Not Updating

  1. Verify your token and domain are correct in the duck.sh script
  2. Check that the script is executable (chmod 700 ~/duckdns/duck.sh)
  3. Ensure your Pi has internet access

Security Considerations

When exposing your Raspberry Pi to the internet, security becomes paramount:

  1. Change default passwords - Never use the default "raspberry" password for the "pi" user
  2. Use SSH keys instead of passwords when possible
  3. Keep your system updated:
    sudo apt update && sudo apt upgrade -y
    
  4. Consider using a firewall:
    sudo apt install ufw
    sudo ufw allow 22  # Replace with ports you need
    sudo ufw enable
    
  5. Use non-standard ports for services to avoid automated scanning

Managing Multiple Services with DuckDNS

You can use a single DuckDNS domain for multiple services by using different ports:

  • Web server: yoursubdomain.duckdns.org:80
  • SSH: yoursubdomain.duckdns.org:22 (or a non-standard port)
  • Game server: yoursubdomain.duckdns.org:25565 (for Minecraft)
  • Other services on their respective ports

Alternatively, you can create multiple DuckDNS subdomains to make it easier to remember which domain goes with which service.

Using DuckDNS with Docker

If you're using Docker on your Raspberry Pi, you can set up the DuckDNS client as a container:

docker run -d \
  --name=duckdns \
  -e PUID=1000 \
  -e PGID=1000 \
  -e TZ=Europe/London \
  -e SUBDOMAINS=yoursubdomain \
  -e TOKEN=your-duckdns-token \
  -e LOG_FILE=true \
  --restart unless-stopped \
  linuxserver/duckdns

Conclusion

DuckDNS provides an elegant, free solution for the dynamic IP problem, making it perfect for Raspberry Pi enthusiasts who want to access their projects remotely. With the setup described in this guide, you can easily access your Pi from anywhere using a memorable domain name.

Whether you're running a personal website, a game server, a home automation dashboard, or just want SSH access to your Pi while away from home, DuckDNS helps make your Raspberry Pi truly accessible from anywhere in the world.

Remember to keep security in mind when exposing services to the internet, and enjoy the flexibility that comes with having your own personal domain pointing to your Raspberry Pi!