Skip to main content

Dev tools and workflow · Beginner · 14 min

Keep terminal sessions running on a server with tmux

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · tmux 3.6a (Ubuntu package; upstream latest is 3.8)

You will start a long job inside tmux on a server, drop the connection, reattach to find it still running, and finish with a short ~/.tmux.conf and a script that builds a named dev layout in one command.

On this page

When you run a command over SSH, it belongs to that SSH connection. If the Wi-Fi drops, the laptop sleeps or a mobile data bundle runs out halfway through a database import, the connection closes, the shell gets a hang-up signal and the import dies with it.

tmux fixes that by putting a small server process on the machine between you and your shells. Your shells run inside tmux, and your SSH session only shows you a view of them. When the view goes away, tmux and everything inside it keep running, and you reconnect to the same screen later, with the output still there. The same tool splits one terminal into several windows and panes, which is handy on a server where you cannot open another tab.

You will practise on a harmless "long job" that counts for ten minutes, in a throwaway folder, ~/tmux-demo. Every check uses tmux's own commands, so you can see the result in plain text as well as on screen. If you connect from an Android phone, tmux also runs inside Termux (pkg install tmux), and on an unreliable mobile link it is the difference between losing work and carrying on.

Step 1 · Install tmux

On the server:

Bash
sudo apt update
sudo apt install -y tmux
tmux -V

Ubuntu 26.04 packages tmux 3.6a, so the last line prints tmux 3.6a. The newest upstream release is 3.8; everything in this tutorial works the same on both. On a Mac, brew install tmux gives you the current release.

Check it worked

Bash
command -v tmux

The output is /usr/bin/tmux.

Step 2 · Start a long job in a named session

A session is one tmux workspace: a set of windows that survive together. Give each session a name that says what it is for, so you can find it again. Make the demo folder, then start a session called long-job:

Bash
mkdir -p ~/tmux-demo
tmux new -s long-job

Your screen clears and a green status bar appears along the bottom, showing [long-job] on the left. You are now in a shell that tmux owns. Start the job in it:

Bash
for i in $(seq 1 600); do echo "tick $i"; sleep 1; done | tee ~/tmux-demo/job.log

It prints tick 1, tick 2 and so on, once a second, and copies each line into job.log.

If you run tmux new while already inside tmux, it warns that sessions should be nested with care. Detach first (next step) rather than nesting.

Check it worked

Open a second SSH connection to the server (or a second terminal) and ask tmux what it is running:

Bash
tmux ls
tail -n 2 ~/tmux-demo/job.log

tmux ls lists something like long-job: 1 windows (created Wed Nov 18 10:02:11 2026), followed by (attached) while your first terminal is showing it. The log ends with the latest ticks.

Step 3 · Detach, lose the connection and reattach

Every tmux key binding starts with the prefix, Ctrl-b by default: press and release Ctrl-b, then press the next key. Detach now with Ctrl-b then d. You drop back to your normal shell and tmux prints [detached (from session long-job)]. The job carries on.

Now lose the connection on purpose, the way it happens in real life. Close the terminal window, or kill SSH by typing Enter, ~ and . (the OpenSSH escape that ends a connection at once). It does not matter whether you detached first: when the SSH connection dies, tmux detaches that view for you and keeps the session.

Reconnect to the server and list the sessions, then attach to yours by name:

Bash
tmux ls
tmux attach -t long-job

You are back on the same screen, and the counter has moved on while you were away. If the old connection is still hanging on somewhere (a half-dead SSH session on another device), tmux attach -d -t long-job attaches and detaches every other view at the same time.

Check it worked

Before attaching, tmux ls shows the session without (attached). You can also read what is on its screen without attaching at all, which is useful in scripts:

Bash
tmux ls
tmux capture-pane -p -t long-job | grep '^tick' | tail -n 3

The last three tick lines shown are recent numbers, higher than when you left.

Step 4 · Use windows and panes

A session holds windows, like tabs, and each window can be split into panes. These are the bindings worth learning first. Press the prefix, Ctrl-b, before each:

  • c creates a new window, n and p go to the next and previous one, and 0 to 9 jump to a window by number.
  • , renames the current window, and $ renames the session.
  • % splits the current pane left and right, and " splits it top and bottom.
  • The arrow keys move between panes, and z zooms the current pane to fill the window and back.
  • x closes the current pane and & the current window, after asking you to confirm with y.
  • w shows a tree of every session and window to pick from, and s shows just the sessions.
  • d detaches, ? lists every binding, and : opens tmux's command prompt.

In the long-job session, press Ctrl-b then c for a new window, then Ctrl-b then , and rename it to logs. In that window press Ctrl-b then % to split it. In the right-hand pane, follow the log:

Bash
tail -f ~/tmux-demo/job.log

Move to the left pane with Ctrl-b then the left arrow, and you have a free shell next to a live log. Go back to the job's window with Ctrl-b then 0.

Every key binding is a tmux command underneath, and you can type the command at the Ctrl-b : prompt or run it from a shell. Ctrl-b c is new-window, and Ctrl-b % is split-window -h.

Check it worked

Bash
tmux list-windows -t long-job -F '#{window_index} #{window_name} #{window_panes} panes'

The output shows two windows: 0 with 1 pane (its name is the command running, such as bash) and 1 logs 2 panes.

Step 5 · Scroll back and copy with copy mode

The output that scrolled off the top of a pane is still held by tmux, but your terminal's scroll bar cannot reach it, because tmux draws the screen itself. Copy mode lets you move through it.

  • Ctrl-b then [ enters copy mode. A position marker such as [0/37] appears in the top right corner.
  • Move with the arrow keys and Page Up / Page Down. Press q to leave.
  • Search backwards with Ctrl-r and forwards with Ctrl-s, then type the text, such as tick 5.
  • To copy, move to the start, press Ctrl-Space, move to the end and press Alt-w. Paste into any pane with Ctrl-b then ].

Those are the default emacs-style keys. tmux switches copy mode to vi-style keys (/ and ? to search, Space and Enter to select and copy) if your VISUAL or EDITOR variable contains vi. You can set it yourself with setw -g mode-keys vi in the config in the next step.

Check it worked

Enter copy mode in the job's window and press Alt-< (or g with vi-style keys) to jump to the top of the history: near the top, just under the command you typed, is tick 1. A search such as Ctrl-r then tick 5 stops at the nearest match above the cursor instead, so it may land on tick 59 rather than tick 5. The same history is available as text from outside, with -S - meaning "from the start of the history":

Bash
tmux capture-pane -p -S - -t long-job:0 | grep -m 1 '^tick'

The output is tick 1.

Step 6 · Write a short ~/.tmux.conf

The defaults are usable, but four changes help almost everyone: mouse support, windows and panes numbered from 1 so the numbers match the keyboard, a longer scrollback, and windows that renumber when one closes.

The next command replaces ~/.tmux.conf. If you already have one, it is copied to ~/.tmux.conf.bak first:

Bash
if [ -f ~/.tmux.conf ]; then cp ~/.tmux.conf ~/.tmux.conf.bak; fi
cat > ~/.tmux.conf <<'EOF'
# Click to pick panes and windows, drag borders to resize, scroll to enter copy mode
set -g mouse on

# Number windows and panes from 1, and close gaps when a window closes
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on

# Keep 50,000 lines of scrollback per pane (applies to new panes)
set -g history-limit 50000

# Prefix then r reloads this file
bind r source-file ~/.tmux.conf \; display-message "Config reloaded"
EOF
tmux source-file ~/.tmux.conf

tmux reads this file when its server starts. source-file loads it into the server that is already running, so you do not have to stop your job. After that, Ctrl-b then r reloads it whenever you edit it. Existing windows keep their numbers and existing panes keep their old history limit; new ones get the new settings. With the mouse on, hold Shift while selecting text if you want your terminal's own selection instead of tmux's.

Check it worked

Bash
tmux show -g mouse
tmux show -gw pane-base-index
tmux new-session -d -s conf-check
tmux list-windows -t conf-check -F '#{window_index}'
tmux kill-session -t conf-check

The output is mouse on, pane-base-index 1, and 1: the first window of a new session is now window 1.

Step 7 · Build a dev layout with a script

Setting up the same windows by hand every morning gets old. tmux commands work from a script, so you can describe a layout once. This one makes a session called dev with an editor window and a logs window split into two panes, does nothing if the session already exists, and attaches you to it.

Bash
cat > ~/tmux-demo/dev-layout.sh <<'EOF'
#!/usr/bin/env bash
set -euo pipefail

SESSION=dev
DIR="$HOME/tmux-demo"

if ! tmux has-session -t "$SESSION" 2>/dev/null; then
  # -d: create it in the background, so the commands below can fill it in
  tmux new-session -d -s "$SESSION" -n editor -c "$DIR"
  tmux send-keys -t "$SESSION:editor" 'ls -la' Enter

  tmux new-window -t "$SESSION" -n logs -c "$DIR"
  tmux send-keys -t "$SESSION:logs" 'tail -f job.log' Enter
  bottom=$(tmux split-window -v -t "$SESSION:logs" -c "$DIR" -P -F '#{pane_id}')
  tmux send-keys -t "$bottom" 'date' Enter

  tmux select-window -t "$SESSION:editor"
fi

# Attach only when run from a terminal; switch instead when already inside tmux
if [ -t 1 ]; then
  if [ -n "${TMUX:-}" ]; then
    tmux switch-client -t "$SESSION"
  else
    tmux attach -t "$SESSION"
  fi
fi
EOF
chmod +x ~/tmux-demo/dev-layout.sh
~/tmux-demo/dev-layout.sh

The send-keys ... Enter lines type a command into a pane and press Enter, exactly as you would. split-window -P -F '#{pane_id}' prints the new pane's ID, such as %7, so the script can target that pane without guessing its number. Targets are written session:window, using names, which keeps the script working whatever your base-index is.

Run the script once and you are dropped into dev. Detach, run it again, and it attaches to the existing session instead of building a second one. Copy it into a project and change the window commands to the ones you use, such as your editor, a test watcher and a server log.

Check it worked

Bash
tmux list-windows -t dev -F '#{window_name} #{window_panes}'
sleep 2
tmux capture-pane -p -t 'dev:logs.{top}' | grep '^tick' | tail -n 1

The first command prints editor 1 and logs 2. The second prints a recent tick line from the top pane of the logs window ({top} is tmux's name for the topmost pane), which is following the job's log.

Step 8 · Clean up

Close the demo sessions. kill-session ends every program running in that session, so check the name before you run it:

Bash
tmux kill-session -t dev
tmux kill-session -t long-job
tmux ls || echo "no sessions left"
rm -rf ~/tmux-demo

The last tmux ls prints something like no server running on /tmp/tmux-1000/default, then no sessions left, because tmux stops its server when the last session closes. Keep ~/.tmux.conf for everyday use. To undo it, delete it with rm ~/.tmux.conf, and if the earlier step made ~/.tmux.conf.bak, move that back with mv ~/.tmux.conf.bak ~/.tmux.conf.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.