gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 12:43:22 26 4
gpt4 key购买 nike

我在 /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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com