Operations 8 min read

How to Prevent SSH Session Freezes: Keep Your Remote Connections Alive

Learn why SSH connections drop due to TCP timeout settings and follow step‑by‑step Linux, Windows (PuTTY), and server‑side configurations—including tcp_keepalive and ServerAlive options—to keep remote sessions active and avoid idle‑disconnects for developers.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Prevent SSH Session Freezes: Keep Your Remote Connections Alive

Why SSH Sessions Freeze

SSH (Secure Shell) is essential for remote server management and secure data transfer, but idle connections are often terminated because of TCP timeout settings. When the kernel considers a TCP connection unresponsive, it closes the session, causing lost work and frustration.

Key TCP Keepalive Parameters

Linux provides three sysctl parameters that control TCP keepalive behavior:

tcp_keepalive_time : Interval (seconds) between sending keepalive probes on an idle TCP connection.

tcp_keepalive_probes : Number of unanswered probes before the connection is considered dead.

tcp_keepalive_intvl : Interval (seconds) between successive keepalive probes.

These values can be inspected with the following commands:

% 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
75

With the default settings, the kernel sends nine probes 75 seconds apart (total 675 seconds ≈ 11 minutes). If no activity occurs, the SSH session is terminated after about 11 minutes.

Keeping the SSH Session Alive – Client Side

Linux Client Configuration

Create or edit ~/.ssh/config and add the following options:

Host *
    ServerAliveInterval 120
    ServerAliveCountMax 30

This configuration makes the client send a keepalive message every 120 seconds. After 30 unanswered messages (≈ 1 hour), the server will close the connection.

Typical commands to set up the file:

% touch ~/.ssh/config
% mkdir -p ~/.ssh
% chmod 700 ~/.ssh
% nano ~/.ssh/config

Windows Client Configuration (PuTTY)

In PuTTY, open the Connection category and set Seconds between keepalives (0 to turn off) to a value greater than zero, e.g., 60. This causes PuTTY to send a keepalive packet every minute.

Remember to save the session settings (Category → Saved Sessions → Save).

Keeping the SSH Session Alive – Server Side

Edit /etc/ssh/sshd_config and ensure the following options are present:

TCPKeepAlive yes
ClientAliveInterval 120
ClientAliveCountMax 30

TCPKeepAlive enables the kernel’s TCP keepalive messages. ClientAliveInterval defines the interval (seconds) after which the server sends a keepalive request if no data has been received. ClientAliveCountMax specifies how many unanswered keepalive requests are allowed before the server disconnects the client.

After editing, restart the SSH service:

% sudo systemctl restart ssh

Summary

Adjusting both client‑side and server‑side keepalive settings balances security and usability: idle sessions are terminated to reduce attack surface, while periodic keepalive messages prevent unintended disconnects during legitimate long‑running work.

Administrators should choose values that suit their network latency and user workflow, typically keeping the total allowed idle time around one hour.

SSH keepalive configuration example
SSH keepalive configuration example
LinuxSSHKeepalivesession timeoutPuttysshd_config
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.