远程主机标识已更改!

背景

好久没有更新的 blog ,今天更新了一篇文章,发现推送到 github 上的时候,显示了一个告警信息,导致推送失败,具体原因如下:

我是使用的ssh,跟远程仓库链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PS D:\mine-project\blog> git push
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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
SHA256:uNiVztksCsDhcc0u9e8BujQXVUpKZIDTMczCvj3tD2s.
Please contact your system administrator.
Add correct host key in /c/Users/xiao/.ssh/known_hosts to get rid of this message.
Offending RSA key in /c/Users/xiao/.ssh/known_hosts:5
RSA host key for github.com has changed and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
PS D:\mine-project\blog>

原因

  • WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  • 警告:远程主机标识已更改!

此报错是由于远程的主机的公钥发生了变化导致的。
ssh服务是通过公钥和私钥来进行连接的,它会把每个曾经访问过计算机或服务器的公钥(public key),记录在~/.ssh/known_hosts 中,当下次访问曾经访问过的计算机或服务器时,ssh就会核对公钥,如果和上次记录的不同,OpenSSH会发出警告。
解决

  1. 使用命令清除所连接的IP或域名
    1
    ssh-keygen -R XX.XX.XX.XX (或者域名)
    1
    2
    3
    4
    5
    PS C:\Users\xiao\.ssh> ssh-keygen -R github.com
    # Host github.com found: line 5
    C:\Users\xiao/.ssh/known_hosts updated.
    Original contents retained as C:\Users\xiao/.ssh/known_hosts.old
    PS C:\Users\xiao\.ssh>

执行完这条命令,就会删除 known_hosts 文件里的关于 github.com 的记录

  1. 当重新链接 github.com 的主机时,会重新生成指纹,建立链接。
    1
    2
    3
    4
    PS D:\mine-project\blog> git push
    The authenticity of host 'github.com (20.205.243.166)' can't be established.
    ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

打开 known_hosts 文件,可以看到以及重新添加了github 的记录

关于SSH更多请点击:常用的SSH,你了解多少?