Linux SSH Public Key登錄與免密登錄
- ssh 遠程登錄密碼認證的方式有三種,
password
、Keyboard Interactive
、Public Key
前面兩種方式就是密碼認證,含義都是一樣大同小異。第三種是登錄方式最安全的一種。接下來就是討論這種。
原理
- ssh客戶端利用服務端發過來的私鑰,進行登錄的認證,認證服務端的公鑰。從而實現安全的訪問。
- 這種方式是使用ssh的publickey可以進行免密登錄,假設我們要登錄的遠程服務器爲linux,本地可以是windows或者linux都行,操作如下:
兩臺機器的命令執行
- 打開命令行,執行命令
ssh-keygen
,兩臺機器都要執行的,windows和linux存放的位置不一樣,但是名字是一樣的。
jackdan@jackdan-ThinkPad-T430:~$ ssh-keygen
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:lVbwkC08s+Qh56PrdxfSGYoifBrg7lSbWDinZnfRwI8 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| .o+. |
| .. X=. |
| o*+*. |
| .. B= . |
| .oo+ E.oo o o |
| .B+o+.. o + |
| .* +=.o . . |
| +..... . . . |
| .. ... . . |
+----[SHA256]-----+
Enter file in which to save the key (/root/.ssh/id_rsa):
默認存儲路徑;
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
- 兩次回車,代表免密登錄
ssh文件目錄
- 進入上面這個目錄,windows目錄是
C:/User/.ssh/
這個目錄,linux目錄是~/.ssh/
這個目錄。
jackdan@jackdan-ThinkPad-T430:~/.ssh$ ll
total 44
drwx------ 2 jackdan jackdan 4096 7月 25 14:36 ./
drwxr-xr-x 87 jackdan jackdan 4096 9月 16 16:38 ../
-rw------- 1 jackdan jackdan 1675 7月 10 2018 id_rsa
-rw-r--r-- 1 jackdan jackdan 406 7月 10 2018 id_rsa.pub
...
id_rsa
私鑰文件;id_rsa.pub
公鑰文件。
本地公鑰文件拷貝到遠程服務器上
- 將本地的公鑰拷貝一份放到遠程服務器的下面這個目錄(直接動手進行復制,或者
ssh-copy-id -i id_rsa.pub [email protected]
),將10.10.1.2
換成遠程服務器地址。
[root@localhost .ssh]# ssh-copy-id -i id_rsa.pub [email protected]
[root@localhost .ssh]# ll
-rw-------. 1 root root 408 9月 17 16:27 authorized_keys
...
authorized_keys
存放免密認證電腦的公鑰。
免密登錄遠程服務器
- 最後,你就可以直接登錄遠程服務器了,使用命令
ssh [email protected]
,其中把127.0.0.1換成遠程服務器的ip
,再補充一下,ssh指定端口號登錄ssh -p xx user@ip
,xx爲端口號、user爲用戶名、ip爲要登錄的ip。
JackDan9 Thanking