How to run a long command so that it finishes even if the connection drops: tmux sessions, windows, panes, scrolling and a short cheat sheet. Plus a word about screen.
Contents
When an SSH connection drops, everything running in it stops too: a system update, a file copy, a database transfer. tmux solves this simply: your programs run in a session on the server itself, and you connect to it and disconnect from it whenever you like. If the connection is lost, the command carries on; you log in again and find it exactly where it was.
As a bonus, you get several terminals in a single SSH window: an update running in one, a log open in another.
Installing
tmux is already installed on Ubuntu. On other systems:
sudo apt install tmux # Debian
sudo dnf install tmux # AlmaLinux
sudo pkg install tmux # FreeBSDSessions: start, disconnect, come back
tmux new -s workA green bar appears at the bottom — you are in the session work. Run whatever you need. To disconnect and leave everything running, press Ctrl+b, release it, then press d. Now you can close the terminal.
To come back:
tmux ls # list the sessions
tmux attach -t workIf there is only one session, tmux attach is enough, or the shorter tmux a. After a dropped connection it’s the same: the session is still alive, so log in over SSH and attach.
To end a session, type exit in its last window, or run tmux kill-session -t work.
Keys
Every tmux command starts with Ctrl+b: press it, release it, then press the key you want.
| Keys | What they do |
|---|---|
| Ctrl+b d | disconnect from the session |
| Ctrl+b c | new window |
| Ctrl+b n, Ctrl+b p | next and previous window |
| Ctrl+b 0…9 | go to a window by number |
| Ctrl+b , | rename the window |
| Ctrl+b % | split the window into two panes side by side |
| Ctrl+b " | split the window into two panes, one above the other |
| Ctrl+b arrow | move to the neighbouring pane |
| Ctrl+b z | zoom a pane to fill the window, and back |
| Ctrl+b [ | scroll through the history: arrows, PgUp and PgDn, press q to leave |
| Ctrl+b x | close the pane |
| Ctrl+b ? | list all keys |
Mouse and scrolling
By default the mouse wheel doesn’t scroll the history in tmux. Turn the mouse on and you can scroll with the wheel, select panes with a click and resize them:
echo 'set -g mouse on' >> ~/.tmux.conf
tmux source-file ~/.tmux.confWith the mouse turned on, hold Shift while selecting text to copy it — on macOS, hold Option in iTerm2 or Fn in Terminal.
If tmux complains
no server runningorno sessions— there are no sessions: the server has rebooted or the session was closed.sessions should be nested with care— you are already inside tmux. Open a new window (Ctrl+b c) instead of a new session.- You can’t see your session in
tmux ls— each user has their own sessions. If you started it aftersudo -i, look for it as root too.
What about screen?
screen is an older program with the same idea. If you’re used to it: screen -S work starts a new session, Ctrl+a d disconnects, screen -ls lists sessions and screen -r work takes you back. It is installed from the screen package. On AlmaLinux it isn’t in the main repositories, only in EPEL, so it’s simpler to use tmux there.
Didn’t find the answer? Ask our support team — we’ll help.