Why Use tmux on a Server
tmux is a terminal multiplexer: it runs shells inside sessions that live on the server, not in your SSH connection. If the connection drops or you close your laptop, programs inside tmux keep running, and when you attach again everything is where you left it. Use it for system upgrades, large copies, database imports, and editing files over an unstable link. A tmux session does not survive a reboot of the server.
Install tmux
On Debian and Ubuntu:
sudo apt install tmux
On AlmaLinux and Rocky Linux:
sudo dnf install tmux
tmux -V prints the installed version.
Session Commands
Type these at the shell prompt:
| Command | What it does |
|---|---|
tmux | Starts an unnamed session. |
tmux new -s work | Starts a session named work. |
tmux ls | Lists sessions. |
tmux attach -t work | Attaches to work. |
tmux a | Attaches to the most recent session. |
tmux kill-session -t work | Ends one session. |
tmux kill-server | Ends all sessions. |
Ending a session stops every program running in it, so check with tmux ls and attach first if you are not sure what is inside.
The Prefix Key
Inside tmux, shortcuts start with the prefix Ctrl+b. Ctrl+b d means: hold Ctrl, press b, release both, then press d.
| Keys | What it does |
|---|---|
Ctrl+b d | Detaches; the session keeps running. |
Ctrl+b s | Lists sessions to switch to. |
Ctrl+b $ | Renames the session. |
Ctrl+b ? | Lists all key bindings. |
Ctrl+b : | Opens the tmux command prompt. |
Window Commands
A window fills the screen, like a tab. The status bar at the bottom shows each window of the session by number and name.
| Keys | What it does |
|---|---|
Ctrl+b c | New window. |
Ctrl+b n | Next window. |
Ctrl+b p | Previous window. |
Ctrl+b 0 to 9 | Window by number. |
Ctrl+b w | Choose from a list. |
Ctrl+b , | Rename the window. |
Ctrl+b & | Close the window; asks first. |
Pane Commands
| Keys | What it does |
|---|---|
Ctrl+b % | Splits side by side. |
Ctrl+b " | Splits top and bottom. |
Ctrl+b arrow | Moves to the pane that way. |
Ctrl+b o | Next pane. |
Ctrl+b z | Zooms a pane in or out. |
Ctrl+b q | Shows pane numbers. |
Ctrl+b x | Closes the pane; asks first. |
Resize and Arrange Panes
| Keys | What it does |
|---|---|
Ctrl+b Ctrl+arrow | Resizes by one cell. |
Ctrl+b Alt+arrow | Resizes by five cells. |
Ctrl+b Space | Next preset layout. |
Ctrl+b { or } | Swaps panes. |
Some terminal programs on your own computer catch Ctrl or Alt with arrow keys for their own use. If resizing does nothing, use the command prompt instead: Ctrl+b : and then resize-pane -D 10 (or -U, -L, -R), or turn on mouse support as shown below and drag the border.
Scroll and Copy
Ctrl+b [ enters copy mode. Scroll with the arrow keys or Page Up and Page Down, search with / when vi keys are on, and press q to leave. Ctrl+b ] pastes the last copied text. With vi keys on, Space starts a selection and Enter copies it.
Configure tmux in ~/.tmux.conf
# ~/.tmux.conf
set -g mouse on
set -g history-limit 50000
set -g base-index 1
setw -g mode-keys vi
# Use Ctrl+a as the prefix
unbind C-b
set -g prefix C-a
bind C-a send-prefix
Reload it without restarting tmux: tmux source-file ~/.tmux.conf. With the mouse on, you can click panes, drag their borders, and scroll with the wheel. The last block moves the prefix to Ctrl+a; send-prefix lets you press it twice to send a real Ctrl+a to the shell. Keep your shell settings in ~/.bashrc; see what .bashrc is and how to edit it.
A Typical Session on a Server
- Connect:
ssh user@your_server_ip. New to SSH? See how to use PuTTY to SSH into a server. - Start a session:
tmux new -s upgrade. - Run the long job, for example
sudo apt upgrade. - Detach with
Ctrl+b dand log out if you like. - Later, connect again and run
tmux attach -t upgrade.
tmux new -A -s upgrade attaches when the session exists and creates it when it does not. If the upgrade asks for a reboot, finish your work in tmux first and follow how to reboot a Linux server. Editing files in a tmux pane is covered in how to save and quit in Vim.