linux主機互信操作

Mrwhite86 發表於 2021-10-14
Linux

一.主機互信原理
兩個主機之間ssh登入需要提示輸入對方的密碼,當頻繁需要登入操作時,可以通過linux公鑰和祕鑰,建立雙機信任關係。
把你源主機的公鑰檔案內容追加到目的主機對應使用者下的authorized_keys檔案中(如果沒有這個檔案,則建立一個)

 

二.操作流程

兩臺主機分別是node1的ip是1192.168.37.8,node2的ip是192.168.37.9
1. 生成祕鑰(兩臺主機分別操作)

ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa              #生成rsa

1) node1節點操作 - 生成金鑰

[[email protected] ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa   #node1節點生成金鑰
Generating public/private rsa key pair.
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:1RzOG0HzkXAAZz6YdYMp6vgGev0zzv3zzM3VF5cKb30 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|          [email protected]==. |
|          .%oB.o |
|         .+.O .  |
|        ..   +  .|
|       oS  ..  o.|
|      o .   o o +|
|     . +     + .E|
|    . . +.o.. .+=|
|     . . o+o...o*|
+----[SHA256]-----+
[[email protected] ~]#

2) node2節點操作 -生成金鑰

[[email protected] ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa   #node2節點生成金鑰
Generating public/private rsa key pair.
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:9mRYbTPIQ3pSYwdMO5/byswezu7uicSAF9AJAxPisqU [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|  . +o+o +B..    |
| . . . oo*.*     |
|. o     + O =    |
| =     . * = +   |
|E     . S o o    |
|       o *   o   |
|          + o .  |
|         . B +   |
|          .*#    |
+----[SHA256]-----+
[[email protected] ~]#

2.信任主機操作(兩臺主機分別操作)

ssh-copy-id -i  ~/.ssh/id_rsa.pub [email protected]對方ip

1) node1節點設定互信

[[email protected] ~]# ssh-copy-id -i  ~/.ssh/id_rsa.pub [email protected]192.168.37.9  #複製37的公鑰到38機器上,這樣就可以使用在37機器上免密碼登入38機器了。
/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]192.168.37.9's password: 

Number of key(s) added: 1

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

[[email protected] ~]#

2) node2節點設定互信

[[email protected] ~]# ssh-copy-id -i  ~/.ssh/id_rsa.pub [email protected]192.168.37.8  #複製node2的公鑰到node1機器上,這樣就可以使用在node2機器上免密碼登入node1機器了
/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]192.168.37.8's password: 

Number of key(s) added: 1

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

[[email protected] ~]#

 

3.測試ssh機器免密

1) node1節點訪問node2

[[email protected] ~]# 
[[email protected] ~]# ssh 192.168.37.9
Last login: Wed Oct 13 22:13:07 2021 from 192.168.37.8
[[email protected] ~]#

node2節點訪問node1

[[email protected] ~]# ssh 192.168.37.8
Last login: Mon Sep 20 18:30:35 2021 from 192.168.37.9
[[email protected] ~]#