TCP Keepalive: Detecting Dead Peers and Keeping NAT Mappings Alive
How TCP keepalive probes an idle connection to find out whether the peer is still there: the idle, interval and count settings, what happens when probes are answered, unanswered or reset, and how keepalives stop NAT devices from silently dropping idle connections, measured in the lab.
kvi5hnuCode Sprout team member11 min readOn this page
An idle TCP connection sends nothing at all. That's efficient, but it means neither end notices if the other disappears: a crashed server, a laptop that left the Wi-Fi, a NAT device that dropped its mapping. As article 65 showed, a connection only detects a dead peer by retransmitting, and an idle connection has nothing to retransmit. It can stay "established" forever while going nowhere.
Keepalive fixes that by sending a small probe after a period of silence. It isn't part of the core TCP specification: RFC 1122 and RFC 9293 allow implementations to include it, but it must be off by default and enabled per connection, and the default idle time must be at least two hours.
The settings
A socket enables keepalive with SO_KEEPALIVE. Three values control it, system-wide via sysctl or per socket via options:
| Per-socket option | sysctl | Linux default | Meaning |
|---|---|---|---|
TCP_KEEPIDLE | net.ipv4.tcp_keepalive_time | 7,200 s | Idle time before the first probe |
TCP_KEEPINTVL | net.ipv4.tcp_keepalive_intvl | 75 s | Time between unanswered probes |
TCP_KEEPCNT | net.ipv4.tcp_keepalive_probes | 9 | Unanswered probes before giving up |
The lab's defaults:
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_intvl = 75
net.ipv4.tcp_keepalive_probes = 9
The timeline below shows how the three values combine.

With the defaults, a dead peer is detected after 7,200 + 9 × 75 = 7,875 seconds, about 2 hours and 11 minutes. That's far too slow for most applications, so programs that care set their own values per socket.
Probes, answered and unanswered
In the lab, a client connected to a server and then sent nothing, with keepalive set to an idle time of 2 seconds, an interval of 1 second and a count of 3. After about 7 seconds, the router started dropping everything from the server:
2.01 s client seq=0 ack=1 len=0 flags=·······A····
2.03 s server seq=1 ack=1 len=0 flags=·······A····
4.05 s client seq=0 ack=1 len=0 flags=·······A····
4.08 s server seq=1 ack=1 len=0 flags=·······A····
6.10 s client seq=0 ack=1 len=0 flags=·······A····
6.13 s server seq=1 ack=1 len=0 flags=·······A····
8.15 s client seq=0 ack=1 len=0 flags=·······A····
9.18 s client seq=0 ack=1 len=0 flags=·······A····
10.20 s client seq=0 ack=1 len=0 flags=·······A····
11.22 s client seq=1 ack=1 len=0 flags=·······A·R··
client: recv() failed: [Errno 110] Connection timed out after 11.2 s
The chart below draws the same probes and replies on a time axis.

Reading the capture:
- A probe carries no data. Linux sends a segment with a sequence number one below the next byte (relative seq 0 here, while the next byte is 1). The receiver must answer an out-of-window segment with an ACK, so a live peer always replies, and no application data is involved. Some older stacks send one garbage byte instead, for peers that don't answer empty segments.
- Answered probes restart the idle timer. Each reply was followed by 2 seconds of silence, then the next probe.
- Unanswered probes repeat every interval. After the replies stopped, probes went out 1 second apart.
- After the last probe went unanswered for one more interval, the connection was declared dead. The client sent a reset (in case the peer was alive and reachable in one direction) and
recv()failed withETIMEDOUT, about 5 seconds (2 + 3 × 1) after the last reply.
While waiting, ss -o shows the keepalive timer:
timer:(keepalive,640ms,0)
A peer that lost the connection
If the peer is up but no longer knows the connection, for example because it rebooted, the probe gets a reset in reply. The lab made the server discard its connection silently (as in article 62) and waited for the client's first probe:
client: recv() failed: [Errno 104] Connection reset by peer after 3.0 s
3.01 s client seq=0 ack=1 len=0 flags=·······A····
3.04 s server seq=1 ack=0 len=0 flags=·········R··
The first probe, 3 seconds after the last activity, was answered with a reset, and the client's recv() failed with ECONNRESET at once. That's the case keepalive handles best: a server that crashed and came back, while its clients sat idle.
Keepalives and NAT
The more common reason to enable keepalives today is the middle of the path. A NAT device or stateful firewall keeps a mapping for each connection and drops it after a period of inactivity. Once the mapping is gone, packets for that connection can't be translated, and the connection is broken without either end knowing. RFC 5382 asks NATs to keep established TCP mappings for at least 2 hours and 4 minutes, but many home routers, firewalls and cloud NAT services use far shorter timeouts.
In the lab, the router masqueraded the client's address (rewrote it to the router's own public address, the common home-router form of NAT, article 35), and the idle timeout of its connection tracking table, the kernel's list of NAT mappings, was cut to 5 seconds for established connections:
net.netfilter.nf_conntrack_tcp_timeout_established = 5
A server then pushed a message to the client after 9 seconds of silence, once with no keepalive and once with keepalive probes every 2 seconds:
keepalive off : server push after 9 s idle: nothing received within 13 s
keepalive every 2 s: server push after 9 s idle: received b'update' at 9.0 s
Without keepalive, the push never arrived. The capture next to the server shows why:
9.03 s 198.51.100.10 -> 198.51.100.1 len=6 flags=·······AP···
9.05 s 198.51.100.1 -> 198.51.100.10 len=0 flags=·········R··
13.05 s 192.0.2.10 -> 198.51.100.10 len=0 flags=·······A···F
13.05 s 198.51.100.10 -> 192.0.2.10 len=0 flags=·········R··
The server's message was addressed to the router's public address, but the router no longer had a mapping, so it answered with a reset itself. The server's connection was gone, while the client's still looked established. When the client finally closed, its FIN was no longer translated, and the server answered that with a reset too. With keepalive every 2 seconds, the mapping never went idle long enough to expire, and the push arrived on time.
This is why long-lived connections that may sit idle, such as push notification channels, database connection pools, SSH sessions and message broker clients, enable keepalives, either TCP's or an application-level heartbeat, with an interval shorter than the NAT and firewall timeouts on the path.
TCP keepalive or application heartbeats?
Many protocols have their own heartbeats: HTTP/2 PING frames, QUIC PING frames (QUIC runs over UDP, so TCP keepalive can't help it; it has its own idle timeout, article 86), WebSocket ping frames, SSH's ServerAliveInterval, and similar messages in message brokers and databases. They have advantages:
- They check the application, not just the kernel. A peer whose process is hung still answers TCP keepalives.
- They're visible to the application, which can measure latency and decide what to do.
- They work through proxies, which terminate TCP and would answer keepalives themselves.
TCP keepalive costs nothing in the application and needs no protocol support, so it's a good baseline. Many systems use both: TCP keepalive for the NAT mappings and dead hosts, and application heartbeats for liveness.
Two interactions to remember on Linux:
- Keepalive probes are sent only on an idle connection. If data is unacknowledged, the retransmission limits apply instead (
tcp_retries2orTCP_USER_TIMEOUT, article 65). - If
TCP_USER_TIMEOUTis set, it also limits keepalive: the connection is declared dead once nothing has been heard from the peer for that long, even if fewer thanTCP_KEEPCNTprobes have been sent.
Common questions
Why is keepalive off by default?
Probes cost bandwidth and can keep metered or battery-powered links awake, and a short outage that would have healed silently can cause a keepalive failure that kills a perfectly good connection. RFC 1122 therefore makes it opt-in, with a long default.
Does keepalive detect a dead peer while data is outstanding?
No. Keepalive probes are sent only when the connection is idle. With unacknowledged data, the connection is retransmitting, and the retransmission limits decide when it fails.
Can a keepalive keep a dead connection alive?
Only if something answers the probes. A transparent proxy or load balancer that terminates TCP will answer for the server. That's one reason to use application-level heartbeats for end-to-end liveness.
How do I change keepalive for a program I can't modify?
The system-wide sysctls apply to every socket that enables SO_KEEPALIVE, but they can't turn keepalive on for sockets that don't. Some runtimes and libraries enable it by default (Go, for example, enables keepalive on TCP connections with a 15-second period).
Is TCP keepalive the same as HTTP keep-alive?
No. HTTP keep-alive (persistent connections, the default since HTTP/1.1) means reusing one TCP connection for several requests instead of closing it after each response. It sends no probes. A server's HTTP keep-alive timeout only decides how long an idle connection is kept open before the server closes it.
What's a sensible setting for a connection through NAT?
An idle time below the smallest NAT or firewall timeout on the path, often 30 to 120 seconds, with an interval of 10 to 30 seconds and a count of 3 to 5. That detects a dead peer in a few minutes and keeps mappings alive.
Summary
- An idle TCP connection can't detect a dead peer or a lost NAT mapping, because it sends nothing.
- Keepalive sends an empty probe after
TCP_KEEPIDLEof silence, repeats everyTCP_KEEPINTVLif unanswered, and gives up afterTCP_KEEPCNTprobes. Linux's defaults (7,200 s, 75 s, 9) take over 2 hours. - In the lab, with 2 s, 1 s and 3, answered probes came every 2 seconds, and after the replies stopped, the connection failed with
ETIMEDOUTabout 5 seconds after the last reply. A peer that had lost the connection answered the first probe with a reset (ECONNRESET). - NAT devices drop idle mappings. In the lab, a 5-second NAT timeout broke a server push after 9 idle seconds, while 2-second keepalives kept it working.
- Application heartbeats check the peer application end to end. TCP keepalive is a cheap baseline for dead hosts and NAT mappings.
Practice questions
1. A socket sets TCP_KEEPIDLE to 60, TCP_KEEPINTVL to 10 and TCP_KEEPCNT to 5. The peer vanishes right after the last packet. When does the connection fail?
Answer
After 60 + 5 × 10 = 110 seconds. The first probe goes out at 60 s, then probes at 70, 80, 90 and 100 s, and the connection is declared dead when the fifth goes unanswered for another 10 seconds.
2. Why does Linux send a keepalive probe with a sequence number one below the next byte, and no data?
Answer
A segment with an old sequence number is out of the receiver's window, and TCP must answer such segments with an ACK. So a live peer always replies, and no application data is sent or acknowledged.
3. A client behind NAT keeps an idle connection to a server for push notifications. The NAT drops mappings after 5 minutes of inactivity. What happens without keepalive when the server pushes after 10 minutes, and how do you fix it?
Answer
The NAT has no mapping, so it drops the server's packets or answers with a reset, as in the lab. The client never gets the push and still thinks the connection is open. Enable keepalives (or application heartbeats) with an idle time well under 5 minutes, such as 60 seconds.
4. An application has a query outstanding on a connection whose server just lost power. Keepalive is on with 30-second settings. Will keepalive detect it?
Answer
No. The connection has unacknowledged data, so it's retransmitting, and keepalive probes aren't sent. Failure is decided by tcp_retries2 (about 15 minutes by default) or by TCP_USER_TIMEOUT if the application set it.
5. A load balancer terminates client TCP connections and opens its own to back-end servers. A back-end process hangs but its kernel is fine. Which detects the problem: TCP keepalive from the client, or an application-level heartbeat?
Answer
Only the application heartbeat. TCP keepalive from the client is answered by the load balancer, and even a keepalive to the back end is answered by its kernel, which is still running. An application heartbeat needs the back-end process itself to respond.
Review quiz
Test yourself on Part 11 (articles 64 to 71): Quiz: TCP Reliability and Flow Control (TCP/IP Part 11)
Previous: Large Windows and Buffers: Bandwidth-Delay Product, Auto-Tuning and Urgent Data
Next: Congestion Control: Slow Start, Congestion Avoidance and Fast Recovery

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.