Skip to content

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.

# Set time to first probe to 300 seconds (5 minutes)
sysctl -w net.ipv4.tcp_keepalive_time=300
Tip

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:

ParameterDefaultRecommendedWhat it controls
tcp_keepalive_time75 s≥300 sInterval of inactivity before first probe
tcp_keepalive_intvl75 s30 sInterval between subsequent probes
tcp_keepalive_probes93Count 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.

# Reduce timeout to 30 seconds for quick port release
sysctl -w net.ipv4.tcp_fin_timeout=30
Warning

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 -l and ss -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=30 is enough; tcp_keepalive_time can stay at 120 s if LB expects quick idle detection
  • Add settings to /etc/sysctl.d/99-tcp-optimizations.conf and apply via sysctl --system after deployment

5. What to avoid when configuring

  • Reducing tcp_fin_timeout below 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_intvl and tcp_keepalive_probes — changing only time without corresponding probes may never tear down an idle session
  • Assuming tcp_fin_timeout only affects new connections — it also governs close() behavior and SO_LINGER under load

Tuning these two parameters is one of the few ways to balance fast port recovery with active session persistence without touching application logic.