免密登陆
需求描述:
用户A登陆服务器A,通过用户B远程免密登陆服务器B
操作步骤:
1) 用户A登陆服务器A,执行如下命令
ssh-keygen
此时,会出现以下提示,一直回车,都选择默认
++++++++++++++++++++++++++++++++++++++++++++++++++++++
Generating public/private rsa key pair.
Enter file in which to save the key (/home/用户A/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/用户A/.ssh/id_rsa.
Your public key has been saved in /home/用户A/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:o8iR7Ib+lfses6VkKFKfOfdfdSH2jpxNYGVhlQCNxlgJwfdd4PDhQ 用户A@服务器A
The key's randomart image is:
+---[RSA 2048]----+
|oE+ =+BBo. |
| o * ++o+ |
| fd. o .... |
| o + oorero |
| * =.S. |
| =.B.+ o . |
| fd. Bo= o |
| . ..+ fdd. |
| fdf..o.... |
+----[SHA256]-----+
++++++++++++++++++++++++++++++++++++++++++++++++++++
此时,会在以下路径生成如下两个文件:
/home/用户A/.ssh/id_rsa
/home/用户A/.ssh/id_rsa.pub
2) 将id_rsa.pub中的文本拷贝到authorized_keys文件中,执行如下命令:
cat /home/用户A/.ssh/id_rsa.pub > /home/用户A/.ssh/authorized_keys
3) 将authorized_keys文件拷贝到用户B在服务器B的默认路径,即如下路径:
/home/用户B/.ssh/authorized_keys
4) 确保服务器B以下文件夹及文件的权限,权限不能太大也不能太小
chmod 600 authorized_keys
chmod 700 .ssh
5) 测试免密登陆
ssh -i /home/用户A/.ssh/id_rsa 用户B@服务器B
注意,第一次登陆时,是需要输入密码的,然后再登陆时,就无需密码了