When establishing SSH connections between Ubuntu clients and CentOS servers, many developers encounter persistent connection drops during idle periods, culminating in the frustrating "Write failed: Broken pipe" error. This behavior typically indicates TCP-level connection timeouts rather than an SSH protocol failure.
# Check current SSH server keepalive settings
grep -E "ClientAlive|TCPKeepAlive" /etc/ssh/sshd_config
# Typical output showing default (often problematic) settings:
# TCPKeepAlive yes
# ClientAliveInterval 0
# ClientAliveCountMax 3
The critical parameters needing adjustment are:
- ClientAliveInterval: Time (seconds) between keepalive packets (recommended: 60-120)
- ClientAliveCountMax: Number of failed keepalives before termination (recommended: 3-5)
# Edit the SSH daemon configuration
sudo nano /etc/ssh/sshd_config
# Add or modify these lines:
ClientAliveInterval 60
ClientAliveCountMax 5
TCPKeepAlive yes
After saving changes, restart the SSH service:
sudo service sshd restart # CentOS 5.5 syntax
# For modern systems:
# sudo systemctl restart sshd
For additional reliability, configure your SSH client (~/.ssh/config):
Host *
ServerAliveInterval 30
ServerAliveCountMax 5
TCPKeepAlive yes
When the issue persists despite configuration changes, examine potential network interruptions:
# Check for packet drops during connection
tcpdump -i eth0 'port 22 and (tcp-syn|tcp-ack|tcp-rst)'
For development environments requiring stable long-term connections:
# Using tmux for session persistence
ssh user@host -t 'tmux new -A -s dev_session'
# With automatic reconnection:
while true; do ssh -o ConnectTimeout=5 user@host; sleep 2; done
Some firewall configurations aggressively terminate idle connections. Check for rules affecting SSH timeouts:
# For iptables-based firewalls
sudo iptables -L -n --line-numbers | grep -i timeout
When working with remote servers through SSH, connection stability is crucial for productive development workflows. The "Write failed: Broken pipe" error typically occurs when:
- Network inactivity triggers TCP timeout
- Intermediate network devices drop idle connections
- Server-side SSH daemon configuration has aggressive timeout settings
- Client-side keepalive mechanisms aren't properly configured
On your CentOS 5.5 server, edit the SSH daemon configuration:
# sudo vi /etc/ssh/sshd_config
# Add or modify these parameters:
ClientAliveInterval 60
ClientAliveCountMax 3
TCPKeepAlive yes
This configuration tells the server to:
- Send keepalive messages every 60 seconds (ClientAliveInterval)
- Terminate the connection after 3 failed keepalive attempts (ClientAliveCountMax)
- Enable TCP-level keepalive packets (TCPKeepAlive)
For your Ubuntu 11.04 machine, create or modify your SSH client configuration:
# vi ~/.ssh/config
Host *
ServerAliveInterval 30
ServerAliveCountMax 2
TCPKeepAlive yes
This client-side configuration:
- Sends keepalive packets every 30 seconds
- Gives up after 2 failed attempts
- Works alongside server-side settings
For critical development sessions, consider using terminal multiplexers:
# On the server (install if needed)
sudo yum install tmux
# Start a persistent session
tmux new -s dev_session
# Later, to reattach:
tmux attach -t dev_session
When troubleshooting, these commands help identify connection issues:
# Check TCP connection state
netstat -tn | grep ssh
# Monitor network traffic
tcpdump -i eth0 'port 22'
# Test connection quality
mtr -4 -rwc 100 your.server.ip
Ensure your firewall isn't dropping idle connections. For iptables:
# Allow established SSH connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Adjust connection tracking timeout
modprobe ip_conntrack
echo 3600 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established