无法连接到AWS EC2实例 – “主机密钥validation失败”

我已经build立了一个Rails包的Ubuntu实例,部署我的应用程序,它工作正常。

但是当我尝试做SSH时,它不允许我进行远程login,并抛出如下错误: Host key verification failed

这个问题似乎是持久的。 我已经附加弹性IP到这个实例,我无法看到公共DNS。

我的实例正在新加坡地区运行。

sshdebugging输出:

 OpenSSH_5.8p1 Debian-7ubuntu1, OpenSSL 1.0.0e 6 Sep 2011 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to 46.137.253.231 [46.137.253.231] port 22. debug1: Connection established. debug1: identity file st.pem type -1 debug1: identity file st.pem-cert type -1 debug1: Remote protocol version 2.0, remote software version OpenSSH_5.5p1 Debian-4ubuntu6 debug1: match: OpenSSH_5.5p1 Debian-4ubuntu6 pat OpenSSH* debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_5.8p1 Debian-7ubuntu1 debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client aes128-ctr hmac-md5 none debug1: kex: client->server aes128-ctr hmac-md5 none debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP debug1: SSH2_MSG_KEX_DH_GEX_INIT sent debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by the remote host is. Please contact your system administrator. Add correct host key in /home/ubuntu/.ssh/known_hosts to get rid of this message. Offending RSA key in /home/ubuntu/.ssh/known_hosts:1 remove with: ssh-keygen -f "/home/ubuntu/.ssh/known_hosts" -R 46.137.253.231 RSA host key for 46.137.253.231 has changed and you have requested strict checking. Host key verification failed. 

当您连接到ssh服务器时,您的ssh客户端将可信主机的列表保存为IP和ssh服务器指纹的键值对。 在ec2中,您经常重复使用与多个导致冲突的服务器实例相同的IP。

如果您已经使用此IP连接到较早的ec2实例,并且现在连接到具有相同IP的新实例,则您的计算机将会抱怨“主机validation失败”,因为之前存储的对不再与新对匹配。

错误消息告诉你如何解决它:

在/home/ubuntu/.ssh/known_hosts:1中出错RSA密钥
删除:ssh-keygen -f“/home/ubuntu/.ssh/known_hosts”-R 46.137.253.231“

另一种方法是打开/home/ubuntu/.ssh/known_hosts并删除第1行(如“:1”所示)。

您现在可以连接并接收新的主机validation。

请注意,通常ssh的known_hosts文件通常为hostname或ip6存储了第二行对,所以您可能需要删除几行。

警告:主机validation非常重要,这是您获得此警告的一个很好的理由。 确保您期望主机validation失败。 如果不确定,请不要移除validation键值对。

@ flurdy的答案是一次性的决议。

但是,如果你经常:

  • 启动新的EC2实例,
  • 启动和停止EC2实例,

..不使用弹性IP(永久连接到您的服务器),那么你总是处理新的/变化的IP /主机名。

如果是这样,那么您可能需要永久停止SSH检查并存储EC2公用主机名的服务器指纹

要做到这一点,只需将其添加到~/.ssh/config

 # AWS EC2 public hostnames (changing IPs) Host *.compute.amazonaws.com StrictHostKeyChecking no UserKnownHostsFile /dev/null 

请注意,SSH仍会说Warning: Permanently added (...) to the list of known hosts. 当连接,但只是意味着它已经将其添加到/dev/null

然而,SSH会停止询问你confirm the authenticity of host并继续连接。

所以它更方便,并且可以避免在使用EC2实例时不总是冗长的SSH连接错误 。

我必须补充一点,理论上这个设置会降低SSH连接的安全性,但在现实生活中,您可能无法检查一次性EC2实例的指纹。

正如@flurdy所说:

另一种方法是打开/home/ubuntu/.ssh/known_hosts并删除第1行(如“:1”所示)。

这工作,很简单。