2015-12-24 54 views
2

我在/etc/hosts中定义了一个名为web1的主机。 有一个名为store的码头集装箱。如何正确配置ssh proxycommand以运行docker exec?

虽然我的工作站上我可以ssh到机器并执行命令以交互方式进入容器这样

ssh -t -t web1 docker exec -ti store /bin/bash 

它正确滴我对入容器,我所希望的根。

不过,我真的要定义一个名为store一个伪主机和我~/.ssh/config文件中像这样利用ProxyCommand设定,让我可以使用ssh store

Host store 
ProxyCommand ssh -t -t web1 docker exec -ti store /bin/bash 

但它失败,出现以下错误:

Bad packet length 218958363. 
ssh_dispatch_run_fatal: Connection to UNKNOWN: message authentication code incorrect 
Killed by signal 1. 

如果我添加-v一些调试,最后两行之前的块以上是

debug1: Authenticating to store:22 as 'user1' 
debug1: SSH2_MSG_KEXINIT sent 
  1. 我认为这是试图sshstore容器,而不是只执行其中抛出该错误的命令,是正确的?如果不是什么问题?
  2. 有没有办法做到这一点使用ProxyCommand而不尝试SSH入容器,而是只使用码头执行?
  3. 容易将ssh安装到容器中吗?我们目前并没有将这作为一个实践问题。
  4. ssh-store的别名外,还有其他选择吗?

的最终目标是拥有一个虚拟主机定义,我只能说ssh store并让它在store容器最终在web1

被修改:

解决方案:

作为Jakuje所示,使用ProxyCommand使用ssh不会允许非SSH进一步命令。因此,我只是使用别名和潜在的bash函数来完成它。我已经设置了两个。

每Jakuje的在〜/ .ssh/config中

Host web1 
RequestTTY yes 
在〜/ .bash_aliases

建议

而且

alias ssh-store="ssh web1 docker exec -ti store /bin/bash" 

所以我可以做ssh-store和容器中的最终

或在〜/中。在.bashrc

function ssh-web1 { ssh web1 docker exec -ti $1 /bin/bash; } 

所以我可以做ssh-web1 store,并在容器中结束了

回答

0

I think it is trying ssh into the store container instead of just executing the command which is throwing that error, is that correct? If not what is the issue?

Is there a way to do this using ProxyCommand without trying to ssh into the container but instead just use the docker exec?

不是,它不会以这种方式工作。 ProxyCommand期望另一步也是SSH会话,而不是直接bash提示符。

Is it easy enough to also setup the ssh into the container? We currently aren't doing that as a matter of practice.

我认为这是不必要的开销。但是,正如在这里的许多其他问题中所描述的那样。

至少你可以通过在你的~/.ssh/config中指定RequestTTY来摆脱-t -t。但其余的必须是bash别名或功能(如果你有更多主机function更合适)。

function ssh-docker { 
    ssh web1 docker exec -ti $1 /bin/bash 
} 

,然后你可以调用它,无论这样的容器:

ssh-docker store 

你只是这样的功能存储到您的.bashrc或您保存您的别名。

+0

感谢您的回答。我在我的'〜/ .ssh/config'中为'web1'条目添加了'RequestTTY yes',我现在至少有一个别名'ssh-store',使用别名ssh-store =“ssh web1 docker exec -ti store/bin/bash“'什么是你使用bash函数表示的例子?该函数除了别名正在做什么以及函数在哪里生活? – Streamline

+0

增加了功能示例 – Jakuje

+0

啊,这太好了。感谢您的好消息。作为奖励,我发现即使是像这样定义的函数也可以通过Tab完成来获得,所以我可以设置多个函数和别名,如Tab和Tab,完成它们。尼斯。谢谢。 – Streamline

相关问题