Skip to content
Contents
GuidesOpenWrt

Rotate between WireGuard peers on OpenWrt with a tested fallback

Set up interfaces and routing tables on both endpoints, each under your own or authorized control.

Set up interfaces and routing tables on both endpoints, each under your own or authorized control. Perform health checks to access a service behind the peer's infrastructure, and separately count the handshake and application success. For idle tunnels, the handshake can be older; after actively sending packets, check for returned bytes.

First manually complete one atomic switch

After verifying the backup path, DNS, and MTU, switch the test subnet strategy. New connections are checked for actual egress; existing sessions are retried based on business logic. In scheduled tasks, add mutual exclusion and timeout to ensure only one switching process modifies configuration at a time. On configuration change failure, restore the previous valid strategy, record the status, and prevent repeated triggers.

Comparing New and Old Sessions After Switching and DNS

During the test period, switch between two authorized WireGuard endpoints. Maintain one long-lived connection, and establish a new short-lived connection for comparison. Record echo responses, handshake times, routing behavior, and DNS resolution results.

Verify the office whitelist, external IP logins, and long-running tasks separately. After node replacement, the outbound traffic, device identity, and account are independently managed. Retain a management channel and disable scheduled task entry.

New sessions should use the selected egress. If the peer fails, restore the last working configuration and verify DNS. Record which existing sessions and applications reconnect.

Rotate one peer’s key and verify the handshake

A keeps the same address and public key throughout the recording. B replaces its key and reconnects to A. To test the dual-exit routing discussed earlier, make a separate client request and check which exit it uses.

Establish a WireGuard handshake across two isolated routers, rotate one peer and verify a handshake with the new public key. · OpenWrt 25.12.6 x86/64 (QEMU/KVM).
Check the terminal output
Establish a WireGuard handshake across two isolated routers, rotate one peer and verify a handshake with the new public key. · OpenWrt 25.12.6 x86/64 (QEMU/KVM).
Establish a WireGuard handshake across two isolated routers, rotate one peer and verify a handshake with the new public key. · OpenWrt 25.12.6 x86/64 (QEMU/KVM). Open the original image

Run the five blocks in order in one Bash session on your management computer. Use two OpenWrt routers with independent SSH access, wg and kernel WireGuard support. Set A, B and A_ENDPOINT for your lab. Reserve gm_rotate, 10.252.21.0/30 and UDP 51825 on both routers. B must be able to reach that UDP port on A.

Build the temporary tunnel. Each router generates its own private key. Only public keys return to the management computer. Resolve any error before continuing.

A=192.168.21.1
B=192.168.21.2
A_ENDPOINT=192.168.21.1:51825
a() { ssh -n -l root "$A" "$@"; }
b() { ssh -n -l root "$B" "$@"; }

prepare() {
    command -v wg >/dev/null && command -v ip >/dev/null || return 1
    ip link add gm_rotate type wireguard || return 1
    umask 077
    dir=$(mktemp -d /tmp/gm-rotate.XXXXXX) || return 1
    wg genkey > "$dir/initial" && printf '%s\n' "$dir"
}

AD=$(a "$(typeset -f prepare); prepare") &&
BD=$(b "$(typeset -f prepare); prepare") &&
A_PUB=$(a "wg pubkey < '$AD/initial'") &&
B_OLD=$(b "wg pubkey < '$BD/initial'") &&
a "ip addr add 10.252.21.1/30 dev gm_rotate &&
   wg set gm_rotate listen-port 51825 private-key '$AD/initial' peer '$B_OLD' allowed-ips 10.252.21.2/32 &&
   ip link set gm_rotate up" &&
b "ip addr add 10.252.21.2/30 dev gm_rotate &&
   wg set gm_rotate listen-port 51825 private-key '$BD/initial' peer '$A_PUB' endpoint '$A_ENDPOINT' allowed-ips 10.252.21.1/32 &&
   ip link set gm_rotate up"

Send traffic from B and read the handshake on A. The first field should match B_OLD and the second should be a nonzero Unix timestamp. In our lab the ping triggered a handshake even though no echo reply arrived. Test application reachability against the service itself.

handshake() {
    for attempt in 1 2 3; do
        b 'ping -c 1 -W 1 10.252.21.1' || :
        if a 'wg show gm_rotate latest-handshakes' |
            awk -v key="$1" '$1 == key && $2 > 0 { print; seen=1 } END { exit !seen }'
        then
            return 0
        fi
        sleep 2
    done
    return 1
}
handshake "$B_OLD"

Rotate B’s key. B loads the new private key and A replaces the peer allowed to use 10.252.21.2/32. Traffic may pause between those changes. A nonzero timestamp beside B_NEW confirms the operation shown in the clip.

B_NEW=$(b "umask 077; wg genkey > '$BD/next' && wg pubkey < '$BD/next'") &&
test -n "$B_NEW" && test "$B_NEW" != "$B_OLD" &&
b "wg set gm_rotate private-key '$BD/next'" &&
a "wg set gm_rotate peer '$B_OLD' remove peer '$B_NEW' allowed-ips 10.252.21.2/32" &&
handshake "$B_NEW"

Rehearse rollback. Restore B’s initial private key and the corresponding public key on A. Look for B_OLD again with a fresh handshake timestamp.

b "wg set gm_rotate private-key '$BD/initial'" &&
a "wg set gm_rotate peer '$B_NEW' remove peer '$B_OLD' allowed-ips 10.252.21.2/32" &&
handshake "$B_OLD"

Remove the test interfaces and temporary keys. The wg set changes are runtime-only. For a persistent rotation, save the matching keys in the OpenWrt network configuration on both ends.

if [[ "$AD" == /tmp/gm-rotate.?????? ]]; then
    a "ip link del gm_rotate && rm -f '$AD/initial' && rmdir '$AD'"
fi
if [[ "$BD" == /tmp/gm-rotate.?????? ]]; then
    b "ip link del gm_rotate && rm -f '$BD/initial' '$BD/next' && rmdir '$BD'"
fi

Reference

Related guides

Reference

Check the model, region and hardware revision on the label before selecting firmware.

Explore hardware

Comments