50

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.

2
  • 1
    Have 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
    Aug 3 '18 at 6:50
  • I think this would be amazing if it were possible: submitted a feature request at the tmux project (github.com/tmux/tmux/issues/2084) - lets see if anything comes of it...
    – jkp
    Feb 15 '20 at 15:11
26

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.

4
  • 34
    Doesn'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
    Nov 18 '16 at 15:40
  • 1
    If there are more than one sessions attached at the remote host you can use: ssh <remote host> -t tmux a -t <session-name> Mar 23 '18 at 3:45
  • 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
    Feb 28 '19 at 23:48
  • 6
    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. May 20 '19 at 15:58
4

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.

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.

2
  • Maybe tunneling socat throughssh? 🙂 May 20 '19 at 15:59
  • at this point tmate might be the best tool for remote tmux handling
    – untore
    May 21 '19 at 9:27
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.