Tmux attach and detach: connect to a session and leave it running

11 November 2026 · Daniel Bilsborough · Reviewed 11 November 2026

Attach: tmux attach -t name. Detach: press Ctrl-b, then d. The session keeps running on the server after you detach, and attaching again puts you back on the same screen.

Below: finding the session name, creating and attaching in one command, what to do when the window is the wrong size, and reconnecting after an SSH drop.

Attach to an existing session

List what's running first:

$ tmux ls
agents: 3 windows (created Wed Nov 11 09:14:02 2026) (attached)
logs: 1 windows (created Wed Nov 11 09:20:47 2026)

The name is the part before the colon. Then connect to the session by name:

tmux attach -t logs

attach is short for attach-session, and tmux a works too. Without -t, Tmux picks the most recently used session, preferring one that nothing else is attached to. Sessions you never named are numbered from 0, so tmux attach -t 0 attaches to the first unnamed one.

If no server is running, tmux ls reports that there's no server, which means there are no sessions to attach to.

Create a session, or attach if it already exists

To create a new session with a name:

tmux new -s work

Naming sessions makes attaching later a short command. The most useful form for daily use combines both jobs: -A attaches to work if it exists and creates it if it doesn't.

tmux new -A -s work

From a laptop, this opens SSH and lands in the session in one line. The -t gives SSH a terminal to run Tmux in:

ssh -t you@server tmux new -A -s work

Detach without killing the session

Detach and exit are different. exit closes the shell, and when the last shell in a session closes, the session ends. Detach when something should keep running; exit when you're done with it. To end a session from outside, tmux kill-session -t work.

Attaching from a second device: the window size

A session can have several clients attached at once, say a laptop and a phone. By default each window is sized to the most recently active client, but older versions and some settings size it to the smallest, leaving dots around the edge on the bigger screen. To take the session over and detach everyone else:

tmux attach -d -t work

Moving between sessions from inside Tmux

Running tmux attach inside a session gives "sessions should be nested with care, unset $TMUX to force". Switch instead:

Keys or commandWhat it does
Ctrl-b sPick a session from a list
Ctrl-b ( / Ctrl-b )Previous / next session
Ctrl-b LBack to the last session you were in
tmux switch-client -t logsSwitch to a session by name

Reconnecting after an SSH drop

When the SSH connection drops, the Tmux client goes with it, but the session is untouched: Tmux treats it as a detach. Whatever was running, a build, a log tail or an AI coding agent, keeps going.

  1. SSH back in.
  2. tmux ls if you need the name.
  3. tmux attach -t name, or tmux new -A -s name to cover both cases.

The session only disappears if the server reboots, someone kills it, or the last program in it exits.

Attach and detach from a phone

On an iPhone or iPad the commands are the same; the keys are the awkward part. In Shell Drop:

The Tmux cheat sheet has the rest of the session, window and pane commands, and Tmux scrollback covers scrolling up once you're attached.

Questions

How do I attach to a tmux session?

Run tmux ls to see the sessions, then tmux attach -t name with the session's name or number. tmux a attaches to the most recently used session, preferring one nobody else is attached to.

How do I detach from tmux?

Press Ctrl-b, let go, then press d. The session keeps running on the server. From a shell inside Tmux, tmux detach does the same.

How do I reconnect to a tmux session after SSH disconnects?

SSH back in and run tmux attach -t name, or tmux a for the most recent session. A dropped connection only detaches the session, so everything inside it kept running.

What does “sessions should be nested with care” mean?

You ran tmux attach or tmux new from inside a Tmux session. Detach first and attach from a plain shell, or switch sessions from inside with Ctrl-b s or tmux switch-client -t name.

Does detaching kill the programs in the session?

No. Detaching only disconnects your terminal. Programs keep running until they finish or the session is killed with tmux kill-session -t name.

Get Shell Drop on the App Store QR code for Shell Drop on the App Store Reading on a computer? Scan the code with your iPhone camera.