These instructions are designed to run on a Raspberry Pi. If you don't have a Raspberry Pi or need help setting it up, check out the sections on purchasing a Raspberry Pi and configuring it for the first time.
Purchasing a Raspberry Pi
If you don't already have a Raspberry Pi, you can purchase one from various online retailers. Here are a few options:
Configuring Your Raspberry Pi for the First Time
To set up your Raspberry Pi optimally for this function, follow these steps:
- Download Raspberry Pi OS: Visit the official website to download the latest Raspberry Pi OS. Choose the "Lite" version for a headless setup as it uses fewer resources and is ideal for running automated scripts like NoIP updates.
- Install Raspberry Pi Imager: Use the Raspberry Pi Imager tool to write the downloaded OS image to an SD card. Configure the OS with these settings:
- Set a default hostname (e.g.,
raspberrypi
). - Enable SSH for remote access.
- Create a default username and password for secure login.
- Set a default hostname (e.g.,
- Boot Your Raspberry Pi: Insert the SD card into your Raspberry Pi and power it on. Connect the device to your network using an Ethernet cable for stability or configure Wi-Fi during the OS setup. Follow the on-screen instructions to complete the setup.
- Update and Install Required Tools:
- Log in via SSH or directly on the Raspberry Pi.
- Update the system:
sudo apt update && sudo apt upgrade -y
- Install essential packages for the script:
sudo apt install curl python3 cron -y
- Configure a Static IP:
To ensure your Raspberry Pi always uses the same IP address on your network, configure the
/etc/dhcpcd.conf
file:- Edit the configuration file:
sudo nano /etc/dhcpcd.conf
- Add static IP settings at the end of the file:
interface eth0 # Replace eth0 with wlan0 if using Wi-Fi static ip_address=192.168.1.100/24 # Replace 192.168.1.100 with your desired IP address static routers=192.168.1.1 # Replace 192.168.1.1 with your router's IP address static domain_name_servers=8.8.8.8 8.8.4.4 # Replace with DNS servers (e.g., Google's DNS)
Explanation:
interface eth0
specifies the network interface (usewlan0
for Wi-Fi).static ip_address
defines the desired static IP and subnet mask (e.g.,/24
for 255.255.255.0).static routers
is your router's IP address (the gateway).static domain_name_servers
specifies the DNS servers (e.g., Google's public DNS).
- Save and exit the editor (
Ctrl+O
, thenEnter
, followed byCtrl+X
). - Restart the DHCP client service:
sudo systemctl restart dhcpcd
- Verify the static IP address:
ip addr show eth0 # Replace eth0 with wlan0 for Wi-Fi
- Edit the configuration file:
Step 1: Create a NoIP Account
- Sign Up: Visit the NoIP website and sign up for a free account.
- Confirm Email: After registration, confirm your email address by clicking on the verification link sent to your inbox.
Step 2: Set Up a Hostname
- Log In: Log in to your NoIP account.
- Add a Host: Navigate to “Dynamic DNS” and click on “Add a Hostname.”
- Hostname Details: Choose a desired hostname (e.g., mynetwork.ddns.net) and select the appropriate domain from the dropdown menu.
- IP Address: Leave the IP address field set to “Auto-detect IP” to automatically detect and update your IP address.
- Save: Click “Add Hostname” to save your settings.
Step 3: Configure Your Router (Optional)
If your router supports DDNS services like NoIP natively, configure it to update NoIP directly. If not, or if you prefer more control, focus on automating updates with the script below.
Step 4: Automate Updates with a Script
Here’s an updated bash script to check for IP changes, log the details, and track the last IP change:
#!/bin/bash
# Variables
PYTHON_SCRIPT="/home/pi/noip-renew.py"
IP_FILE="/home/pi/last_ip.txt"
LOG_FILE="/home/pi/noip-renew.log"
LAST_CHANGED_FILE="/home/pi/last_changed.txt"
# Get current date and time
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Get the current external IP
CURRENT_IP=$(curl -s http://checkip.amazonaws.com)
# Get the last known IP
if [ -f "$IP_FILE" ]; then
LAST_IP=$(cat "$IP_FILE")
else
LAST_IP=""
fi
# Get the last time the IP changed
if [ -f "$LAST_CHANGED_FILE" ];then
LAST_CHANGED=$(cat "$LAST_CHANGED_FILE")
else
LAST_CHANGED="Never"
fi
# Overwrite the log file with the latest details
echo "Current Date: $TIMESTAMP" > $LOG_FILE
echo "Current IP: $CURRENT_IP" >> $LOG_FILE
echo "Last IP: $LAST_IP" >> $LOG_FILE
echo "Last IP Change: $LAST_CHANGED" >> $LOG_FILE
# Compare current IP with last known IP
if [ "$CURRENT_IP" != "$LAST_IP" ]; then
echo "IP has changed. Running the Python script." >> $LOG_FILE
/usr/bin/python3 "$PYTHON_SCRIPT"
echo "$CURRENT_IP" > "$IP_FILE"
echo "$TIMESTAMP" > "$LAST_CHANGED_FILE" # Update the last-changed time
else
echo "IP has not changed. Not running the Python script." >> $LOG_FILE
fi
Step 5: Schedule the Script
- Edit Crontab: Open the cron table for editing by running
crontab -e
. - Add a Cron Job: Add the following line to schedule the script to run every 15 minutes:
*/15 * * * * /home/pi/schedule-noip-renew.sh
- Save and Exit: Save the file and exit the editor.
Step 6: Verify the Setup
Check logs to ensure the script is working:
- View
/home/pi/noip-renew.log
for IP and status updates. - Check
/home/pi/last_changed.txt
for the last IP change timestamp.
cat /home/pi/noip-renew.log
cat /home/pi/last_changed.txt
Step 7: Test Your Configuration
Simulate an IP change by updating last_ip.txt
, then run the script manually:
echo "0.0.0.0" > /home/pi/last_ip.txt
/home/pi/schedule-noip-renew.sh
Verify the updated logs and the last_changed.txt
file.
Conclusion
With this setup, your Raspberry Pi reliably tracks IP changes and updates NoIP automatically, ensuring dynamic DNS functionality. Configuring a static IP and automating updates with the provided script guarantees stability and ease of use for your dynamic DNS solution. Happy networking!