An SSH session that works while you’re typing but drops after sitting idle may be hitting a NAT or firewall timeout. A client-side keepalive is a sensible first test—but it won’t preserve your shell if the server closes it or your network connection disappears.
For OpenSSH, try sending a keepalive request every 60 seconds and allowing three unanswered requests:
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@server.example.com
If that helps, save the settings for the affected host. If it doesn’t, avoid increasing numbers at random: first identify whether the connection is failing or an administrative policy is ending the session.
What a keepalive does—and doesn’t do
OpenSSH’s ServerAliveInterval makes the client send a request through the encrypted SSH connection after it has received no data from the server for the specified interval. The server replies. That traffic can keep an otherwise healthy connection from appearing idle to network equipment, and it gives the client a way to notice that the remote endpoint is no longer responding.
It cannot revive a TCP connection that has already been destroyed. It also can’t keep a laptop’s network alive through sleep or roaming, prevent a server reboot, or override a server’s shell timeout. Keepalive is about checking and maintaining a working connection—not saving the state of your work.
The two options have different jobs:
-
ServerAliveIntervalsets how often the client sends a request when no server data has arrived. -
ServerAliveCountMaxsets how many unanswered requests the client tolerates before disconnecting.
With an interval of 60 and a count of 3, an unresponsive connection may be closed after roughly three minutes. That is not a guarantee the session will stay open for that long: if replies stop arriving, the client can decide the connection is dead.
Configure one host, then verify it
OpenSSH reads client settings from ~/.ssh/config on Linux and macOS. Windows OpenSSH uses %USERPROFILE%\.ssh\config. Add a host block such as:
Host my-server
HostName server.example.com
User deploy
ServerAliveInterval 60
ServerAliveCountMax 3
Replace the example hostname and username with your own. Connect using the alias with ssh my-server. For other ways to organize host-specific settings, see these practical OpenSSH config examples.
Start with a single host rather than applying keepalives globally. If you do use a Host * block, put more specific host blocks above it: OpenSSH generally uses the first value it reads for an option. A broad setting can affect connections that don’t need it.
Check what OpenSSH will actually use before troubleshooting the server:
ssh -G my-server | grep -E '^(hostname|user|serveraliveinterval|serveralivecountmax) '
On Windows PowerShell, use:
ssh -G my-server | Select-String '^(hostname|user|serveraliveinterval|serveralivecountmax) '
ssh -G prints the effective client configuration without connecting. If the values aren’t what you expected, check the host alias, block order, included config files, and command-line options. Editing the client file does not require restarting the server’s SSH daemon.
Keep client and server settings separate
ServerAliveInterval belongs in the client’s OpenSSH configuration. The similarly named ClientAliveInterval belongs in the server’s sshd_config; it lets the server probe an unresponsive client. Server administrators may also set ClientAliveCountMax to control how many unanswered probes are tolerated.
If you administer the server, inspect its effective settings before changing them:
sudo sshd -T | grep -E '^(clientaliveinterval|clientalivecountmax|channeltimeout) '
sudo sshd -t
The first command prints parsed settings, and the second checks configuration syntax. Match rules can make effective settings depend on the connecting user or address. Be cautious with production changes: keep an existing session open and make sure you have another way to access the machine. Don’t change a server-wide policy just to address a problem affecting one client.
A server can also close an idle shell through an inactivity policy, including ChannelTimeout on supported OpenSSH versions. Keepalive traffic does not necessarily count as activity inside the shell. If a session ends at a consistent interval even while the connection is healthy, ask the administrator about server policies and logs.
Know when you need persistence instead
If the SSH connection drops and a running command disappears with it, keepalive isn’t the right tool to preserve that work. On a server where it’s available and permitted, use tmux:
tmux new -s maintenance
Run your work inside the session. To detach without stopping it, press Ctrl+B, release both keys, then press D. After reconnecting, list and reattach to the session:
tmux ls
tmux attach -t maintenance
tmux keeps its session and processes running on the server while that server remains available. It doesn’t make the SSH connection unbreakable or survive every reboot. On managed or HPC systems, follow the site’s rules for long-running processes and job schedulers.
A quick way to narrow down the problem
Try the client-side keepalive on one host and compare the result on the same network. If the session still closes, note whether it happens after a consistent idle period, after the computer sleeps, or during a network or VPN change.
If SSH never connects in the first place, keepalive settings won’t help: they apply after a connection is established. For that separate failure, use this guide to diagnosing SSH connection refusals.
For an idle disconnect, begin with ServerAliveInterval 60 and ServerAliveCountMax 3, then verify the effective values with ssh -G. If the session still ends, investigate server policy or network changes. Use tmux when the goal is to preserve work across a disconnect.
I originally published a more detailed version of this guide on the ShadeTerm blog.
I'm also building ShadeTerm — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.
Top comments (0)