linux - 如何正确配置 ssh proxycommand 以运行 docker exec?

标签 linux ubuntu ssh proxy docker

我在 /etc/hosts 中定义了一个主机称为 web1 . 有一个名为 store 的 docker 容器.

在我的工作站上,我可以通过 ssh 进入机器并执行命令以交互方式进入容器

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

正如我所希望的那样,它正确地将我作为 root 直接放入容器中。

但是,我真的很想定义一个名为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 进行一些调试,上面 block 之前的最后两行是

debug1: Authenticating to store:22 as 'user1'
debug1: SSH2_MSG_KEXINIT sent
  1. 我认为它正在尝试 ssh进入store容器而不是仅仅执行抛出该错误的命令,是否正确?如果不是,问题是什么?
  2. 有没有一种方法可以使用 ProxyCommand 来做到这一点,而不是尝试通过 ssh 进入容器,而只是使用 docker exec?
  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并最终进入容器

最佳答案

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 session ,而不是直接的 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 或您存储别名的位置。

关于linux - 如何正确配置 ssh proxycommand 以运行 docker exec?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34445796/

相关文章:

linux - 如何编写文件创建脚本?

ssh - 服务器 ip 上的 Jenkins ssh 作为构建参数传递

linux - 恢复了 .ssh/authorized_keys 文件,但仍然收到权限被拒绝(公钥)消息

linux - 如何从 Visual Studio 2010 中远程编译和调试 Linux

python - 数据值超出使用 cdo daysum 的有效范围

linux - 为什么 OpenCL 拒绝在 Ubuntu 20.04 下兼容硬件?

docker - nginx 的 gitlab 容器代理传递不适用于推送请求

php - LoadModule 有两个参数,一个模块名称和一个共享对象文件的名称来加载它

java - 从 bin 文件夹运行 logstash 脚本并设置 java.io.tmpdir

python - 我在 1and1 上的 python 安装停止工作(bashrc 问题)