1.通过ssh-keygen命令生成密钥。
[root@master1 ~]# 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:KO5bQregq17ztbYnL4WiF1eSxjBZGCy09nqr8u665n0 root@master1
The key's randomart image is:
+---[RSA 2048]----+
| ....=. |
| ..* |
| o. + . |
| . . * . |
| +.+ S |
| +.* + . |
| .++.*.. |
| oo++Eo+.. |
|=*O**+.o*. |
+----[SHA256]-----+
2.通过命令ssh-copy-id master2 ,输入master2密码。即可
root@master1 ~]# ssh-copy-id master2
/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
root@master2's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'master2'"
and check to make sure that only the key(s) you wanted were added.
3.ssh master2 验证
[root@master1 ~]# ssh master2
Last login: Mon May 6 13:44:35 2024 from node1
[root@master2 ~]#