如何使用堡垒主机通过 SSH 连接到目标 AWS 机器

How to SSH to target AWS machine using a bastion host

假设 Machine A 是我最终要通过 SSH 连接到的目标机器,而 Machine B 是桥接机器(堡垒主机)。可以使用相同的 PEM 文件访问这两台机器。

Machine A 的安全组仅允许来自 Machine B 的 SSH 连接。所以如果我想连接到 Machine A,我需要通过 Machine B.

连接

如果不将 PEM file 放在堡垒主机上,如何做到这一点?

要通过 public 子网中的堡垒主机访问私有子网中的 EC2 实例,而不将您的 SSH 私钥放在堡垒上,您需要使用 SSH 代理转发。

提供了具体说明here

您可以使用 ProxyCommand。我更喜欢在 ~/.ssh/config 文件中定义以下内容。

host MachineB
 HostName <MachineB-IP>
 IdentityFile <Full Path of .pem file>
 User username

host MachineA
 HostName <MachineA-IP>
 ProxyCommand  ssh MachineB nc -w 120 %h %p
 IdentityFile <Full Path of .pem file>
 User username

然后像这样访问 MachineA:

$ ssh MachineA