Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Keeping SSH session alive on Linux
Secure Shell (SSH) is a network protocol that allows secure remote connections between two systems. It is commonly used to access and manage Linux servers remotely. However, SSH sessions can be terminated due to network timeouts, inactivity, or connection drops, which can be frustrating during long-running tasks. This article discusses various methods to keep SSH sessions alive on Linux systems.
Server-Side Configuration
Using ClientAliveInterval Option
The most effective way to prevent SSH sessions from timing out is to configure the SSH server to send keep-alive packets. This is done using the ClientAliveInterval option in the SSH server configuration.
Edit the SSH server configuration file located at /etc/ssh/sshd_config and add the following lines
ClientAliveInterval 60 ClientAliveCountMax 3
The ClientAliveInterval sends a keep-alive packet every 60 seconds, while ClientAliveCountMax specifies how many unresponsive packets to tolerate before disconnecting (3 × 60 = 180 seconds total timeout).
After making changes, restart the SSH service
sudo systemctl restart ssh
Client-Side Configuration
You can also configure the SSH client to send keep-alive packets by editing ~/.ssh/config or using command-line options
# In ~/.ssh/config
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
# Or use command-line option
ssh -o ServerAliveInterval=60 user@server
Session Management Tools
Using Screen Command
The screen command creates persistent terminal sessions that survive SSH disconnections. This is ideal for long-running processes that need to continue even if your connection drops.
Basic screen usage
# Start a new screen session screen # Detach from session (Ctrl+a, then d) # Session continues running in background # List available sessions screen -ls # Reattach to a session screen -r # Create named session screen -S mysession # Reattach to named session screen -r mysession
Using Tmux Command
tmux is a modern alternative to screen with more features and better window management
# Start new session tmux # Detach session (Ctrl+b, then d) # List sessions tmux ls # Attach to session tmux attach # Create named session tmux new-session -s mysession # Attach to named session tmux attach-session -t mysession
Using Nohup Command
The nohup command runs processes that ignore hangup signals, allowing them to continue after SSH disconnection
# Run command with nohup nohup long-running-command > output.log 2>&1 & # Check background jobs jobs # Bring job to foreground fg %1
Comparison of Methods
| Method | Use Case | Pros | Cons |
|---|---|---|---|
| ClientAliveInterval | Prevent timeouts | Server-wide, automatic | Requires root access |
| ServerAliveInterval | Client-side keep-alive | User configurable | Per-client setting |
| Screen/Tmux | Persistent sessions | Session survives disconnection | Learning curve |
| Nohup | Background processes | Simple, lightweight | No session control |
Best Practices
For optimal SSH session management, combine multiple approaches
Configure keep-alive settings on both client and server
Use
screenortmuxfor interactive long-running tasksUse
nohupfor fire-and-forget background processesSet reasonable timeout values to balance connectivity and security
Conclusion
Keeping SSH sessions alive requires a combination of proper configuration and appropriate tools. Server-side keep-alive settings prevent timeout disconnections, while session managers like screen and tmux provide persistent environments that survive connection drops. Choose the method that best fits your workflow and security requirements.
