Skip to main content

Practical Tmux

Tmux is a terminal multiplexer. It runs a tmux server on the remote machine and organizes work into session → window → pane. For example, a vps-maintenance session can have one window running a shell and journalctl, and another running watch or docker stats for monitoring.

If SSH disconnects, the tmux session stays on the VPS, and you can attach to it after reconnecting over SSH. It is useful for upgrades, builds, watching logs, and longer migrations. It cannot replace a backup or a second SSH access path.

Remember the prefix key​

The default prefix is Ctrl-b. Most shortcuts are a sequence, rather than three keys pressed together:

  1. Hold Ctrl and tap b.
  2. Release both keys.
  3. Press the command key, such as d.

The notation below is Ctrl-b d.

A minimal workflow​

[REMOTE: VPS]
# Create a named session
tmux new -s maintenance

# Work normally inside tmux
sudo apt update

# Ctrl-b d: detach; tasks keep running

# List sessions and reattach from a regular shell
tmux ls
tmux attach -t maintenance

An especially useful idempotent entry point is:

tmux new-session -A -s maintenance

It attaches if the session exists and creates it otherwise.

Windows and panes​

ShortcutAction
Ctrl-b cCreate a window
Ctrl-b ,Rename the current window
Ctrl-b n / pNext / previous window
Ctrl-b 0..9Jump to a numbered window
Ctrl-b %Split into panes side by side
Ctrl-b "Split into panes above and below
Ctrl-b + arrowSwitch panes
Ctrl-b zTemporarily zoom the current pane; press again to restore
Ctrl-b xClose the current pane, with confirmation
Ctrl-b dDetach from the session without ending its tasks

Give windows names that describe their purpose, such as deploy, logs, and monitor, rather than opening seven windows all named bash.

Viewing output history​

The terminal's scroll wheel does not always scroll through tmux history. Press:

Ctrl-b [

This enters copy mode. Move with the arrow keys, Page Up/Page Down, or Vim-style keys, and press q to leave. For a quick look at logs, you can also let the tool limit its own output:

journalctl -u docker -n 200 --no-pager
docker compose logs --tail 200

How Ctrl-Z, detach, and exit differ​

ActionWhat happens
Ctrl-ZThe shell sends a suspend signal to the foreground process, making it a stopped job
bgResumes a stopped job in the current shell in the background
fgBrings a job back to the foreground
Ctrl-b dThe tmux client leaves, while programs in the session continue running normally
exitExits the current shell; the session ends when its last pane exits

After pressing Ctrl-Z accidentally, first check:

jobs -l
fg %1 # Resume job 1 in the foreground
# Or bg %1 # Continue in the background; logs and lifecycle still belong to this shell

For a long-running task, start in tmux rather than relying on bg after the fact.

A layout for everyday VPS work​

tmux new -s ops
  • deploy window: enter /srv/<service> and run Compose.
  • logs window: docker compose logs -f --tail 100.
  • health window: watch -n 2 'docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'.

You do not need to keep every window open indefinitely. A session with a clear current task is more useful than an attic full of terminals.

Common problems​

  • no sessions: the session ended or was never created. Use tmux new -s <name>.
  • sessions should be nested with care: tmux was started from inside tmux. Check $TMUX; nesting is usually unnecessary.
  • Attach reports an existing connection: use tmux attach -d -t <name> to detach the old client and attach here.
  • A task disappears after a reboot: tmux survives an SSH disconnect, not a VPS reboot. Permanent services need a Docker restart policy or systemd.
  • You cannot tell where you are: tmux display-message -p '#S:#I.#P' displays the session, window, and pane.

Ending a session​

Prefer stopping programs cleanly in each pane and entering exit. To terminate an entire session deliberately:

tmux kill-session -t maintenance

This terminates the processes in that session, so check the target name before running it.

Explore connectionsOpen network