背景:
A主机(node32)需要免密登录B(node33)主机
步骤:
1.在A主机运行如下命令然后一直按回车键,生成密钥对,包含公钥和私钥
ssh-keygen -t rsa
一直按回车键,显示如下:
[root@node32 .ssh]# ssh-keygen -t rsa
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:ACaurLVh+vMvDPOB4tGK4XZjegO7IsTfK0Rj0c35kPA [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| . +.+ o |
| . + o.* |
| . . .Eo |
|.. + . . |
|o.B.. S |
|+O+=. |
|BoO=.. |
|+B.O+. |
|+oBo=+o |
+----[SHA256]-----+
会在 ~/.ssh / 目录下生成两个文件:私钥:id_rsa 公钥:id_rsa.pub
2.在A主机运行如下命令将生成的公钥发送到B主机
ssh-copy-id node33
输入B主机的root密码完成操作。显示如下:
[root@node33 .ssh]# ssh-copy-id node33
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node33 (172.16.18.33)' can't be established.
ECDSA key fingerprint is SHA256:591R1j/G7VowLEhaiZoIXQZ2hfJyk8T7kf8h5BjEshE.
ECDSA key fingerprint is MD5:ac:a8:0a:5d:d4:1d:f5:29:28:8d:12:98:c8:22:b3:1e.
Are you sure you want to continue connecting (yes/no)? yes
/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@node32's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'node33'"
and check to make sure that only the key(s) you wanted were added.
完成:
现在可以在A主机尝试免密连接B主机:
ssh node33
注意:可能还需要输入密码, 这是因为权限问题, /root 目录权限:700 .ssh/目录权限:700 authorized_keys 文件权限: 700 或者600