You start a long upgrade over SSH. Twenty minutes in, your laptop sleeps, the wifi changes, or the connection simply dies. The remote shell is killed, and every process it owned dies with it — halfway through a package upgrade, which is the worst possible moment.
A terminal multiplexer fixes this completely. Your shell runs inside a session that belongs to the server, not to your connection. Drop the connection and the session keeps running; reconnect and reattach exactly where you left off.
Of everything in this section, this is the tool that most changes how you work on remote machines. It is also the one people put off learning longest.
The short verdict
Learn tmux. It is actively developed, its splitting and scripting are better, and it is what nearly everyone else uses now.
Know enough screen to reattach a session — three commands’ worth — because it is preinstalled on many older systems where tmux is not, and because someone else’s abandoned screen session is a thing you will eventually need to get into.
Use one of them for anything long-running over SSH. Which one matters far less than the habit.
What you get
| screen | tmux | |
|---|---|---|
| Survives a dropped connection | Yes | Yes |
| Multiple windows | Yes | Yes |
| Split panes | Horizontal only, awkward | Both directions, easily |
| Rename and organise windows | Basic | Good |
| Status bar | Minimal | Configurable |
| Scripting a layout | Painful | Straightforward |
| Share a session with a colleague | Yes | Yes |
| Actively developed | Barely | Yes |
| Preinstalled | Often | Sometimes |
| Default prefix key | Ctrl+a | Ctrl+b |
The row that decides it for most people is scripting a layout. Being able to run one command that opens a session with logs in one pane, a shell in another and a monitor in a third — the same way every time — is worth more than any individual feature.
Installing
sudo apt install tmux # Debian, Ubuntu
sudo dnf install tmux # Fedora, RHEL, Rocky, Alma
sudo pacman -S tmux # ArchInstall it on every server you administer, at the same time you do the rest of the first-hour setup in securing a new server. It weighs almost nothing and the first time it saves an interrupted upgrade it has paid for itself.
The prefix key
Everything in tmux is Ctrl+b, released, then a key. That two-step is the thing that feels strange for a day and then disappears.
It exists because tmux has to pass every other keystroke through to the program you are running. Reserving one prefix means Ctrl+c, Ctrl+r and everything else still reach your shell normally.
Below, prefix means “press Ctrl+b, let go, then press the key”.
The five commands that matter
You can get the entire benefit of tmux from these. Everything else is refinement.
# Start a named session
tmux new -s upgrade
# Detach and leave it running: prefix, then d
# What is running?
tmux ls
# Reattach
tmux attach -t upgrade
# Attach to whatever is there, or create it if not
tmux new -A -s workName your sessions. tmux new -s upgrade costs four extra characters and means that in a week you can tell what the three detached sessions on that server are. Unnamed sessions get numbers, and numbers tell you nothing.
tmux new -A -s work is the one to build a habit around: attach if it exists, create it if it does not. It behaves correctly whether you are starting fresh or coming back, so it is the right thing to type every time.
Windows and panes
A session contains windows (like browser tabs), and a window contains panes (splits of the screen).
| Keys | Does |
|---|---|
prefix d | Detach — the one to remember first |
prefix c | New window |
prefix n / p | Next / previous window |
prefix 0–9 | Jump to a window by number |
prefix , | Rename the current window |
prefix % | Split left/right |
prefix " | Split top/bottom |
prefix arrow | Move between panes |
prefix z | Zoom a pane full-screen, and back |
prefix x | Close the current pane |
prefix [ | Scroll back through output (q to exit) |
prefix ? | Every binding |
Two of those are worth singling out. prefix z temporarily expands one pane to the full window — invaluable when you have split the screen four ways and now need to read something properly. Press it again to restore the layout.
prefix [ catches everyone out. Inside tmux, your mouse wheel and PageUp do not scroll the terminal’s own history — tmux owns the screen. prefix [ enters copy mode, where arrows and PageUp work, and q leaves it. Without this, tmux feels broken the first time you try to look at something that scrolled past.
Configuration worth doing
tmux is usable unconfigured. Four lines in ~/.tmux.conf remove most of the friction:
# Mouse support: click panes, scroll naturally, drag borders
set -g mouse on
# Start numbering at 1 - matches the keyboard
set -g base-index 1
setw -g pane-base-index 1
# A scrollback worth having
set -g history-limit 50000
# Reload this file with: prefix r
bind r source-file ~/.tmux.conf \; display "Reloaded"set -g mouse on is the single highest-value line. Scrolling works as you expect, clicking a pane focuses it, and dragging a border resizes it — which removes most of the reasons beginners give up on tmux.
Many people also remap the prefix to Ctrl+a, matching screen and being easier to reach:
unbind C-b
set -g prefix C-a
bind C-a send-prefixWorth knowing the trade-off before you do: Ctrl+a is also “jump to start of line” in bash and in readline generally. The send-prefix line lets you get it back by pressing it twice, but it is a real cost. If you are starting fresh, staying on Ctrl+b avoids the conflict entirely.
Scripting a layout
This is where tmux clearly beats screen. A few lines produce the same working environment every time:
#!/bin/bash
# Set up a monitoring session on a web server
tmux new-session -d -s monitor
tmux send-keys -t monitor 'sudo journalctl -u nginx -f' C-m
tmux split-window -h -t monitor
tmux send-keys -t monitor 'htop' C-m
tmux split-window -v -t monitor
tmux send-keys -t monitor 'sudo tail -f /var/log/nginx/error.log' C-m
tmux attach -t monitorSave that as ~/.local/bin/monitor, make it executable, and one word gives you a full dashboard. C-m is Enter. See reading Linux logs for what to put in the panes and htop vs top for the middle one.
Enough screen to get by
You will meet screen on older servers and in other people’s abandoned sessions. Its prefix is Ctrl+a.
# Start named
screen -S upgrade
# Detach: Ctrl+a then d
# List sessions
screen -ls
# Reattach
screen -r upgrade
# Steal a session that is still attached elsewhere
# (e.g. attached to a connection that has since died)
screen -dr upgradescreen -dr is the one worth remembering. A session left attached to a connection that no longer exists refuses a plain -r; -dr detaches it from the ghost and gives it to you. The tmux equivalent is tmux attach -d -t name.
| Task | screen | tmux |
|---|---|---|
| Start named | screen -S name | tmux new -s name |
| Detach | Ctrl+a d | Ctrl+b d |
| List | screen -ls | tmux ls |
| Reattach | screen -r name | tmux attach -t name |
| Force reattach | screen -dr name | tmux attach -d -t name |
| New window | Ctrl+a c | Ctrl+b c |
| Kill session | Ctrl+a k | tmux kill-session -t name |
Where screen is still the right answer
- It is already there and tmux is not. Older RHEL and CentOS systems, appliances, embedded devices. The multiplexer you have beats the one you would have to install.
- Serial console access.
screen /dev/ttyUSB0 115200is the standard way to talk to a switch, router or embedded board over serial, and tmux does not do this at all. On a network engineer’s machine that alone justifies keeping it. - Attaching to a session someone else started. If a colleague left work running in screen, you need screen.
The serial console case is the genuinely non-replaceable one. Everything else is availability.
And if you have neither
On a locked-down machine with no multiplexer, nohup at least stops a long job dying with your connection:
nohup ./long-import.sh > import.log 2>&1 &
# Then watch it
tail -f import.logIt is a poor substitute — you cannot interact with the job, only read its output — but it is far better than losing a database import halfway through. If a job has already started in the foreground, Ctrl+z then disown -h %1 can sometimes rescue it.
Quick reference
| You want | Do this |
|---|---|
| Start or resume a session | tmux new -A -s work |
| Leave it running | prefix d |
| See what is running | tmux ls |
| Come back | tmux attach -t work |
| Take over a stuck session | tmux attach -d -t work |
| Split vertically / horizontally | prefix % / prefix " |
| Focus one pane full-screen | prefix z |
| Scroll back | prefix [, then q |
| Turn on the mouse | set -g mouse on |
| Talk to a serial console | screen /dev/ttyUSB0 115200 |
Related reading
- ssh — the connection tmux protects you from losing
- htop and btop vs top — what to put in the other pane
- Reading Linux logs — following logs while you work
- Securing a new server — install tmux during the first-hour setup
- Migrating a server — where a dropped connection does real damage
- Processes and memory — why a dropped shell kills its children
