Skip to content
RIPE NCC member · Local Internet RegistryCall us on 020 3318 3449sales@hosting-telecom.uk

tmux: keep your work running if SSH drops

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    # FreeBSD

Sessions: start, disconnect, come back

tmux new -s work

A 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 work

If 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.

Tip A session lives only as long as the server is running — it won’t survive a reboot. For a program that must always be running, set it up as a service: Running your own program at startup.

Keys

Every tmux command starts with Ctrl+b: press it, release it, then press the key you want.

KeysWhat they do
Ctrl+b ddisconnect from the session
Ctrl+b cnew window
Ctrl+b n, Ctrl+b pnext and previous window
Ctrl+b 0…9go 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 arrowmove to the neighbouring pane
Ctrl+b zzoom a pane to fill the window, and back
Ctrl+b [scroll through the history: arrows, PgUp and PgDn, press q to leave
Ctrl+b xclose 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.conf

With 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 running or no 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 after sudo -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.

Choose which cookies we may use. You can change your mind at any time with the “Cookie settings” link at the bottom of every page.

Necessary

Always active

They keep the site secure and remember your cookie choice. They don’t identify you.

Google Analytics counts visits and shows which pages people find useful, so we can improve the site. Which cookies