一、問題描述
今天下午使用ssh連接其他服務器進行scp操作的時候,提示失敗,如下所示:
[root@localhost backups]# scp root@172.xxx.xxx.xxx:/data/gitlabData/backups/1539717714_2018_10_17_9.4.3_gitlab_backup.tar . @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ 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 ECDSA key sent by the remote host is 85:82:b1:58:20:21:a5:da:be:24:e8:14:9a:12:b2:d2. Please contact your system administrator. Add correct host key in /root/.ssh/known_hosts to get rid of this message. Offending ECDSA key in /root/.ssh/known_hosts:5 ECDSA host key for 172.xxx.xxx.xxx has changed and you have requested strict checking. Host key verification failed.
二、分析原因
用OpenSSH的人都知ssh會把你每個你訪問過計算機的公鑰(public key)都記錄在~/.ssh/known_hosts。當下次訪問相同計算機時,OpenSSH會核對公鑰。如果公鑰不同,OpenSSH會發出警告,避免你受到DNS Hijack之類的攻擊。
SSH對主機的public_key的檢查等級是根據StrictHostKeyChecking變量來配置的。默認情況下,StrictHostKeyChecking=ask。
簡單說下它的三種配置值:
- StrictHostKeyChecking=no
最不安全的級別,當然也沒有那么多煩人的提示了,相對安全的內網時建議使用。如果連接server的key在本地不存在,那么就自動添加到文件中(默認是known_hosts),並且給出一個警告。
- StrictHostKeyChecking=ask
默認的級別,就是出現剛才的提示了。如果連接和key不匹配,給出提示,並拒絕登錄。
- StrictHostKeyChecking=yes
最安全的級別,如果連接與key不匹配,就拒絕連接,不會提示詳細信息。
三、解決問題
方法一、刪除~/.ssh/known_hosts文件中對應ip的相關rsa信息
輸入命令vi ~/.ssh/known_hosts
,編輯文件
刪除對應ip的相關rsa信息,即可。
刪除完畢之后,重新執行命令,會要求輸入密碼。
密碼正確的話,命令正常執行。
方法二、 使用 ssh-keygen -R hostname 命令
ssh-keygen -R xxx.xxx.xxx.xxx (服務器ip地址)
目的是清除你當前機器里關於你的遠程服務器的緩存和公鑰信息,注意是大寫的字母“R”。
比如 ~/.ssh/known_hosts文件中有一條 172.28.3.55 的配置。
現在我們執行ssh-keygen -R hostname
命令
[root@localhost ~]# vi ~/.ssh/known_hosts [root@localhost ~]# ssh-keygen -R 172.28.3.55 # Host 172.28.3.55 found: line 6 type RSA /root/.ssh/known_hosts updated. Original contents retained as /root/.ssh/known_hosts.old [root@localhost ~]#
我們再查看下 ~/.ssh/known_hosts文件的內容,如下所示:
刪除完配置之后,重新執行命令,輸入命令然后輸入密碼即可。
如果想以后都不輸入密碼直接執行命令的話,則需要參考鏈接 https://blog.csdn.net/ouyang_peng/article/details/77334215 然后重新配置ssh秘鑰,然后再執行命令。