
當我嘗試從遠端電腦連接到家庭電腦時,如何驗證新連接主機的指紋。第一次連接時我得到了這個:
[email protected]$ ssh [email protected]:~/
The authenticity of host '[home-machine.org]:222 ([x.xx.xx.xx]:222)' can't be established.
ECDSA key fingerprint is SHA256:6lr/VtTwgrKZVNZQ8y8Le/ilfBYfo0e+9UliSg+AD2k.
Are you sure you want to continue connecting (yes/no)?
如何查看我的“家用機器”的指紋?我試圖在我的家用機器上列出它們,如下所示:
[email protected]:~$ for pubkey_file in /etc/ssh/*.pub; do ssh-keygen -lf ${pubkey_file}; done
1024 d1:ef:db:b4:24:fc:ca:fe:e1:11:8c:36:0a:77:90:49 /etc/ssh/ssh_host_dsa_key.pub (DSA)
256 d9:25:51:16:ca:76:bd:8f:b8:6a:79:a2:1c:81:4b:4c /etc/ssh/ssh_host_ecdsa_key.pub (ECDSA)
2048 3a:c6:2d:29:7c:b9:16:e8:ed:1c:a8:26:5d:ab:0d:1e /etc/ssh/ssh_host_rsa_key.pub (RSA)
[email protected]:~$
但這些密鑰的格式似乎不同。這是指紋的某種 MD5 嗎?如何才能獲得統一格式的指紋?
答案1
您的 SSH 伺服器提供 SHA256 公鑰雜湊值,這比 MD5 雜湊值安全得多。
然後,您必須向 ssh-keygen 指定您需要 SHA256 而不是 MD5 雜湊值。嘗試在您的電腦上執行命令家用機(在範例中您嘗試從遠端電腦連線):
for pubkey_file in /etc/ssh/*.pub; do ssh-keygen -lf ${pubkey_file} -E sha256; done
答案2
使用的命令
顯示儲存在伺服器上的公用主機金鑰的 ascii-art(在伺服器端完成,即透過 ssh 連線到的伺服器端):
ssh-keygen -l -v -E md5 -f /etc/ssh/ssh_host_ecdsa_key.pub
-l:顯示指定公鑰檔案的指紋。
-v:視覺(ascii-art)
-E md5:用於計算指紋的雜湊演算法(“md5”或“sha256”)。 (如果可用,最好使用“sha256”)。 (舊版的 ssh-keygen 中可能不可用)
-F: 文件
顯示遠端伺服器公用主機金鑰的 ascii-art(在用戶端完成,即透過 ssh 連線的用戶端):
ssh -o visualhostkey=yes -o FingerprintHash=md5 <host_server_to_connect>
-o: 選項
視覺主機鍵:視覺(ascii-art)
指紋哈希:要使用的雜湊演算法(使用與從伺服器取得的相同的演算法:md5 或 sha256)
檢查主機/伺服器真實性的步驟
首先,1。是在伺服器本地完成的(您想透過 ssh 連線的伺服器):它將為您提供第一個 ascii-art。列印或拍照。
第二,2。在第一次 SSH 連線時完成;它將顯示第二個 ASCII-art。如果ascii-art相同,那麼你可以回答是的到“我信任嗎?”問題(即
Are you sure you want to continue connecting (yes/no)
)。
例子
- 1、伺服器端
$ ssh-keygen -l -v -E md5 -f /etc/ssh/ssh_host_ecdsa_key.pub
256 2e:a6:b3:27:14:12:0b:79:df:9a:7f:bd:4d:b1:e0:b6 (ECDSA)
+--[ECDSA 256]---+
| . |
|o o |
| o + . |
| o o . |
| . + S . . |
| + . . . o |
| . .o ..o o |
| ooo....+ |
| o= . E.. |
+-----------------+
- 2、客戶端
$ ssh -o visualhostkey=yes -o FingerprintHash=md5 192.168.12.211
The authenticity of host '192.168.12.211 (192.168.12.211)' can't be established.
ECDSA key fingerprint is MD5:2e:a6:b3:27:14:12:0b:79:df:9a:7f:bd:4d:b1:e0:b6.
+---[ECDSA 256]---+
| . |
|o o |
| o + . |
| o o . |
| . + S . . |
| + . . . o |
| . .o ..o o |
| ooo....+ |
| o= . E.. |
+------[MD5]------+
Are you sure you want to continue connecting (yes/no)?
更多解釋
第一個命令將顯示與您作為輸入提供的文件的指紋(以及指紋本身)相對應的 ascii-art。您作為輸入提供的文件是公開的主持人伺服器的密鑰。當客戶端連接時(不僅是第一次),伺服器將發送其公共主機金鑰。將在 中搜尋該公用主機金鑰~/.ssh/known_hosts
。如果公鑰在檔案中,那麼就可以了:主機(伺服器)已知,因此我們繼續下一步以驗證使用者身份(本文中未描述使用者身份驗證)。如果公鑰不在檔案中,則用戶端將使用雜湊演算法計算該公共主機金鑰的指紋(不同的雜湊演算法將給出不同的指紋)。 This fingerprint previously calculated is displayed (along with the ascii-art if corresponding option provided) and you will have to answer yes or no depending on you recognising this fingerprint or no (this fingerprint is the image/hash of the public host key of the伺服器).如果您選擇“是”,那麼伺服器的公鑰(而不是其指紋)將被添加到檔案中~/.ssh/known_hosts
。
我們可以注意到~/.ssh/known_hosts
在你的主 (~) 目錄,因為你信任該主機(伺服器),但不同的使用者可能不像您那樣信任。此外,伺服器的主機公鑰不依賴用戶,因此它儲存在/etc/ssh/
.
第二個命令將顯示從接收到的公鑰的指紋和 ASCII-art要連接的主機伺服器(根據選項中給出的哈希演算法)。它與僅執行 ssh 相同,但具有更多可視選項,因此連接將以與正常 ssh 連接相同的方式繼續。
答案3
伺服器
ssh-keygen -l -v -f /etc/ssh/ssh_host_ed25519_key
您可能需要將其與客戶端的演算法以及該目錄中的其他檔案之一進行配對。這些檔案具有正確的權限,因此它將使用公鑰。
例如,您可能需要寫
ssh-keygen -l -v -f /etc/ssh/ssh_host_rsa_key
客戶
ssh -o visualhostkey=yes -o FingerprintHash=sha256