Router Auto-Config After Reset

Tags:

TL;DR

This guide shows you how to automatically reconfigure your router after a factory reset using Dynamic DNS (DDNS) and a script that runs on startup. This means you don’t have to manually update settings every time.

Steps

Understand the Problem

When a router resets, it loses its configuration, including your port forwarding rules and any custom DNS settings. This guide automates restoring those settings.

Choose a Dynamic DNS (DDNS) Provider

DDNS services give you a consistent domain name even if your IP address changes. Popular options include No-IP, DynDNS, and DuckDNS. For this example, we’ll assume you’ve chosen DuckDNS as it’s free.

Create an account on DuckDNS.
Add a domain name (e.g., myrouter.duckdns.org).
Note your token and domain name – you’ll need these later.

Configure Your Router for DDNS

Most routers have built-in DDNS support. The exact steps vary by router model, but generally:

Log in to your router’s web interface (usually 192.168.1.1 or 192.168.0.1).
Find the DDNS settings section (often under Advanced Settings, WAN, or Dynamic DNS).
Enter your DuckDNS domain name and token.
Save the changes. Your router should now automatically update its IP address with DuckDNS.

Create a Configuration Script

This script will run after each reboot to restore your desired settings.

The script language depends on your router’s firmware. Many routers support shell scripts (.sh) or Lua. We’ll use a shell script for this example.
Create a file named router_config.sh with the following content, replacing placeholders with your actual values:

#!/bin/sh

# Your DuckDNS token and domain name
TOKEN=”YOUR_DUCKDNS_TOKEN”
DOMAIN=”myrouter.duckdns.org”

# Get the current public IP address
IP=$(curl -s https://api.ipify.org)

# Update DuckDNS record
curl -s “https://www.duckdns.org/update?domains=$DOMAIN&token=$TOKEN&ip=$IP” > /dev/null

# Restore port forwarding rules (example for port 80 to internal IP 192.168.1.10)
pfctl -a “rdr pass on eth0 proto tcp from any to any port 80 -> 192.168.1.10 port 80”

# Restore custom DNS settings (example using nsupdate, requires configuration in /etc/nsupdate.conf)
# nsupdate -f /etc/nsupdate.conf

echo “Router config updated successfully!”

Important: The pfctl command is specific to OpenBSD-based routers (like some ASUS models). Adjust this part of the script based on your router’s firewall configuration.

Configure Router Startup Script Execution

This step tells your router to run the router_config.sh script automatically after each reboot.

Again, this varies by router model. Look for a section in the web interface called “Startup Scripts”, “Custom Commands”, or similar.
Add the following command:

/path/to/router_config.sh

Replace /path/to/router_config.sh with the actual location of your script on the router’s filesystem.

Test Your Configuration

Reboot your router.
Check that your DDNS record is updated correctly on DuckDNS (or your chosen provider).
Verify that port forwarding rules are working as expected.
If there are errors, check the script’s output (if available) or examine the router’s system logs for clues.

Troubleshooting

Script Permissions: Ensure the script has execute permissions. You might need to use SSH to connect to your router and run chmod +x /path/to/router_config.sh.
Firewall Rules: Double-check that your firewall rules allow outbound connections for DDNS updates (usually port 80 or 443).
Script Errors: Carefully review the script for typos and incorrect commands. Use SSH to connect to the router and run the script manually to see any error messages.

The post Router Auto-Config After Reset appeared first on Blog | G5 Cyber Security.

Categories

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *