how to keep a ssh connection permanent
- User
how to keep a ssh connection permanent
- Assistant
There are several ways to keep SSH connections persistent and prevent them from dropping:
SSH Configuration Options
Add these settings to your SSH client configuration (
~/.ssh/config):Host * ServerAliveInterval 60 ServerAliveCountMax 3 TCPKeepAlive yesOr use them directly in your SSH command:
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@hostThese settings send keepalive packets every 60 seconds to prevent the connection from timing out.
Terminal Multiplexers
Screen or tmux let you maintain persistent sessions that survive disconnections:
# Start a new screen session screen -S mysession # Detach with Ctrl+A, then D # Reattach later with: screen -r mysessionFor tmux:
# Start new session tmux new -s mysession # Detach with Ctrl+B, then D # Reattach with: tmux attach -t mysessionAutossh
Autossh automatically restarts SSH connections when they drop:
# Install autossh sudo apt-get install autossh # Debian/Ubuntu brew install autossh # macOS # Use it like regular SSH autossh -M 0 user@hostMosh (Mobile Shell)
Mosh is designed for unreliable connections and roaming:
# Install on both client and server sudo apt-get install mosh # Connect mosh user@hostSSH Multiplexing
Reuse a single connection for multiple sessions by adding to
~/.ssh/config:Host * ControlMaster auto ControlPath ~/.ssh/sockets/%r@%h-%p ControlPersist 600The best approach depends on your specific needs - use keepalive settings for simple timeout prevention, terminal multiplexers for persistent work sessions, or autossh/mosh for unreliable connections.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.