ssh 密鑰詳解


原理:

密匙認證需要依靠密匙,首先創建一對密匙(包括公匙和密匙,並且用公匙加密的數據只能用密匙解密),並把公匙放到需要遠程服務器上。這樣當登錄遠程 服務器時,客戶端軟件就會向服務器發出請求,請求用你的密匙進行認證。服務器收到請求之后,先在你在該服務器的宿主目錄下尋找你的公匙,然后檢查該公匙是 否是合法,如果合法就用公匙加密一隨機數(即所謂的challenge)並發送給客戶端軟件。客戶端軟件收到 “challenge”之后就用私匙解密再把它發送給服務器。因為用公匙加密的數據只能用密匙解密,服務器經過比較就可以知道該客戶連接的合法性。

客戶機:172.16.142.4
遠端主機:172.16.142.5

在客戶機以root用戶執行下述命令:
[root@localhost .ssh]# /usr/bin/ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
30:f6:d7:2a:ac:56:eb:3f:fa:40:25:8d:90:96:68:cb root@localhost.localdomain
------------------------------------------------
說明:
該命令將在用戶的主目錄/.ssh目錄下面產生一對密鑰
一般采用的ssh的rsa密鑰:
id_rsa     私鑰
id_rsa.pub 公鑰
下述命令產生不同類型的密鑰
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1
-------------------------------------------------------
[root@localhost .ssh]# scp /root/.ssh/id_rsa.pub root@172.16.142.5:/root/.ssh/authorized_keys

The authenticity of host '172.16.142.5 (172.16.142.5)' can't be established.
RSA key fingerprint is 4b:a5:74:fb:2e:08:60:af:fa:76:d4:b0:26:4c:13:75.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.142.5' (RSA) to the list of known hosts.
root@172.16.142.5's password:

id_rsa.pub                                                                                        100% 236     0.2KB/s   00:00
-------------------------------------------------------------
說明:
將公鑰拷貝到遠端主機,並寫入授權列表文件
你也可以把公鑰文件拷貝過去后,在遠端主機下直接執行
touch /root/.ssh/authorized_keys
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

-------------------------------------------------------------

操作完畢,登陸檢查。  
[root@localhost .ssh]# ssh 172.16.142.5
root@172.16.142.5's password:

請注意此時如果仍提示輸入密碼,請檢查如下文件夾和文件的操作權限,這是非常重要的, 否則ssh公鑰認證體制不能正常工作:

172.16.142.4(客戶端)
/home/root文件夾的權限是600
/home/root/.ssh文件夾的權限是600 (好像這個權限關系不是很大)
/home/root/.ssh/id_dsa私鑰的權限600

172.16.142.5(遠端主機)
/home/root文件夾的權限是644
/home/root/.ssh文件夾的權限是644 (好像這個權限關系不是很大)
/root/.ssh/authorized_keys公鑰的權限644

--------------------------------------------------------------
[root@localhost ~]# ssh 172.16.142.5
Last login: Sat Dec 15 21:10:17 2007 from 172.16.142.4
[root@localhost ~]#
無密碼SSH登陸成功!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM