1. 程式人生 > >非對稱加密演算法:實現ssh免密碼登入

非對稱加密演算法:實現ssh免密碼登入

**描述:利用非對稱加密演算法,實現兩臺主機之間可以免密碼直接登入

,如下圖:**

整個實驗的原理如下圖:
這裡寫圖片描述
具體步驟如下:


  • 1、先準備好實驗需要的環境(兩臺虛擬機器–A:192.168.72.73,B:192.168.72.23),此時,我們遠端登入主機B,必須輸入主機B的密碼以後才可以成功登入,如下:
[root@server-140 .ssh]# ssh 192.168.72.23
root@192.168.72.23's password: 
Last login: Sun Sep  9 15:40:44 2018 from 192.168
.72.73 [root@server-23 ~]#

  • 2、有些時候為了方便快速,我們並不想總是輸入密碼,現在可以利用上我們的非對稱加密演算法了 執行ssh-keygen -t rsa, 然後一路回車,就可以生成公鑰和私鑰對了
[[email protected]23 ~]# 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: SHA256:nlrLZB+BVgkuypbsKaaySggvW4ANLtzjn7Fwn029B4Q [email protected]23 The key's randomart image is: +---[RSA 2048]----+ | . | | . . . | |. . . + | |+o.o o . E . | |=o.o* S o | |+o.o.. o ..o | |o.=ooo *.... | |o* .+ =*+o ... | |B. +.oo.... | +----[SHA256]-----+ 進入/root/.ssh檢視公鑰和私鑰是否生成 [
[email protected]
23 ~]# cd /root/.ssh/ [[email protected]23 ~/.ssh]# ll 總用量 16 -rw-r--r-- 1 root root 397 99 15:42 authorized_keys -rw------- 1 root root 1679 99 15:50 id_rsa -rw-r--r-- 1 root root 396 99 15:50 id_rsa.pub -rw-r--r-- 1 root root 350 98 13:24 known_hosts 注:id_rsa為私鑰,id_rsa.pub是公鑰

  • 3、下一步就是把公鑰的內容拷貝給主機B下/root/.ssh/authorized_keys,authorized_keys如果不存在,自己建立就行
  • 我是用Xshell登入的,就直接手動複製過去了,如果是用虛擬機器的,也可以選擇遠端拷貝Scp
[[email protected]-23 ~/.ssh]# ll
總用量 16
-rw-r--r-- 1 root root  847 9月   9 16:00 authorized_keys

  • 4、操作成功後,就可以直接在主機A登入主機B了
[[email protected]23 ~/.ssh]# ssh 192.168.72.23
The authenticity of host '192.168.72.23 (192.168.72.23)' can't be established.
ECDSA key fingerprint is SHA256:OMFX56Bo/D68z1hPiIv/CmYFiICWnDaayJfx7XGfnAI.
ECDSA key fingerprint is MD5:ab:59:6a:61:b8:c0:4a:98:71:3e:7f:20:1f:ff:02:ae.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.72.23' (ECDSA) to the list of known hosts.
Last login: Sun Sep  9 15:45:29 2018 from 192.168.72.73
[[email protected]23 ~]# exit
登出
Connection to 192.168.72.23 closed.
[[email protected]23 ~/.ssh]# ssh 192.168.72.23
Last login: Sun Sep  9 16:01:27 2018 from 192.168.72.23
注:只有第一次登入時需要確認,以後就可以免密碼直接登入了

那我們的ssh的免密碼登入就成功了,以後需要輸入密碼的操作都可以不用輸入密碼了,除了ssh,還有如Scp,rsync等
但是需要我們注意的是,複製公鑰的時候要保證兩邊的公鑰完全相同,有一點不同,就無法實現免密碼登入,所以,要細心喲!!!

如果有什麼不同意見或者疑問,歡迎留言!