How to Keep SSH Sessions Alive and Prevent Freezes Using TCP Keepalive Settings
This guide explains why SSH connections may close due to TCP timeouts, describes the relevant kernel parameters, and provides step‑by‑step client and server configuration instructions for Linux and Windows to keep SSH sessions active without interruption.
SSH (Secure Shell) is essential for remote server management, but idle sessions often freeze or disconnect because of TCP timeout settings.
Why does SSH close connections?
TCP timeout determines how long a connection waits for a response before considering the session failed. In Linux, parameters such as tcp_keepalive_time , tcp_keepalive_probes , and tcp_keepalive_intvl control this behavior.
Typical default values are:
% cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
% cat /proc/sys/net/ipv4/tcp_keepalive_probes
9
% cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75These defaults mean the system sends nine probes at 75‑second intervals after 7200 seconds of inactivity, causing the SSH session to terminate after roughly 11 minutes of no user input.
How to keep SSH sessions active
Linux client configuration
Create or edit ~/.ssh/config and add keepalive options:
touch ~/.ssh/config
mkdir ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/configKey options:
Host : Apply settings to all hosts using * .
ServerAliveInterval : Send a keepalive message every 120 seconds (default 0, disabled).
ServerAliveCountMax : Disconnect after 30 unanswered keepalive messages.
This results in a total of 1 hour of inactivity tolerance (120 s × 30 = 3600 s).
Windows client configuration
In PuTTY, set “Seconds between keepalives” (under the Connection tab) to a value greater than zero, e.g., 60 seconds, and save the session.
Server‑side configuration
Edit /etc/ssh/sshd_config and set:
sudo nano /etc/ssh/sshd_config TCPKeepAlive yes
ClientAliveInterval 120
ClientAliveCountMax 30These settings make the server send keepalive messages every 120 seconds and terminate after 30 missed responses, also yielding a 1‑hour inactivity window.
After editing, restart the SSH service:
sudo systemctl restart sshConclusion
Properly configuring SSH keepalive balances security—by automatically closing idle sessions—and usability—by preventing unexpected disconnects—ensuring a reliable and safe remote access environment.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.