Computing and the Command Line

Long Sessions with tmux

If an SSH connection drops, the shell and whatever it was running go with it. tmux keeps sessions running on the server: start a named session, detach with Ctrl-b d, list and reattach later from anywhere. Windows like tabs, panes that split the screen, scrollback, and a small ~/.tmux.conf for your dotfiles. A worked example: a long copy on the server that survives closing the laptop.

  • 5 min
  • 8 steps
  • 2 questions
  • Lesson 58 of 80

In this lesson

  1. The problem
  2. The prefix key
  3. Sessions: detach and attach
  4. Windows and panes
  5. A worked example
  6. A ~/.tmux.conf
  7. Your turn
  8. So

The problem

Start a big copy or an upgrade on the server over SSH, and then the Wi-Fi drops or the laptop sleeps. The connection ends, the shell on the server ends with it, and so does whatever it was running 1. A terminal multiplexer fixes this: it runs your shells on the server, independently of the connection, so you can disconnect and later reattach to exactly where you were 2 1.

tmux is the usual one, and it’s installed on Ubuntu Server already; on the desktop, sudo apt install tmux 2. It’s useful locally too, for splitting one terminal into several 3.

Left, sessions, windows, panes: a session called backup holds windows 0 and 1, and window 0 is split into two panes; a session called work has one window and one pane. Windows are like tabs; panes split a window; each runs a shell. Middle, when the connection drops: without tmux, the shell ends and the job it was running is stopped; with tmux, the session keeps running on the server; reconnect with ssh server, then tmux attach -t backup, and it's exactly as you left it. Right, prefix Ctrl-b, then: d detach; c new window; n or p next or previous window; percent split side by side; double quote split top and bottom; arrows move between panes; z zoom a pane; left bracket scroll back, q to leave. Commands: tmux new -s backup, tmux ls, tmux attach -t backup. Bottom, ~/.tmux.conf for your dotfiles: set -g mouse on, to click panes and windows and scroll with the wheel; set-option -g history-limit 10000, to keep 10,000 lines of scrollback instead of the default 2,000.
Start long jobs inside tmux; if the connection drops, reattach. Credit: StudyCorner diagram · CC BY 4.0 · Source

The prefix key

Every tmux command is two steps: press Ctrl-b, let go, then press a key 2 3. Ctrl-b is the prefix; on its own it does nothing visible. Below, “Ctrl-b d” means Ctrl-b, release, then d.

Sessions: detach and attach

A session is a workspace that keeps running whether or not anyone’s looking at it, kept alive by a tmux server process on the machine 2 4:

me@garden-server:~$ tmux new -s backup

starts one named backup, and you’re inside it: a shell, with a status bar along the bottom. Run things as usual. Then:

  • Ctrl-b d detaches: you’re back at your ordinary shell, and the session carries on in the background 2. You can log out, or lose the connection; it doesn’t matter.
  • tmux ls lists sessions 2.
  • tmux attach -t backup (or tmux a -t backup) reattaches; plain tmux attach takes the most recent 2.
  • tmux kill-session -t backup ends one, or type exit in its last shell 2.

Quick check

You’re running a long rsync on the server inside tmux and need to close the laptop. What do you do?

Windows and panes

Inside a session, windows are like browser tabs, each a full screen with its own shell 2:

  • Ctrl-b c creates a window; Ctrl-b n and Ctrl-b p go to the next and previous; Ctrl-b 0 to 9 jump by number; Ctrl-b , renames one; Ctrl-b w lists them to pick from 2.

Panes split one window into several shells side by side 2:

  • Ctrl-b % splits side by side; Ctrl-b “ splits top and bottom.
  • Ctrl-b and an arrow key moves between panes; Ctrl-b z zooms one to full size and back 3.
  • Ctrl-b x closes a pane, ending its shell 2.

A typical layout on the server: the job running in one pane, journalctl -f or htop watching in another.

Scrolling back works differently in tmux, since it manages the screen itself: Ctrl-b [ enters scroll mode, then arrows or Page Up and Page Down, and q to leave 2.

Quick check

In tmux, how do you split the current window side by side?

A worked example

Copying a photo collection to the server, which will take an hour:

me@garden-laptop:~$ ssh server
me@garden-server:~$ tmux new -s copy
me@garden-server:~$ rsync -av /mnt/backup/photos/ ~/photos/

Press Ctrl-b d, then exit, and close the laptop. Next morning:

me@garden-laptop:~$ ssh server
me@garden-server:~$ tmux attach -t copy

and there’s the rsync, finished, with its full output above the prompt.

A ~/.tmux.conf

tmux reads ~/.tmux.conf at start 2. Two settings from Ubuntu’s documentation are worth having:

# Click to choose panes and windows, drag borders, scroll with the wheel
set -g mouse on
# Keep 10,000 lines of scrollback instead of 2,000
set-option -g history-limit 10000

After changing it, reload it inside tmux with Ctrl-b : and then source-file ~/.tmux.conf, which keeps your sessions running 2. Add it to your dotfiles (as tmux.conf, with a line in install.sh’s LINKS) so every machine gets it.

Ubuntu’s documentation also mentions Mosh, a replacement for the SSH connection itself that survives changing networks and sleep, if your connection is often flaky 2. tmux is enough most of the time.

Your turn

Exercises

  1. Install tmux and start tmux new -s practice. Run top, detach with Ctrl-b d, and run tmux ls.
  2. Reattach. Is top still running?
  3. Create a second window, switch between them, and rename one.
  4. Split a window both ways, move between the panes, and zoom one.
  5. Scroll back through some long output (try dmesg or journalctl -b) with Ctrl-b [.
  6. Over SSH to another machine, start a session, run sleep 600; echo done, detach, disconnect, reconnect, and reattach.
  7. Add a tmux.conf to your dotfiles repository and link it with install.sh.
Answers
  1. tmux ls shows practice: 1 windows (created ...).
  2. Yes: detaching doesn’t stop anything.
  3. Ctrl-b c, Ctrl-b n and p, Ctrl-b , to rename. The status bar shows the window names.
  4. After reattaching, the sleep is still counting down, or done is printed.
  5. Add "tmux.conf:.tmux.conf" to LINKS, run ./install.sh, then commit and push.

So

tmux keeps shells running on the server whether or not you’re connected. tmux new -s name starts a session, Ctrl-b d detaches, tmux ls lists, and tmux attach -t name reattaches. Inside, Ctrl-b c makes windows (n and p to move), Ctrl-b % and ” split panes (arrows to move, z to zoom), and Ctrl-b [ scrolls back. Start anything long-running on another machine inside tmux, and put a ~/.tmux.conf with mouse support and more history in your dotfiles.

Lesson complete

Nice work.

1day streak
0/1today's goal
–correct

Up next · 7 min

Firewall, Updates, and Accounts

Next lesson
Sources for this lesson
  1. 1
    OpenSSH server (Ubuntu Server documentation). Canonical. verifiedOpenSSH replaces insecure tools like telnet; sudo apt install openssh-server; configuration in /etc/ssh/sshd_config or snippets in /etc/ssh/sshd_config.d/, included at the top so they override (first value wins); check with sudo sshd -t before sudo systemctl restart ssh.service; a mistake can lock you out; ed25519 keys recommended; ssh-copy-id to install a key; chmod go-w ~/.ssh/authorized_keys; sudo journalctl -fu ssh.service to troubleshoot; ssh-import-id gh:user imports keys from GitHub; terminal multiplexers keep sessions through disconnects.
  2. 2
    Terminal multiplexers (Ubuntu Server documentation). Canonical. verifiedtmux, preinstalled on Ubuntu Server: prefix Ctrl+b then a key; d detaches; tmux new-session -s name, tmux ls, tmux attach-session -t name (tmux a -t name), kill-session; windows c, n, p, 0-9, comma to rename, w to list; panes % (side by side) and " (top and bottom), o or arrows to move, x to close; scrollback Ctrl+b [ and q, default 2000 lines; ~/.tmux.conf examples set -g mouse on and set-option -g history-limit 10000, reload with source-file; Mosh for flaky connections.
  3. 3
    Anish Athalye, Jon Gjengset, Jose Javier Gonzalez Ortiz. Command-line Environment (The Missing Semester of Your CS Education, 2026). MIT CSAIL. 2026. verifiedCC BY-NC-SA. How shell programs communicate: arguments, streams, environment variables, return codes, and signals; scripts with #!/usr/bin/env bash, $1 and [[ -f $1 ]], exit codes, errors to >&2; trap with a cleanup function to remove temporary files on exit; signals, job control with Ctrl-Z, bg, fg, and nohup; terminal multiplexers and SSH.
  4. 4
    tmux(1) manual page. OpenBSD manual pages. verifiedTerminal multiplexer: sessions, windows, and panes, managed by a server that keeps running when clients detach; default prefix C-b; configuration in ~/.tmux.conf.