How to Detach a Session in Tmux: 4 Comprehensive Methods

Written by

Reviewed by

Last updated: June 25, 2023

Expert verified

SVG Image


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:
<strong>trap "tmux detach-client -s `tmux display-message -p "#S"`" EXIT</strong>
  1. Save the file and exit the text editor.
adding code to configuration file

3 Common Errors When Detaching Session in Tmux

To detach a session in tmux, you may encounter some common errors. Remember to review any error messages carefully, as they often provide valuable insights into the cause of the issue. Here are three frequently encountered errors and their solutions:

  • 🔍 “No sessions found.” error: This error occurs when attempting to detach a session in tmux with a specified name or ID that does not exist. Double-check the session name or ID, ensuring it matches the active sessions listed with tmux ls. If the session doesn’t exist, create a new session using tmux new-session -s SESSION_NAME or use the correct existing session name or ID.
  • 🔑 “Permission denied (public key)” error: This error arises when detaching a tmux session remotely via SSH, indicating SSH authentication failure due to incorrect or missing SSH keys. Generate SSH keys locally, copy the public key to the remote server’s authorized_keys file, and set the correct permissions. Test the SSH connection with ssh user@remote_server to resolve the error.
  • 🔍 “Cannot find session” error:  This error occurs while attempting to reattach a detached session using tmux attach-session, but the specified session is not found. Check the active sessions using tmux ls. If the session isn’t present, it may have been terminated. Verify the correct session name or ID and ensure it matches the available sessions for successful reattachment.

In Conclusion

You have now gained a comprehensive understanding of detaching sessions in tmux, equipping yourself with valuable methods to enhance your command-line productivity. Throughout this article, I have explored four powerful techniques for detaching sessions and learned how to overcome common errors that may arise along the way.

To explore further into the world of tmux and optimize your productivity, I recommend exploring the following articles, Tmux Session Management, Customizing tmux for Personalized Workflow Optimization, and Effective Collaboration with tmux. By expanding your knowledge and leveraging the power of tmux, you can unlock new levels of efficiency and streamline your Terminal-based workflow.

Frequently Asked Questions

How can I reattach a detached tmux session?

To reattach a detached session, you can use the following command, tmux attach-session -t SESSION_NAME. Replace SESSION_NAME with the name or ID of the detached session that you want to reattach. This command will connect you back to the specified session, allowing you to resume your work where you left off. It is important to ensure that the session you are trying to reattach exists and is not terminated. Using this command, you can easily switch between different tmux sessions and seamlessly continue your workflow.

Is it possible to detach and reattach sessions on different devices?

Yes, it is indeed possible to detach and reattach tmux sessions on different devices using SSH (Secure Shell). By detaching a session on one device and reattaching it on another, you can seamlessly transfer your work environment between devices without losing progress. To achieve this, follow the same detachment method mentioned earlier for detaching remotely via SSH. Make sure you have SSH access to both the source and target devices. This capability allows for flexible and convenient work scenarios, enabling you to access and manage your tmux sessions across various machines or servers.

Can I detach a session without closing any running processes?

Absolutely! The methods discussed in this article allow you to detach a session in tmux without terminating any running processes. When you detach a session in tmux, all the processes and applications running within that session continue to operate in the background, unaffected by the detachment. This ensures that your work progresses uninterrupted, even when you switch to a different session or device. Whether it’s a long-running command, a server process, or any other task, detaching a tmux session keeps everything running smoothly. You can later reattach the session to resume your work right where you left off, without disrupting your ongoing processes.



Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.



Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

3 Best Ways to Swap Panes in Tmux

Next Post

How to Find IP Address in Linux Command Line [5 Easy Methods]

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next