I keep seeing the same advice: "add PersistentKeepalive = 25 if your client is behind NAT". Fine, but nobody says what actually breaks without it, or how the value relates to the NAT timer. So I measured it on a real kernel (Linux 6.8, wireguard-tools 1.0.20210914), with three throwaway network namespaces: client (192.168.1.2), a NAT router (nftables masquerade), and a server (203.0.113.2).
What breaks without a keepalive
The tunnel works at first: the client handshakes, the server can ping the client's tunnel IP. Then the tunnel goes idle. The router forgets the UDP mapping (conntrack entry expires). From that moment the server still sends to the client's last known endpoint, but the NAT has nothing to match it to, so the packet is dropped. The client can still reach the server (a client packet creates a new mapping), but the server cannot reach the client until the client talks first.
That is the classic symptom: "I can ping the server from my laptop, but the server cannot ping my laptop / reach my NAS after a while".
Measurement
To keep the test fast I shortened the router's UDP conntrack timers to 5 s (nf_conntrack_udp_timeout and nf_conntrack_udp_timeout_stream) instead of the Linux defaults of 30 s and 120 s. What matters is the ratio keepalive / NAT timer, not the absolute value. Each run: handshake, one ping server to client (always worked), 12 s idle, then 3 pings server to client (ping -c3 -W1 10.9.0.2 from the server namespace).
| client PersistentKeepalive | NAT UDP timer | runs | pings server to client after 12 s idle |
|---|---|---|---|
| off | 5 s | 3 | 0/3 in every run |
| 3 s | 5 s | 3 | 3/3 in every run |
| 4 s | 5 s | 3 | 3/3 in every run |
| 6 s | 5 s | 3 | 2/3 in every run |
| 9 s | 5 s | 3 | 3/3 in 2 runs, 1 run not parsed (my script's grep returned nothing) |
Reading it:
- Off: the server is cut off from the client as soon as the NAT timer runs out. Zero out of nine pings in the three runs.
- Keepalive shorter than the NAT timer: the mapping never expires, server-initiated traffic always passes.
- Keepalive longer than the NAT timer (6 s against 5 s): the mapping dies between two keepalives. A server packet gets through only in the short window after a keepalive has recreated the mapping. Here 2 of 3 pings passed, because the first ping landed right after a keepalive and later ones did not. Do not read the 9 s row as "9 s is fine": my 12 s idle happened to end 3 s after a keepalive, inside a 5 s window. The result depends on when the server happens to send, which is exactly what makes this failure intermittent and miserable to debug.
What I would do with this
- Set the keepalive on the side behind NAT only (the client). The server, with a public IP, does not need it.
- Choose it under your real NAT timer, not under a folklore number. Many home routers keep UDP mappings for 30 s or more, some carrier-grade NATs less. 25 s is a safe default for the usual 30 s timer, and you can raise it if you know yours is longer.
- If the symptom is "server cannot reach the client after idle, client can reach the server", test this first, before touching MTU or AllowedIPs.
Limits of this test: one Linux conntrack NAT, not a consumer router or a CGNAT, shortened timers, 3 runs per row, IPv4 only. It shows the mechanism and the ratio, not the timer of your box.
Reproduce it
You need root, wireguard-tools, nft and the wireguard kernel module. Usage: sudo bash nat-keepalive.sh <keepalive or 0> <idle seconds> <nat udp timer seconds>, for example sudo bash nat-keepalive.sh 0 12 5 then sudo bash nat-keepalive.sh 3 12 5.
#!/bin/bash # Mesure : PersistentKeepalive derriere un NAT (conntrack UDP), netns jetables. Usage: sudo bash nat-keepalive.sh <keepalive|0> <idle_s> <udp_stream_timeout_s> KA=$1; IDLE=$2; TMO=$3 WG=${WG:-wg} ns() { local n=$1; shift; ip netns exec $n "$@"; } cleanup(){ for n in wgc wgr wgs; do ip netns del $n 2>/dev/null; done; } cleanup; trap cleanup EXIT for n in wgc wgr wgs; do ip netns add $n; ns $n ip link set lo up; done ip link add c0 netns wgc type veth peer name r0 netns wgr ip link add s0 netns wgs type veth peer name r1 netns wgr ns wgc ip addr add 192.168.1.2/24 dev c0; ns wgc ip link set c0 up; ns wgc ip route add default via 192.168.1.1 ns wgr ip addr add 192.168.1.1/24 dev r0; ns wgr ip link set r0 up ns wgr ip addr add 203.0.113.1/24 dev r1; ns wgr ip link set r1 up ns wgs ip addr add 203.0.113.2/24 dev s0; ns wgs ip link set s0 up; ns wgs ip route add default via 203.0.113.1 ns wgr bash -c "echo 1 > /proc/sys/net/ipv4/ip_forward" ns wgr /usr/sbin/nft add table ip nat ns wgr /usr/sbin/nft 'add chain ip nat post { type nat hook postrouting priority 100; }' ns wgr /usr/sbin/nft add rule ip nat post oifname r1 masquerade ns wgr bash -c "echo $TMO > /proc/sys/net/netfilter/nf_conntrack_udp_timeout; echo $TMO > /proc/sys/net/netfilter/nf_conntrack_udp_timeout_stream" SK=$($WG genkey); SP=$(echo $SK | $WG pubkey); CK=$($WG genkey); CP=$(echo $CK | $WG pubkey) echo $SK > /tmp/sk.$$; echo $CK > /tmp/ck.$$ ns wgs ip link add wg0 type wireguard; ns wgs ip addr add 10.9.0.1/24 dev wg0 ns wgs $WG set wg0 listen-port 51820 private-key /tmp/sk.$$ peer $CP allowed-ips 10.9.0.2/32 ns wgs ip link set wg0 up ns wgc ip link add wg0 type wireguard; ns wgc ip addr add 10.9.0.2/24 dev wg0 ARGS=""; [ "$KA" != 0 ] && ARGS="persistent-keepalive $KA" ns wgc $WG set wg0 private-key /tmp/ck.$$ peer $SP endpoint 203.0.113.2:51820 allowed-ips 10.9.0.1/32 $ARGS ns wgc ip link set wg0 up ns wgc ping -c1 -W2 10.9.0.1 >/dev/null A=$(ns wgs ping -c1 -W2 10.9.0.2 2>&1 | grep -c '1 received') sleep $IDLE B=$(ns wgs ping -c3 -W1 10.9.0.2 2>&1 | grep -oE '[0-9]+ received' | cut -d' ' -f1) CT=$(ns wgr cat /proc/net/nf_conntrack 2>/dev/null | grep -c 51820) echo "keepalive=$KA idle=${IDLE}s nat_udp_timeout=${TMO}s | server->client avant idle: $A/1 | apres idle: ${B}/3 recus | entrees conntrack 51820: $CT" rm -f /tmp/sk.$$ /tmp/ck.$$ (The script prints one line per run; the conntrack column at the end of that line is not reliable, ignore it.)
Check your own config
If you do not want to read a config by eye, I built a free checker that flags a missing or invalid PersistentKeepalive, overlapping AllowedIPs, bad MTU and malformed endpoints, in the browser and without sign-up: wg.vpnsmith.com/check. Paste a client config and it tells you what to fix. Earlier articles in this series: AllowedIPs overlaps and MTU behind PPPoE.