使用不同的SSH密钥部署私有EC2实例(例如Ec2.pem)和Bastion主机(例如BastionKey.pem) - AWS VPC

问题描述 投票:0回答:1

我理解不建议在一个帖子中提出多个问题但是它们都是紧密耦合的,因此在一个帖子中询问它们。

我试图使用Bastion主机通过互联网SSH连接到VPC中的私有EC2实例。我遇到了两种方法。注意当我启动我的私有EC2实例和Bastion主机时,我选择了不同的密钥。(在我看到的两种方法中,作者使用相同的EC2和Bastion的密钥)

方法1:配置SSH ProxyCommand,如https://www.youtube.com/watch?v=EpFAHis4O4g中所示

方法2:使用ssh -A选项https://aws.amazon.com/blogs/security/securely-connect-to-linux-instances-running-in-a-private-amazon-vpc/的ssh-agent命令

问题1:在方法2中,我做到了

ssh-add ~/Downloads/Ec2.pem
ssh-add ~/Downloads/BastionKey.pem

我的方法是将两个密钥添加到ssh-agent是否正确?这样我就可以作为第一步进入Bastion,然后作为第二步,我必须明确地将ssh转换为私有EC2。

问题2:方法1违反了(方法2)2步骤ssh过程的想法,但提出了ProxyCommand,以便最终用户能够在一个步骤中直接ssh到私有EC2。这样,Bastion主机的系统管理员就无法控制对各个EC2实例的ssh。事实上,作者证明了如果你进入Bastion主机(它会成功)然后ssh到EC2就会失败。我的理解在这里是否正确?

问题3:方法1对我不起作用。我对〜/ .ssh / config使用了相同的结构,但使用了不同的密钥文件路径。我无法直接与我的私人EC2合作。我甚至尝试了两步过程,但我只能登录Bastion主机,我不能ssh到EC2。我的所有安全组,ACL,Internet网关,NAT网关和VPC设置都很好,否则方法2不起作用。我能做错什么?

cat ~/.ssh/config 
HOST bastion
Hostname ec2-5x-xx-xx-xx.compute-1.amazonaws.com
User ec2-user
IdentityFile /Users/myname/Downloads/BastionKey.pem

HOST *.ec2.internal
User ec2-user
IdentityFile /Users/myname/Downloads/Ec2.pem
ProxyCommand ssh -q -W %h:%p bastion

问题4:1和2中的推荐方法是什么?当为Bastion和EC2选择不同的密钥时,必须遵循哪些附加步骤?

错误输出:

ssh -v ip-10-0-1-12.ec2.internal
OpenSSH_7.8p1, LibreSSL 2.6.2
debug1: Reading configuration data /Users/myname/.ssh/config
debug1: /Users/myname/.ssh/config line 6: Applying options for *.ec2.internal
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Executing proxy command: exec ssh -q -W ip-10-0-1-12.ec2.internal:22 bastion
debug1: identity file /Users/myname/Downloads/Ec2.pem type -1
debug1: identity file /Users/myname/Downloads/Ec2.pem-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.8
/bin/false: No such file or directory
ssh_exchange_identification: Connection closed by remote host
amazon-web-services ssh-keys amazon-vpc
1个回答
1
投票
HOST bastion
Hostname ec2-example-ip.compute-1.amazonaws.com
User ec2-user
IdentityFile /Users/myname/Downloads/BastionKey.pem

HOST *.ec2.internal
User ec2-user
IdentityFile /Users/myname/Downloads/Ec2/Ec2.pem
ProxyCommand ssh -q -W %h:%p bastion

尝试在ssh配置中使用堡垒公共DNS名称,也使用内部ec2 DNS名称作为VPC后面的名称。 (有时AWS AWS最多是粗略的)

注意:* .ec2.internal assums您将使用相同的ssh密钥为您需要通过堡垒访问代理的每个ec2。如果不是这种情况,请将* .ec2.internal替换为whatever-internal-ip.ec2.internal,并为每个ec2添加一个条目。

希望这可以解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.