How to Detach Tmux Session: 4 Comprehensive Methods

Written by

Reviewed by

Last updated: May 16, 2024

Expert verified

SVG Image


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

  1. Shortcut Key Combination: Detach a tmux session quickly by pressing Ctrl+b followed by d.
  2. Command-Line Option: Detach a tmux session using the command tmux detach in the terminal for more granular control.

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

Managing multiple tasks on a Linux system can be challenging, especially when you need to keep your work organized and accessible. Luckily, tmux offers a powerful solution. If you’ve ever wondered how to detach a tmux session to keep your workspace tidy, you’re in the right place. In this post I will show you several ways to detach session Tmux, making it easy to switch tasks without losing progress. I’ll cover shortcut key combinations, command-line options, handling nested sessions, and automating the detachment process. By the end, you’ll know how to manage tmux sessions efficiently, making your Linux experience smoother and more productive.

How to Detach Tmux Session?

To detach Tmux session, you can use a simple shortcut. Press Ctrl+b followed by d. This key combination will detach the current session, leaving it running in the background. You can then reattach to the session later without losing any work. This method is quick and efficient for managing multiple tasks.

That was the quick answer. Here is the detailed step-by-step guide of four different methods to Tmux exit session:

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 Tmux disconnect session.
pressing keys
  1. The output will be:
detaching tmux session

2. Command-Line Option

An alternative approach to Tmux detach from session 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:
tmux detach

The Tmux detach command will detach the specified session.

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. Detach from Tmux session that is detached 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:
tmux switch-client -t PARENT_SESSION_NAME

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:
trap "tmux detach-client -s `tmux display-message -p "#S"`" EXIT
  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.

Tmux Detach Session: Wrapping Up

In this article, I provided step-by-step methods to detach a tmux session using shortcuts, command-line options, handling nested sessions, and automating the process on shell exit. Additionally, I covered troubleshooting common errors you might encounter, such as “No sessions found” and “Permission denied (public key),” ensuring you can smoothly manage your tmux sessions.

If you found this guide helpful, consider exploring these related topics

  • Discover how to swap panes in tmux to manage your workspace more effectively, allowing you to reorganize panes with ease
  • Learn to adjust pane sizes in tmux, improving your ability to multitask and enhancing visibility within your sessions
  • Enhance your navigation skills by mastering how to scroll up and down in tmux, making it easier to review content within your sessions

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.

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.

Is there a way to Tmux detach without closing a session?

Yes, you can easily detach a tmux session without closing it. Use the shortcut Ctrl+b followed by d to detach. This will leave the session running in the background, allowing you to reattach later without losing any progress or active processes.

Can I detach multiple Tmux sessions at once?

While tmux does not support detaching multiple sessions simultaneously with a single command, you can script this process. Create a loop that iterates through each session ID and detaches them individually using the command tmux detach -t SESSION_NAME. This allows for batch detachment through automation.



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 Tmux Switch Panes

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