Tmux and Screen Guide: SSH Session Management
Manage SSH sessions with Tmux and GNU Screen. Installation, window/pane management, detach/attach, session persistence, and comparison guide.
Table of Contents
Tmux and Screen Guide: SSH Session Management
One of the biggest challenges when working on remote servers is SSH connection drops. When the connection is lost, running processes terminate. Tmux and GNU Screen are terminal multiplexers that solve this problem. This guide covers both tools and helps you understand when to use each.
Why Use Tmux or Screen?
When your SSH session drops:
- Running build processes stop
- Downloads are interrupted
- Long-running scripts terminate
- Open text editors close
Tmux and Screen keep sessions alive on the server side. Even if your connection drops, processes continue running in the background. When you reconnect, you pick up exactly where you left off.
Tmux is the more modern, actively developed tool. Screen is older and often comes pre-installed. Knowing both is useful.
Installing Tmux
Ubuntu/Debian
sudo apt update
sudo apt install tmux -y
# Check version
tmux -V
CentOS/AlmaLinux/RHEL
sudo dnf install tmux -y
# or on older systems
sudo yum install tmux -y
Tmux Basic Usage
Session Management
# Start a new session
tmux
# Start a named session
tmux new-session -s myproject
# or shorthand
tmux new -s myproject
# Detach from session — processes keep running
# Ctrl+b, then d
# List existing sessions
tmux list-sessions
# or shorthand
tmux ls
# Reattach to a session
tmux attach-session -t myproject
# or shorthand
tmux attach -t myproject
# Attach to the last session
tmux attach
# Kill a session
tmux kill-session -t myproject
# Kill all sessions
tmux kill-server
The Tmux Prefix Key
All tmux commands start with a prefix key. Default prefix: Ctrl+b
After sending the prefix, press the command key:
| Command | Description |
|---|---|
Ctrl+b d | Detach from session |
Ctrl+b c | Create new window |
Ctrl+b n | Next window |
Ctrl+b p | Previous window |
Ctrl+b 0-9 | Switch to numbered window |
Ctrl+b , | Rename window |
Ctrl+b & | Close current window |
Ctrl+b % | Split vertically (right) |
Ctrl+b " | Split horizontally (below) |
Ctrl+b arrow keys | Navigate between panes |
Ctrl+b z | Toggle pane fullscreen |
Ctrl+b [ | Enter scroll mode |
Ctrl+b ? | Help screen |
Window Management
# Create a new window inside tmux
# Ctrl+b, then c
# List windows
# Ctrl+b, then w
# Rename window
# Ctrl+b, then ,
# Close window
# Ctrl+b, then &
Pane Management
Panes let you split a single window into multiple terminals.
# Vertical split (side by side)
# Ctrl+b, then %
# Horizontal split (top/bottom)
# Ctrl+b, then "
# Navigate between panes
# Ctrl+b, then arrow keys
# Toggle pane fullscreen
# Ctrl+b, then z
# Close pane
# Ctrl+b, then x
# Resize pane
# Ctrl+b, then Ctrl+arrow keys
Tmux Configuration
Create ~/.tmux.conf to customize tmux:
nano ~/.tmux.conf
# Change prefix to Ctrl+a (like Screen)
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
# Enable mouse support
set -g mouse on
# Start window numbering at 1
set -g base-index 1
setw -g pane-base-index 1
# Customize status bar
set -g status-bg colour235
set -g status-fg white
set -g status-left '[#S] '
set -g status-right '%H:%M %d-%b-%y'
# Increase history limit
set -g history-limit 10000
# Reload configuration
tmux source-file ~/.tmux.conf
# or inside tmux:
# Ctrl+b, then :source-file ~/.tmux.conf
GNU Screen Usage
Screen is older than tmux but often comes pre-installed on many systems.
Installing Screen
# Ubuntu/Debian
sudo apt install screen -y
# CentOS/AlmaLinux
sudo dnf install screen -y
Screen Basic Commands
# Start a new session
screen
# Start a named session
screen -S myproject
# Detach from session
# Ctrl+a, then d
# List sessions
screen -ls
# Reattach to session
screen -r myproject
# Attach by ID if multiple sessions exist
screen -r 12345.myproject
# Kill a session
# Inside screen: Ctrl+a, then k
# From outside:
screen -X -S myproject quit
Screen Shortcuts
Screen's prefix key is Ctrl+a:
| Command | Description |
|---|---|
Ctrl+a d | Detach from session |
Ctrl+a c | Create new window |
Ctrl+a n | Next window |
Ctrl+a p | Previous window |
Ctrl+a " | Window list |
Ctrl+a A | Rename window |
Ctrl+a S | Horizontal split |
| `Ctrl+a | ` |
Ctrl+a Tab | Switch between panes |
Ctrl+a [ | Scroll mode |
Ctrl+a ? | Help |
Tmux vs Screen Comparison
| Feature | Tmux | Screen |
|---|---|---|
| Active development | Yes | No |
| Vertical split | Yes | Yes (newer versions) |
| Mouse support | Yes | Limited |
| Configuration | Powerful | Basic |
| Pre-installed | No | Often yes |
| Learning curve | Medium | Low |
| Scripting support | Excellent | Good |
Practical Use Cases
Long-Running Processes
# Start a new session
tmux new -s build
# Run a long command
make all
# Detach — process continues
# Ctrl+b d
# Check back later
tmux attach -t build
Multi-Panel Server Monitoring
# Start session
tmux new -s monitoring
# Vertical split
# Ctrl+b %
# Monitor CPU in left pane
htop
# Switch to right pane (Ctrl+b right arrow)
# Monitor disk I/O
iostat -x 2
Auto-Attach on SSH Login
# Add to ~/.bashrc or ~/.bash_profile
if command -v tmux &> /dev/null && [ -z "$TMUX" ]; then
tmux attach -t default || tmux new -s default
fi
Use auto-attach carefully. It can conflict with SSH key-based automation on some systems.
Extend tmux with Tmux Plugin Manager (TPM). The tmux-resurrect plugin preserves sessions even across system reboots.
Conclusion
Tmux and Screen are essential tools for remote server management. When running long processes, build tasks, or continuous monitoring commands on REXE servers, these tools ensure your work is safe even if the SSH connection drops. Screen is a simpler starting point for beginners, but Tmux's flexibility and features make it the better long-term choice.
Related Articles
How to Connect to Your VDS Server via SSH (Complete Guide)
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
How to Connect to Windows VDS via RDP: Complete Guide
Complete guide to connecting to Windows Server VDS via Remote Desktop (RDP): setup steps, NLA security, Path Panel firewall rule, and troubleshooting tips.
How to Change VDS Server OS: Step-by-Step Reinstall Guide
Step-by-step guide to changing your VDS server OS via REXE panel: backup tips, supported Linux distributions and Windows Server versions explained.