You step away for a coffee, come back to your terminal, hit a key, and get client_loop: send disconnect: Broken pipe. Your SSH session to the VPS is gone, and if you were halfway through a long apt upgrade or a database import, that job may have died with it. This one's annoying but very fixable. Here's what's actually going on and how to stop it for good.

The Symptom

You'll usually see one of these after the session has been idle for a few minutes:

  • client_loop: send disconnect: Broken pipe
  • packet_write_wait: Connection to 203.0.113.10 port 22: Broken pipe
  • Connection reset by 203.0.113.10 port 22
  • The terminal just freezes, accepts no input, and eventually drops

The pattern matters. If it only happens when the session sits idle, it's almost always a timeout somewhere between you and the server. If it drops in the middle of active typing or a running command, you're looking at a different problem (network instability or the server itself), which we'll cover further down.

The Cause: Something Forgets Your Connection

An SSH session is a single long-lived TCP connection. When nobody types, no packets flow. That's fine for the client and server, but not for everything sitting in between:

  • Home and office routers doing NAT keep a table of active connections. Many cheap routers drop idle entries after 5 to 15 minutes.
  • Corporate firewalls and VPNs often have aggressive idle timeouts, sometimes as short as 60 seconds.
  • Mobile hotspots and CGNAT from Indian ISPs and mobile carriers are notorious for this. The carrier silently recycles your NAT mapping.
  • The server's own settings. A ClientAliveInterval plus a low ClientAliveCountMax, or a shell TMOUT variable, can kick idle users on purpose.

Once a device in the middle forgets the mapping, the next packet you send has nowhere to go. Your client notices the write failed, and you get "Broken pipe".

Fix 1: Send Keepalives From Your Computer (Fastest Fix)

The fix most people need is client-side. You tell your SSH client to send a tiny encrypted "still here" packet every so often, so routers see traffic and keep the connection alive.

On Linux or macOS, edit (or create) ~/.ssh/config:

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
    TCPKeepAlive yes

What these do:

  • ServerAliveInterval 60 sends a keepalive every 60 seconds if nothing else has been sent.
  • ServerAliveCountMax 3 means the client gives up after 3 missed replies, so a genuinely dead connection is closed in about 3 minutes instead of hanging forever.
  • TCPKeepAlive yes enables the lower-level TCP keepalive too. It's on by default, but being explicit doesn't hurt.

If 60 seconds still drops on a strict corporate network, try 30. Fix the file permissions if SSH complains:

chmod 600 ~/.ssh/config

Want to test without editing anything? Pass it once on the command line:

ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 root@203.0.113.10

On Windows

  • Windows Terminal / PowerShell (OpenSSH): use the same config in C:\Users\YourName\.ssh\config.
  • PuTTY: go to Connection and set Seconds between keepalives to 60. Tick Enable TCP keepalives. Then go back to Session and click Save, or the setting won't stick.
  • MobaXterm: Settings → Configuration → SSH, and enable SSH keepalive.

Fix 2: Configure Keepalives on the Server

If several people on your team connect to the same VPS, it's cleaner to set this once on the server. Edit /etc/ssh/sshd_config (or better, drop a file into /etc/ssh/sshd_config.d/ on Ubuntu 22.04+, AlmaLinux 9 and Rocky 9):

sudo nano /etc/ssh/sshd_config.d/10-keepalive.conf
ClientAliveInterval 60
ClientAliveCountMax 3
TCPKeepAlive yes

Validate the config before you restart. A typo here can lock you out:

sudo sshd -t

No output means it's fine. Then reload:

# Ubuntu / Debian
sudo systemctl reload ssh

# AlmaLinux / Rocky / CentOS
sudo systemctl reload sshd

Keep your current session open and test with a new terminal first. Only close the old one once the new login works.

Watch Out for Conflicting Settings

sshd uses the first value it finds for most options. If the main sshd_config already sets ClientAliveInterval 0 above the Include line, your drop-in file may be ignored. Check what's actually in effect:

sudo sshd -T | grep -Ei 'clientalive|tcpkeepalive'

Fix 3: Check for a Shell Auto-Logout (TMOUT)

Some hardened images and security scripts set a TMOUT variable, which makes bash log you out after a set number of idle seconds. Keepalives won't help here, because the shell is closing the session on purpose.

echo $TMOUT
grep -rn TMOUT /etc/profile /etc/profile.d/ /etc/bash.bashrc ~/.bashrc 2>/dev/null

If it returns something like TMOUT=300, that's a 5-minute idle logout. On a production server it might be there for compliance, so check before removing it. If it's your own box and you don't need it, comment out the line and log in again.

Fix 4: Protect Long Jobs With tmux or screen

Keepalives reduce disconnects. They can't stop your Wi-Fi from dropping or your laptop from sleeping. For anything that takes more than a few minutes (backups, migrations, large rsync transfers, mysql imports), run it inside tmux, so the job keeps going on the server even if your connection dies.

# Install
sudo apt install tmux        # Ubuntu / Debian
sudo dnf install tmux        # AlmaLinux / Rocky

# Start a named session
tmux new -s migrate

# Run your long command, then detach with Ctrl+b, then d

# Reconnect later and reattach
tmux attach -t migrate

Lost the session name? tmux ls lists everything that's still running.

When It Drops While You're Active

If the connection dies mid-command, keepalives aren't the answer. Work through these instead:

What you seeLikely causeWhat to check
Drops at random, also on other sitesUnstable local network or Wi-FiRun ping -c 100 your-server-ip and look for packet loss
Drops during heavy output or large transfersMTU problems, common over VPNs and PPPoETry ping -M do -s 1400 your-server-ip and lower the size until it works
Drops and the server is slow to come backServer out of memory, sshd killeddmesg -T | grep -i oom and journalctl -u ssh --since "1 hour ago"
Drops right after you connect from a new IPfail2ban or the firewall banning yousudo fail2ban-client status sshd
Everyone disconnected at the same momentsshd restarted, or a reboot or network changelast -x | head and uptime

Prevention Checklist

  1. Add ServerAliveInterval 60 to ~/.ssh/config on every machine you use to manage servers.
  2. Set ClientAliveInterval 60 / ClientAliveCountMax 3 on the VPS so new team members are covered too.
  3. Always validate with sshd -t and test from a second terminal before closing your session.
  4. Get into the habit of starting tmux before any job that runs longer than a couple of minutes.
  5. If you're on mobile data or a hotspot, use 30-second keepalives. Carrier NAT is far less forgiving than a home router.
Quick rule of thumb: if it drops when idle, fix keepalives. If it drops while active, look at the network or the server.

Frequently Asked Questions

Is it safe to set ServerAliveInterval for all hosts?

Yes. The keepalive packets are tiny and go through the encrypted channel, so they don't add any real load or security risk. Using Host * is the normal way to set it.

Should I set keepalives on the client or the server?

Either one works, since both generate traffic that keeps routers from expiring the connection. Client-side is quicker for you personally. Server-side covers everyone who logs in. Many admins set both.

Will keepalives keep a dead session open forever?

No. With ServerAliveCountMax 3 and a 60-second interval, the client gives up after about three minutes of no response. That's usually better than the default behaviour, where a frozen session can hang for a long time.

My long-running command died when SSH dropped. Can I get it back?

If it wasn't running inside tmux, screen or nohup, the process usually received a hangup signal and stopped. Check whether it left partial output, then rerun it inside tmux so the next disconnect doesn't matter.

Does this also fix SFTP disconnects in FileZilla or WinSCP?

The server-side ClientAliveInterval setting helps all SSH-based connections, including SFTP. In FileZilla you can also enable Send FTP keep-alive commands under Edit → Settings → Connection. In WinSCP, set keepalives under Advanced → Connection.