設置Linux主機免密認證

1、生成公鑰

首先檢查主機是否存在公鑰:

進入./ssh目錄,查詢是否存在 id_rsa 和 id_rsa.pub(公鑰)。

[root@server1 ~]# cd .ssh/
[root@server1 .ssh]# ll
總用量 16
-rw-r--r-- 1 root root  802 5月  22 10:35 authorized_keys
-rw------- 1 root root 1675 5月  22 10:34 id_rsa
-rw-r--r-- 1 root root  394 5月  22 10:34 id_rsa.pub
-rw-r--r-- 1 root root  522 5月  20 16:31 known_hosts

如果公鑰不存在,需要生成公鑰:

使用命令 ssh-keygen 生成公鑰,在後續的回顯中一路Enter

[root@host199 ~]# 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:2CQOB8XnJktJnasdTcTr2J9fI9k4iZufr0tJfUBnK9s root@host199
The key's randomart image is:
+---[RSA 2048]----+
|    .o.. +.   . o|
|     .o + o  . o.|
|    ..o+.+ . ... |
|     ++== o   =. |
|     .o*S=   o E.|
|      o o o o * .|
|           o X o.|
|            * +..|
|           o.*=. |
+----[SHA256]-----+

 

2、對目標主機設置免密

使用命令 ssh-copy-id <ip> 對目標主機設置免密。ip爲目標主機的ip。

命令回顯需輸入目標主機的密碼。

[root@host199 ~]# ssh-copy-id 10.1.104.188
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.1.104.188'"
and check to make sure that only the key(s) you wanted were added.

此時,主機10.1.104.188對當前主機免密。

使用ssh測試一下:

[root@host199 ~]# ssh [email protected]
Last login: Thu May 28 18:53:33 2020 from 10.2.3.200
[root@server1 ~]#

 

⚠️ 注意:如果要實現雙向免密,需要在目標主機上重複操作一次即可。

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