๐ Auto Update IP of EC2 With DuckDNS
2025-04-10
DuckDNS Auto Update IP Setup on EC2 Startup
This guide will walk you through setting up a script that updates DuckDNS with your EC2 instance's public IP at startup.
1. Create the setup-duckdns.sh file
Run the following commands to create the update script:
vim setup-duckdns.sh
Paste the content below into the file:
#!/bin/bash # === CONFIGURATION === DUCKDNS_DOMAIN="your-subdomain" # e.g., nikitech DUCKDNS_TOKEN="your-token" # e.g., 0123abcd-4567efgh... USER_HOME=$(eval echo ~${SUDO_USER}) SCRIPT_PATH="$USER_HOME/update_duckdns.sh" echo "๐ฆ Creating DuckDNS update script at $SCRIPT_PATH" # Create the DuckDNS update shell script cat <<EOF > $SCRIPT_PATH #!/bin/bash echo "[\$(date)] Updating DuckDNS..." >> /var/log/duckdns.log curl -s "https://www.duckdns.org/update?domains=$DUCKDNS_DOMAIN&token=$DUCKDNS_TOKEN&ip=" >> /var/log/duckdns.log echo "" >> /var/log/duckdns.log EOF chmod +x $SCRIPT_PATH # Create the systemd service SERVICE_FILE="/etc/systemd/system/duckdns-update.service" echo "๐ ๏ธ Creating service at $SERVICE_FILE" cat <<EOF | sudo tee $SERVICE_FILE > /dev/null [Unit] Description=Update DuckDNS IP at startup After=network.target [Service] Type=oneshot ExecStart=$SCRIPT_PATH RemainAfterExit=true [Install] WantedBy=multi-user.target EOF # Reload systemd and enable the service echo "๐ Reloading systemd and enabling the service..." sudo systemctl daemon-reexec sudo systemctl daemon-reload sudo systemctl enable duckdns-update.service echo "โ Completed! DuckDNS will be updated each time EC2 starts." echo "๐ Logs will be saved to: /var/log/duckdns.log"
2. Run the .sh file on EC2
Step 1: Grant execute permission
chmod +x setup-duckdns.sh
Step 2: Run with root privileges (use sudo)
sudo ./setup-duckdns.sh
โ ๏ธ Note:
If you're using Amazon Linux 2023, the script will work as expected because it uses systemd.
If you're using Ubuntu 20.04 or 22.04, it is also fully compatible.
3. Verify the setup
After completing the setup:
Restart EC2:
sudo reboot
Check the DuckDNS log:
cat /var/log/duckdns.log
You should see a line like this in the log:
[Mon Apr 1 12:00:00 UTC 2025] Updating DuckDNS... OK