How to Keep SSH Sessions Alive: Prevent Timeout on Client and Server
This tutorial explains why SSH connections close, shows how to configure both client and server settings to keep sessions alive, and discusses why permanently disabling timeouts may not be advisable, especially on cloud platforms.
1. Overview
Many times we want to keep an SSH session connected to keep applications running or avoid frustration when returning to the terminal. This tutorial shows how to prevent SSH session timeout until you close the terminal window.
2. Why does SSH close the connection?
The SSH daemon (sshd) on the server must stay running. If the SSH client does not send data for a period, the server will close the connection. To prevent this, you can configure either the client or the server.
3. Configuration files
Several configuration files can be modified to keep SSH sessions alive. The steps differ for client and server configurations.
3.1 Client configuration
Client configuration file location: $HOME/.ssh/config If the file does not exist, create the .ssh directory and the file:
mkdir $HOME/.ssh touch $HOME/.ssh/configSet the file permissions so only the owner can read/write: chmod 600 $HOME/.ssh/config Edit the file with your preferred editor (e.g., nano or vim) and add the desired settings. For a specific host:
Host example</code>
<code> Hostname example.com</code>
<code> ServerAliveInterval 240The ServerAliveInterval option defines how many seconds the client waits before sending a keep‑alive signal.
To apply the setting to all hosts, use a wildcard:
Host *</code>
<code> ServerAliveInterval 240You can also limit the number of unanswered keep‑alive messages with ServerAliveCountMax:
Host *</code>
<code> ServerAliveInterval 240</code>
<code> ServerAliveCountMax 2If the client does not receive a response twice, it will close the SSH session.
3.2 Server configuration
Server‑side configuration file location: /etc/ssh/sshd_config.
Add the following lines (note the use of ClientAliveInterval instead of ServerAliveInterval):
ClientAliveInterval 60</code>
<code>ClientAliveCountMax 2 ClientAliveIntervalis specified in seconds; if the server does not receive data from the client within this interval, it sends a request for a response.
3.3 Why not set the connection to never disconnect?
Keeping an SSH session permanently open can be costly on cloud platforms that charge per minute of server usage, even when the session is idle. Setting reasonable timeouts helps avoid unnecessary charges.
4. Conclusion
We have learned how to create SSH configuration files on both client and server, which settings prevent SSH session timeout, and why it is generally better not to disable timeouts entirely.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
