Understanding and Configuring TCP Keepalive Settings

TCP keepalive settings control how long-idle connections are detected and maintained — important for connections through NAT, firewalls, or load balancers that might otherwise silently drop idle connections. This guide covers understanding and tuning these settings.

The Problem TCP Keepalive Solves

A TCP connection can appear "established" indefinitely from an application's perspective, but intermediate network devices (NAT gateways, stateful firewalls, load balancers) often silently drop connection state for idle connections after some timeout — the application doesn't know the connection is actually dead until it tries to use it and fails.

How TCP Keepalive Works

Periodic probe packets sent during idle periods confirm the connection is still genuinely alive — if probes go unanswered after a configured number of retries, the OS considers the connection dead and notifies the application, rather than leaving it in an ambiguous "maybe still alive" state.

Checking Current Keepalive Settings

sysctl net.ipv4.tcp_keepalive_time
sysctl net.ipv4.tcp_keepalive_intvl
sysctl net.ipv4.tcp_keepalive_probes

Understanding the Three Key Parameters

ParameterMeaningCommon Default
tcp_keepalive_timeIdle time before first probe sent7200 seconds (2 hours)
tcp_keepalive_intvlInterval between probe retries75 seconds
tcp_keepalive_probesNumber of failed probes before declaring connection dead9

Why Default Settings Are Often Too Conservative

The default 2-hour initial idle period is often longer than many NAT/firewall idle timeouts (commonly much shorter) — meaning the connection may already be silently dropped by an intermediate device long before your keepalive would even send its first probe.

Setting More Aggressive Keepalive for Long-Lived Connections

net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 4
sudo sysctl -p

A 5-minute initial idle period with more frequent, faster-failing probes detects dead connections much sooner — particularly relevant for database connections, persistent API connections, or any long-lived connection through NAT/firewall infrastructure.

Application-Level Keepalive Configuration

const client = new Client({
  keepAlive: true,
  keepAliveInitialDelayMillis: 10000,
});

Many database clients and connection libraries support their own application-level keepalive settings, sometimes independent of OS-level TCP settings — check your specific client library's documentation, since both layers can matter.

Keepalive for SSH Connections

ClientAliveInterval 60
ClientAliveCountMax 3

See related SSH configuration for keeping SSH sessions alive through network intermediaries — a common, practical application of this same underlying concept, directly configurable in sshd_config.

Balancing Keepalive Aggressiveness with Overhead

Very aggressive keepalive (frequent probes) adds a small amount of ongoing network overhead — for most use cases this is negligible, but consider the trade-off for genuinely high-connection-count scenarios where the cumulative overhead across many connections could matter.

Common Errors

Long-lived connections still dropping despite keepalive configuration — verify the setting was actually applied (check sysctl values after reboot, since /etc/sysctl.conf changes need to persist correctly), and confirm both ends of the connection have reasonable keepalive settings, not just your server.

Continue Reading

Browse more articles in Advanced Networking & VPN.

  • tcp keepalive settings, tcp_keepalive_time sysctl, prevent nat connection drop, keepalive firewall timeout
  • 0 Utilisateurs l'ont trouvée utile
Cette réponse était-elle pertinente?

Articles connexes

How to Set Up a VPN Server with WireGuard

WireGuard is a modern, fast, and simple VPN protocol — significantly easier to configure...

How to Set Up an OpenVPN Server on a VPS

OpenVPN is a mature, widely-supported VPN protocol — a solid choice when you need broad...

How to Configure a VPS as a Forward Proxy with Squid

A forward proxy routes outbound requests through your VPS, useful for accessing geo-restricted...

How to Set Up IPv6 on Your VPS

IPv6 adoption continues to grow, and many VPS providers now offer IPv6 addresses alongside IPv4....

How to Bond Multiple Network Interfaces for Redundancy

Network interface bonding combines multiple physical/virtual network interfaces into a single...