Every sysadmin and developer knows the pain: you're in the middle of editing a critical config file or running a long process when suddenly - "Connection reset by peer". The remote server is still running your session, but your local SSH client lost its connection due to network instability.
SSH sessions can persist on the server even after client disconnection because:
- The shell process (bash/zsh) continues running
- Background processes maintain their state
- Temporary files (like vim swap files) preserve edit state
The best approach is preventing disconnections from being fatal:
# ~/.ssh/config settings for resilient connections
Host *
ServerAliveInterval 60
ServerAliveCountMax 5
TCPKeepAlive yes
ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r
ControlPersist 1h
Terminal multiplexers provide session persistence regardless of connectivity:
# Starting a named tmux session
tmux new -s mysession
# Later, after disconnect
tmux attach -t mysession
# Basic screen usage
screen -S work_session
screen -r work_session # To reattach
For existing disconnected sessions without multiplexers:
# Find orphaned processes
ps -ft $(who | grep 'pts/' | awk '{print $2}')
# Resume vim sessions
vim -r /path/to/swapfile.swp
# Check for existing screen sessions
screen -ls
Some SSH implementations support reconnect attempts:
- Type "Enter ~" (tilde at beginning of new line)
- Then type "." to terminate or "R" to retry
For chronic connection issues, consider a wrapper:
#!/bin/bash
while true; do
ssh -o ConnectTimeout=5 user@host
sleep 2
echo "Attempting reconnect..."
done
Persistent connections work best with:
- VPN tunnels for stable routing
- QoS settings prioritizing SSH traffic
- Alternative ports during congestion
Network instability is a common issue when working with remote servers via SSH. When your connection drops unexpectedly, you might lose:
- Active file edits in progress
- Long-running processes
- Debug sessions
- Complex terminal states
Before we discuss reconnection, let's make your SSH sessions more resilient:
# ~/.ssh/config
Host *
ServerAliveInterval 60
ServerAliveCountMax 5
TCPKeepAlive yes
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 10m
The professional approach is to use terminal multiplexers:
# Start a new tmux session
tmux new -s mysession
# If disconnected, reconnect with:
tmux attach -t mysession
# For screen users:
screen -S mysession
screen -r mysession
Modern SSH versions support session recovery through connection sharing:
# First connection (master)
ssh -M -S ~/.ssh/sessions/example.com user@example.com
# Subsequent connections will reuse the master
ssh -S ~/.ssh/sessions/example.com user@example.com
Sometimes your session might still be running on the server. Check with:
ps aux | grep sshd
who -a
For unreliable connections, use this bash script:
#!/bin/bash
while true; do
ssh -o ConnectTimeout=5 -o ConnectionAttempts=1 user@host
if [[ $? -ne 0 ]]; then
echo "Connection failed, retrying in 5 seconds..."
sleep 5
else
break
fi
done
Consider these tools to monitor your connection quality:
# Check packet loss
ping -c 100 example.com | grep "packet loss"
# Measure connection stability
mtr --report example.com