← All posts
TCP/IP

TCP Keepalive: What It Is, What It Is Not, and How to Read It in Wireshark

TCP Keepalive: What It Is, What It Is Not, and How to Read It in Wireshark

The Problem Keepalive Solves

TCP is a stateful protocol. Both sides maintain session state: sequence numbers, window sizes, timers. But the network between them is stateless. Firewalls and NAT devices track connections using their own tables, and those tables have idle timeouts.

If a TCP session sits idle for long enough, the firewall drops its state entry. The two endpoints don't know this happened. They think the session is still up. The first time either side tries to send data, they get a reset or just silence.

I've seen this kill BGP sessions on networks with overly aggressive NAT timeouts. I've seen it break long-running SSH sessions through firewalls. TCP keepalive is the mechanism that prevents it.

What a Keepalive Probe Actually Is

A TCP keepalive probe is a segment sent after a period of inactivity to verify that the other side is still there. It carries the same sequence number as the last acknowledged byte, with no new data (or one byte of data, depending on the implementation).

The receiver is supposed to respond with an ACK. If it does, both sides know the path is still up. If it doesn't respond after a configured number of probes, the sender tears down the session with a RST.

This is not a network-level heartbeat. It's an application socket option. The TCP stack handles it, but only if the application (or the OS default) has enabled it.

The Three Keepalive Parameters

On Linux, three kernel parameters control keepalive behavior:

tcp_keepalive_time # seconds of idle before first probe (default: 7200)
tcp_keepalive_intvl # seconds between probes (default: 75)
tcp_keepalive_probes # how many probes before giving up (default: 9)

With defaults, a session has to sit idle for 2 hours before the first probe fires. Then 9 probes 75 seconds apart. That's over 2 hours and 11 minutes before the OS gives up and resets the session.

For most applications that's too slow. If your firewall drops state after 30 minutes of idle, the default Linux keepalive does nothing useful.

Changing the System-Wide Values

sysctl -w net.ipv4.tcp_keepalive_time=60
sysctl -w net.ipv4.tcp_keepalive_intvl=10
sysctl -w net.ipv4.tcp_keepalive_probes=5

This fires the first probe after 60 seconds of idle, then every 10 seconds, giving up after 5 failures. That's 110 seconds total to detect a dead session. Much more useful.

To make changes persist across reboots, write them to /etc/sysctl.conf or a file under /etc/sysctl.d/.

Enabling Per-Socket

System-wide values set the defaults. Individual applications can override them using socket options:

SO_KEEPALIVE # enables keepalive on the socket
TCP_KEEPIDLE # overrides tcp_keepalive_time for this socket
TCP_KEEPINTVL # overrides tcp_keepalive_intvl
TCP_KEEPCNT # overrides tcp_keepalive_probes

BGP daemons like FRR and OpenBGPD set these per-socket. That's why BGP keepalive behavior can differ from what your sysctl values suggest.

Reading Keepalive in Wireshark

Wireshark identifies keepalive probes automatically. You'll see the info column show [TCP Keep-Alive] and the response show [TCP Keep-Alive ACK].

The key indicators in the packet detail:

  • Sequence number is one less than the next expected byte (or the same as the last ACK)

  • ACK flag is set, PSH flag is not

  • Length is 0 (or 1 on some stacks)

  • Wireshark expert info flags it in the analysis column

A healthy keepalive sequence looks like: probe, ACK, probe, ACK. If you see probes with no ACK in return, then a RST or just nothing, the session was dead. The keepalive detected it.

What to Look For When Troubleshooting

If you're seeing keepalive probes in a capture and you're not sure if they're expected, check the timing. Probes firing every 10-30 seconds usually mean the application set aggressive keepalive to work around a middlebox.

Probes followed by resets mean the other end is there but rejecting the session. That's often a firewall or load balancer that dropped state and is now sending RSTs on behalf of the original server.

Probes with no response at all, eventually followed by a RST from the sender itself, means the path is dead or the other end is unresponsive.

TCP Keepalive vs Application-Layer Keepalives

This is where confusion happens. TCP keepalive is not the same as BGP keepalive, SSH ServerAliveInterval, or HTTP keep-alive. These are different mechanisms at different layers.

TCP keepalive operates at the transport layer. It's invisible to the application. The kernel sends the probes. BGP doesn't know it's happening.

BGP keepalive operates at the application layer. BGP peers exchange KEEPALIVE messages to maintain the session. The BGP hold timer (default 90 seconds) defines how long a peer waits before declaring the session dead. KEEPALIVE messages fire every 30 seconds by default.

If the TCP keepalive detects a dead session first, it tears it down. BGP then sees its TCP session drop and reacts. If BGP's hold timer fires first because packets are being dropped but the TCP session is technically still up, BGP tears down first.

In practice, for BGP over long-haul links, you want both. TCP keepalive catches dead NAT state. BGP keepalive catches routing process failures and black holes.

When Keepalive Causes Problems

Keepalive is not free. Each probe generates a round trip. On high-scale systems with thousands of connections, that traffic adds up.

On asymmetric paths, the outbound probe might succeed but the return ACK might take a different path that's broken. The sender sees no ACK and starts the failure counter. The session might get torn down even though data could flow in one direction.

Some load balancers and transparent proxies intercept TCP sessions. They respond to keepalive probes themselves, which means the probe never reaches the actual server. You can get a false positive that the session is healthy when the backend is actually down.

The Practical Takeaway

If you see TCP Keep-Alive in a Wireshark capture, that's normal. It means someone enabled keepalive on that socket and the connection has been idle for the configured time.

If you see keepalive probes followed by no ACK and eventually a reset, the session was dead. The question then is why: network path down, firewall dropped state, server crashed, or remote end went away.

If you're running BGP over NAT or through stateful firewalls, tune your keepalive values down from the 2-hour default. Sixty seconds idle time with 5 probes at 10-second intervals is a reasonable starting point for most network environments.

Watch the video

Prefer video? See TCP keepalive in a live Wireshark capture.

Watch on YouTube →

Next in this series

TCP Zero Window and Congestion Avoidance: Reading Slow Networks in Wireshark →

Comments (0)

Please sign in to comment.

  • Be the first to comment.