Blog | G5 Cyber Security

Router Auto-Config After Reset

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

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

  3. Choose a Dynamic DNS (DDNS) Provider
  4. 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.

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

  • Create a Configuration Script
  • This script will run after each reboot to restore your desired settings.

    #!/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.

    /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
  • Troubleshooting

    Exit mobile version