Use it when idle connections fail on their next use, or when a server holds connections to clients that have long since disappeared.
TCP Keepalive Evidence
TCP keepalive is an optional feature that sends a small probe on an idle connection at intervals, to check that the other end still answers. It is off unless the application enables it.
1. ss -to state established — does the connection have a keepalive timer?
2. None → the application did not enable it; system defaults alone change nothing.
3. Compare the keepalive time with the shortest idle timeout on the path.
4. Longer → lower it in the application, or use an application heartbeat.
5. Hanging terminal sessions on a Cisco device → service tcp-keepalives-in.
application enables keepalive → idle time shorter than the shortest timeout on the path → probe sent on an idle connection → acknowledged by the peer → middlebox state refreshed → connection usable when next needed
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
ss -to state establishedVerification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Operational verification Verify boundary
sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_intvl net.ipv4.tcp_keepalive_probes
ss -to state established
tcpdump -ni eth0 host 192.0.2.10 and tcp port 5432
The sysctl line shows a Linux host's default timers. ss -to lists established connections with their timers: an entry such as timer:(keepalive,29min,0) shows that keepalive is active on that connection and when the next probe is due; no keepalive timer means the application did not enable it. A capture left running on an idle connection shows the probes and their answers.
Wireshark labels the packets "TCP Keep-Alive" and "TCP Keep-Alive ACK" (tcp.analysis.keep_alive).
Also find out the idle timeout of every firewall, load balancer and NAT device on the path, and compare.
Known-good: the connection shows a keepalive timer shorter than the shortest idle timeout on the path, and probes are answered.
Step 2 Failure isolation workflow Narrow the fault
- Idle connections fail on the next use. A device in the path dropped its state; keepalive is off, or its idle time is longer than that device's timeout.
ssshows no keepalive timer. The application did not enable it; changing system defaults has no effect until it does.- Probes are sent and never answered. The peer is gone or the path is broken; the connection will be closed after the probe count.
- A probe is answered with a reset. The peer restarted, or a device in between has already forgotten the connection.
- Dead connections linger for hours on a server. No keepalive, so nothing ever discovers the clients are gone.
- Keepalives are answered, but the application is hung. The operating system answers probes regardless; an application-level heartbeat is needed.
- Hanging terminal sessions on a network device. Enable
service tcp-keepalives-in.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
The default idle time before the first probe is two hours — far longer than the idle timeout of most firewalls and NAT devices. For a keepalive to hold such a device's state, its interval must be shorter than that timeout. Probes are answered by the operating system, so they do not show that the application is alive.
sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_intvl net.ipv4.tcp_keepalive_probes— host defaults.ss -to state established— atimer:(keepalive,…)entry shows it is active on a connection.tcpdumpon the idle connection — probes and their answers.- The idle timeouts of every firewall, NAT device and load balancer on the path.
ReferenceWhat it is1 section · depth on demand
Mechanics & mental model
A TCP connection with nothing to send is completely silent; no packets flow, and neither end can tell whether the other is still there. TCP keepalive is an optional feature that sends a small probe on an idle connection at intervals, to check that the other end still answers.
ReferenceWhy it matters1 section · depth on demand
Why it matters operationally
Keepalives matter for two reasons. They let a system notice that its peer has disappeared, where it would otherwise hold a dead connection for ever. And they generate traffic, which stops firewalls and NAT devices from forgetting an idle connection. The default timing — two hours before the first probe — is too slow for the second purpose, which is behind many "connection drops after being idle" problems.
ReferenceHow it works1 section · depth on demand
How it works
How it works. After a connection has been idle for a set time, the operating system sends a probe: an empty segment, or one carrying a single byte, whose sequence number is one less than the next expected. The other end sees data it has already acknowledged and answers with a plain acknowledgment. That answer proves it is alive and still knows the connection.
Three timers.
| Setting | Meaning | Linux default | Windows default |
|---|---|---|---|
| Idle time | Silence before the first probe | 7200 s (2 hours) | 2 hours |
| Interval | Time between probes when there is no answer | 75 s | 1 s |
| Probe count | Unanswered probes before giving up | 9 | 10 |
It is off unless requested. Keepalive is enabled per connection by the application (the SO_KEEPALIVE socket option). Applications can also set their own timers for a connection. Many do neither.
Possible outcomes of a probe.
- An acknowledgment: the peer is alive; the idle timer restarts.
- A reset: the peer is up but has no record of the connection — it restarted. The connection is closed at once.
- Nothing, repeatedly: after the probe count is exhausted the connection is declared dead and the application gets an error.
Firewalls and NAT. These devices drop their record of a TCP connection after it has been idle for some time — commonly between a few minutes and an hour. The endpoints are not told. When one of them sends data later, the packet is dropped or answered with a reset. A keepalive interval shorter than the device's idle timeout prevents this; the two-hour default does not.
Application heartbeats are a different thing: messages defined by the application protocol itself, such as SSH's ServerAliveInterval or a database ping. They also test that the application at the other end is responding, which a TCP keepalive — answered by the operating system — does not.
On Cisco devices, service tcp-keepalives-in and service tcp-keepalives-out enable keepalives for the device's own terminal sessions, so that abandoned sessions are cleared.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Keep-alives: rules and real-world tuning
An idle TCP connection sends nothing, so neither end can tell whether the other still exists. RFC 9293 makes keep-alives optional, with strict rules if they are implemented. They must be off by default, the application must be able to switch them on per connection, and the interval must be configurable with a default of no less than two hours. Probes may be sent only when no data is outstanding and nothing has been received during the interval. A missing reply to a single probe must not be treated as a dead connection.
Keep-alive settings on common systems
| System | Idle time before first probe | Interval | Probes before giving up |
|---|---|---|---|
Linux (tcp_keepalive_*) | 7200 s | 75 s | 9 |
| Windows (registry defaults) | 2 hours | 1 s | 10 |
Cisco IOS XE (service tcp-keepalives-in/out) | 60 s | 60 s | 5 |
Why defaults often fail
| Problem | Effect |
|---|---|
| Firewall or NAT idle timeout (often 30–60 minutes or less) shorter than 2 hours | The session is removed before the first probe |
| Keep-alives not enabled by the application | No probes at all |
| Load balancer idle timeout | The connection is reset or blackholed after idle |
Choosing values
Set the keep-alive idle time below the shortest idle timeout on the path. For example, use 300 seconds when the firewall's TCP idle timeout is 3600 seconds. Many protocols, such as SSH (ServerAliveInterval), HTTP/2 PING and database drivers, offer application keep-alives that work regardless of the TCP settings.
host$ sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_intvl net.ipv4.tcp_keepalive_probes
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_intvl = 75
net.ipv4.tcp_keepalive_probes = 9
host$ ss -tno state established dst 10.30.0.15 | grep -o "timer:(keepalive[^)]*)"
timer:(keepalive,4min52sec,0)
host$ sudo tcpdump -ni eth0 'tcp and host 10.30.0.15 and tcp[tcpflags] & tcp-ack != 0 and len <= 66'
R1(config)# service tcp-keepalives-in
R1(config)# service tcp-keepalives-out
fw# show running-config timeout | include conn
On the wire, a keep-alive probe is an ACK whose sequence number is one byte below the expected one, usually with no data. RFC 9293 allows sending one garbage byte for compatibility. The peer answers with a normal ACK.
Documented example, not captured output.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Network & Protocols → TCP Keepalive Evidence. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.rfc-editor.org/rfc/rfc9293.html · confidence 94%Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.