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
| Concept | Description |
|---|
| Session | A top-level container that persists independently of your terminal |
| Window | A full-screen tab inside a session |
| Pane | A split subdivision within a window |
The default prefix key is Ctrl+b - press it before every tmux command.
Session Management
Start a Named Session
Always name your sessions - it makes reattaching much easier.
Detach from a Session
The session keeps running in the background. You can now safely close the SSH connection.
List All Sessions
Reattach to a Session
1
| tmux attach -t mysession
|
If there is only one session running:
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.
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: