An SSH session dies when your network does, and with it every program you started. A terminal multiplexer keeps a shell running on the server independently of your connection: you detach, go home, reattach, and the build is still scrolling. It also splits one terminal into several panes — logs on one side, an editor on the other — which is how most administrators work on remote machines. This lesson teaches tmux, the modern standard, and enough screen to cope on systems that only have that.
tmux has three levels. A session is a workspace that survives disconnects. A session holds windows (like tabs), and each window can be split into panes. You interact through a prefix key, Ctrl+b by default, followed by a command key.
sudo apt install tmux
tmux new -s work # create a session named "work"
tmux ls # list sessions
tmux attach -t work # reattach (or: tmux a)
tmux kill-session -t work
tmux new -s work -d # create detached, for scriptsssh web
tmux new -s migrate
./run-migration.sh # takes two hours
# Ctrl+b, then d -> "[detached (from session migrate)]"
exit # close SSH; the migration keeps running
# Later, from anywhere
ssh web
tmux attach -t migrateIf your connection drops without detaching, the session simply stays attached from tmux's point of view; tmux attach -d -t migrate detaches the old client and takes over.
All keys are pressed after the Ctrl+b prefix.
| Key | Action |
|---|---|
| d | detach |
| c | new window |
| n / p / 0-9 | next / previous / numbered window |
| , | rename window |
| w | interactive window list |
| % | split pane left/right |
| " | split pane top/bottom |
| arrow keys | move between panes |
| z | zoom the current pane to full size (toggle) |
| x | close the current pane |
| o | cycle panes |
| Space | cycle layouts |
| { / } | swap pane with previous / next |
| [ | enter copy (scroll) mode; q exits |
| ? | list all bindings |
| : | tmux command prompt |
Scrolling is the first thing that surprises new users: the mouse wheel does nothing until you enter copy mode with [ (or enable mouse support in the config below).
Inside copy mode, navigate with arrows or PgUp/PgDn, search backwards with ?, start a selection with Space, and copy with Enter. Paste with Ctrl+b ]. With setw -g mode-keys vi in your config, the keys become h j k l, /, v and y.
Settings live in ~/.tmux.conf. This one enables the mouse, moves the prefix to Ctrl+a (easier to reach and matches screen), starts numbering at 1 and makes splits keep the current directory.
# ~/.tmux.conf
set -g prefix C-a
unbind C-b
bind C-a send-prefix
set -g mouse on
set -g base-index 1
setw -g pane-base-index 1
set -g history-limit 50000
setw -g mode-keys vi
set -g default-terminal "tmux-256color"
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
bind r source-file ~/.tmux.conf \; display "reloaded"Apply it without restarting: tmux source-file ~/.tmux.conf. The status bar at the bottom shows the session name, windows and time, and is fully customisable with set -g status-left and status-right.
Because everything is a command, a layout can be built from a script and a command can be sent into a running pane — handy for launching a development environment or running a job under supervision.
tmux new -d -s dev -n editor 'vim'
tmux split-window -t dev -h 'npm run dev'
tmux split-window -t dev -v 'tail -f logs/app.log'
tmux select-pane -t dev:1.1
tmux attach -t dev
tmux send-keys -t migrate "./run-migration.sh" Enter # type into a session
tmux capture-pane -t migrate -p | tail -20 # read its screenscreen predates tmux and is still the only multiplexer on some minimal or older systems. The concepts are the same; the prefix is Ctrl+a.
| Task | screen |
|---|---|
| start named session | screen -S work |
| detach | Ctrl+a d |
| list | screen -ls |
| reattach | screen -r work (-d -r to steal an attached one) |
| new window | Ctrl+a c |
| next / previous window | Ctrl+a n / Ctrl+a p |
| split horizontal / vertical | Ctrl+a S / Ctrl+a \| |
| scroll mode | Ctrl+a [ |
| kill window | Ctrl+a k |
screen also has a niche use tmux lacks: connecting to serial consoles (screen /dev/ttyUSB0 115200).
tmux attach with no -t attaches to the most recent.mouse on or entering copy mode.Which key sequence detaches from a tmux session with the default prefix?
tmux new -s name, prefix d to detach, tmux attach -t name to return.c, n, p) which contain panes (%, ", arrows, z).[) or enable set -g mouse on in ~/.tmux.conf.split-window, send-keys and capture-pane build and drive layouts.screen uses Ctrl+a and the same ideas; use systemd, not a multiplexer, for services that must survive reboots.Next lesson: Security Basics: ufw Firewall, fail2ban and Server Hardening — close every port you do not need and stop brute-force attacks automatically.