主要实现两台虚拟机之间的免密登录,总所周知,虚拟机之间登录使用的协议是ssh协议,端口号是 22
主机
[root@web-2 ~]# 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:KAYYbgx3bYuBTcMzbr2mtG3+4HxNmSRtZOqtDFwOAhM root@web-2
The key's randomart image is:
+---[RSA 2048]----+
|o E*+. |
|+++.*.o o |
|.+.+ B . = |
|. .= +.+ + |
| .oo.*S= o |
| ...= o = |
| . =.o + |
| ooo.+ . |
| o+o. |
+----[SHA256]-----+
# 使用 ssh-keygen 的命令的话,可以直接一路回车到底,它采用的默认加密算法是 RSA
复制到从机中
[root@web-2 ~]# ssh-copy-id root@192.168.37.122
这样的话,就可以实现无密码登录,我们的实验过程也会顺畅很多。 注意,如果出现了一下报错:
-bash: ssh-copy-id: command not found
那么就证明我们需要安装一个包:
yum -y install openssh-clients
把包安装上即可。
主机登录到从机中
[root@web-2 ~]# ssh 192.168.37.122
如果在使用ansible之类的,无法进行命令好之间的交互,可以直接使用下面这行代码,直接一行代码创建密钥
[root@localhost ~]# ssh-keygen -f ~/.ssh/id_rsa -P '' -q
