Auto Disconnect Idle SSH Connections in Linux
In this article
Yes, you can auto-disconnect idle or inactive SSH connections in Linux by setting the ClientAliveInterval and ClientAliveCountMax options in the SSH server configuration, or by using ServerAliveInterval on the client side. These settings send keep-alive messages and terminate the connection if no response is received, effectively closing sessions that are no longer active.
Why SSH Connections Stay Open
By default, an SSH session remains open indefinitely as long as the network link is up. This is by design—SSH is meant to be a persistent, reliable remote shell. However, this behavior becomes a problem in real-world environments:
- Idle users forget to log out, leaving sessions that consume server resources (processes, PIDs, memory).
- Network interruptions (laptop sleep, Wi-Fi drops) can leave half-dead connections that the server still considers active, holding ports and file descriptors.
- Security policies often require sessions to close after a period of inactivity to reduce the risk of unattended terminals being accessed.
- Load balancers and firewalls may drop idle TCP connections after a timeout, causing the SSH client to hang indefinitely without any notification.
Without an auto-disconnect mechanism, both client and server can be left with stale connections that waste resources and frustrate users who reconnect only to find a frozen session.
How SSH Keep-Alive and Timeout Work
SSH has built-in mechanisms to detect dead connections and enforce timeouts. The two most relevant settings are:
ClientAliveInterval(server side) – The server sends an encrypted message to the client after this many seconds of inactivity. If the client is alive, it responds. This is not a “keep-alive” that prevents disconnection; it’s a liveness probe.ClientAliveCountMax(server side) – The number of consecutive missed responses before the server disconnects the client. The total timeout is roughlyClientAliveInterval × ClientAliveCountMaxseconds.ServerAliveInterval(client side) – The client sends a request to the server after this many seconds of inactivity, and waits for a response.ServerAliveCountMax(client side) – The number of missed server responses before the client gives up and disconnects.
For example, if you set ClientAliveInterval 300 and ClientAliveCountMax 2, the server will send a probe after 5 minutes of inactivity, and if it gets no response for two consecutive probes (10 minutes total), it will close the connection. This effectively disconnects idle sessions after about 10 minutes.
Configuring the SSH Server (sshd)
To enforce idle disconnection for all users, edit the SSH daemon configuration file, usually /etc/ssh/sshd_config. You need root privileges.
- Open the file in a text editor:
sudo nano /etc/ssh/sshd_config - Add or modify these lines (uncomment if they exist):
ClientAliveInterval 300ClientAliveCountMax 2 - Save the file and restart the SSH service:
sudo systemctl restart sshd(orsudo service ssh restarton older systems).
After restart, new connections will use these settings. Existing sessions are not affected until they reconnect.
Configuring the SSH Client
If you cannot change the server (for example, you connect to a third-party service), you can set the client to disconnect itself after the server stops responding. Edit your local SSH client configuration at ~/.ssh/config (create it if it doesn’t exist).
- Add a block for a specific host or use
Host *for all connections:
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
- Save the file. The next SSH connection will send a keep-alive every 60 seconds and terminate if 3 consecutive probes go unanswered (roughly 3 minutes).
You can also pass these options on the command line: ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@host
Choosing the Right Timeout Values
There is no one-size-fits-all timeout. The right values depend on your use case:
- Short timeouts (1–5 minutes) – Good for high-security environments, shared servers, or when you want to free resources quickly. But they can be annoying if you often pause to think or read output.
- Medium timeouts (10–30 minutes) – A common balance for most users. Long enough to avoid disconnecting during brief pauses, short enough to clean up forgotten sessions.
- Long timeouts (1 hour+) – Suitable for long-running tasks or when you trust the user. However, they leave stale sessions around longer.
Also consider the network environment. If you’re on an unstable connection, a shorter ServerAliveInterval (like 15–30 seconds) on the client will detect drops faster, but it generates more traffic. For most purposes, 60 seconds is a safe default.
Common Problems and Misconceptions
“ClientAliveInterval keeps my connection alive, so it never disconnects.” This is a common misunderstanding. The setting does not prevent disconnection; it actively checks whether the connection is still alive. If the client responds, the session stays open. If not, it disconnects. It’s a liveness probe, not a keep-alive that prevents timeouts.
“I set ClientAliveInterval but it doesn’t work.” Make sure you edited sshd_config (not ssh_config), and that you restarted the SSH daemon. Also, some distributions use sshd_config.d/ drop-in files that override the main file—check those. Finally, verify with sshd -T | grep clientalive to see the effective values.
“Idle disconnection kills my long-running processes.” If you have a process that should continue after you disconnect, use nohup, screen, or tmux. These tools detach the process from the terminal, so it survives the SSH session ending. Auto-disconnect only terminates the shell session, not the underlying process—unless it’s tied to the terminal.
“I want to disconnect only specific users.” You can use a Match User block in sshd_config to apply different settings to different users or groups. For example:
Match User bob
ClientAliveInterval 60
ClientAliveCountMax 1
This applies only to the user bob.
Alternatives to SSH Timeouts
If you need more control or don’t want to change global settings, consider these alternatives:
- tmux or screen – Run your session inside a terminal multiplexer. You can detach and reattach, and the session persists even if the SSH connection drops. This is the standard solution for long-running tasks.
- Idle timeout in shell profile – Set
TMOUTin~/.bashrcor/etc/profile. This is a bash feature that automatically logs out an interactive shell after a period of inactivity. For example,TMOUT=600logs out after 10 minutes. It works only for bash and doesn’t require SSH settings. - Connection multiplexing – OpenSSH’s
ControlMasterfeature reuses a single TCP connection for multiple sessions, which can reduce the number of idle connections. It doesn’t auto-disconnect, but it makes managing multiple sessions easier. - Fail2ban or monitoring – These tools don’t disconnect idle sessions, but they can detect and block suspicious activity. Not a direct alternative, but useful for security.
Frequently Asked Questions
What is the difference between ClientAliveInterval and ServerAliveInterval?
ClientAliveInterval is set on the SSH server (sshd) and sends probes to the client. ServerAliveInterval is set on the SSH client and sends probes to the server. Both serve the same purpose: detect dead connections and disconnect after a timeout. You can use either, or both, depending on which side you control.
Will these settings disconnect an active session that is just not typing?
No. The probes are sent only when there is no traffic on the connection. If you are running a command that produces output, or if you are typing, the connection is considered active, and the timer resets. The timeout only applies to periods of complete inactivity (no data sent or received).
How do I check the current timeout values on my server?
Run sshd -T | grep -i clientalive on the server to see the effective values. On the client, you can check the configuration with ssh -G host | grep -i serveralive (replace host with the actual hostname).
Can I set a timeout for only one SSH session without changing global config?
Yes. On the client, use the -o option when connecting: ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=2 user@host. This overrides the config for that session only. On the server, you can’t set per-session timeouts from the client side, but you can use Match User or Match Address to apply specific settings.
What happens to my running processes when the SSH connection is auto-disconnected?
When the SSH session ends, the shell receives a SIGHUP (hangup) signal, which typically terminates the shell and its child processes. To keep processes running, use nohup, screen, or tmux to detach them from the terminal. They will continue running even after the SSH session closes.
Does setting a short timeout affect file transfers or long commands?
No, because the timeout only applies to idle periods. If you are transferring a file or running a command that produces output, the connection is active and won’t be disconnected. However, if a long command produces no output for a while (e.g., a silent build), the server may consider it idle and disconnect. In that case, increase the timeout or use a terminal multiplexer.