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.
tmux (recommended)
Installation
# Ubuntu/Debian
sudo apt install tmux -y
# CentOS/AlmaLinux
sudo dnf install tmux -yBasic 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 attachKeyboard shortcuts (prefix: Ctrl+B)
| Combination | Action |
|---|---|
Ctrl+B then D | Detach from session (leaves it active) |
Ctrl+B then C | Create new window |
Ctrl+B then N | Next window |
Ctrl+B then P | Previous window |
Ctrl+B then W | List windows |
Ctrl+B then % | Split vertically |
Ctrl+B then " | Split horizontally |
Ctrl+B then [ | Scroll mode (exit with Q) |
Ctrl+B then X | Close 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 activeBasic 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.confDelete sessions
# Delete specific session
tmux kill-session -t sessionname
# Delete all sessions
tmux kill-serverScreen (classic alternative)
screen is older but present on almost all systems.
sudo apt install screen -yBasic 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 KScreen shortcuts (prefix: Ctrl+A)
| Combination | Action |
|---|---|
Ctrl+A then D | Detach |
Ctrl+A then C | New window |
Ctrl+A then N | Next window |
Ctrl+A then [ | Scroll mode |
Ctrl+A then K | Close and terminate |
tmux vs screen
| tmux | screen | |
|---|---|---|
| Active development | Yes | Outdated |
| Split panes | ✅ Native | ❌ Limited |
| Scroll with mouse | ✅ | ❌ |
| Configuration | Rich | Simple |
| Availability | Install needed | Often 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.targetsudo systemctl enable tmux-boot
sudo systemctl start tmux-boot