如何配置 SSH 以使用一个 SSH 密钥通过代理连接到主机

pko*_*out 1 linux ssh proxy

我有这样的网络拓扑:

Laptop -> Bastion -> Destination
Run Code Online (Sandbox Code Playgroud)

Bastion 和 Destination 是 EC2 实例,使用相同的 SSH 密钥进行 SSH 访问。但是,无法从 Internet 访问 Destination。其 IP 地址仅对堡垒可见。

我能够连接到堡垒并使用代理转发来传递 SSH 密钥,然后从堡垒单独连接到目标服务器。但是,我想以.ssh/config这样一种方式配置我的文件,以便我可以使用笔记本电脑上的一个命令通过 SSH 连接到目标服务器。我当前的.ssh/config文件如下所示:

Host Bastion
  Hostname <redacted>
  IdentityFile ~/.ssh/mykey.pem

Host Destination
  Hostname <redacted>
  User ubuntu
  ProxyCommand ssh -A bastion-dev -W %h:%p
Run Code Online (Sandbox Code Playgroud)

但是当我跑

ssh -A ubuntu@Destination
Run Code Online (Sandbox Code Playgroud)

SSH 响应:

Permission denied (publickey).
ssh_exchange_identification: Connection closed by remote host
Run Code Online (Sandbox Code Playgroud)

如何正确地将 SSH 密钥从我的本地传递到堡垒服务器,而不必将其存储在服务器上?我可以通过.ssh/config文件配置所有这些,以便我可以使用单个命令登录目标服务器吗?

HBr*_*ijn 7

使用ssh -v来自客户端的一个或多个标志进行调试,并检查相关服务器上的日志以查看问题出在哪里。

我经常为不同的客户、站点和项目使用不同的密钥,并且MaxAuthTries当 ssh-agent 仍在尝试每个潜在的密钥并且尚未到达正确的密钥时,我遇到了远程 ssh 服务器的设置。检查你的服务器日志。

通常,我工作站上的用户名与在堡垒或目标上分配的用户名不匹配,因此我更喜欢在我的配置中明确设置所有设置。这样我就不需要使用任何命令行标志,只需输入即可ssh Destination完成。

Host Bastion
    Hostname bastion.example.com
    User hbruijn-adm
    ForwardAgent yes
    AddKeysToAgent yes
    UseKeychain yes                                  # Specific to OS X 
    IdentityFile ~/.ssh/id_rsa.bastion

Host Destination
    Hostname destination.example.com
    User ubuntu 
    ForwardAgent yes
    AddKeysToAgent yes
    UseKeychain yes                                  # Specific to OS X
    IdentityFile ~/.ssh/id_rsa.destination
    ProxyJump Bastion
Run Code Online (Sandbox Code Playgroud)

ProxyJump是一个相对较新的设置,我发现它比ProxyCommand.