使用 A 上的 IdentityFile 通过 SSH 从 A 经过 B 到达 C

使用 A 上的 IdentityFile 通过 SSH 从 A 经过 B 到达 C

我看到过一些类似的问题,但没有明确的解决办法,如果这个问题之前已经得到解答,请关闭这个问题。

我的情况是:

  • 主机 C 无法从 A 访问。
  • 主机 B 可从 A 访问。
  • 可以从 B 访问主机 C。
  • A 包含一个用于访问 C 的 pem 文件。
  • 只有 B 在 authorized_keys 中拥有 ~/.ssh/id_rsa.pub (来自 A)

我在 .ssh/config 上找到了以下内容

Host bastion-host
  HostName servername
  Port servercustomport

Host A.*
  User custom_user
  IdentityFile path/key.pem
  ForwardAgent yes
  ProxyCommand ssh bastion-host 'ssh-add -t 1 && nc %h %p'

Host A.j
  HostName ip

然后使用 ssh -vvv Aj 我得到:

debug1: Reading configuration data ~/.ssh/config
debug1: ~/.ssh/config line 16: Applying options for A.*
debug1: ~/.ssh/config line 22: Applying options for A.j
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug2: resolve_canonicalize: hostname ip is address
debug1: Executing proxy command: exec ssh bastion-host 'ssh-add -t 1 && nc ip 22'
debug1: identity file path/key.pem type -1
debug1: identity file path/key.pem-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
kex_exchange_identification: Connection closed by remote host
kex_exchange_identification: Connection closed by remote host

我做错了什么?可以通过这种方式连接吗?
最后要注意的是,这可能不是最好的安全流程,即使是 1s 密钥也存储在堡垒上,但堡垒位于没有公共访问权限的私有网络上。

答案1

尝试这个:

Host A.*
  User custom_user
  IdentityFile path/key.pem
  ProxyJump bastion-host

如果使用代理命令,则不需要转发代理。代理命令意味着主 ssh 隧道将端口转发到目标主机,并在您的主机和目标主机之间建立连接,但代理转发是将 ssh 连接链接到另一个。

答案2

从主机 A 尝试此操作:

ssh -tt Host-A ssh -tt Host-B ssh -tt Host-C

答案3

我有一些系统无法直接通过 ssh 访问,所以我必须通过 Solaris 系统。

您只需要在主机 A 上配置您的 .ssh/config:

host *
ForwardAgent yes

host B
hostname servername
user username

host C
hostname servername
user username
ProxyCommand ssh -q -W %h:%p B

然后只需从 A 进行 ssh C 即可。

这样,我只使用一个系统就可以访问其余部分、部署 ansible 等。

希望对您有帮助,祝您一切顺利!

相关内容