tmux: persistent terminal sessions
tmux runs your shell inside a session that lives on the server. If your SSH connection drops, or you close your laptop, the session and everything running in it carry on; you simply attach to it again. It can also split one terminal into several panes.
tmux new -s name # new named session
tmux attach -t name # reattach
tmux ls # list sessions
The basic workflow
ssh server
tmux new -s upgrade # start a session called "upgrade"
sudo apt full-upgrade # long job…
# press Ctrl+b, then d → detach; the job keeps running
exit # log out
# later, from anywhere:
ssh server
tmux attach -t upgrade # back exactly where you left off
Every tmux command key starts with the prefix, Ctrl+b by default: press it, release, then press the command key.
Essential keys (after Ctrl+b)
| Key | Action |
|---|---|
| d | Detach from the session |
| c | New window |
| n / p / 0-9 | Next / previous / numbered window |
| , | Rename the window |
| % / " | Split side by side / top and bottom |
| arrow keys | Move between panes |
| z | Zoom the current pane to full size and back |
| x | Close the current pane |
| [ | Scroll / copy mode (arrows or PgUp, q to leave) |
| s | Choose a session from a list |
| ? | List all key bindings |
Useful commands
tmux new -As work # attach to "work", or create it if missing
tmux kill-session -t upgrade
tmux new -d -s backup '/usr/local/bin/backup.sh' # start a job in a detached session
tmux attach -d -t work # attach and detach other clients (e.g. a stale connection)
A small ~/.tmux.conf
set -g mouse on # click panes, scroll with the wheel
set -g history-limit 50000 # more scrollback
set -g base-index 1 # windows numbered from 1
bind r source-file ~/.tmux.conf \; display "reloaded"
GNU screen
The older screen does the same job and is still found on many systems. Its prefix is Ctrl+a: screen -S name starts a session, Ctrl+a d detaches, screen -r name reattaches, and screen -ls lists sessions.
For a single job that must survive logout without an interactive session, nohup or a systemd service also work; see kill and systemctl.