本次测试需要服务器己安装好 ssh-keygen和ssh-copy-id。安装方式如下:
# 安装ssh-keygen
[root@localhost ~]# yum install -y ssh-keygen
# 安装ssh-copy-id
[root@localhost ~]# yum install -y ssh-copy-id
1、在客户端机器上生成密钥,这里连续三个回车默认即可。?
[root@localhost ~]# 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:KjTbCQYF1WzPvmkbxlZH5lU1yYYEaY3H4LHKoXVObzE root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| .ooo +O.o.=|
| . + .+o= =.|
| . . o o.=+Eo |
| . * *+..o |
| = oSo..oo |
| o = +.. .. |
| o + =o |
| . o+. |
| ... |
+----[SHA256]-----+
[root@localhost ~]#
3、进入到/root/.ssh/目录下,查看是否生成密钥成功,这里一共生成三个文件,本次主要用到的是id_rsa.pub:私钥(本次实验用到的)
[root@localhost ~]# cd /root/.ssh/
[root@localhost .ssh]# ls
id_rsa id_rsa.pub known_hosts
?4、通过命令复制私钥到服务器端,这里因为还没免密登录所以这里会要输入服务器密码。
[root@localhost .ssh]# scp id_rsa.pub root@192.168.137.130:/root/.ssh
root@192.168.137.130's password:
id_rsa.pub
5、登录到服务器上,查看客户端通过SCP复制文件是否成功,并查看密钥内容不为空。并通过使用>>进行追加至authorized_keys, 不要用>, 那会清空原有内容?。
[root@localhost .ssh]# cd /root/.ssh/
[root@localhost .ssh]# ls
known_hosts
[root@localhost .ssh]# ls
id_rsa.pub known_hosts
[root@localhost .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2PCL8vIWQAToT2NZ4PCHRqW3Yqic8nQAuk4g6Z2VQ7rUR/uxNnqwAPBFDA/RT7ejA7z+71sESqbQPBaIxgPDNtNAQVoRLQZ8YSoMG5q10uy+4QNP6DhRv4Bu116Xnm3BXDJKWatnu5x6+XYaygEAga/imp7JUmmtKz8xWnx+Ajwl0UMqIqzB3FH01KWlBGNOgr1VQgO4RyV3/cZTkJS6AGEuUmFKGpQwUJ8c6H4iokcPpy2Y/YMmKvCPMWN9hyoIiwKFyXQJ/VcfnIgDHjGOJ7ZJV68zQF6mLCXbBMEeofDgXKBLnxXRIYN9wy0zq8HE4uiUWTPHB8BtdE/4joQQP root@localhost.localdomain
[root@localhost .ssh]#
[root@localhost .ssh]#
[root@localhost .ssh]# cat id_rsa.pub >> authorized_keys
[root@localhost .ssh]# ls
authorized_keys id_rsa.pub known_hosts
6、追加内容成功后这里cat一下确定成功后,修改权限为644
[root@localhost .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2PCL8vIWQAToT2NZ4PCHRqW3Yqic8nQAuk4g6Z2VQ7rUR/uxNnqwAPBFDA/RT7ejA7z+71sESqbQPBaIxgPDNtNAQVoRLQZ8YSoMG5q10uy+4QNP6DhRv4Bu116Xnm3BXDJKWatnu5x6+XYaygEAga/imp7JUmmtKz8xWnx+Ajwl0UMqIqzB3FH01KWlBGNOgr1VQgO4RyV3/cZTkJS6AGEuUmFKGpQwUJ8c6H4iokcPpy2Y/YMmKvCPMWN9hyoIiwKFyXQJ/VcfnIgDHjGOJ7ZJV68zQF6mLCXbBMEeofDgXKBLnxXRIYN9wy0zq8HE4uiUWTPHB8BtdE/4joQQP root@localhost.localdomain
[root@localhost .ssh]#
[root@localhost .ssh]# chmod 644 authorized_keys
关于权限注意:
????????对普通用户,建议设置成600权限: chmod 600 authorized_keys ????????对root用户,建议设置成644权限: chmod 644 authorized_keys
7、在客户端上通过ssh免密登录成功
[root@localhost .ssh]# ssh root@192.168.137.130
Last login: Tue May 10 15:53:32 2022 from 192.168.137.133
[root@localhost ~]#
|