Attach a tmux session to a remote machine

32

12

I am using tmux on my local machine and usually have several sessions simultaneously.

What I usually do is I have a session with different windows to work locally and the other sessions in which I connect several windows to one host per session.

A session / window tree would look like this in the daily use I make of tmux:

(TMUX on my local machine)
 |
 +- session 1: local
 |  \_ window 1: local shell
 |  \_ window 2: local shell
 |  \_ ...
 |
 + session 2: somehost
 |  \_ window1: ssh user@somehost
 |  \_ window2: ssh user@somehost
 |  \_ ...
 |
 + session 3: someotherhost
    \_ window1: ssh user@someotherhost
    \_ window2: ssh user@someotherhost
    \_ ...

Is there a way to make session 2 & session 3 some sort of remote sessions connecting to a tmux session created on somehost & someotherhost?

The above tree would look like this:

(TMUX on my local machine)
 |
 +- session 1: local
 |  \_ window 1: local shell
 |  \_ window 2: local shell
 |  \_ ...
 |
 + session 2 linked to an existing session on somehost
 |  \_ window1: shell on somehost
 |  \_ window2: shell on somehost
 |  \_ ...
 |
 + session 3 linked to an existing session on someotherhost
    \_ window1: shell on someotherhost
    \_ window2: shell on someotherhost
    \_ ...

I found this topic but I am not sure this is what I want to do: Is sharing a tmux sockets between hosts possible?

I suppose what I am looking for would require me to have the same tmux configuration on my local machine, somehost & someotherhost but that would not be a problem.

lilorox

Posted 2015-02-17T08:40:57.787

Reputation: 323

1Have you figured out a workaround for this? I think having a remote session that you can manage like it's local is great idea, but it looks like the highest voted answer doesn't actually your question – mbigras – 2018-08-03T06:50:56.670

I think this would be amazing if it were possible: submitted a feature request at the tmux project (https://github.com/tmux/tmux/issues/2084) - lets see if anything comes of it...

– jkp – 2020-02-15T15:11:00.527

Answers

18

You can pass a command to ssh when connecting to a remote host. Include the -t option with tmux attach-session to connect to the remote tmux session:

ssh <remote host> -t tmux attach-session

This post on attaching to a tmux session via ssh explains it in more detail.

m__

Posted 2015-02-17T08:40:57.787

Reputation: 317

18Doesn't this just give you a nested session? I believe what the OP is trying to do is have the remote session appear alongside their local session so they can navigate within one tmux's choose-window/choose-tree. – stsquad – 2016-11-18T15:40:45.757

If there are more than one sessions attached at the remote host you can use: ssh <remote host> -t tmux a -t <session-name> – user1757247 – 2018-03-23T03:45:56.593

I made some edits to fix a problem i encountered and a step-by-step. Hopefully peer review will finish soon. This worked ssh <remote host> -t /path/to/tmux a -t <my-session-name>, needed an extra -t – Merlin – 2019-02-28T23:48:09.770

I have the same question as the OP. This doesn't answer my question. It creates a nested tmux session where I have to double escape all the tmux commands I want sent to that session. I want something where the remote session shows up in my session list just like a local session would. – Omnifarious – 2019-05-20T15:58:23.920

2

If your main problem is that you have to type ssh remotehost everytime you open new window, the default-command session option may be a workaround.

It automatically execute the command you specify when you open new windows in the sessions you set it to.

A simple setting in .tmux.conf:

bind-key C-r new-session ssh remotehost \; set-option default-command "ssh remotehost"

This key binding Ctrl-r creates a new session where every newly opened window firstly execute ssh remotehost on behalf of you.

You can choose hosts to connnect at session creation time with the settings below:

bind-key C-r command-prompt "new-session -s %1 ssh %1 \; set-option default-command \"ssh %1\""

This one asks you the host you want to do ssh at each session creation (not at each window creation) and starts the session dedicated to that host.

It takes little time to execute many ssh if you enable SSH multiplexing in your ssh config. The .ssh/config example is below:

Host remotehost
    ControlMaster auto
    ControlPath ~/.ssh/mux-%r@%h:%p
    ControlPersist 10

Note that this tmux setting does not create tmux sessions in remote hosts. If ssh connection is lost, affected tmux windows never return.

somay

Posted 2015-02-17T08:40:57.787

Reputation: 21

1

You could use a tool like socat to tunnel the unix domain socket used by tmux to your local machine. But you are really just swapping a tunnel (ssh) for another (socat) so it is not really better, and also insecure.

untore

Posted 2015-02-17T08:40:57.787

Reputation: 131

Maybe tunneling socat throughssh? – Omnifarious – 2019-05-20T15:59:26.243

at this point tmate might be the best tool for remote tmux handling – untore – 2019-05-21T09:27:27.163

0

If you talking about remote shell, you mean that you can execute commands there, in other words, you have direct access. In this case you can simply use ssh connection for each remote window.

You can tell there something like:

"Hey, but there are no way to have multiple windows for each remote server!"

and it is true, due to ssh restrictions. But, you can start tmux session on each or your remote server. NOTE: in this case you need to rebind default Ctrl+b on local machine to prevent collisions.

Also I recomend you to use tmuxinator, it is very helpful tool if you rectreate your tmux session at least every day.

outoftime

Posted 2015-02-17T08:40:57.787

Reputation: 101