来自公众号:IT人家
我们在使用ssh客户端远程连接Linux服务器时,为了考虑安全方面的因素,通常使用密钥的方式来登录。密钥分为公钥和私钥,这两把密钥可以互为加解密。公钥是公开的,私钥是由个人自己持有,并且必须妥善保管和注意保密。
执行ssh-keygen命令,生成id_rsa和id_rsa.pub两个文件,id_rsa是私钥(重要,需安全保管),id_rsa.pub是公钥,密钥生成过程中可根据提示对密钥设置密码,也可留空直接回车。

解释:
1.查看密钥认证文件authorized_keys是否存在,若不存在则创建并授权,命令如下:
[root@server1 ~]# touch ~/.ssh/authorized_keys
[root@server1 ~]# chmod 600 ~/.ssh/authorized_keys
2.将公钥内容追加到authorized_keys文件中
[root@server1 ~]# cd ~/.ssh
[root@server1 .ssh]# cat id_rsa.pub >> authorized_keysRSAAuthentication yes
PubkeyAuthentication yes
PermitRootLogin yes
AuthorizedKeysFile .ssh/authorized_keys
[root@server1 ~]# systemctl restart sshd[root@server1 .ssh]# ssh root@localhost -i id_rsa
Last login: Wed Sep 13 17:13:28 2023 from 192.168.15.1

PasswordAuthentication no[root@server1 ~]# systemctl restart sshd至此,Linux已经设置为密钥登录,相对于使用密码认证登录的方式更为安全,前提是私钥要安全保管。今天的分享就到这里了,你学会了吗?