Fixing the SSH 'Broken pipe' Error: How to Keep Your Sessions Alive

beginner🌐 Networking2026-07-13| Linux (Ubuntu, Debian, CentOS), macOS, WSL2, and Windows OpenSSH clients connecting to remote servers via SSH.

Error Message

packet_write_wait: Connection to 192.168.1.100 port 22: Broken pipe
#ssh#networking#linux#devops#sysadmin

The Problem: The Frozen Terminal

You’re deep into a configuration task, step away for a five-minute coffee break, and return to find your terminal completely unresponsive. After a few seconds of frantic typing, the dreaded message appears:

packet_write_wait: Connection to 192.168.1.100 port 22: Broken pipe

This isn't just a minor annoyance. It disrupts your workflow, kills running processes, and forces you to re-authenticate and navigate back to your working directory.

Why Does the Pipe Break?

A Broken pipe error happens when your SSH client tries to send data through a connection that the other side—or a device in the middle—has already closed. This usually stems from one of three issues:

  • Aggressive Firewall Rules: Many corporate firewalls or home NAT routers automatically prune "idle" connections from their memory after 300 seconds (5 minutes) to save resources.
  • Server-Side Timeouts: To maintain security, the remote server might be configured to kick off users who haven't typed anything for a set duration.
  • Network Fluctuation: If your Wi-Fi blips or your VPN switches servers, your local IP address might change, instantly invalidating the existing TCP socket.

How to Fix It

The most reliable fix is to enable "KeepAlive" packets. These are tiny, encrypted heartbeats sent through the SSH tunnel. They keep the connection busy enough that firewalls won't mark it as idle.

Method 1: The Client-Side Fix (Best for Most Users)

This is the preferred method. It fixes the problem for every server you connect to without needing administrative rights on the remote machine.

  • Open your local SSH configuration file (or create it if it doesn't exist):

nano ~/.ssh/config

  
  - Add these lines to the top of the file:

Host * ServerAliveInterval 60 ServerAliveCountMax 3

  
  - Save and exit (Ctrl+O, Enter, Ctrl+X).

**How this works:** Your computer will now send a "null packet" every 60 seconds. If the server fails to respond to three of these in a row (after 180 seconds total), the client will gracefully close the session. This prevents your terminal from hanging for 15 minutes while waiting for a dead network to respond.

### Method 2: The Server-Side Fix (Best for Admins)
If you manage a fleet of servers and want to ensure your team doesn't get kicked off, modify the SSH daemon settings on the host.

  - Edit the global SSH configuration:
    ```
sudo nano /etc/ssh/sshd_config
  • Find or add these directives:
ClientAliveInterval 60
ClientAliveCountMax 3
  • Restart the service to apply the new rules:

sudo systemctl restart ssh

  

### Method 3: The Quick Command-Line Hack
If you are using a public computer or a borrowed terminal and can't edit config files, pass the settings directly into your connection command:

ssh -o ServerAliveInterval=60 user@192.168.1.100


## Testing the Results
Validation is simple. Connect to your server and let it sit idle. If you can still run `ls` or `htop` after 15 minutes of inactivity, your heartbeats are doing their job. The intermediate router's NAT table is being kept fresh by your 60-second packets.

## Pro Tips for Unstable Connections
**Avoid the TCPKeepAlive Trap:** You might see `TCPKeepAlive yes` in some tutorials. While it sounds similar, this setting works at the TCP layer rather than the SSH layer. Many smart firewalls ignore these packets or spoof them, making `ServerAliveInterval` a much more robust choice.

**Use Mosh for Mobile Work:** If you frequently work from trains, cafes, or via cellular hotspots, try **Mosh (Mobile Shell)**. Unlike SSH, Mosh uses UDP and supports roaming. You can close your laptop, change Wi-Fi networks, and reopen it to find your session exactly where you left it.

**Always Use Tmux:** For long-running scripts, always start a `tmux` or `screen` session. Even if your "pipe breaks," the process keeps running on the server. You can simply reconnect and run `tmux attach` to pick up right where you were.

Related Error Notes