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:
- Hold
Ctrland tapb. - Release both keys.
- Press the command key, such as
d.
The notation below is Ctrl-b d.
A minimal workflow
# 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
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
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
deploywindow: enter/srv/<service>and run Compose.logswindow:docker compose logs -f --tail 100.healthwindow: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. Usetmux 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.