English
简体中文繁體中文EnglishРусскийفارسی

Home › Blog › Settings

A flash every 25 seconds

Why PersistentKeepalive is 25 seconds in WireGuard, when it is required, when to leave it out, and what it costs on a phone battery.

Compiled on

A regular flash says: still here
A regular flash says: still here Anita Ritenour from Santa Maria, CA, USA, CC BY 2.0

With nothing to send WireGuard says nothing. That saves power and hides it, but NAT routers forget it.

Perhaps the most copied and least understood line. One sentence: it is about others finding me, not about me getting out.

NAT forgets

A home router notes a temporary port for each UDP flow and deletes it when it is quiet, often after about 30 seconds. After that nothing can come in. An empty packet every 25 seconds is just in time.

A lighthouse beam: a regular flash tells the far side you are still there
A lighthouse beam: a regular flash tells the far side you are still there Radomianin, CC BY-SA 4.0

Do you need it?

CaseAdd it?
Behind NAT and others must reach itYes
The box at home you reach from outsideYes
A phone that only connects outNo
A server with a public IPNo
Public IPs on both sidesNo

Write it only on the side behind NAT.

Battery

An empty packet is tiny, under 1 MB a day. The cost is waking: an idle phone's radio must wake every 25 seconds. If nothing needs to reach the phone, leave it out.

  1. Nobody connects to itDelete the line
  2. You want pushes from homeKeep it, or try 60
  3. Only at workUse on-demand instead
On a phone
A blinking LED: every 25 seconds keeps the router from forgetting you
A blinking LED: every 25 seconds keeps the router from forgetting you Dcaldero8983, CC BY-SA 3.0

Why not 60

Some routers forget after 30 seconds, and 60 works only now and then. 25 is the value from the official documentation that suits most NATs.

An example

A small machine sits behind a router without a forward; a VPS has a public address. The machine sends first, and the router notes: inside 192.168.1.20:51820 is outside port 40123 towards the VPS. Answers to 40123 find their way. After a minute without traffic the router deletes the note. Now you connect from outside, the VPS sends to 40123, and the router drops it: "it worked a moment ago". An empty packet every 25 seconds keeps the note alive.

Where to write it

On the side behind NAT that must be found, in the [Peer] that points to the public side.

The small machine
[Peer]
PublicKey = vps-public-key
Endpoint = vps-address:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25

The VPS does not need it.

Who does not need it

Phones above all. You open the NAS from the phone, so the phone sends first and the note is made on the spot. Only if something at home must reach the phone is it needed. Laptops are alike; a desktop you connect to remotely does need it.

How long does my router remember

On the VPS, after removing the keepalive
sleep 30; ping -c1 10.8.0.2
sleep 60; ping -c1 10.8.0.2
sleep 120; ping -c1 10.8.0.2

Let the machine send one packet first. The note was deleted before the first failure. Home routers keep it 30 seconds to 3 minutes. With three minutes, 60 is enough and wakes a phone half as often.

Battery

A keepalive is under a hundred bytes; three thousand a day are a few hundred KB. The cost is waking the radio, which needs seconds to sleep again. Every 25 seconds it hardly sleeps at all, and a few percent overnight is normal.

Other keepalives

NameLayerFor
PersistentKeepaliveWireGuardThe NAT note
TCP keepaliveConnections insideNoticing the other end is gone
ServerAliveIntervalSSHNot losing the session

If the tunnel is up and SSH still drops, it is the last two.

Is 1 more stable?

No. More battery and more visible.

Still dropping?

It may not be NAT. If the handshake in wg show is over three minutes old, see no handshake.

Both behind NAT, both 25?

No; neither can be found first. Forward a port on one or add a relay.

The counters move by themselves?

Yes, that is the keepalive.

What is 0?

Off, the same as leaving it out.

Next letter

On the same round

Postcodes: PersistentKeepalive 25, WireGuard keepalive, WireGuard battery