How to Detach a Session in Tmux?

The Tmux is an open-source application used to open several virtual terminals. It efficiently manages the virtual terminal, and we can open multiple terminals or panes in a terminal that can be attached or detached from each other. We can adjust the Tmux panes by switching between different panes, moving the panes from one window to another, and resizing.

The guide will teach you how to detach a session in Tmux with the following supporting content:

Let’s get into the basics.

How to Open an Attached and Detached Tmux Session in linux?

To open a simple tmux session by using its name, the “s” (select) option is used as shown in the below general command:

$ tmux new -s <session-name>

For instance, to open an attached using named “session name”, use the below command:

$ tmux new -s session1

We can open the attached or detached session directly from the terminal. The default opened session will be an attached session, while we can use the “d” option to start a detached session.

For instance, to open a detached session named “session2”, run the below command in the terminal:

$ tmux new -s session2 -d

To check the above two sessions that session1 is attached and session2 is detached, execute the following “ls” command:

$ tmux ls

After detaching the session, you will return the terminal from where you opened the tmux and using the “ls” command verifies that session2 is detached.

How to detach a session in Tmux?

To detach a session means that you are exiting the current session, and different methods are used to detach a session in Tmux.

Note: This write-up will use three Tmux sessions, named “session1”, “session2”, and “session3”.

Method 1: Using detach Command

The common way to detach a Tmux session is using the “detach” command by writing the specific session name with it:

Note: “tmux ls” list all the Tmux sessions.

$ tmux detach

After running the detach command, you will detach from the other Tmux open sessions and will be back to the Linux shell terminal. While the detached session will be running in the background. The desired session is detached, which can be verified by running the “ls” command:

$ tmux ls

The current working session, named “session1” is detached from other sessions.

If you want to detach a session using its name, the “s” (select) option is used. For instance, to detach a session named “session2”, the below command is used:

$ tmux detach -s session2

After executing the above command, the session2 will be detached, and will the below output on the terminal that can be verified using the “ls” command:

$ tmux ls

Method 2: Using Shortcut Key (Ctrl+b d)

To detach a session using the shortcut key, you must use the prefix “Ctrl + b” and then press the “d” key to detach or exit the current session.

For instance, to detach a session, let’s first check the current active attached sessions by using the “ls” command:

$ tmux ls

After pressing the “Ctrl + b” prefix keys and then the “d” key, the current working session will be detached that will show the detached output which can be verified using the “ls” command:

$ tmux new -s session2

Method 3: Using detach-client Command

The detach-client can be used to detach the Tmux session from other active sessions. To detach a session, use the below command:

$ tmux detach-client

After executing the detach-client command, the user will detach the current working session from all other active sessions:

$ tmux new -s session1

The current working session “session1” is detached from other sessions.

Similarly, we can detach any Tmux session by using its name. For instance, to detach the session3 by using its name, execute the below command in Tmux session (terminal):

$ tmux detach-client -s session2

After the detach-client command execution, the “session2” will be detached from other attached sessions by showing the detached [(from session session2)] output. The output can be verified by the “ls” command:

$ tmux new -s session2

Method 4: Suspend Tmux Session

The suspend command can be used in the Tmux session to suspend that session and detach it from other active sessions. Let’s check all the currently active sessions using the “ls” command and use the below command to detach the session:

$ tmux suspend

The execution of the “tmux suspend” command shows the below output “stopped”, which can be verified using the “ls” command:

$ tmux new -s session1

Method 5: Detach a Tmux Session From Sessions Menu

We can detach any session from the sessions menu. The sessions menu list can be opened by pressing the Tmux prefix command “Ctrl + b” and then by “Shift + d”:

All the active sessions can be seen in the above output.

Now, select the desired session using the “up or down” arrow keys; in this case, session3 is selected. After selecting the desired session, use the “d” key to detach it:

After pressing the d shortcut key to detach it, the below output will show that your selected session is detached:

Method 6: By Closing the Terminal (GUI)

We can simply press the close option (X) at the top-right corner of the specified Tmux session terminal to detach it from other active sessions:

After pressing the close icon symbol (x), a prompt will show with a warning to kill it. Press the “Close Terminal” option to detach it:

To verify that the session is detached from other active Tmux sessions use the “ls” command:

$ tmux ls

It verifies that the session is detached.

These are all the methods to detach a session in tmux.

Conclusion

To detach a session in Tmux means exiting the session, which can be done using the several methods explained in this write-up. After detaching the session, you can attach that session again to other active sessions. Moreover, we have also learned the methods to open attached and detached Tmux sessions directly from the Linux terminal.