1. 程式人生 > >ssh密鑰認證

ssh密鑰認證

roc 復制 blog logging ber ssh root 加密 mach xshel

??基於密鑰的認證
實現基於密鑰的認證,在登錄過程中不需要輸入密碼,客戶端和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?
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?
?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/>?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賬戶。?
?![](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?
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?
?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/>?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賬戶。?
?localhost:
?[root@localhost?.ssh]$?ll?~/.ssh
br/>?查看localhost和xad各自的公鑰
?localhost:
?[root@localhost?.ssh]$?ll?~/.ssh
?-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:
?[root@xad?.ssh]$?ll?~/.ssh
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:
?[root@xad?.ssh]$?ll?~/.ssh
?-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指明是要註冊到對方服務器哪個賬戶下。
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指明是要註冊到對方服務器哪個賬戶下。
?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/>?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:?????????????#需要先輸入一次密碼進行登錄
?
?Now?try?logging?into?the?machine,?with:???"ssh?‘root@xad‘"
br/>?Number?of?key(s)?added:?1??#顯示註冊了一個key文件
?
?Now?try?logging?into?the?machine,?with:???"ssh?‘root@xad‘"
br/>?[root@localhost?.ssh]$?ssh?root@xad
br/>?[root@xad?~]$???????????#成功從localhost通過密鑰認證登錄xad
?[root@xad?.ssh]$?ssh-copy-id?-i?id_dsa.pub?root@localhost
br/>?xad:
?[root@xad?.ssh]$?ssh-copy-id?-i?id_dsa.pub?root@localhost
br/>?Now?try?logging?into?the?machine,?with?"ssh?‘root@localhost‘",?and?check?in:
?
?to?make?sure?we?haven‘t?added?extra?keys?that?you?weren‘t?expecting.
?
?[root@xad?.ssh]$?ssh?root@localhost
br/>???.ssh/authorized_keys
?
?to?make?sure?we?haven‘t?added?extra?keys?that?you?weren‘t?expecting.
?
?[root@xad?.ssh]$?ssh?root@localhost
br/>?[root@localhost?~]$???????#從xad通過密鑰認證成功登錄localhost
?xshell實現基於密鑰認證
?
?
?在xshell軟件中選擇“工具”,然後選擇“新建用戶密鑰生成向導”

?
?
?選擇生產密鑰的參數,圖中選擇的是DSA加密算法,密鑰長度2048位。

?
?
?密鑰對生成成功

?
?
?給用戶私鑰取名,設置密碼(可以不設置密碼)

?
?
?保存公鑰為某文件,文件名保持默認即可

?
?
?密鑰對創建完成

?
?
?確認服務器端配置文件/etc/ssh/sshd_config中啟用了PubkeyAuthentication和AuthorizedKeysFile。
這裏只需要確認這兩項不是配置為no且未被註釋掉即可,配置文件中默認為這兩項即為yes,盡管其前面有#,初始狀態下的/etc/ssh/sshd_config中的內容都可以理解為sshd的默認配置。

?
?
?將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的公鑰
?

ssh密鑰認證