226

I've opened a tmux session on my local machine, and ssh'd into the remote machine. After this, I typed tmux attach on the remote machine, then I got a remote tmux session on my local tmux session.

Now I want to detach from the remote tmux session, I've tried CTRL+B D but it detached my local tmux session rather than the remote one.

How can I detach the remote tmux sesstion?

superadmin
  • 2,373
  • 2
  • 14
  • 10

8 Answers8

313

CTRL+B CTRL+B D

(assuming default bindings)

The first CTRL+B is interpreted by your local tmux (because it is the first to see all your keystrokes). The second CTRL+B is a command that causes your local tmux to send a CTRL+B to its active pane; this generated CTRL+B arrives at the remote tmux. The D passes through the local tmux unchanged; when it gets to the remote tmux it triggers the detach command.

  1. You type CTRL+B.
    Your local tmux interprets it as the prefix key; nothing is sent to the processes running under the local tmux.
  2. You type CTRL+B.
    Your local tmux has it bound to the send-prefix command.
    1. Your local tmux sends a CTRL+B to the process running in the active pane (ssh).
    2. ssh forwards it (through sshd, etc.) to the process running on the remote end (remote tmux).
      Your remote tmux interprets it as the prefix key; nothing is sent to the processes running under the remote tmux.
  3. You type D.
    Your local tmux passes it through normally (since the second CTRL+B finished a full command key sequence for the local tmux).
    Your remote tmux has it bound to detach-client; it detaches the active client.

This is the same as when you need to send a CTRL+B to any program running inside a tmux session. If you wanted to send CTRL+B to your normal shell (e.g. because your shell is using Emacs-style editing where CTRL+B is backward-char (and you dislike using the arrow keys)) you would need to use CTRL+B CTRL+B to get a single CTRL+B to the shell.

Chris Johnsen
  • 42,029
87

Another way to do it without worrying about the keybindings making it to the right tmux instance is to type tmux detach in the remote tmux session.

13

I tried the first answer without success.

I was able to get the results I wanted by doing the following:

tmux attach

I entered tmux and saw the other session was still attached

So I detached my current session to get back to a shell: CTRL+B D

Then I issued the following: tmux attach -d

This says to attach to the default session, and detach all other sessions currently attached. See the man page under Clients and Sessions

attach-session [-dr] [-t target-session] (alias: attach) If run from outside tmux, create a new client in the current terminal and attach it to target-session. If used from inside, switch the current client. If -d is specified, any other clients attached to the session are detached. -r signifies the client is read-only (only keys bound to the detach-client command have any effect)

bobby
  • 477
3

If you want to try an other option follow below steps

  1. detach current tmux session with CTRL+B D . So that you now you go to normal terminal command line prompt.
  2. Do a tmux ls . Look for session that is currently attached
  3. Execute this command to detach the already attached session tmux detach-client -s <attached_session_name>
Ram
  • 31
  • 1
2

I prefer tmux detach, it doesn't require a keyboard since it's a command.

0

If you have modified your tmux config to use something other than Ctrl+b in both tmux sessions you still need to press Ctrl+b the second time.

For example my binding is Ctrl+/ in both tmux sessions so I press:

C-/ C-b d
0

You can use CTRL+B D to detach and CTRL+B S to see all sessions, CTRL+B C to open a tmux session inside a tmux session

0

Say you ssh into a machine with an attached tmux session So MAC to raspberry pi with TMUX session running

robins-Mac-mini:~ robin$ ssh 192.168.0.22
robin@192.168.0.22's password:
Linux CANMONRPI4 6.1.77-v8+ #1730 SMP PREEMPT Thu Feb  8 15:26:11 GMT 2024 aarch64

The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Sat Dec 14 11:57:47 2024 from 192.168.0.190 robin@CANMONRPI4:~ $ tmux ls 0: 2 windows (created Fri Dec 13 15:31:23 2024) (attached) 1: 1 windows (created Fri Dec 13 15:34:31 2024) 2: 1 windows (created Fri Dec 13 15:34:40 2024) 3: 1 windows (created Fri Dec 13 15:43:53 2024) 4: 1 windows (created Fri Dec 13 16:36:14 2024)

And you want to take over the session, you simply run

tmux attach -t 0

and now you have control of the tmux session!

Robin
  • 161