WireGuard behind NAT stops working when idle: PersistentKeepalive, tested
Tested on
Date: 17 November 2026 · Machine: a Debian 12 virtual machine in our lab, with five network namespaces standing in for the internet, a public server, a provider's CGNAT box, a home router and a home server (a web page on port 8080) · Tools: WireGuard and nftables.
Simulated: both NAT boxes, and the idle timeout (we emptied the NAT's connection table with conntrack -F). Not tested: real provider or home-router timeouts, which vary, and how much traffic keepalive adds over weeks. Addresses are documentation examples; 10.0.0.1 and 10.0.0.2 are the two ends of the tunnel. ns <name> <command> means "run this command inside that network namespace".
Symptom: the tunnel is up, then inbound traffic stops when idle
You have a WireGuard tunnel from your home server to a public server. The tunnel works fine when you're actively using it. But after some time of inactivity, you can no longer reach your home server from the internet. The tunnel appears to be up, but it no longer passes traffic in both directions.
Why: a router or provider NAT forgets an idle flow
When your home server connects out through a NAT (like a home router or a provider's CGNAT), the NAT keeps a record of that connection. This record is used to allow return traffic and to route traffic back to your server.
But NATs often have a timeout for idle connections. If no traffic goes through the tunnel for a while, the NAT forgets the mapping and stops routing traffic to your home server. This is why inbound traffic stops after the tunnel has been idle.
The test we ran
We tested this on a Debian 12 virtual machine with five network namespaces to simulate a home server, a public server, and two NATs (a home router and a provider CGNAT). The home server ran a web page on port 8080. The public server ran WireGuard and used nftables to forward traffic from port 8080 to the home server through the tunnel.
We tested two scenarios:
- PersistentKeepalive OFF: The WireGuard tunnel had no keepalive setting. After the NAT table was emptied (to simulate an idle timeout), the tunnel stopped working until the home server sent traffic again.
- PersistentKeepalive ON: The WireGuard tunnel had a keepalive setting. After the NAT table was emptied and a 7-second wait, the tunnel was working again without any traffic from us.
Result, with the real output
When the tunnel had no keepalive, the public server could no longer reach the home server after the NAT table was emptied:
$ ns vps ping -c2 -W2 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
--- 10.0.0.2 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1018ms
And visitors could not reach the web page:
$ ns inet curl -s -m 4 -o /dev/null -w %{http_code}\n http://203.0.113.10:8080/
000
Only when the home server sent traffic did the tunnel come back:
$ ns home ping -c1 -W2 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.391 ms
When the tunnel had keepalive ON (every 5 seconds for the test), we emptied the NAT table again and waited 7 seconds without sending anything ourselves. The public server could still reach the home server:
$ ns vps ping -c2 -W2 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.319 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.499 ms
And visitors could still reach the web page:
$ ns inet curl -s -m 4 http://203.0.113.10:8080/
hello from home
The setting (PersistentKeepalive) and how to undo it
To keep the tunnel working when idle, set the PersistentKeepalive option on the home side of the WireGuard tunnel. This tells WireGuard to send a small packet at regular intervals, keeping the NAT mapping alive.
To enable it, use this command:
wg set wg0 peer <vps key> persistent-keepalive 5
This sends a keepalive packet every 5 seconds, which let us see the effect quickly in a test. A real setup uses 25 seconds.
To disable it, use this command:
wg set wg0 peer <vps key> persistent-keepalive 0
What we did not test
- How long the idle timeout is on real home routers or provider CGNAT boxes. It differs by device and provider, which is why a short, regular keepalive is the safe choice.
- How much traffic keepalive adds over a long time. The packets are small, but we did not measure them.
If you are not sure your line is behind CGNAT, start with Am I behind CGNAT?.