1、生成公钥和私钥
如果本机里面没有私钥或者公钥文件,需要用ssh-keygen生成一个公钥和私钥。随后进行免密登录copy密码到受控主机上即可。
[root@localhost .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //确认存放公钥的地址,默认就是上面说的路径,直接enter键确认
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:J3pKt1ZW4NBpp8TuuvsCPn+T3aMO3ZCLGI7IbZHTBR8 root@localhost
The key's randomart image is:
+---[RSA 2048]----+
| .oE. |
| .oB.. |
| *o+ |
| o .+ .. |
| + S...o |
| . o.* =+o + |
| o.*.=+ooo.. |
| oo++.+.. o |
| .+=*.oo. . |
+----[SHA256]-----+
您在 /var/spool/mail/root 中有新邮件
[root@localhost .ssh]# ls
id_rsa id_rsa.pub known_hosts
2、设置免密登录
将公钥拷贝到要免登陆的机器上
[root@localhost ssh]# ssh-copy-id ip