Post

tmux - Terminal Multiplexer Guide

tmux - Terminal Multiplexer Guide

When working over SSH, one of the most frustrating experiences is a dropped connection killing a long-running process. tmux (terminal multiplexer) solves this - and much more. It lets you run persistent sessions, split your terminal into panes, and detach/reattach from anywhere.


Installation

1
2
# Debian / Ubuntu
sudo apt install tmux

Core Concepts

ConceptDescription
SessionA top-level container that persists independently of your terminal
WindowA full-screen tab inside a session
PaneA split subdivision within a window

The default prefix key is Ctrl+b - press it before every tmux command.


Session Management

Start a Named Session

1
tmux new -s mysession

Always name your sessions - it makes reattaching much easier.

Detach from a Session

1
Ctrl+b  d

The session keeps running in the background. You can now safely close the SSH connection.

List All Sessions

1
tmux ls

Reattach to a Session

1
tmux attach -t mysession

If there is only one session running:

1
tmux attach

Kill a Session

1
tmux kill-session -t mysession

Typical SSH Workflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Local Machine] ──SSH──> [Remote Server]
                              │
                         tmux new -s work
                              │
                    ┌─────────┴──────────┐
                    │   long build run   │
                    │  (safe to detach)  │
                    └────────────────────┘
                              │
                       Ctrl+b  d  (detach)
                              │
              [SSH connection drops  -  no problem]
                              │
                    [Reconnect later via SSH]
                              │
                   tmux attach -t work

This is the core use case: start a job, detach, close your laptop, come back the next day and reattach.



Copy Mode (Scrollback)

SSH terminals often have limited scrollback. tmux has its own scrollback buffer:

1
2
3
Ctrl+b  [        # Enter copy mode
Arrow keys        # Scroll up/down
q                 # Exit copy mode

To scroll with the mouse wheel, add this to ~/.tmux.conf:

1
set -g mouse on
This post is licensed under CC BY 4.0 by the author.