TCP keepalive timeout for host services
Standard OS timeouts often fail to balance fast port release with persistence of long-lived sessions. For host services, especially over SSH or TLS, this leads to reconnection delays and socket accumulation in TIME_WAIT. Properly setting tcp_keepalive_time and tcp_fin_timeout solves both problems without losing stability.
1. Why standard timeouts don’t suit
Linux defaults set tcp_keepalive_time to 75 seconds and tcp_fin_timeout to 60 seconds. In production host services, a 75-second keepalive may trigger probes too early for long-lived SSH sessions, breaking expectations of the TCP stack. Meanwhile, a 60-second fin_timeout holds sockets in FIN_WAIT2 if the remote side delays ACKs, reducing availability of ephemeral ports. These values are calibrated for desktop use, not for stable host-to-service connections behind firewalls or NAT.
2. Configuring tcp_keepalive_time
Controls the interval of inactivity before sending the first keepalive probe. The third parameter — tcp_keepalive_intvl (interval between probes) and tcp_keepalive_probes (count of probes before break) are typically set together.
Values below 60 s are rarely useful, above 600 s risk missing dead connections. 300 s is a balanced default for most host services.
Table of keepalive parameter interaction:
| Parameter | Default | Recommended | What it controls |
|---|---|---|---|
tcp_keepalive_time | 75 s | ≥300 s | Interval of inactivity before first probe |
tcp_keepalive_intvl | 75 s | 30 s | Interval between subsequent probes |
tcp_keepalive_probes | 9 | 3 | Count of unanswered probes before disconnect |
After changes, make configuration persistent across reboots: sysctl --system or host reboot.
3. Handling tcp_fin_timeout
Determines seconds a socket sits in FIN_WAIT2 after local close before the kernel forcibly destroys it.
Aggressive reduction below 15 s may tear down ESTABLISHED connections on network restart, as FIN_WAIT2 sockets get deleted before new stack infrastructure can account for them.
Practice: keep tcp_fin_timeout at least 30 s for most services; reduce to 20–30 s only under measured port pressure and with ss -s timeout monitoring.
4. Practical recommendations
- Check current values:
sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_fin_timeout - Observe sockets:
ss -t state TIME_WAIT | wc -landss -s | grep -i timeout - For SSH bastions:
tcp_keepalive_time=300,tcp_keepalive_intvl=30,tcp_keepalive_probes=3 - For internal HTTP microservices behind a load balancer:
tcp_fin_timeout=30is enough;tcp_keepalive_timecan stay at 120 s if LB expects quick idle detection - Add settings to
/etc/sysctl.d/99-tcp-optimizations.confand apply viasysctl --systemafter deployment
5. What to avoid when configuring
- Reducing
tcp_fin_timeoutbelow 30 s without prior port load measurement - Completely disabling keepalive (
net.ipv4.tcp_keepalive_time=0) — dead connections will remain for hours until application timeout - Applying the same values to all services: a DNS resolver and a long-lived TLS session have opposite needs
- Ignoring
tcp_keepalive_intvlandtcp_keepalive_probes— changing onlytimewithout correspondingprobesmay never tear down an idle session - Assuming
tcp_fin_timeoutonly affects new connections — it also governsclose()behavior andSO_LINGERunder load
Tuning these two parameters is one of the few ways to balance fast port recovery with active session persistence without touching application logic.