You SSH into a server, leave the window open while you go to a meeting, and come back to a terminal that doesn’t respond. After a while an error appears and the connection is gone:
client_loop: send disconnect: Broken pipe
Or it’s Connection reset by peer, or, from older versions, packet_write_wait: Connection to 203.0.113.7 port 22: Broken pipe. The server is fine and so is your network, so why does a connection drop just because it sat idle? Here’s how connections get dropped, how to keep them up, and how to keep your work when they drop anyway.
Something in the middle forgets the connection
SSH runs over a TCP connection, and TCP has no heartbeat of its own: when neither end has anything to say, not a single packet crosses the wire, and the connection can sit like that for days.
The trouble is in between. Between your computer and the server sit your home router, the office firewall and your provider’s NAT, and in the cloud often a NAT gateway or a load balancer too. Each of them keeps a table of the connections passing through, who’s talking to whom on which port, so it knows where replies should go. The table has limited room, so an entry that sees no traffic for long enough gets thrown out. How long is long enough varies, and in the cloud it’s often minutes: 350 seconds for an AWS NAT gateway, 4 minutes by default for an Azure load balancer.
Neither end is told. When you come back and type a key, the packet reaches that device in the middle, which no longer knows the connection. Either it drops the packet without a word, so the terminal freezes until your system gives up resending, or it sends back a reset (RST) and ssh fails at once. The errors above all say the same thing: the connection died in the middle a while ago, and your computer only found out when it sent something.
Two other kinds of disconnect have nothing to do with the middle:
- The server logs idle users out. The shell’s
TMOUTvariable ends the session after a set time, withtimed out waiting for input: auto-logout. An administrator set that on purpose, and the keepalives below won’t get around it. - You changed networks. Moving from the office Wi-Fi to a phone hotspot, or a VPN reconnecting, gives your computer a new address, and a TCP connection can’t move with it. All you can do is reconnect.
Keepalives: say something every so often
If connections are forgotten for being quiet too long, the fix is to say something every so often. SSH has this built in: every so often the client asks the server “still there?” inside the encrypted connection, and the server answers. That exchange keeps the devices in the middle remembering the connection, and if several questions in a row go unanswered, ssh stops waiting and disconnects, saying why.
Add this to ~/.ssh/config on your computer (C:\Users\you\.ssh\config on Windows):
Host *
ServerAliveInterval 30
ServerAliveCountMax 3
ServerAliveInterval 30 asks after 30 seconds with nothing from the server; ServerAliveCountMax 3 disconnects after three unanswered questions, so about a minute and a half after the server goes silent. Then you see:
Timeout, server 203.0.113.7 not responding.
ServerAliveInterval is 0 by default, which means never ask, and that’s why connections get forgotten so easily. Thirty seconds is shorter than nearly any device’s timeout, and each question is a few dozen bytes, so traffic isn’t a concern. To set it for one machine only, put its name in place of Host *; for a one-off, put it on the command line: ssh -o ServerAliveInterval=30 web-01. In PuTTY, the setting is “Seconds between keepalives” on the Connection page.
Setting it on the server
If you run the server, you can set it on that end instead, and it covers every client that connects. Edit /etc/ssh/sshd_config:
ClientAliveInterval 60
ClientAliveCountMax 3
These work like the client’s pair, with the server asking: after 60 seconds with nothing from the client it asks, and after three unanswered questions it disconnects. A bonus: dead connections left behind by a closed laptop lid or a pulled cable get cleaned up within minutes instead of hanging around. Reload after changing it:
sudo systemctl reload ssh # Debian, Ubuntu
sudo systemctl reload sshd # RHEL, Rocky Linux and the like
Why not TCPKeepAlive?
ssh_config and sshd_config also have TCPKeepAlive, which is on by default and sounds like the same thing. Why doesn’t it help? It’s the operating system’s own TCP keepalive, and Linux sends its first probe only after a connection has been idle for 2 hours (tcp_keepalive_time is 7200 seconds). The devices in the middle forgot the connection long before that. It also travels unencrypted and can be spoofed. ServerAliveInterval and ClientAliveInterval go through SSH’s own encrypted channel, at an interval you choose, so they’re the ones to use.
It drops when the network hiccups, too?
The opposite case: the connection isn’t idle, the network just stops for a moment, say Wi-Fi switching access points or a VPN reconnecting, and it’s back within seconds, but the connection is already gone.
While the network is down, what you send gets no acknowledgement, so the system resends it every so often, and after a certain number of tries it gives up on the connection. How soon depends on the system: Linux keeps resending for about fifteen minutes by default; Windows resends 5 times and gives up after about 20 seconds. So the same hiccup is much more likely to drop a connection on Windows.
Your keepalive settings matter here too: ServerAliveInterval times ServerAliveCountMax is how long you’ll put up with a silent server. Set them too low (asking every 5 seconds and giving up after one miss, say), and the slightest hiccup disconnects you. Thirty seconds and three tries is a safe pair.
Keep your work when it drops: tmux and mosh
Keepalives only stop a connection being forgotten. When the network really goes down or your address changes, the connection still drops, and the programs you started in that login usually end with it: a build or an import that was half done has to start over. To keep them going:
- Work inside tmux. Run
tmux new -s workon the server and run your commands in it. When the connection drops, tmux and everything in it keep running on the server; after reconnecting,tmux attach -t workbrings it all back as it was. For a single command,nohup command &lets it finish in the background. - Use mosh instead. mosh uses UDP in place of TCP and carries on through sleep, network changes and new addresses, and typing stays responsive on a bad network. The cost: the server needs mosh installed and its firewall must allow UDP ports 60000–61000, and there’s no port forwarding.
In ShellRS
ShellRS is a free, open-source SSH client, and its keepalives need no setup:
- SSH connections have keepalives built in: ShellRS asks the server every 30 seconds and gives up only after about two minutes without an answer, in terminals and SFTP alike.
- On Windows it’s about two minutes too: ShellRS gives the system longer to resend, so a VPN or Wi-Fi hiccup doesn’t end the session after the 20 or so seconds Windows allows by default.
- When a connection drops, the terminal says “Connection lost” and why, such as “The server stopped responding” or “The connection was reset”, and pressing Enter reconnects in the same tab. While connected, ⌘⇧R (Ctrl+Shift+R on Windows and Linux) disconnects and reconnects.
- A port forward that drops reconnects on its own, up to three times (after 1, 3 and 10 seconds).
More in the ShellRS manual, in the Terminal section. ShellRS is available from the download page.