Have you ever encountered the dreaded Client_loop: send disconnect: Broken pipe error while using SSH? It’s a frustrating issue, especially when you’re managing remote systems and lose your connection due to inactivity. Fortunately, it’s easy to fix with a few adjustments.
What Causes This Error?
This error typically appears when your SSH session remains idle for too long. After a certain period of inactivity, the server assumes the client is no longer available and forcibly closes the connection.
Most users try to avoid this by periodically hitting the Enter key or typing random commands to keep the session alive — but there’s a better, permanent solution.
The Fix: Keep SSH Alive
The solution is to configure SSH to send keep-alive messages. You can do this by adjusting the server configuration, client command-line options, or both.
1. Server-side Fix
On the remote Linux server, you need to modify the SSH daemon configuration file:
sudo vi /etc/ssh/sshd_config
Add or update the following two lines:
ClientAliveInterval 300
ClientAliveCountMax 3
ClientAliveInterval: Number of seconds the server waits before sending a keep-alive message (300 seconds = 5 minutes).ClientAliveCountMax: How many unanswered keep-alive messages to send before disconnecting (3 times in this case).
These settings mean the server will wait 5 minutes of inactivity, send a keep-alive, repeat two more times if no response, and then disconnect if still silent after 15 minutes.
After making changes, restart the SSH service:
sudo systemctl restart sshd
2. Client-side Fix
If you don’t have access to the server or want a temporary fix from your local terminal:
ssh -o ServerAliveInterval=300 username@server_ip
This sends a keep-alive packet every 5 minutes from your SSH client.
Automate the Fix: Shell Script
You can automate the server-side configuration with this script:
#!/bin/bash
CONFIG_FILE="/etc/ssh/sshd_config"
# Backup the original configuration file
sudo cp "$CONFIG_FILE" "${CONFIG_FILE}.bak"
# Update or append ClientAliveInterval and ClientAliveCountMax
sudo sed -i '/^ClientAliveInterval/d' "$CONFIG_FILE"
sudo sed -i '/^ClientAliveCountMax/d' "$CONFIG_FILE"
echo "ClientAliveInterval 300" | sudo tee -a "$CONFIG_FILE"
echo "ClientAliveCountMax 3" | sudo tee -a "$CONFIG_FILE"
# Restart SSH service
sudo systemctl restart sshd
echo "SSH keep-alive configuration applied successfully."
To run the script:
chmod +x fix_ssh_broken_pipe.sh
sudo ./fix_ssh_broken_pipe.sh
Conclusion
With just a few lines of configuration, you can prevent your SSH sessions from timing out due to inactivity. Whether you’re administering servers or just keeping an eye on logs, uninterrupted access is key to productivity.
Tired of losing SSH sessions? Apply this fix today and stay connected!

