使用SSH远程连接Linux系统的ECS实例时,提示“Host key verification failed”错误怎么办?

更新时间:
复制 MD 格式

本文介绍使用SSH远程连接Linux系统的ECS实例时,提示“Host key verification failed”的错误原因和解决方案。

问题现象

  • 本地客户端为Linux环境或macOS环境,使用SSH方式无法正常连接Linux系统的ECS实例时,提示如下错误信息。

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @ 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 the RSA host key has just been changed.
    The fingerprint for the RSA key sent by the remote host is
    ae:6e:68:4c:97:a6:91:81:11:38:8d:64:ff:92:13:50.
    Please contact your system administrator.
    Add correct host key in /root/.ssh/known_hosts to get rid of this message.
    Offending key in /root/.ssh/known_hosts:70
    RSA host key for x.x.x.x has changed and you have requested strict checking.
    Host key verification failed.
  • 本地客户端为Windows环境,使用常见的SSH客户端(如PuttyMobaXterm)连接Linux系统的ECS实例时,提示类似“The host key does not match the one Putty has cached for this server”或“remote server identification has changed”错误信息。

    PuTTY 弹出 WARNING - POTENTIAL SECURITY BREACH! 安全警告对话框,密钥类型为 ssh-ed25519,用户可单击 AcceptConnect OnceCancel 按钮进行响应。

    MobaXterm的警告对话框提供三个操作按钮:Accept the new server hostkey and carry on connectingRefuse the new server hostkey and abort the connectionAccept and save as additional hostkey (keep the existing ones)

问题原因

ECS实例可能进行过重装系统、账户信息变更等操作,导致ECS实例SSH公钥变更,造成本地客户端保存的公钥指纹与服务器端不一致,出现该提示信息。

解决方案

您可以根据本地客户端环境不同,选择合适的方案。

本地客户端为Windows环境

本文以SSH客户端PuttyMobaXterm进行介绍,不同SSH客户端操作有所差异,请根据实际的客户端进行操作。

客户端为Putty时,单击Accept,Putty会自动更新密钥指纹信息,即可成功登录实例。

客户端为MobaXterm时,单击Accept the new server hostkey and carry on connecting,MobaXterm会自动更新密钥指纹信息,即可成功登录实例。

本地客户端为LinuxmacOS环境

  1. 在本地客户端中,执行如下命令,进入对应账号的known_hosts文件。

    vim ~/.ssh/known_hosts
  2. i键进入编辑模式。

  3. 删除ECS实例IP对应的条目,如以下示例所示。

    39.105.    ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBEPMCfKNtd5Pkjp4A+ocwNzeTN
    fLNrUf3a0EfqXj4X4URJMPEzEl94ftuA7ZltOM0cAMkeZzOql
    ~
  4. Esc键,输入:wq保存并退出。

  5. 重新连接Linux实例,确保可以正常连接。