Security

Using tmux for Persistent Terminal Sessions on a VPS

The first time you were debugging a production issue on a VPS and your SSH client froze or your laptop closed the lid, you realized the terminal was gone, and so was your work. That's where tmux comes in. It's a terminal multiplexer that keeps your session alive on the server, independent of your SSH connection. Install it once, and you can disconnect, reconnect, and pick up exactly where you left off. This guide walks through installing, configuring, and using tmux on a Linux VPS (Ubuntu 24.04 LTS in the examples), from basic session management to advanced split-pane workflows and custom bindings.

Prerequisites

  • A Linux VPS running a supported distribution (Ubuntu 24.04 LTS, Debian 12, AlmaLinux 9, Rocky Linux 9). All examples assume a non-root sudo user.
  • SSH access to that server.
  • Basic comfort with the Linux terminal.

Why a Terminal Multiplexer Matters for VPS Work

A modern web server runs multiple services: Nginx, MariaDB, Redis, a Node.js app, a queue worker. If you're managing these manually, you might open one SSH window per process. Each depends on a stable SSH connection. If the connection drops, and it will, especially on flaky Wi-Fi or a long-running deploy, every child process dies with it. The cleanup: start over.

tmux decouples your terminal from the SSH session. You start a tmux session, run your tasks inside it, and detach. The session stays alive on the server. Later, you reconnect and reattach. It also lets you split the terminal into multiple panes, watch logs in one, run queries in another, edit config in a third, all within a single SSH connection. No extra windows, no context switching. It's the default tool for anyone who lives in the terminal, and once you get used to it, you'll miss it everywhere else.

Step 1, Installing tmux

tmux is in every major distro's repo. Install it with one command.

Ubuntu / Debian:

sudo apt update
sudo apt install tmux -y

AlmaLinux / Rocky Linux:

sudo dnf install tmux -y

Verify:

tmux -V

Expected output (exact version may differ):

tmux 3.4

If you see a version number, you are ready.

Step 2, Starting Your First Session

Run:

tmux

A new terminal appears. This is your tmux session. Important: the status bar at the bottom shows [0] 0:bash*, the session number, window list, and an asterisk for the active window.

The default prefix key to send commands to tmux is Ctrl+b. To detach from this session without killing it, press:

Ctrl+b d

You are back in your regular SSH terminal. The session still runs on the server. Reattach with:

tmux attach -t 0

Or, if you want to name your session at creation (much better for multiple tasks):

tmux new -s deploy

Then detach and reattach by name:

tmux attach -t deploy

Why naming matters: If you run three sessions, one for app logs, one for a database import, one for a config edit, you attach by name, not by guessing the number.

Step 3, Managing Multiple Sessions and Windows

List all active tmux sessions on the server:

tmux ls

Expected output:

deploy: 1 windows (created Mon Feb 24 19:14:03 2026)
monitor: 1 windows (created Mon Feb 24 19:15:10 2026)

Kill a session when it's no longer needed:

tmux kill-session -t monitor

Inside a session, you can create multiple windows (like tabs):

  • Ctrl+b c, create a new window
  • Ctrl+b 0, Ctrl+b 1, switch to window by number
  • Ctrl+b n, next window
  • Ctrl+b p, previous window
  • Ctrl+b w, list windows interactively
  • Ctrl+b &, kill the current window (confirm with y)

Each window holds its own shell. Perfect for splitting tasks that shouldn't share a pane.

Step 4, Splitting Panes for Side-by-Side Views

This is where tmux becomes a productivity multiplier. Within a single window, split the screen into panes.

Horizontal split (top/bottom):

Ctrl+b "

Vertical split (left/right):

Ctrl+b %

Navigate between panes:

  • Ctrl+b ←, Ctrl+b →, Ctrl+b ↑, Ctrl+b ↓, move to the pane in that direction

Resize the current pane (hold the prefix, then press the arrow key repeatedly):

  • Ctrl+b Ctrl+←, resize left by 5 units
  • Ctrl+b Ctrl+→, resize right
  • Ctrl+b Ctrl+↑, resize up
  • Ctrl+b Ctrl+↓, resize down

Kill the current pane:

Ctrl+b x

Real-world example, debugging a slow query while tailing application logs:

# Start a new session named "debug"
tmux new -s debug
# Split vertically: left pane for logs, right pane for MySQL
Ctrl+b %
# In the left pane:
tail -f /var/log/nginx/access.log
# Switch to the right pane:
Ctrl+b →
# In the right pane:
mysql -u root -p database -e "SHOW FULL PROCESSLIST;"

Step 5, Customizing tmux with ~/.tmux.conf

The default bindings work, but you'll want a few tweaks for speed. Create a config file:

nano ~/.tmux.conf

Start with this minimal, battle-tested config:

# Set prefix to Ctrl+a (avoids the Ctrl+b stretch and frees Ctrl+b for other things)
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# Reload config
bind r source-file ~/.tmux.conf \; display-message "Config reloaded"

# Mouse support (scroll, select panes, resize)
set -g mouse on

# Increase scrollback buffer to 10000 lines
set -g history-limit 10000

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

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

# Vi-style copy mode keys (familiar if you use vim)
setw -g mode-keys vi

# Easier pane navigation (Alt+arrow, no prefix needed)
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

Save and exit. Reload the config in any running session:

Ctrl+b :source-file ~/.tmux.conf

Or start a new session, it reads the config automatically.

Key changes:

  • Prefix switched to Ctrl+a, your left hand stays on home row, no reaching.
  • Mouse support enabled, click panes, scroll the buffer, resize with the mouse. Handy for quick interaction, but for heavy terminal work you'll want the keyboard.
  • Vi keys in copy mode, if you use vim, copy mode feels natural: press Space to start selection, Enter to copy to tmux buffer, then Ctrl+b ] to paste.

Step 6, Copy Mode and Scrolling

Scroll back through output that scrolled off the screen. Enter copy mode:

Ctrl+b [

Now navigate:

  • With vi keys (if you set mode-keys vi): j/k up/down, Ctrl+u/Ctrl+d page up/down, gg/G top/bottom.
  • With emacs keys (default): arrow keys, PgUp/PgDn.
  • Start selection: Space (vi) or Ctrl+Space (emacs).
  • Copy to buffer: Enter.
  • Paste from buffer: Ctrl+b ].

With mouse enabled (set -g mouse on), you can also scroll with the mouse wheel while in copy mode.

Step 7, Using tmux with Long-Running Tasks

The real killer feature: start a task, detach, close your laptop, go home, come back, reattach.

Example, running a database migration on production:

# Create a session
tmux new -s migration
# Start the migration
php artisan migrate --force
# Detach with Ctrl+b d
# ...next morning...
ssh user@your-server
tmux attach -t migration

The migration completed while you were disconnected. You see the output right there. No need for nohup, screen, or background processes. tmux keeps the process alive because it's within its own session on the server, not inside the SSH tunnel.

Restarting a crashed service in a tmux pane: If your app crashes while you are away, the pane will show the error. Reattach, fix, restart. This beats hunting through logs blind.

Step 8, Synchronizing Panes (Send Same Input to All Panes)

You have three web servers. You want to run the same command on all of them simultaneously. With tmux, you can synchronize input across panes in the same window.

# First, open SSH connections to each server in separate panes
Ctrl+b %  (split vertically)
Ctrl+b "  (split the right pane horizontally)

# Now toggle sync
Ctrl+b :setw synchronize-panes
# Type a command, it goes to all panes
# Turn sync off the same way
Ctrl+b :setw synchronize-panes

Use with caution: typing reboot in sync mode brings down all three servers at once.

FAQ

How do I reattach to a tmux session after SSH drops?

SSH back into your VPS, then run tmux attach to reattach to the most recent session. If you have multiple sessions, use tmux ls to list them, then tmux attach -t <name>.

Does tmux survive a server reboot?

No. tmux runs in memory. If the server restarts, all sessions are lost. For critical long-running tasks that must survive reboots, use systemd services instead of running them inside tmux.

What is the difference between screen and tmux?

Both are terminal multiplexers. tmux is actively maintained with a cleaner configuration syntax, better default keybindings, and support for vertical splits without patching. screen works but sees fewer updates, stick with tmux on a modern Linux VPS.

Can I share a tmux session with another user?

Yes. One user can attach to another user's session by specifying the socket path. Both users see the same terminal output in real time. Useful for pair debugging, but requires the same user account or socket permissions set accordingly.

How do I kill a hung pane or session?

Inside a pane, type exit or Ctrl+d to kill the shell. If the process is stuck, press Ctrl+b x to kill the pane. To kill a session entirely, run tmux kill-session -t <name>.

Does tmux use extra RAM?

Very little. A single session with one window uses less than 5 MB. The scrollback buffer is stored in memory, so a 10000-line buffer adds maybe 1-2 MB per window. Your VPS with 2 GB RAM won't notice it.

Related articles

Note: This guide is for general reference. Every system and infrastructure has its own specifics, so test each step in a safe environment and consult a qualified engineer before applying it in production.