Skip to content

tmux: keep remote work alive across SSH drops

Run long jobs on a remote server that survive your SSH connection dying. Covers the tmux session model, detach and attach, named sessions, send-keys and a minimal config.

You have a six-hour database migration to run over SSH, and at some point you are going to close your laptop lid. If you launch that migration straight from your SSH prompt, the answer is not in doubt: the connection drops, the shell gets a hangup, and your migration dies four hours in with a half-restored database. tmux fixes this by moving your shell off the SSH connection entirely. Your commands run inside a background server process on the remote host that has no idea SSH exists — you attach to look at it, detach to walk away, and reattach from a different machine tomorrow to find the job exactly where you left it. This guide covers the session/window/pane model, new/attach/ls/detach, the prefix key, scripting with send-keys, a small sane config, and the cases where nohup or a systemd unit is the better tool.

What you’ll accomplish

  • Start a named tmux session on a remote host and run a long job inside it.
  • Detach, close your laptop, reconnect from anywhere, and reattach to the same running job.
  • Split a session into windows and panes so the job, its log, and a shell all live side by side.
  • Drive tmux non-interactively from a script with send-keys and capture-pane.
  • Know when nohup, systemd-run, or a real service unit beats tmux.

Prerequisites

  • A remote Linux or BSD host you can SSH into. If you reach it through a bastion, SSH ProxyJump handles the hop; tmux runs on the final host, not the jump box.
  • tmux 2.1 or newer. Everything below works on 2.1+, which covers every supported distro. A few conveniences need more: new-session -A needs 1.8+, and the modern split-window -h/-v flags have been there since forever. Check with tmux -V.
  • Install it if missing. apt install tmux on Debian/Ubuntu, dnf install tmux on RHEL/Fedora/Rocky, apk add tmux on Alpine. It is not always in a minimal container or a fresh cloud image.
  • Version differences are real. tmux 3.x changed a few option names and made -2 (force 256 colours) a no-op. If a config snippet from the internet errors on your box, check your version before assuming the snippet is wrong.
  • tmux runs on the server. You do not need it installed on your laptop.

How it works

The thing to understand — the thing that makes the SSH-drop behaviour obvious rather than magic — is that tmux is not one program. It is two: a client and a server, talking over a Unix domain socket.

When you type tmux and there is no server yet, tmux forks a server process, detaches it from your terminal, and then starts a client that connects to it. The server is the one that creates the pseudo-terminals and becomes the parent of your shells. Your migration’s real parent process is the tmux server, not sshd.

The tmux client/server model: laptop, sshd and tmux client inside a dashed box marked as dying with the SSH connection, connected by a Unix socket to a separate tmux server box that owns three panes running processes

So when your lid closes, the chain that dies is: laptop terminal, sshd session, tmux client. The server was never a child of any of them. It has no controlling terminal to lose. Your pg_restore keeps writing rows to a socket that is still very much open.

Inside the server, state is nested three levels deep:

  • A session is the top-level container and the unit you attach to. tmux ls lists sessions. Give them names.
  • A window is a full-screen tab inside a session. One session, many windows.
  • A pane is a split of a window. One window, many panes; each pane is one shell process.

Session, window, pane hierarchy: one tmux server containing a migration session with two windows, one of which is split into two panes, plus a second detached dev session

Nearly every keystroke you send inside tmux goes to the shell in the current pane. To talk to tmux itself, you press the prefix first — Ctrl-b by default — then the command key. This guide writes that as prefix + d, meaning press and release Ctrl-b, then press d.

Step-by-step

1. SSH in and start a named session. Never run bare tmux for real work — it creates a session named 0, and by next week you will have 0, 1, and 2 with no idea which is the migration. -s names it.

tmux new -s migration

Your prompt clears and a status bar appears at the bottom:

[migration] 0:bash*                          "db01" 14:22 16-Jul-26

The [migration] on the left is your session name. That bar is how you tell a tmux shell from a plain one.

2. Start the long job. This is an ordinary shell. Run the thing.

pg_restore -d appdb --jobs=4 --verbose dump.pgc 2>&1 | tee migration.log
pg_restore: connecting to database for restore
pg_restore: creating TABLE "public.orders"

3. Detach. Press prefix + d. You drop back to your SSH prompt, and tmux confirms:

[detached (from session migration)]

The job is still running. You are simply not watching it. Detaching is the deliberate version of what a dropped connection does to you by accident — same outcome for the server, minus the panic.

4. Close the laptop. Genuinely. Kill the SSH connection, unplug the network, walk away. Nothing in the chain that just died was the parent of pg_restore.

5. Reconnect and list what is running. From the same laptop, another laptop, or your phone — the session belongs to the server, not to any client.

ssh db01
tmux ls
migration: 1 windows (created Thu Jul 16 14:22:11 2026)

If you get no server running on /tmp/tmux-1000/default, see Troubleshooting — that message means something killed the server, not that you mistyped.

6. Reattach. -t targets the session by name.

tmux attach -t migration

Your screen redraws exactly as you left it, pg_restore still spooling. tmux a -t migration is the short form.

7. Split the window into panes. With the job running in pane 0, prefix + " puts a new pane below, and prefix + % puts one beside. Ignore the words “horizontal” and “vertical” here — they are a coin flip in tmux, since " is bound to split-window -v and % to split-window -h, which is the opposite of how most people read those adjectives. Remember the two keys by shape instead: " is stacked, % is side-by-side. Move between panes with prefix + arrow key. In the new pane, watch the log:

tail -f migration.log

prefix + c creates a whole new window instead, and prefix + 0/prefix + 1 switch between them. Panes for things you want to see at once; windows for things you don’t.

8. Use an idempotent attach. The command you actually want in your notes is new-session -A: attach to migration if it exists, create it if it doesn’t. It never errors, so it is safe in a script or a shell alias.

tmux new-session -A -s migration

9. Clean up when the job is done. Exiting the last shell in a session destroys it. To do it from outside:

tmux kill-session -t migration
tmux ls
no server running on /tmp/tmux-1000/default

The server exits on its own when the last session goes. That message is now the correct, healthy answer.

Validate it works

Prove the survival claim rather than trusting it. Start a detached session (-d creates it without attaching) running a marker job:

tmux new -d -s prooftest 'for i in $(seq 1 600); do echo "tick $i"; sleep 1; done > /tmp/proof.log'

Now kill your SSH connection hard — close the terminal window, or press Enter ~ . (the SSH escape sequence to drop a connection). Reconnect and check:

ssh db01
tmux ls
prooftest: 1 windows (created Thu Jul 16 14:31:02 2026)
tail -2 /tmp/proof.log
tick 47
tick 48

The counter kept ticking across the disconnect. That is the whole feature. Confirm the process tree agrees. #{pid} is the server’s PID, and ps will tell you who its parent is:

ps -o pid,ppid,comm -p "$(tmux display-message -p '#{pid}')"
  PID  PPID COMM
 2118     1 tmux: server

PPID 1 is the proof. The server daemonized at startup and was reparented to init, so it never sat below sshd in the tree — there was no parent for your dropped connection to take down.

Tear it down with tmux kill-session -t prooftest.

Troubleshooting

“no server running on /tmp/tmux-1000/default” after you log out. On systemd hosts with KillUserProcesses=yes in logind.conf, logind kills every process in your user slice when your last session ends — tmux server included. This is the one configuration that genuinely defeats tmux. Check it:

grep -i killuserprocesses /etc/systemd/logind.conf
loginctl enable-linger "$USER"

enable-linger tells logind to keep your user manager (and your processes) alive after logout. It needs root or a policy that allows it. Without it, on such a host, use a systemd unit instead.

“sessions should be nested with care, unset $TMUX to force”. You ran tmux from inside tmux. Usually you meant to attach to a session on a remote host from a local tmux pane, and the local tmux caught it. Detach first with prefix + d, or if you really want nesting, prefix your keys twice (prefix + prefix + d sends the prefix to the inner tmux). To force it:

TMUX= tmux new -s inner

“duplicate session: migration”. tmux new -s migration refuses when the name is taken. That is not an error to work around with a different name — you almost certainly want the existing session:

tmux attach -t migration

Or use tmux new-session -A -s migration, which does the right thing either way.

Your window is squeezed into a tiny box with dead space around it. When two clients attach to one session, tmux sizes it to the smallest attached terminal. Your forgotten session on the phone is why your 4K terminal is rendering at 80x24. Kick the other client off as you attach:

tmux attach -d -t migration

-d detaches every other client first. tmux list-clients shows who else is on.

“open terminal failed: missing or unsuitable terminal”. tmux cannot find terminfo for the TERM your terminal advertises — common when a modern terminal sends xterm-kitty or alacritty to an older server that has never heard of either. Fix the server’s terminfo database, or override for one run:

TERM=xterm-256color tmux attach -t migration

A pane closed and you lost the error. By default a pane vanishes when its process exits, taking the message with it. remain-on-exit keeps the dead pane on screen so you can read what happened:

tmux set-option -g remain-on-exit on

Scripting tmux with send-keys

tmux is drivable from outside, which makes it a decent way for a deploy script to hand a long job to a session and walk away. send-keys types into a target pane; the literal word Enter at the end is the key name, not text — leave it off and your command sits at the prompt unrun.

tmux new -d -s migration
tmux send-keys -t migration 'pg_restore -d appdb dump.pgc' Enter

Read the screen back without attaching — -p prints to stdout:

tmux capture-pane -p -t migration | tail -3
pg_restore: creating INDEX "public.orders_pkey"

And test for a session in a script without tripping set -e; has-session exits non-zero when it is absent:

tmux has-session -t migration 2>/dev/null && echo "already running"

Be honest about what this is: you are typing into a terminal from a script. There is no exit status, no structured output, no way to know the command succeeded except by reading pixels. It is fine for handing off an interactive job. It is a poor substitute for a job runner.

A minimal sane ~/.tmux.conf

Resist the thousand-line config. These are the settings that fix real annoyances:

# Ctrl-b collides with readline's back-a-character. Ctrl-a is the screen habit.
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# The default 500ms delay makes vim's Esc feel broken over SSH.
set -sg escape-time 10

# 2000 lines of scrollback is not enough to find where a build failed.
set -g history-limit 50000

# Windows numbered from 1 match the keyboard.
set -g base-index 1

# Click to select a pane, scroll with the wheel. tmux 2.1+.
set -g mouse on

# Reload without killing the server.
bind r source-file ~/.tmux.conf \; display-message "config reloaded"

Apply it to a running server without restarting anything:

tmux source-file ~/.tmux.conf
config reloaded

Note that set -g sets a session or window option while set -sg sets a server option — escape-time is a server option, which is why it takes -s. Getting that wrong is the usual reason a config line silently does nothing.

When this is the wrong approach

tmux is a terminal multiplexer that happens to be good at persistence. That is a narrower thing than it looks.

  • A job that should always run is a service, not a tmux session. If a human must remember to tmux new -s after every reboot, it will be forgotten. A systemd unit gives you restart policy, ordering, resource limits, and logs in the journal. Anything long-lived and unattended belongs there.
  • nohup is enough for fire-and-forget. If you never need to see the job again, nohup ./import.sh > import.log 2>&1 & does it in one line with nothing to install and nothing to reattach to. tmux earns its place when you want to watch and interact, not merely survive.
  • systemd-run is the better one-shot. For a single long command on a systemd host, systemd-run --unit=migration --user pg_restore -d appdb dump.pgc runs it in its own unit — real logs via journalctl -u migration, real status via systemctl status, and immunity to the logind problem above. You get an exit code instead of scrollback.
  • screen is not wrong, just older. If a host already has screen and not tmux, screen -S migration and screen -r migration cover the same ground. Don’t install tmux on someone else’s server to satisfy a preference.
  • Ephemeral hosts make it pointless. In a container or an autoscaled instance that may vanish mid-job, the tmux server dies with the host. tmux protects against your connection failing, not the machine failing.
  • Scrollback is not a log. If you find yourself scrolling a pane to find out what happened last night, you needed a log file. Pipe to tee from the start.

The official tmux wiki is the maintained reference, and the tmux man page is the complete list of commands and options. If you reach your servers through a jump host, SSH ProxyJump and bastions is the companion piece.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.