brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: PersistentKeepalive keeps a WireGuard peer behind NAT reachable

Network and access
~/tips/tip-wireguard-persistentkeepalive

PersistentKeepalive = 25

Stop a WireGuard tunnel from going deaf when the router forgets the UDP mapping.

WireGuard is silent when there is nothing to send. Most routers forget a UDP mapping after a while of silence, and then the server can no longer reach the peer behind that router until the peer sends something first.

Add one line to the peer section on the side that sits behind NAT, usually the home box or the phone:

[Peer]
PublicKey = abc123...
Endpoint = vpn.example.com:51820
AllowedIPs = 198.51.100.0/24
PersistentKeepalive = 25

Now that peer sends a small packet every 25 seconds, which keeps the router’s mapping open so traffic from the other side gets through. 25 is the value the wg man page uses in its example of a peer behind NAT.

You only need it when the other side must start connections. A laptop that only reaches out to a server does not need it, because its own traffic opens the path. A home server that a VPS must reach does.

The man page says most users will not need it, and I agree. It costs a tiny bit of traffic and battery on phones, so I’d set it only where the problem actually exists.

If the tunnel works right after connecting but the server cannot reach the peer a few minutes later, this is almost always why.

Further reading: wg(8) man page

Going deeper: Why a Tailscale device vanishes after 180 days, and how to stop it

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link