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)
| 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
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)
| 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 |
04
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.
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
Gerelateerde artikelen
