tmux and Screen: Persistent Sessions

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.

02

tmux (recommended)

Installation

bash
# Ubuntu/Debian
sudo apt install tmux -y

# CentOS/AlmaLinux
sudo dnf install tmux -y

Basic usage

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

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

bash
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

bash
# Delete specific session
tmux kill-session -t sessionname

# Delete all sessions
tmux kill-server
03

Screen (classic alternative)

screen is older but present on almost all systems.

bash
sudo apt install screen -y

Basic commands

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

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.

05

Automatic startup on boot

If you want a tmux session to start automatically:

bash
# Create a systemd service
sudo nano /etc/systemd/system/tmux-boot.service
ini
[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
bash
sudo systemctl enable tmux-boot
sudo systemctl start tmux-boot

DeluxHost, founded in 2023, offers high-quality hosting solutions for various digital needs. We provide shared hosting, VPS, and dedicated servers with advanced security and global data centers.

© DeluxHost, All rights reserved. | VAT Number : IT17734661006
All Systems Operational