ssh密鑰認證

br/>基於密鑰的認證
實現基於密鑰的認證,在登錄過程中不需要輸入密碼,客戶端和ssh服務端以密鑰對進行認證,不在網絡中傳輸用戶名和密碼信息,其公鑰存儲於ssh服務端,私鑰當然是存儲在客戶端本地的。認證通過後才能登錄成功。基於密鑰的認證在我們常用的終端軟件上設置好以後,我們在連接Linux主機時可自動登錄,而且比常規ssh口令登錄更加安全,而且服務器之間ssh登錄也可以啓用這種方式省去了輸入口令的過程,而且在修改賬號密碼後也能繼續使用原來的密鑰進行認證。

實現基於密鑰的認證

服務器之間實現密鑰認證

一般來說,要實現將主機A作爲客戶端,登錄作爲ssh服務端的主機B就需要有如下操作:

以賬戶a身份登錄主機A

在主機A上生成a賬戶的密鑰對

將a賬戶的公鑰註冊到主機B目標賬戶b的~/.ssh/authorized_keys文件中

將主機B的sshd啓用公鑰認證

驗證主機A是否可以通過密鑰認證的方式登錄主機B
這樣實現的效果是:先以a的身份登錄主機A,然後在a上可以以b的身份通過密鑰認證的方式登錄到主機B。

以下以兩臺服務器localhost和xad爲例,實現它們之間以root這個賬號可以密鑰登錄。

在localhost和xad上分別生成它們各自的密鑰對

localhost: 
![](http://i2.51cto.com/images/blog/201801/08/d20e0aa18020dc52480f931ef7762178.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
[root@localhost ~]$ ssh-keygen -t dsa   #使用ssh-keygen工具生成localhost上root賬號的密鑰對,-t指定加密算法爲dsa 
Generating public/private dsa key pair. 
br/>Enter file in which to save the key (/home/root/.ssh/id_dsa):   #指定密鑰對的保存位置,默認爲當前用戶的家目錄下的./ssh目錄 
Enter passphrase (empty for no passphrase): #設置密鑰的密碼,設置以後每次使用到這個密鑰都會需求輸入密碼,不設置就留空 
Enter same passphrase again:  
Your identification has been saved in /home/root/.ssh/id_dsa. 
Your public key has been saved in /home/root/.ssh/id_dsa.pub. 
The key fingerprint is: 
51:b1:7c:b4:78:7e:f4:fa:dd:24:04:82:bc:81:47:a4 root@localhost  #這一指紋信息的最後部分表明了密鑰的身份必須是:localhost主機上的root賬戶。 
 
br/>xad: 
![](http://i2.51cto.com/images/blog/201801/08/2647f802ca299c9e8cee157a53f1de91.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
[root@xad ~]$ ssh-keygen -t dsa 
Generating public/private dsa key pair. 
br/>Enter file in which to save the key (/home/root/.ssh/id_dsa):  
Enter passphrase (empty for no passphrase):  
Enter same passphrase again:  
Your identification has been saved in /home/root/.ssh/id_dsa. 
Your public key has been saved in /home/root/.ssh/id_dsa.pub. 
The key fingerprint is: 
c3:63:24:29:dc:7b:12:59:d5:a2:c2:e1:72:e0:ea:9e root@xad  #此密鑰對所有者爲xad中的root賬戶。 
br/>查看localhost和xad各自的公鑰
localhost:
![](http://i2.51cto.com/images/blog/201801/09/2489f2ebe31706b84640f5d822caaeb3.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
[root@localhost .ssh]$ ll ~/.ssh
total 16
br/>-rw------- 1 root root 1116 Jan  4 21:05 authorized_keys
-rw------- 1 root root  668 Jan  5 11:38 id_dsa  #root的私鑰
-rw-r--r-- 1 root root  603 Jan  5 11:38 id_dsa.pub #root的公鑰
-rw-r--r-- 1 root root  395 Dec 30 23:47 known_hosts

xad:
![](http://i2.51cto.com/images/blog/201801/09/ecb89b4638b22ece4a1be5daf8cb546a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
[root@xad .ssh]$ ll ~/.ssh
total 16
br/>-rw-------. 1 root root 1117 Jan  4 21:54 authorized_keys
-rw-------. 1 root root  668 Jan  5 11:57 id_dsa     #root的私鑰
-rw-r--r--. 1 root root  603 Jan  5 11:57 id_dsa.pub #root的公鑰
-rw-r--r--. 1 root root  397 Dec  6 20:33 known_hosts

分別在localhost和xad上註冊對方的公鑰並進行登錄驗證

[root@localhost .ssh]$ ssh-copy-id -i id_dsa.pub root@xad  #使用這一指令實現公鑰自動註冊,-i指明要將哪個公鑰註冊到對方服務器上,root指明是要註冊到對方服務器哪個賬戶下。
The authenticity of host 'xad (192.168.1.201)' can't be established.
br/>RSA key fingerprint is 04:2b:f5:62:4a:e6:8a:17:2b:7f:a8:c2:97:f9:d7:c6.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@xad's password:             #需要先輸入一次密碼進行登錄
br/>Number of key(s) added: 1  #顯示註冊了一個key文件

Now try logging into the machine, with:   "ssh 'root@xad'"
and check to make sure that only the key(s) you wanted were added. #提示已經註冊完成,可以進行驗證
br/>[root@localhost .ssh]$ ssh root@xad
Last login: Wed Jan  4 22:58:28 2017 from 192.168.1.201 
br/>[root@xad ~]$           #成功從localhost通過密鑰認證登錄xad
br/>xad:
[root@xad .ssh]$ ssh-copy-id -i id_dsa.pub root@localhost
root@localhost's password: 
br/>Now try logging into the machine, with "ssh 'root@localhost'", and check in:
br/>  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@xad .ssh]$ ssh root@localhost
Last login: Thu Jan  5 12:15:30 2017 from xad
br/>[root@localhost ~]$       #從xad通過密鑰認證成功登錄localhost

xshell實現基於密鑰認證


在xshell軟件中選擇“工具”,然後選擇“新建用戶密鑰生成嚮導”
ssh密鑰認證
選擇生產密鑰的參數,圖中選擇的是DSA加密算法,密鑰長度2048位。
ssh密鑰認證
密鑰對生成成功
ssh密鑰認證
給用戶私鑰取名,設置密碼(可以不設置密碼)
ssh密鑰認證
保存公鑰爲某文件,文件名保持默認即可
ssh密鑰認證
密鑰對創建完成
ssh密鑰認證
確認服務器端配置文件/etc/ssh/sshd_config中啓用了PubkeyAuthentication和AuthorizedKeysFile。
這裏只需要確認這兩項不是配置爲no且未被註釋掉即可,配置文件中默認爲這兩項即爲yes,儘管其前面有#,初始狀態下的/etc/ssh/sshd_config中的內容都可以理解爲sshd的默認配置。
ssh密鑰認證
將xshell生成的公鑰添加到/home/root/.ssh/authorized_keys文件中
打開之前保存下來的公鑰文件,然後複製,在vim authorized_keys時粘貼過去。


在xshell中進行登錄驗證
在xshell中使用ssh root@ ,彈出登錄框時,選擇“Public key",然後選擇剛生成的私鑰,如果有設置私鑰密碼一併輸入密碼,然後”確定“即可建立連接。

也可在xshell中新建會話時,即指定此會話的身份驗證方式爲Public key,以後在打開這一會話時就會自動登錄,而不用再去手動選擇Public key。


SecureCRT實現基於密鑰認證
SecureCRT要實現基於密鑰的認證,方法和xshell大同小異。先是生成密鑰對,然後將公鑰註冊到SSH服務器,同時在建立的會話屬性,認證方法處只選擇”Public key“,如下所示:

需要注意的是SecureCRT生成密鑰時,格式選擇爲”openssh format"。其生成公鑰文件,只能從其保存的文件Identity.pub(默認文件名,可按需修改)文件中查看,只能從它複製,然後在vim authorized_keys時粘貼過去,無法在key生成工具處直接複製。

簡單地總結一下:要想實現A機器的a賬戶密鑰認證登錄B機器的b賬戶上,取決於A機器a賬戶是否保存的有a的私鑰,並且B機器b賬戶中是否保存有a的公鑰

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章