Tmux Attach & Detach: Reconnect to a Running Session
TL;DR — Leave the session running with Ctrl+b then d, and come back with tmux attach -t mysession. Add -d to take the session over from another client.
Attach to a tmux session
Attaching connects your current terminal to an existing session. The session must already exist; tmux attach does not create one itself (only indirectly: if it has to start the server, ~/.tmux.conf runs and may create a session).
# attach to the most recently used session that is not attached
tmux attach
# attach to a specific session by name
tmux attach -t mysession
# attach and detach any other client already on that session
tmux attach -d -t mysession
# attach-session is the long form; tmux a is an abbreviation
tmux attach-session -t mysessionWithout -t, tmux prefers the most recently used unattached session. If every session is already attached elsewhere, it still attaches you as an additional client on the most recently active one instead of failing; use -t to name a session and -d to detach its other clients.
Running tmux attach from a shell inside tmux does not nest clients: tmux refuses it with sessions should be nested with care, unset $TMUX to force. Use tmux switch-client -t mysession (or Ctrl+b s) to move the client you are already in.
Detach from a tmux session
Detaching disconnects your terminal from the session and returns you to the shell. The tmux server keeps running, so every process in the session continues.
| Where you are | How to detach |
|---|---|
| Inside tmux | Ctrl+b then d |
| Shell inside tmux (in a pane) | |
| Shell, targeting one client | |
| Shell, list attached clients first | |
Detaching from a pane works because tmux resolves the client from that pane's session. From a shell outside tmux, -t selects a client (see tmux list-clients); without -t, tmux detaches the current client, or fails if none is attached.
After detaching, tmux prints a confirmation with the session name:
[detached (from session mysession)]An SSH disconnect or a closed terminal window detaches in the same way: the client exits, but the session survives on the server. That is the whole reason to run long jobs inside tmux.
Attach or create in one step
new-session -A is the reliable "attach if it exists, create it if not" form. It avoids the no sessions error and the duplicate-session mistake:
tmux new -As mysession
# -D also detaches other clients, like attach -d
tmux new -ADs mysessionSwitch between sessions without detaching
If the session you want is on the same server, switching is faster than detach plus attach:
| Task | Shortcut / command |
|---|---|
| Pick a session from a list | Ctrl+b s |
| Previous / next session | Ctrl+b ( / Ctrl+b ) |
| Jump to the last session | Ctrl+b L |
| Switch from a shell inside tmux | |
Common attach problems
| Symptom | Cause and fix |
|---|---|
no sessions | Nothing is running on the server. tmux attach starts a server and reports no sessions, while tmux ls does not start one and reports no server running on /tmp/tmux-<uid>/default instead. The remedy is the same for both: start one with tmux new -s mysession, or use tmux new -As mysession next time. |
| The window is smaller after attaching | With the default window-size latest the window is sized to the most recently active client, so it now follows the terminal you just attached from — expected, not a bug. Attach from a terminal at least as large, or keep set -g window-size largest while a bigger client stays attached; largest and smallest follow the largest or smallest client attached to a session with that window, and manual plus resize-window ignores client sizes entirely. |
sessions should be nested with care, unset $TMUX to force | You are already inside tmux. Use Ctrl+b s or tmux switch-client instead of starting a nested client. |
Wrong colours or $TERM after reattaching | The client terminal changed. set -s default-terminal "tmux-256color" sets the TERM of newly spawned panes only (3.8 usually already sets it); it does not control how tmux renders colours to the attached client, which depends on that client's own terminfo. For a wrong pane-side TERM, reload with tmux source-file ~/.tmux.conf and open a new window or split; reattaching alone does not re-read the config, and existing panes keep their original TERM. For wrong colours on the client side, set $TERM in the attaching terminal to a matching terminfo entry, and use terminal-features or terminal-overrides if that terminfo under-reports colour support. |
can't find session | No session matches the target. 3.8 tries an exact name first, then a unique prefix, then a glob pattern, so an ambiguous prefix or pattern gives this error too; use the full name, or =name for an exact match. Check tmux ls; names are case-sensitive. If no sessions exist at all, tmux attach reports no sessions instead. |
See detach, list, and reattach in action
The recording leaves a running job behind, checks the session from the shell, returns to it, and finishes with create-or-attach.
Frequently asked questions
How do I detach from tmux?
Press Ctrl+b, release, then press d. tmux returns you to the shell and prints [detached (from session name)]. The session and every program inside it keep running. From outside tmux you can detach a client with tmux detach-client.
How do I reattach to a tmux session?
Run tmux attach to attach to the most recently used unattached session, or name it explicitly with tmux attach -t mysession. If every session is already attached, tmux attaches you as an additional client instead of failing. Use tmux ls first if you do not remember the session name.
Does detaching kill my running processes?
No. Detaching only disconnects the client from the session; the tmux server and every process in the session keep running. Use tmux kill-session (or Ctrl+b then & / x for windows and panes) when you actually want to stop them.
How do I attach to a session someone else is using?
Run tmux attach -d -t mysession. The -d flag detaches every other client on that session so the session is handed to you. Without -d, several clients can share one session; with the default window-size latest the window follows the most recently active client. Use tmux list-clients to see who is attached.
Why does tmux say "no sessions"?
tmux attach needs an existing session. Run tmux ls: if the server has no sessions, start one with tmux new -s mysession, or use tmux new -A -s mysession to create it or attach if it already exists. A killed server also removes every session.
Continue with the essential command list in tmux commands.