Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

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:

CommandWhat it does
tmuxStarts an unnamed session.
tmux new -s workStarts a session named work.
tmux lsLists sessions.
tmux attach -t workAttaches to work.
tmux aAttaches to the most recent session.
tmux kill-session -t workEnds one session.
tmux kill-serverEnds 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.

KeysWhat it does
Ctrl+b dDetaches; the session keeps running.
Ctrl+b sLists 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.

KeysWhat it does
Ctrl+b cNew window.
Ctrl+b nNext window.
Ctrl+b pPrevious window.
Ctrl+b 0 to 9Window by number.
Ctrl+b wChoose from a list.
Ctrl+b ,Rename the window.
Ctrl+b &Close the window; asks first.

Pane Commands

KeysWhat it does
Ctrl+b %Splits side by side.
Ctrl+b "Splits top and bottom.
Ctrl+b arrowMoves to the pane that way.
Ctrl+b oNext pane.
Ctrl+b zZooms a pane in or out.
Ctrl+b qShows pane numbers.
Ctrl+b xCloses the pane; asks first.

Resize and Arrange Panes

KeysWhat it does
Ctrl+b Ctrl+arrowResizes by one cell.
Ctrl+b Alt+arrowResizes by five cells.
Ctrl+b SpaceNext 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

  1. Connect: ssh user@your_server_ip. New to SSH? See how to use PuTTY to SSH into a server.
  2. Start a session: tmux new -s upgrade.
  3. Run the long job, for example sudo apt upgrade.
  4. Detach with Ctrl+b d and log out if you like.
  5. 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.

Was this answer helpful?
Loading...