Ssh

關閉對 ssh 密鑰的嚴格檢查

  • December 21, 2019

每個使用者每天創建和銷毀 15 個以上的 VM。虛擬機是在公司內部的 OpenStack 雲中創建的。

每次為新的 vm 分配一個先前已分發的 ip 地址時,使用者都會收到可怕的主機密鑰驗證失敗錯誤。known_hosts這是因為 ssh 密鑰與使用者文件中的 IP 地址不匹配。

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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
xxxxxxxxxxx
Please contact your system administrator.
Add correct host key in /home/user/.ssh/known_hosts to get rid of this message.
Offending key in /home/user/.ssh/known_hosts:4
RSA host key for domain.com has changed and you have requested strict checking.
Host key verification failed.

我能看到的兩個解決方案是:

  • 關閉嚴格檢查 - (安全風險)
  • 讓使用者執行ssh-keygen -RipAddress- (使用者已經厭倦了這個解決方案,從那以後每天執行多次)

有什麼方法可以防止出現此錯誤消息,同時保持安全?也許只關閉特定子網的安全檢查?

強大的功能HostKeyAlias解決了您的問題:

ssh -o HostKeyAlias=hostkeyalias__vm_2013-05-11_07 user@host

在 中創建一個條目hostkeyalias__vm_2013-05-11_07(沒有 IP)known_hosts。當然,您可以編寫腳本或 shell 函式,在每次 ssh 呼叫之前設置此值。或者您使用 shell 變數:

HOSTKEYALIAS=hostkeyalias__vm_2013-05-11_07
ssh -o HostKeyAlias=$HOSTKEYALIAS user@host

$HOSTKEYALIAS在 VM 更改時更改。不時應刪除舊條目known_hosts

引用自:https://serverfault.com/questions/506864