open atlas
↑ Back to track
Command line CLI · 09 · 04

tmux basics

tmux keeps processes running after SSH disconnects by hosting them inside a server process that survives terminal closure. Sessions contain windows; windows contain panes. Detach with C-b d, reattach with tmux attach — your work is exactly where you left it.

CLI Senior ◷ 25 min
Level
FoundationsJuniorMiddleSenior

You start a long-running database migration over SSH. Twenty minutes in, your laptop closes the lid — the SSH connection drops, and the migration process receives a SIGHUP and dies. Half-migrated database, weekend ruined. tmux solves this class of problem permanently. It runs a server process on the remote machine that owns your shell and all child processes. When your SSH connection drops, the tmux server keeps running. When you reconnect, you reattach to the same session and your terminal is exactly where you left it — running migration at 78%, scrollback intact.

tmux also solves a completely separate problem: working in multiple contexts on a single terminal. Sessions, windows, and panes let you run a build, tail logs, and edit a config file simultaneously in one SSH connection.

Goal

After this lesson you can start a named tmux session, navigate sessions, windows, and panes using the prefix key C-b, detach from a session with C-b d, reattach with tmux attach, and explain why tmux sessions survive SSH disconnects while processes in a plain SSH shell do not.

1

tmux survives SSH disconnects because its server process is independent of your SSH session. When you SSH without tmux:

SSH client → SSH connection → bash (your shell)
                                └─ your processes (children of bash)

When the SSH connection drops, the kernel sends SIGHUP to bash. Bash sends SIGHUP to all its children, then exits. Your processes die.

With tmux:

SSH client → SSH connection → tmux client → tmux server (independent process)
                                                └─ your sessions / windows / panes

The tmux server is started the first time you run tmux. It continues running as a daemon on the remote machine regardless of SSH connections. When your SSH drops, only the tmux client dies — the server and everything inside it keep running. Reconnect via SSH, reattach the client, and you are back.

2

Start and name sessions; understand the three-tier hierarchy. The basic hierarchy:

  • Session — a collection of windows; has a name; survives disconnects.
  • Window — one full-screen terminal inside a session; like a browser tab.
  • Pane — a rectangular split of a window; each pane has its own shell.
tmux new-session -s deploy    # create a new session named "deploy"
# or shorter:
tmux new -s deploy

Inside a session, the green status bar at the bottom shows the session name, window list, and hostname. Everything you do inside this session is owned by the tmux server and survives your SSH dropping.

List running sessions from outside tmux:

tmux list-sessions   # or: tmux ls
3

The prefix key C-b precedes every tmux command. All tmux key bindings start with pressing Ctrl+b (the prefix), releasing it, then pressing the command key. You do not hold Ctrl while pressing the second key.

Essential bindings:

ActionKeys
Detach from sessionC-b d
New windowC-b c
Next windowC-b n
Previous windowC-b p
List windowsC-b w
Rename windowC-b ,
Split pane horizontallyC-b %
Split pane verticallyC-b "
Move between panesC-b + arrow key
Kill current paneC-b x
Show key bindingsC-b ?

The most important: C-b d detaches you from the session without stopping it. Everything inside keeps running. This is the operation that makes tmux valuable for long-running remote work.

4

Reattach to a session with tmux attach. After detaching or after your SSH reconnects:

tmux attach            # attach to the most recent session
tmux attach -t deploy  # attach to the session named "deploy"
tmux a -t deploy       # shorthand

If you have multiple sessions and forget their names:

tmux ls
# deploy: 2 windows (created Mon Jun 21 10:30:00 2026)
# monitor: 1 windows (created Mon Jun 21 09:15:00 2026)

Then attach by name. When you attach, the terminal renders the session exactly as it was: scrollback preserved, processes mid-execution, cursor position where you left it.

To kill a session (stop everything in it):

tmux kill-session -t deploy
5

Windows and panes let you multiplex one SSH connection. Practical workflow on a remote server: one window for your editor, one for the build, one for log tailing, all in a single SSH connection with a single tmux session.

# Window 0: running a build
make build

# C-b c → new window
# Window 1: tail application logs
tail -f /var/log/app/production.log

# C-b c → new window
# Window 2: monitor resources
htop

# C-b n / C-b p → switch between windows
# C-b 0 / C-b 1 / C-b 2 → jump directly to window by index

Panes split a window into regions:

C-b %     # split current pane left/right
C-b "     # split current pane top/bottom
C-b →     # move focus to right pane

Each pane is a full independent shell. You can watch logs in one pane while editing a config in another — all in the same SSH connection.

Worked example

Run a long database migration safely using tmux.

# 1. SSH to the server
ssh deploy@server.example

# 2. Start a named tmux session
tmux new -s migration

# 3. Run the migration inside the session
cd /app && ./bin/migrate --env production

# 4. The migration is running. You need to step away.
#    Detach without stopping the migration:
#    Press C-b d
# Output: [detached (from session migration)]

# 5. Your SSH connection can now drop safely.
#    The tmux server keeps the migration running.

# 6. Later: SSH back in and reattach
ssh deploy@server.example
tmux attach -t migration

# The migration output is scrolled and the process is
# still running (or has completed) exactly as you left it.

For extra safety during multi-hour operations, also pipe output to a log file so you can review it even if the session scrollback was lost:

./bin/migrate --env production 2>&1 | tee /var/log/migration-$(date +%Y%m%d).log
Why this works

On macOS, tmux is not installed by default. Install with brew install tmux. The prefix key and all bindings are identical across platforms. One macOS-specific behaviour: clipboard integration requires either reattach-to-user-namespace (older approach) or the set -g mouse on + pbcopy/pbpaste wiring. On Linux, xclip or xsel provides the same. For most remote server work you never need clipboard integration — the terminal emulator’s own selection handles it.

Common mistake

Do not confuse C-b d (detach, session keeps running) with closing the terminal window or typing exit inside tmux (which closes the shell, and if it is the last shell in the session, destroys the session). Also: if you SSH to a server and run a process directly — without tmux — nohup ./script & is a fallback that prevents SIGHUP from killing the process, but it gives you no way to reattach and see live output. tmux is the correct tool. nohup is the emergency workaround when tmux is unavailable.

Check yourself
Quiz

You are running a long process inside a tmux session. Your SSH connection drops. What happens to the process?

Recap

tmux runs a server process on the remote host that owns your sessions. When SSH drops, only the client disconnects — the server and all running processes continue. Sessions (named with -s) survive disconnects and contain windows (like tabs) which contain panes (splits). The prefix key C-b precedes every command: C-b d detaches (session keeps running), C-b c opens a new window, C-b %/C-b " split panes. tmux attach -t name reconnects you to an existing session with scrollback intact. For any long-running remote task — migrations, builds, deployments — start tmux first, then run the command inside it.

Practice

Start at the top. Tasks go easiest → hardest: recall a fact, apply it to a case, then a senior-level stretch. Open one, attempt it, then reveal.

recallapplystretch0 of 4 done

Something unclear?

Ask a question about this lesson. Questions are anonymous and go straight to the author to make the lesson better.

shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources1
expand
  1. 01

Trademarks belong to their respective owners. Editorial reference only.