Can tmux remove ssh connection

Can I run tmux locally and connect via ssh to the remote computer .. and after that any new panel and / or screen will work with the remote shell machine ... What I say, I can not install tmux on the remote machine, but I don’t want to make an ssh connection from each panel, but ssh-login only once.

It is possible. thanks

+7
source share
4 answers

If you want to log in only once, you can use the ControlMaster function for ssh. Add the following configuration, for example to your ~/.ssh/config :

 ControlMaster auto ControlPath /tmp/ssh_mux_%h_%p_%r 

If you log on to the same server (the same user) several times (either on the same tmux or not), ssh will reuse the connection, so you won’t need to reconnect and log in.

+5
source

lilydjwg explained what I never understood before. Knowing how to configure ControlMaster makes the following much more reasonable, as it makes it easier to create multiple ssh connections. You only need to authenticate once, and the remote host does not need to execute the sshd process for each connection.

In your .tmux.conf file:

 # What host do you usually log in to? # We'll ssh there by default each time a new window or pane is opened. REMOTE_HOST=your.usual.host set-option -g default-command "ssh $REMOTE_HOST" # Simple interface to change which host is connected to when you create # a new window or pane. bind-key Ch command-prompt -p "Set remote host: " -I $REMOTE_HOST "set-option default-command 'ssh %%'" # In case you really do want a new window with a local shell. bind-key C new-window "" 
+4
source

I do not think tmux can. One way would be to add something like this to tmux.conf.

 bind-key X new-window "ssh HOST" 

Then, new windows will start on the remote host.

+3
source

I am using tmux 1.8 and did not find a built-in solution. These workarounds are suitable, at least for my general use cases:

  • Capturing the contents of the full panel and searching for the last ssh command in it (I use the knowledge of completing my tooltip to find the command more or less reliably)
  • If this fails, I will check the command that the panel could be created using the shell-command parameter of the tmux new-window or split-window commands

My reconnect.sh script looks like this. The dirtiest thing about this is the way to get the latest ssh command from the buffer. So far, "> ssh" has been enough for my situations to reliably detect a string containing a request for an ssh connection, but any better solution would be appreciated.

 #!/bin/bash # @TODO: change this according to your own prompt # This is used to find lines connect ssh command in the pane buffer PROMPT_SEPARATOR="> " # get current pane buffer size and dimensions HISTORY_LIMIT=`tmux display-message -p "#{history_limit}"` VISIBLE_LINES=`tmux display-message -p "#{pane_height}"` # search last ssh command in pane content LINE=`tmux capture-pane -p -J -S -$HISTORY_LIMIT -E $VISIBLE_LINES | grep "${PROMPT_SEPARATOR}ssh " | tail -1` if [ -n "$LINE" ]; then echo $LINE | sed "s/.*$PROMPT_SEPARATOR//;" else # fall back to the command that might have been used to create the pane # (not necessarily ssh but helpful anyway) tmux list-panes -F "#{pane_active} #{pane_start_command}" | grep "^1 " | tail -1 | cut -d ' ' -f2- fi 

I saved this script in my ~ / .tmux directory and changed the key bindings for the various split-window and new-window shortcuts in my .tmux.conf , similar to this:

 # try to reconnect to remote host when creating new window bind c run-shell 'CMD=`~/.tmux/reconnect.sh`; tmux new-window "$CMD"' 
0
source

All Articles