Published: July 22, 2026
Read: 7 min
In: Technology & Development

Written by

Reviewed by

Last updated: June 25, 2023

Expert verified

SVG Image

TL;DR

To detach a session in Tmux, you can try these four methods:

  1. Shortcut Key Combination: Press Ctrl+b, then d.
  2. Command-Line Option: Run the command tmux detach.
  3. Detaching from a Detached Session: Use the command tmux switch-client -t PARENT_SESSION_NAME, then press Ctrl+b, then d.
  4. Automatically on Shell Exit: Add the line trap tmux detach-client -stmux display-message -p #S EXIT to your shell configuration file.

Detaching sessions in tmux can sometimes result in common errors. Check for “No sessions found.” error by verifying the session name or ID, resolve “Permission denied (public key)” error by generating SSH keys and testing the connection, and address the “Cannot find session” error by checking active sessions and ensuring the correct session name or ID is used for reattachment.

Read the article below to learn how to detach a session in tmux and discover common errors with possible solutions.

In the world of command-line productivity, where multitasking and efficiency are paramount, tmux stands as a robust tool for managing Terminal sessions. But what sets tmux apart from other Terminal multiplexers is its ability to detach sessions. The power of detaching sessions in tmux lies in its ability to liberate your workflow, enabling you to seamlessly switch between tasks, devices, and even network connections. In this article, I will explore four comprehensive methods to detach a session in tmux, equipping you with the knowledge to harness its full potential while overcoming common errors. 

How to Detach a Session in Tmux

1. Shortcut Key Combination

The most common and convenient method of detaching a tmux session is by using a predefined shortcut key combination. A significant majority of tmux users favor this method due to its simplicity and speed. To detach a session in tmux using this method, follow these steps:

  1. Access the tmux command prompt.
tmux session
  1. Press Ctrl+b, then release the Ctrl key and press d to detach the session.
pressing keys
  1. The output will be:
detaching tmux session

2. Command-Line Option

An alternative approach to detaching tmux sessions is by utilizing a command-line option. This method provides flexibility and enables automation in detaching sessions. The command-line option offers more granular control over detaching sessions and is favored by users who prefer a command-driven workflow. To detach a session in tmux using a command-line option, follow these steps:

  1. Open your Terminal window.
opening terminal 8
  1.  Enter the following command:

<strong>tmux detach</strong>
  1. Execute the command, and the specified session will be detached.
detach from current running session of tmux

3. Detaching from a Detached Session

In complex workflows and multitasking scenarios, detaching from an already detached tmux session can be beneficial. This method enables you to detach from a nested session while preserving the state of the parent session. Detaching from a detached session allows you to navigate through nested sessions effortlessly, ensuring optimal organization and control over your work. To detach from a detached session, follow these steps:

  1. Identify the nested session you want to detach from using the tmux ls command.
lisitng active sessions
  1. Switch to the parent session using the tmux switch-client command:

<strong>tmux switch-client -t PARENT_SESSION_NAME</strong>

Replace PARENT_SESSION_NAME with the name or ID of the parent session.

switching between the sessions
  1. Detach the nested session using the shortcut key combination Ctrl+b, followed by d.
pressing keys to detach a session in tmux
  1. The output will be:
session detached after pressing the keys

4. Automatically on Shell Exit

To prevent accidental termination of sessions and ensure seamless detachment when exiting the shell, automating the detachment process can be immensely helpful. Enabling automatic detachment on shell exit ensures that tmux will automatically detach the current session whenever you exit your shell, preserving your work and preventing accidental session closure. To enable automatic detachment on shell exit, follow these steps:

  1. Open your shell configuration file, such as .bashrc or .zshrc, in a text editor.
opening shell configuration file
  1. Add the following line at the end of the file: