Using tmux to Manage Terminal Sessions on VPS

Using tmux to Manage Terminal Sessions on VPS

June 2026 · 7 min read

tmux (terminal multiplexer) is an essential tool for VPS work, allowing you to run multiple terminal windows simultaneously in a single pane. Most importantly, sessions persist after SSH disconnects, ensuring that running processes don't die even if your connection drops. This is critical for long-running tasks like backups, database migrations, or compilations that take hours.

Installing tmux

sudo apt install tmux -y

# Verify installation
tmux -V

Basic Session Commands

# Create a new session
tmux new -s mysession

# Detach (exit without killing) — press Ctrl+B then D
# Or type:
tmux detach

# List running sessions
tmux ls

# Attach back into a session
tmux attach -t mysession
# Or
tmux a -t mysession

# Kill a session
tmux kill-session -t mysession

Essential Shortcuts (prefix = Ctrl+B)

# --- Session ---
Ctrl+B + d        → Detach from session
Ctrl+B + $        → Rename session
Ctrl+B + s        → List and switch sessions

# --- Window ---
Ctrl+B + c        → Create new window
Ctrl+B + n        → Go to next window
Ctrl+B + p        → Go to previous window
Ctrl+B + 0-9      → Go to window number
Ctrl+B + ,        → Rename window

# --- Pane (split pane) ---
Ctrl+B + %        → Split pane vertically
Ctrl+B + "        → Split pane horizontally
Ctrl+B + Arrow    → Navigate between panes
Ctrl+B + x        → Close current pane
Ctrl+B + z        → Zoom pane to full screen

Configuring .tmux.conf (Personal Config)

nano ~/.tmux.conf
# Change prefix to Ctrl+A (like screen)
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# Enable mouse support
set -g mouse on

# Increase history limit
set -g history-limit 50000

# Start window numbering at 1
set -g base-index 1
setw -g pane-base-index 1

# Status bar
set -g status-right '%Y-%m-%d %H:%M'
set -g status-style bg=black,fg=white
# Apply config without restart
tmux source-file ~/.tmux.conf

Real-World Use Cases on VPS

# Run long-running process without fearing disconnect
tmux new -s backup
./backup-all-databases.sh

# Press Ctrl+B + D (detach) and close SSH immediately
# Attach later to check results

# Monitor multiple things simultaneously
tmux new -s monitor
Ctrl+B + %  # Split into 2 panes
# Left pane: htop
# Right pane: tail -f /var/log/nginx/access.log
Tip: Use tmux new -s deploy -d to create a detached session immediately, then run commands in that session with tmux send-keys -t deploy "command" Enter. This is ideal for automated deployment scripts.

Verify tmux is Working Correctly

# View all sessions
tmux ls

# View processes in a session
tmux list-panes -a -F "#{session_name}: #{pane_pid}"

# Verify a process still runs after detach
ps aux | grep backup-all-databases

# Verify tmux process itself is running
pgrep -l tmux
Tip: Set ServerAliveInterval 60 in SSH ~/.ssh/config on your client to prevent SSH timeout while working in tmux. This reduces frequent connection drops.

Common Issues & Troubleshooting

Sessions disappear after VPS reboot

tmux sessions do not survive a reboot. If you need a process to run persistently after reboot, use a systemd service instead:

sudo systemctl enable myservice
sudo systemctl start myservice
# See systemd article for details

Copy-paste doesn't work in tmux

Use keyboard copy mode instead of mouse when mouse mode is enabled:

# Enter copy mode
Ctrl+B + [
# Use arrow or vi keys to select text
# Press Space to start selection
# Press Enter to copy
# Ctrl+B + ] to paste

Pane display looks corrupted (strange characters)

TERM variable is incorrect. Set TERM in SSH config or .tmux.conf:

set -g default-terminal "screen-256color"
# Or
export TERM=xterm-256color

tmux is slow when typing (lag)

The default escape-time of 500ms makes it feel sluggish. Reduce it:

set -sg escape-time 10  # In ~/.tmux.conf

Frequently Asked Questions

How is tmux different from screen?

Both are terminal multiplexers, but tmux is more actively developed than screen. tmux supports panes, a modern scripting API, and more flexible configuration. screen is an older fork with less ongoing development. Today, tmux is the standard choice for VPS sysadmins.

Can I use tmux together with Mosh?

Yes, they work well together. Mosh solves SSH timeout and packet loss issues, while tmux solves session persistence. Use them together: mosh user@vps -- tmux attach. This allows seamless VPS work even with unstable internet.

Can I share a terminal with someone else in the same session?

Yes, using tmux -S /tmp/shared new -s collab and letting other users attach with the same socket. This is great for pair programming or live debugging with teammates.