10

我可以在本地运行 tmux 并通过 ssh 连接到远程机器吗.. 之后任何新的窗格和/或屏幕都可以与远程机器外壳一起使用...我的意思是我无法在遥控器上安装 tmux机器,但我不想从每个窗格都进行 ssh 连接,而是 ssh-login 一次。

这样的事情可能吗..谢谢

4

4 回答 4

6

If you want to login just once, you can use ControlMaster feature of ssh. Add some config like this to your ~/.ssh/config:

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

If you login to the same server (as the same user) multiple times (either in one tmux or not), ssh will reuse the connection so that you don't need to make connection and login again.

于 2012-05-15T17:40:11.070 回答
5

lilydjwg 解释了一些我以前从未真正理解的东西。了解 ControlMaster 设置会使以下内容更加合理,因为它简化了建立多个ssh连接。您只需要认证一次,远程主机不需要为每个连接运行一个 sshd 进程。

在您的.tmux.conf文件中:

# 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 C-h 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 ""
于 2012-05-27T02:39:09.713 回答
3

我不认为tmux可以。一种解决方法是将这样的内容添加到tmux.conf

bind-key X new-window "ssh HOST"

然后新窗口将在远程主机上启动。

于 2012-05-10T15:53:16.810 回答
0

我正在使用 tmux 1.8 并没有找到内置的解决方案。这些解决方法至少适合我的常见用例:

  • 捕获整个窗格内容并在其中搜索最后一个 ssh 命令(我使用提示结束的知识来或多或少可靠地检测命令)
  • shell-command如果失败,我会使用tmuxnew-windowsplit-windowcommands选项检查窗格可能已创建的命令

我的reconnect.sh脚本看起来像这样。它最脏的地方是从缓冲区中获取最后一个 ssh 命令的方式。到目前为止,“> ssh”足以让我的情况可靠地检测到包含 ssh 连接请求的线路,但任何更好的解决方案将不胜感激。

#!/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

split-window我将此脚本保存在我的 ~/.tmux 目录中,并更改了与此类似的各种快捷new-window方式的键绑定.tmux.conf

# try to reconnect to remote host when creating new window
bind c run-shell 'CMD=`~/.tmux/reconnect.sh`; tmux new-window "$CMD"'
于 2013-11-16T19:11:17.760 回答