设置公钥私钥实现免登陆
一、设置公钥私钥。
1.生成公钥私钥
[root@7-11 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #密钥的保存路径,建议直接回车
Created directory '/root/.ssh'.
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:cLF/ydC8mLd4nblBcAz4TLAaqu+LPPhIUrX72b/y8Xs root@7-11
The key's randomart image is:
+---[RSA 2048]----+
| . .o. |
| o.+.o |
| . . + o++ o |
| . . + + =o= |
| . . . S + * . |
| . o + + o |
|. ..o o o = |
| o.o.+ o. + Eo |
| ..+o*..++.oo. |
+----[SHA256]-----+
2.查看生成的公私钥
[root@7-11 ~]# cd /root/.ssh/
[root@7-11 .ssh]# ls
id_rsa id_rsa.pub
3.把公钥传给服务器2
[root@7-11 ~]# ssh-copy-id root@192.168.100.12
/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@192.168.100.12's password: # 服务器2的密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.100.12'"
and check to make sure that only the key(s) you wanted were added.
4.测试免等是否成功
[root@7-11 ~]# ssh root@192.168.100.12
Last login: Sun May 23 15:57:11 2021 from 192.168.100.253
[root@7-12 ~]#