Software & Configuration

tmux and Screen: Persistent Sessions

How to use tmux and screen to keep processes active on the server even after SSH disconnection

When you disconnect from SSH, all processes started in that session are terminated. tmux and screen solve this by creating persistent sessions on the server.


Installation

# Ubuntu/Debian
sudo apt install tmux -y

# CentOS/AlmaLinux
sudo dnf install tmux -y

Basic usage

# Create new session
tmux

# Create session with name
tmux new -s sessionname

# List active sessions
tmux ls

# Attach an existing session
tmux attach -t sessionname
# or: tmux a -t sessionname

# Attach to the last session
tmux attach

Keyboard shortcuts (prefix: Ctrl+B)

CombinationAction
Ctrl+B then DDetach from session (leaves it active)
Ctrl+B then CCreate new window
Ctrl+B then NNext window
Ctrl+B then PPrevious window
Ctrl+B then WList windows
Ctrl+B then %Split vertically
Ctrl+B then "Split horizontally
Ctrl+B then [Scroll mode (exit with Q)
Ctrl+B then XClose current pane
Ctrl+B then $Rename session

Typical VPS workflow

# Connect via SSH
ssh root@185.100.xxx.xxx

# Create session for your server
tmux new -s minecraft

# Start the server inside tmux
cd /opt/minecraft && java -Xmx2G -jar server.jar nogui

# Press Ctrl+B then D to detach
# The server continues running!

# Disconnect from SSH
exit

# Reconnect the next day
ssh root@185.100.xxx.xxx
tmux attach -t minecraft
# You're back in the session, the server is still active

Basic configuration (~/.tmux.conf)

cat > ~/.tmux.conf << 'EOF'
# Change prefix from Ctrl+B to Ctrl+A (like screen)
# set -g prefix C-a

# Enable mouse
set -g mouse on

# Increase scroll history
set -g history-limit 10000

# Better colors
set -g default-terminal "screen-256color"

# Number windows starting from 1
set -g base-index 1

# Status bar
set -g status-right '%H:%M %d-%m-%Y'
EOF

# Reload config without restarting tmux
tmux source-file ~/.tmux.conf

Delete sessions

# Delete specific session
tmux kill-session -t sessionname

# Delete all sessions
tmux kill-server

Screen (classic alternative)

screen is older but present on almost all systems.

sudo apt install screen -y

Basic commands

# Create new session
screen

# Create session with name
screen -S sessionname

# List sessions
screen -ls

# Attach session
screen -r sessionname
screen -r  # if there's only one

# Detach: Ctrl+A then D

# Close session permanently: Ctrl+A then K

Screen shortcuts (prefix: Ctrl+A)

CombinationAction
Ctrl+A then DDetach
Ctrl+A then CNew window
Ctrl+A then NNext window
Ctrl+A then [Scroll mode
Ctrl+A then KClose and terminate

tmux vs screen

tmuxscreen
Active developmentYesOutdated
Split panes✅ Native❌ Limited
Scroll with mouse
ConfigurationRichSimple
AvailabilityInstall neededOften pre-installed

Recommendation: use tmux for new sessions, screen if tmux is unavailable.


Automatic startup on boot

If you want a tmux session to start automatically:

# Create a systemd service
sudo nano /etc/systemd/system/tmux-boot.service
[Unit]
Description=Start tmux session on boot
After=network.target

[Service]
Type=forking
User=root
ExecStart=/usr/bin/tmux new-session -d -s main -c /root
ExecStop=/usr/bin/tmux kill-session -t main
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
sudo systemctl enable tmux-boot
sudo systemctl start tmux-boot

On this page