Skip to main content
Terminal Multiplexer:

tmux: How to attach and reattach sessions

Summary

This article explains how to use tmux to attach, reattach, and manage terminal sessions. It includes practical examples for listing active sessions, attaching sessions, handling session conflicts, and a list of available tmux attach options.

Introduction #

tmux allows you to manage multiple terminal sessions from a single terminal window. One of its features is the ability to detach from sessions and reattach later.

This article explains how to attach to tmux sessions, how to reattach after disconnection, and how to list all available sessions.

List active tmux sessions #

Before you attach to any session, you should check what sessions are currently active.

Run the following command:

tmux list-sessions

This command can also be shortened to:

tmux ls

Example output:

work: 1 windows (created Mon May 20 14:23:18 2025) [80x24]
mysession: 3 windows (created Mon May 20 13:11:04 2025) [80x24] (attached)

In this output:

  • work and projectX are the session names.
  • projectX is currently attached in another terminal.
  • work has 1 window, while mysession has 3 windows.
  • The value inside the square brackets, [80x24], represents the terminal dimensions (columns x rows) for the session’s window size at the time it was created. In this case, it indicates 80 columns and 24 rows.

Knowing the session names allows you to target them specifically when attaching.

Create sessions for future attachment #

If you plan to return to a session later, you should name it when creating it:

tmux new -s mysession

This starts a new session named mysession. Later, you can reattach to it using:

tmux attach -t mysession

Attach a tmux session #

You will find detailed explanations and examples for each command to understand how session management works in tmux.

Attach to the most recent session #

If you have only one session or want to attach to the last used session, you can run:

tmux attach

or the longer version:

tmux attach-session

This command will attach you to the most recently used session that is not currently attached.

Attach to a specific session #

If you have multiple sessions, you need to specify which one you want to attach to. Use the -t flag followed by the session name:

tmux attach -t mysession

This will attach you to the session named mysession.

You can also use session identifiers if they are known:

tmux attach -t 0

Session identifiers are typically shown when using tmux list-sessions.

Force attachment and detaching other sessions #

If a session is already attached elsewhere, and you want to attach to it while forcing other clients to disconnect, use the -d (detach) option:

tmux attach -d -t mysession

This command forcibly detaches any other clients connected to the mysession session and attaches you to it.

Named sessions are easier to remember and distinguish, especially when multiple users or automation tools are involved.

Reattach a detached tmux session #

If you detach from a tmux session (using Ctrl-b d or tmux detach), the session continues running in the background. To reattach, use the same attach-session command.

Reattaching after detaching #

  1. Detach from the current session by pressing Ctrl-b followed by d.
  2. Reattach to the same session:
tmux attach

If multiple sessions exist, specify the session name, e.g. mysession:

tmux attach -t mysession

Also see how to attach to the most recent session.

Reattach after SSH disconnection or terminal close #

If your SSH connection drops or you close the terminal window, your tmux session remains alive on the server.

To reconnect:

  1. SSH back into the server.

  2. List sessions:

    tmux ls
    
  3. Reattach to the session:

    tmux attach -t mysession
    

No data or state is lost.

Relevant tmux attach options #

OptionDescriptionExample command
-tSpecifies the name or number of the session you want to attach to.tmux attach -t work
-dDetaches other clients attached to the session before attaching you.tmux attach -d -t work
-rAttaches the session in read-only mode. You cannot send input, but you can observe output.tmux attach -r -t work
-2Forces tmux to use 256-color mode, even if the terminal seems to not support it.tmux attach -2 -t work
-CCEnables control mode, which is used when tmux is controlled by an external program (e.g. IDE).tmux attach -CC -t work

FAQ's #

Most common questions and brief, easy-to-understand answers on the topic:

How do I reattach to a tmux session after disconnecting?

Use tmux attach or tmux attach-session to reconnect to the last active or a specific session.

How do I list all active tmux sessions?

Run tmux list-sessions or tmux ls to display all active tmux sessions.

What happens if I attach to a session that is already attached elsewhere?

tmux will display a warning, but the session can still be joined if configured to allow multiple clients.

Can I name a tmux session for easier identification?

Yes. You can create a named session with tmux new -s mysession and attach to it later using tmux attach -t mysession.

How do I force attach to a session and disconnect other clients?

Use tmux attach -d -t mysession to forcibly detach other clients and connect yourself to the session.

What happens if I detach from a tmux session?

The session continues running in the background, and you can reattach later.

Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

tmux: How to attach and reattach sessions by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/how-to-attach-tmux-session">tmux: How to attach and reattach sessions</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the Ditig legal page.

All Topics

Random Quote

“Premature optimization is the root of all evil.”

Donald Ervin Knuth American computer scientistStructured Programming with Goto statements, - IT quotes