Skip to main content
Back to Category

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.

Read time: 12 min Server Management
tmuxscreensshsession managementterminallinuxmultiplexer

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

hljs bash
sudo apt update
sudo apt install tmux -y

# Check version
tmux -V

CentOS/AlmaLinux/RHEL

hljs bash
sudo dnf install tmux -y
# or on older systems
sudo yum install tmux -y

Tmux Basic Usage

Session Management

hljs bash
# 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:

CommandDescription
Ctrl+b dDetach from session
Ctrl+b cCreate new window
Ctrl+b nNext window
Ctrl+b pPrevious window
Ctrl+b 0-9Switch 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 keysNavigate between panes
Ctrl+b zToggle pane fullscreen
Ctrl+b [Enter scroll mode
Ctrl+b ?Help screen

Window Management

hljs bash
# 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.

hljs bash
# 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:

hljs bash
nano ~/.tmux.conf
hljs bash
# 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
hljs bash
# 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

hljs bash
# Ubuntu/Debian
sudo apt install screen -y

# CentOS/AlmaLinux
sudo dnf install screen -y

Screen Basic Commands

hljs bash
# 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:

CommandDescription
Ctrl+a dDetach from session
Ctrl+a cCreate new window
Ctrl+a nNext window
Ctrl+a pPrevious window
Ctrl+a "Window list
Ctrl+a ARename window
Ctrl+a SHorizontal split
`Ctrl+a`
Ctrl+a TabSwitch between panes
Ctrl+a [Scroll mode
Ctrl+a ?Help

Tmux vs Screen Comparison

FeatureTmuxScreen
Active developmentYesNo
Vertical splitYesYes (newer versions)
Mouse supportYesLimited
ConfigurationPowerfulBasic
Pre-installedNoOften yes
Learning curveMediumLow
Scripting supportExcellentGood

Practical Use Cases

Long-Running Processes

hljs bash
# 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

hljs bash
# 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

hljs bash
# 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.