How to Keep SSH Sessions Alive and Prevent Timeout
This tutorial explains why SSH connections close, shows how to configure both client and server settings—including creating the .ssh/config file, setting ServerAliveInterval, ServerAliveCountMax, and adjusting /etc/ssh/sshd_config—to keep sessions alive until you intentionally close the terminal.
Overview
Many users want their SSH sessions to stay connected so that applications keep running or to avoid the frustration of re‑connecting after a period of inactivity. This guide shows how to prevent SSH session timeout until the terminal window is closed.
Why Does SSH Close?
The SSH daemon (sshd) on the remote server will terminate a connection if the client does not send any data for a configured period. To keep the connection alive, you can adjust settings on either the client or the server side.
Configuration Files
Both client and server have configuration files that can be edited to control keep‑alive behavior.
Client Configuration
The client file is $HOME/.ssh/config. If the file does not exist, create it:
mkdir $HOME/.ssh touch $HOME/.ssh/config chmod 600 $HOME/.ssh/configEdit the file with your preferred editor (e.g., nano or vim) and add a host block:
Host example Hostname example.com ServerAliveInterval 240This setting sends a keep‑alive packet every 240 seconds, but only for connections to example . To apply the rule to all hosts, use:
Host * ServerAliveInterval 240Optionally, limit the number of unanswered keep‑alive messages with ServerAliveCountMax:
Host * ServerAliveInterval 240 ServerAliveCountMax 2With this configuration the client will close the session after two consecutive unanswered intervals.
Server Configuration
The server file is /etc/ssh/sshd_config. Add the following lines (note the use of ClientAlive rather than ServerAlive):
ClientAliveInterval 60 ClientAliveCountMax 2 ClientAliveIntervalis specified in seconds; if the server receives no data from the client within this period, it sends a keep‑alive request. If the client fails to respond ClientAliveCountMax times, the server terminates the session.
Why Not Disable Timeouts Entirely?
Keeping an SSH session permanently open can be risky. On self‑hosted machines it may be acceptable, but on cloud platforms (e.g., AWS) idle sessions can incur charges because many providers bill per minute of server usage. Setting reasonable timeouts helps avoid unnecessary costs.
Conclusion
The article demonstrates how to create and edit SSH configuration files on both client and server, shows which parameters prevent session timeout ( ServerAliveInterval, ServerAliveCountMax, ClientAliveInterval, ClientAliveCountMax), and explains why it is generally better to use timeouts rather than keeping sessions alive indefinitely.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
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.
