How do I stop SSH from dropping with "Broken pipe"?

Add ServerAliveInterval 30 to the Host * block of ~/.ssh/config. ssh then sends a small message every 30 seconds, so a router that forgets idle connections sees traffic, and ssh notices when the server stops answering.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

By default ssh sends nothing while you are idle. A router, a firewall or a mobile network may forget an idle connection after a minute or so, and the next key you press ends in client_loop: send disconnect: Broken pipe. Set ServerAliveInterval 30 for all hosts. ssh then sends a message every 30 seconds and the connection never looks idle.

What the defaults are

Ask ssh for the effective values of the three related options. The file for this host has none of them yet:

$ ssh -G app | grep -iE '^(serveralive|tcpkeepalive)'
tcpkeepalive yes
serveralivecountmax 3
serveraliveinterval 0

serveraliveinterval 0 means ssh never sends a keep-alive message. serveralivecountmax 3 is how many unanswered messages it tolerates once you turn the interval on. tcpkeepalive yes is a different mechanism that rarely helps in practice, as the questions below explain.

Turn it on for every host

Open ~/.ssh/config and add two lines at the end, under Host *:

Host *
    ServerAliveInterval 30
    ServerAliveCountMax 3

After 30 seconds without data from the server, ssh sends a message and expects an answer. After three unanswered messages in a row it closes the connection. A dead server is therefore noticed after about 90 seconds (30 times 3), and a router that expires idle connections after a minute never gets the chance. For one session, without touching the file, use the options on the command line. The check below shows the value ssh would use with the option on the command line:

$ ssh -o ServerAliveInterval=30 -G app | grep -iE '^serveralive'
serveralivecountmax 3
serveraliveinterval 30

The Host * block goes last because the first value wins. That rule and the rest of the file format are in the config file article.

The server can do it too

If you manage the server, it can send the messages instead of your clients. The same pair of options exists in sshd_config under the names ClientAliveInterval and ClientAliveCountMax. The effective values show with sshd -T, which needs root:

$ ssh app 'sudo sshd -T | grep -iE "^(clientalive|tcpkeepalive)"'
clientaliveinterval 0
clientalivecountmax 3
tcpkeepalive yes

clientaliveinterval 0 is the default and it means the server stays silent too. Setting it on the server covers every user and every client of that machine, but the change needs a reload of sshd and a person with root.

Watch it detect a dead connection

A connection that has gone silent is hard to produce on purpose. This command is a stand-in. It runs on the server, waits a second so the first line arrives, and stops the server’s own session process with kill -STOP. The TCP connection stays open and the server stops answering, which is how a dropped network looks from the client:

$ ssh -o ServerAliveInterval=1 -o ServerAliveCountMax=2 app 'echo connected; sleep 1; kill -STOP $PPID; sleep 60'; echo "exit status: $?"
connected
Timeout, server app.example.test not responding.
exit status: 255

ssh printed connected, then after about three seconds (1 times 2, plus the first wait) it gave up. The exit status 255 is the one ssh uses for its own errors. Without keep-alive the same command would hang until you pressed a key, and the next key press would bring the broken pipe.

Mistakes people make

The interval is longer than the timeout of the device in between. If something drops idle connections after 60 seconds, an interval of 120 does nothing. Thirty seconds is a safe start.

The count is too small on a bad network. With an interval of 5 and a count of 1, a stall of ten seconds on a mobile connection ends your session. The tolerance is the interval times the count, so raise the count and keep the interval short.

You expect the commands to survive. Keep-alive stops idle drops. When the link really breaks, the shell on the server receives a hangup and ends what it started. Run long jobs in tmux, screen or with nohup.

You set it in one terminal window only. Options in ~/.ssh/config apply to every new connection, to scp and rsync as well. A -o flag applies to that one command. Tunnels from the port forwarding article are the usual place to need the file, since they stay open for hours. Servers behind a bastion, as in the jump host article, get it from the same Host * block.

You rely on TCPKeepAlive. It is on by default, but Linux sends the first probe after two hours of silence, far longer than any firewall waits.

In Valakes

Valakes does not use your ssh settings. It sends its own probe every 15 seconds by default and counts a connection as lost after three missed probes, so a silent drop shows up within about 45 seconds. The interval and the number are in Settings, under SSH.

Questions

What is the difference between ServerAliveInterval and TCPKeepAlive?

ServerAliveInterval sends an encrypted message inside the SSH connection, on a timer you set. TCPKeepAlive uses the TCP probes of the operating system, which are not encrypted and on Linux start only after two hours by default.

Where do I write it so it applies to every server?

In a Host * block at the end of ~/.ssh/config. A value in an earlier block for one server still wins for that server.

Does keep-alive save my running commands when the network really goes down?

No. When the connection closes, the shell on the server gets a hangup and ends its commands. Start long jobs in tmux or screen, or with nohup, so they survive.

Why does ssh say "Timeout, server not responding"?

The server did not answer ServerAliveCountMax messages in a row, each sent after ServerAliveInterval seconds of silence. ssh gave up and closed the connection.

References

The app is free.

Get notified at launch

macOS is coming to the Mac App Store. iOS and Android later.