Fixing OpenWrt WireGuard Auto-Reconnect Issues

A practical method to auto-recover WireGuard on OpenWrt when peer IP changes (for example DDNS scenarios).

OpenWrt WireGuard Auto-Reconnect Workaround

When using WireGuard on OpenWrt with dynamic DNS, a peer IP change may leave the tunnel stale after many hours. In some environments it does not recover automatically and requires manual restart.

Method 1: Custom script

1
2
3
4
5
6
7
8
#!/bin/sh

if ! ping -c 3 <peer-wg-ip> > /dev/null 2>&1 ; then
  echo "The WireGuard is down! Now try restarting wg0!" >> ./ddns-wg0.log
  ifdown wg0
  sleep 3
  ifup wg0
fi

Method 2: Built-in OpenWrt watchdog script

OpenWrt includes /usr/bin/wireguard_watchdog (content omitted here for brevity). It checks peer handshake activity and can re-resolve endpoint hostnames when a peer is idle too long.

Run by cron

Use cron to execute the watchdog periodically.

Via Web UI

  1. Open System -> Scheduled Tasks.
  2. Add:
1
* * * * * /usr/bin/wireguard_watchdog

Via SSH

  1. SSH into OpenWrt.
  2. Run crontab -e.
  3. Add:
1
* * * * * /usr/bin/wireguard_watchdog
  1. Save and restart cron if needed.
记录并分享
Built with Hugo
Theme Stack designed by Jimmy