当我第一次使用 ssh 连接到 AWS EC2 实例时,出现如下错误,因为主机 key 未存储在 sshknown_hosts 文件中。
The authenticity of host 'x.x.x.x' can't be established. ECDSA key fingerprint is xx:yy:.... Are you sure you want to continue connecting (yes/no)?
现在,我正在自动化 ssh。我经常只是在 ssh 命令中添加 StrictHostKeyChecking 选项来避免出现此消息。 但是,我觉得这不是很安全的方式,可能会导致中间人攻击。 有没有任何(或好的)方法可以在 AWS EC2 上安全地获取主机 key ?
最佳答案
我认为唯一的方法是解析控制台输出。
#get the console output of the instance
aws ec2 get-console-output --instance-id <instance id> |\
#use jq to get the Output field
jq .Output -r |\
#use sed to find the interesting bits
sed -n -e '1,/-----BEGIN SSH HOST KEY KEYS-----/d; /-----END SSH HOST KEY KEYS-----/q; p'
注意事项,根据您的应用程序,这可能并不重要:
aws
输出是 JSON,因此我们需要解析它- output requires some sed massage
- 输出可能会因 AMI 而异?
关于amazon-web-services - AWS EC2 : Safe way to get host public key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23331014/