Linux系统设置秘钥登录+禁用密码登录(免密登录)
1、登录云主机生成秘钥对,并设置文件权限
[root@blogs-v2 ~]# ssh-keygen -t rsa # 生成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:+YIAiI2uKdNDWj+7vA82z7QSc6pM2y/qiFuPEZdWCEw root@blogs-v2 The key's randomart image is: +---[RSA 2048]----+ | oE | |.+.. . | |+ o . . | |. . o . | | .+ = S | |.* =o... . | |* * *=o . . | |o* X+O . . | |+oBoBB* | +----[SHA256]-----+ [root@blogs-v2 ~]# ll -a total 727388 dr-xr-x---. 8 root root 4096 Jul 17 22:15 . dr-xr-xr-x. 18 root root 252 Apr 9 2020 .. drwx------ 2 root root 61 Jul 17 23:26 .ssh [root@blogs-v2 ~]# chmod 700 .ssh [root@blogs-v2 ~]# chmod 600 .ssh/authorized_keys [root@blogs-v2 ~]# cd .ssh [root@blogs-v2 .ssh]# cat id_rsa.pub >> authorized_keys # 将id_rsa.pub的内容追加到authorized_keys 【注】如果要设置的用户不是root,比如:用户star.gao的.ssh目录应该是 /home/star.gao/.ssh, 并且要将.ssh目录和authorized_keys文件的文件所有者改为star.gao,否则还是登陆不了的。 [root@blogs-v2 ~]# chown -R star.gao:star.gao /home/star.gao/.ssh [root@blogs-v2 ~]# ll .ssh/ # 查看公私钥 total 12 -rw------- 1 root root 396 Jul 17 23:26 authorized_keys -rw------- 1 root root 1679 Jul 17 23:24 id_rsa -rw-r--r-- 1 root root 395 Jul 17 23:24 id_rsa.pub
3、修改SSH配置
[root@blogs-v2 ~]# vim /etc/ssh/sshd_config PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys #RSAAuthentication yes 【注】CentOS7.4相对于之前版本,做了一些与sshd相关的安全更新来加强sshd的安全性。其中之一就是弃用RSAAuthentication支持。
3、Linux秘钥登录方法
[root@ansible ~]# ssh root@10.25.203.134 -i id_rsa
4、windows秘钥登录
5、秘钥登录验证成功后禁用密码登录
[root@blogs-v2 ~]# vim /etc/ssh/sshd_config PasswordAuthentication no
注意:切记需要先验证秘钥登录的可行性,验证成功,才能禁用密码登录。否则就只能单用户还原配置了!!!
作者:UStarGao
链接:https://www.starcto.com/application_of_operational/257.html
来源:STARCTO
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
UCloud云平台推荐
随便看看
- 2022-02-26Linux iptables控制网络访问
- 2021-06-19Windows分析系统磁盘空间占用及清理
- 2021-09-14开源入侵检测系统OSSEC介绍
- 2021-07-13MongoDB主从复制搭建教程-单机热备
- 2021-05-09CURL方法测试网站响应速度