If SSH works until you leave it idle, try ServerAliveInterval 60 on your computer first. It sends an encrypted SSH keepalive request after a period without server data, which can prevent a NAT or firewall from dropping an otherwise healthy idle connection.
This guide covers OpenSSH on Windows, macOS and Linux, PuTTY and MobaXterm, server-side settings, and the difference between keeping a connection open and preserving work after a disconnect. We also include two small, reproducible OpenSSH tests instead of relying on configuration examples alone.
1. Quick reference: choose the right keepalive setting
The similarly named options belong on different machines. For most personal idle-disconnect issues, start with the client-side setting rather than modifying a production SSH server.
| Setting | Where it goes | Purpose |
|---|---|---|
ServerAliveInterval 60 | Local ~/.ssh/config | Client asks the server for a response after an interval without server data. |
ServerAliveCountMax 3 | Local ~/.ssh/config | Client ends an unresponsive connection after roughly three missed probes. |
ClientAliveInterval 60 | Server sshd_config | Server probes an otherwise silent client. |
ClientAliveCountMax 3 | Server sshd_config | Server limits unanswered client-alive requests. |
TCPKeepAlive | Either SSH endpoint | OS-level TCP probes; separate from SSH protocol keepalives. |
ChannelTimeout | Server sshd_config | Can explicitly expire idle SSH sessions or channels; not an SSH keepalive setting. |
If you need a shell or long-running job to survive disconnection, use tmux or screen in addition to keepalive.
2. Configure ServerAliveInterval on Windows, Linux or macOS
A permanent setting for one server
Edit the client config file on the computer you connect from. OpenSSH uses the same option names on Windows, macOS and Linux.
| Client | Local config path |
|---|---|
| Linux / macOS OpenSSH | ~/.ssh/config |
| Windows OpenSSH | %USERPROFILE%\.ssh\config (Windows environment path) |
Windows PowerShell: open the file with notepad "$HOME\.ssh\config" after creating the .ssh folder if needed. On Linux/macOS, use your preferred text editor.
Host my-server
HostName server.example.com
User deploy
ServerAliveInterval 60
ServerAliveCountMax 3
Replace the hostname and account. New connections using ssh my-server pick up this client-side config; you do not have to restart sshd.
Apply to all hosts (optional)
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
Put any more specific Host blocks above a catch-all Host *: OpenSSH normally uses the first value it reads for each option. Prefer a single host when only one connection needs help. See our SSH config examples for multiple-server layouts.
What do 60 and 3 actually mean?
The client requests a response when it has not received server data for 60 seconds. If the path stops responding, three unanswered probes lead to a disconnect after approximately 180 seconds, not a guarantee that the connection will live for three minutes. A working connection keeps getting new replies.
Distribution detail: the usual upstream ServerAliveInterval default is 0. Debian-derived OpenSSH packages may instead default to 300 when BatchMode=yes. Check the effective settings rather than assuming one default applies everywhere.
3. Verify the effective client settings with ssh -G
Editing a config file is not proof that OpenSSH read the block you intended. ssh -G prints the effective configuration without connecting to a server. We checked this ourselves using a temporary Windows OpenSSH configuration, so no production SSH settings needed to be modified.
PowerShell
ssh -G my-server | Select-String '^(hostname|user|port|serveraliveinterval|serveralivecountmax) '
Linux / macOS
ssh -G my-server | grep -E '^(hostname|user|port|serveraliveinterval|serveralivecountmax) '
Check the actual serveraliveinterval and serveralivecountmax values. If the output differs, examine the host alias, Host block order, Include directives or command-line overrides.

What we verified: the local OpenSSH configuration resolved to serveraliveinterval 60 and serveralivecountmax 3. This confirms which options the client would apply; it does not establish that a router or firewall caused a real-world disconnect.
4. How SSH keepalive works
An SSH session runs over TCP. NAT gateways and firewalls often maintain state for active connections and may expire an entry if they see no traffic for a configured period. ServerAliveInterval makes an OpenSSH client periodically send a request inside the encrypted SSH connection and wait for a reply.
The setting can keep a working but idle path active; it also helps a client detect when the remote endpoint has become unreachable. It does not send keystrokes or restart the shell, and it is not guaranteed to stop every type of server idle policy.
For unreliable connectivity, keepalives also have a trade-off: probing during a brief outage can expose a broken connection sooner and lead to termination even if the network later recovers. Test a moderate interval such as 60 seconds before making it aggressive.
5. Test a quiet SSH session and inspect debug output
To check the behavior rather than relying only on documentation, we used a disposable Ubuntu 24.04 Docker SSH environment and an authenticated client session. We shortened the interval to five seconds and ran a quiet remote command so the SSH protocol traffic could be inspected without waiting several minutes. You can reproduce the same test on a server you control:
ssh -vvv -o ServerAliveInterval=5 -o ServerAliveCountMax=3 user@TEST_HOST 'sleep 18'
The verbose trace lets us inspect keepalive activity during the quiet command. In the OpenSSH implementation used for the test, a keepalive can appear as send packet: type 80 (an SSH global request) followed by receive packet: type 82 (an SSH request-failure reply, which is normal for this keepalive mechanism). These packet numbers are not unique to keepalive; interpret them alongside the command, timing and connection state.

What this test shows — and what it does not: it illustrates SSH keepalive requests and responses on an established, authenticated connection. It does not reproduce an ISP idle timeout or prove that any particular firewall will keep the connection open. To assess a real disconnect, compare sessions with and without keepalive on the same network and record how long each remains idle.
6. Server-side ClientAliveInterval and ClientAliveCountMax
These options belong to sshd on the remote server, not your laptop's SSH client file. They can help the server detect clients that have become unresponsive and can generate traffic while the SSH connection is idle.
# Example server-side settings in sshd_config
ClientAliveInterval 60
ClientAliveCountMax 3
For an administrator, inspect the effective values first:
sudo sshd -T | grep -E '^(clientaliveinterval|clientalivecountmax|channeltimeout) '
sudo sshd -t
sshd -T reports the parsed configuration (possibly without user-specific Match overrides), while sshd -t validates syntax. On systems with Match rules, effective values can depend on the connecting user and address; consult our sshd_config guide before changing them.
Safety: test changes in a disposable container or with the provider's console available, keep an existing session open, validate config first, and reload only the correct service after reviewing its effective policy. Do not restart a production server to solve a client-side NAT issue.
7. Why keepalive cannot override ChannelTimeout or security policy
Some servers enforce inactivity limits independently of the network connection. In newer OpenSSH versions, ChannelTimeout can close inactive SSH channels, including an interactive shell. A healthy keepalive exchange does not automatically count as activity inside that shell.
Other systems enforce logout through shell TMOUT, enterprise access policies or an HPC scheduler. The right fix is to check the applicable policy with the administrator—not to increase ServerAliveCountMax indefinitely.
# Only on a server you administer
sudo sshd -T | grep -E '^(channeltimeout|unusedconnectiontimeout) '
sudo journalctl -u ssh -u sshd -n 60 --no-pager
The output depends on your OpenSSH version and service name. If a connection dies at exactly the same idle duration even with keepalive enabled, record the timing and check server logs or provider rules.
8. PuTTY and MobaXterm: keepalive without editing ssh_config
PuTTY on Windows
Open the PuTTY configuration window. Under Connection, set Seconds between keepalives to 60. Return to Session, select the saved session and save changes. This is PuTTY's application-level keepalive setting; it is not the same control as its separate TCP keepalive checkbox.
MobaXterm
For an SSH session, open the session's SSH settings and enable its SSH keepalive option. Depending on version, the interval or related control may be presented differently. The HKUST HPC instructions recommend 60 seconds for PuTTY and MobaXterm.
Windows OpenSSH uses the CLI and $HOME\.ssh\config; the PuTTY and MobaXterm GUI clients maintain their own SSH settings. Check the client actually being used.
9. TCPKeepAlive vs ServerAliveInterval
ServerAliveInterval sends SSH-protocol requests over the authenticated, encrypted connection. TCPKeepAlive asks the operating system to use TCP keepalive packets. TCP keepalives may use much longer OS defaults and do not offer the same SSH-level authentication guarantees.
| Mechanism | Layer | Best use |
|---|---|---|
ServerAliveInterval | OpenSSH client / encrypted SSH | First setting to try for idle network timeouts. |
ClientAliveInterval | OpenSSH server / encrypted SSH | Server-side dead-client detection; administered on the server. |
TCPKeepAlive | Operating-system TCP stack | Lower-level detection of half-open TCP connections. |
Leave TCPKeepAlive at your OpenSSH implementation's default unless you have a diagnosed reason to change it. See the OpenSSH client manual for the exact supported values in your version.
10. Why SSH sessions freeze or disconnect after inactivity
The symptom often narrows down the cause better than a generic keepalive command. Try the setting once; if the problem remains, use this table instead of blindly raising the interval or probe count.
| Symptom | Likely branch | What to check |
|---|---|---|
| Works during typing, drops after 10–30 min idle | NAT/firewall idle state timeout | Client keepalive at 60 seconds; retest on same network. |
| Frozen terminal, cursor stops responding | Half-open TCP path, Wi-Fi/VPN change | Compare networks; look for Broken pipe or reset after typing. |
| Disconnected on a fixed schedule despite traffic | Administrative timeout or hard session limit | ChannelTimeout, organization policy and server logs. |
| Disconnected when laptop sleeps | Client suspended / network changed | Keepalive cannot preserve a TCP socket through every sleep or roaming event. |
| SSH never connects in the first place | Port/IP/firewall/routing problem | See SSH connection timed out. |
| Remote job dies on disconnect | Job tied to terminal session | Use tmux, screen, or a suitable job manager. |
Try to record the exact disconnect time and whether it occurs on home Wi-Fi, mobile hotspot and VPN. One observation over two controlled attempts is more useful than changing five configuration settings at once.
11. What SSH keepalive cannot fix
A keepalive packet cannot recreate a TCP session after the connection has been destroyed. It does not restore work when a laptop sleeps, a server reboots, a VPN changes the network path or the server deliberately terminates your shell.
Also, keepalive is not session persistence. Reconnecting with ssh creates a new connection, and often a new shell. To preserve a terminal workspace on a server you administer, start your work in a terminal multiplexer before the disconnect.
12. Preserve a long-running shell with tmux
On a Linux server with tmux installed, start a named session:
tmux new -s maintenance
Run a harmless test command such as date. Press Ctrl+B, release both keys, then press D to detach without stopping the session. Later, after reconnecting:
tmux ls
tmux attach -t maintenance
tmux preserves the multiplexer and its processes on the server while the server stays alive. It does not make the underlying SSH connection unbreakable and will not survive every server reboot. For HPC clusters, follow the site's scheduler rules; the HKUST guide also recommends tmux, screen or batch jobs for long operations.
13. SSH keepalive FAQ
Is 60 seconds the best interval?
It's a practical starting point, not a universal optimum. An upstream firewall with a 30-second idle timeout might need a shorter interval, while a reliable path might not need keepalive at all. Choose the least aggressive working setting.
Will ServerAliveCountMax 120 keep a session alive for two hours?
No. The count sets how many unanswered probes are tolerated after a path stops responding; a high count can leave a dead session hanging longer. It does not guarantee two hours of connectivity.
Does adding a setting to ~/.ssh/config automatically configure ShadeTerm or PuTTY?
No assumption is safe for every application. Native OpenSSH reads ~/.ssh/config; a desktop client may interpret only some fields or have separate keepalive settings. Check the client's own documented behavior and connection options.
Should I change the server's ClientAliveInterval too?
Not initially if your own client is the only one affected. Test ServerAliveInterval locally before making server-wide changes, especially if users or remote jobs depend on the daemon.
Sources and reproducible testing
For this update, we combined the OpenSSH manuals with two reproducible checks: resolving a Windows client configuration using ssh -G, and inspecting verbose SSH traffic during an authenticated quiet session in a disposable Ubuntu Docker environment. The screenshots document those limited checks; we did not simulate a real ISP/NAT idle-timeout failure or claim that keepalive prevents every kind of disconnect.
- OpenSSH ssh_config(5) — client-alive request interval, count, configuration precedence.
- OpenSSH sshd_config(5) — ClientAlive settings and ChannelTimeout behavior.
- Debian OpenSSH ssh_config(5) — Debian-specific BatchMode default.
- PuTTY manual — application-level keepalive controls.
- Stack Overflow, Super User — client configuration and idle-session questions.
- HKUST HPC Handbook — OpenSSH, PuTTY, MobaXterm and tmux procedures.
Conclusion
For an SSH connection that works until it sits idle, start with ServerAliveInterval 60 and ServerAliveCountMax 3 on your own OpenSSH client, then verify with ssh -G. If the session still closes, check whether the path is failing or the server is imposing an idle policy. Use tmux for work that must survive the connection disappearing.
After the underlying connection is reliable, ShadeTerm brings terminal sessions, SFTP files, code editing and server monitoring into one workspace.