SSH Session Freeze: Fixing Unresponsive Terminal After Idle Time on Ubuntu


2 views

Many Ubuntu users (particularly on 12.04) experience this annoying behavior: you establish an SSH connection normally:

ssh user@server.example.com

Everything works fine initially, but after switching to other applications or leaving the session idle, the terminal becomes unresponsive. The cursor might stop blinking, and keystrokes don't register - essentially freezing your SSH session.

This issue typically stems from three main factors:

  • Server-side SSH timeouts
  • Client-side TCP keepalive settings
  • Network middleboxes dropping idle connections

Add these settings to your ~/.ssh/config file:

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
    TCPKeepAlive yes

This configuration:

  • Sends keepalive packets every 60 seconds
  • Gives up after 3 failed attempts
  • Enables TCP-level keepalive

On your SSH server, edit /etc/ssh/sshd_config:

ClientAliveInterval 60
ClientAliveCountMax 3
TCPKeepAlive yes

Then restart the SSH service:

sudo service ssh restart

For unreliable connections, consider mosh (Mobile Shell):

sudo apt-get install mosh
mosh user@server.example.com

Mosh handles network changes and intermittent connectivity much better than traditional SSH.

When troubleshooting, run SSH in verbose mode:

ssh -vvv user@server.example.com

This provides detailed connection information that can help identify where the freeze occurs.

Using terminal multiplexers can help maintain sessions even if the connection drops:

# For tmux
tmux new -s mysession

# For screen
screen -S mysession

You can then reattach to these sessions after reconnecting.


This issue typically manifests when:

  • Your SSH session remains idle for several minutes
  • You switch to other applications during an active session
  • Network conditions are unstable
# Typical symptoms:
1. Cursor stops blinking but remains visible
2. Keyboard input shows no response
3. Session appears "frozen" but connection isn't dropped
4. Only solution is terminating and reconnecting

The most common culprits include:

TCP Keepalive Settings

# Check current keepalive settings
cat /proc/sys/net/ipv4/tcp_keepalive_time
cat /proc/sys/net/ipv4/tcp_keepalive_intvl
cat /proc/sys/net/ipv4/tcp_keepalive_probes

SSH Server/Client Configuration

# Server-side (/etc/ssh/sshd_config)
ClientAliveInterval 300
ClientAliveCountMax 2

# Client-side (~/.ssh/config)
Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

Option 1: Adjust SSH Timeout Parameters

# For permanent solution, add to ~/.ssh/config
Host *
    TCPKeepAlive yes
    ServerAliveInterval 120
    ServerAliveCountMax 5

Option 2: System-wide TCP Tweaks

# Add to /etc/sysctl.conf
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 5

# Apply changes
sudo sysctl -p

Option 3: Use Terminal Multiplexer

# Install tmux if not available
sudo apt-get install tmux

# Basic usage:
tmux new -s mysession  # Start new session
[detach with Ctrl+b d]
tmux attach -t mysession # Reattach later

When the problem persists, consider:

# Monitor network traffic
tcpdump -i eth0 port 22

# Check for packet loss
ping -c 100 your.server.com

# Alternative connection method
ssh -v -o "TCPKeepAlive=yes" user@server

For unstable connections:

# Use mosh (mobile shell)
sudo apt-get install mosh
mosh user@server

# Persistent ssh connection with autossh
autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" user@server