CentOS7禁止 root 直接登陆,只能通过 SSH key 登陆
一、禁止 Root 直接登陆
1、新建登陆用户
[root@localhost ~]# useradd haolb [root@localhost ~]# passwd haolb Changing password for user haolb. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. [root@localhost ~]#
2、修改 sshd 配置文件,禁用root用户登录
vi /etc/ssh/sshd_config # 修改内容 PermitRootLogin yes # 此处请留意 root 用户能否通过 SSH 登录,默认为yes。本人暂不禁用。
3、重启 sshd
systemctl restart sshd
4、登陆 通过 haolb 账号登陆后,再切换 root
su root
二、禁止 Root 密码登陆,只能通过 SSH key 登陆
1、生成 key
[root@localhost ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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:8mOUzS3bGA/eF/cgY5KihOMtlnqHeVMXUm0qTBeSTRM root@localhost.localdomain The key's randomart image is: +---[RSA 2048]----+ | .+E+ | | o.+.o | | o o o | | . ++oo | | o o SoO.= o .| | . = =.o.% o +.| | =oo.+.+ + . .| | o+.+. . . | | .. o . | +----[SHA256]-----+ [root@localhost ~]#
2、导入公钥
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
3、设置禁止密码登录
vim /etc/ssh/sshd_config # 禁止密码登陆 PasswordAuthentication no
4、重启服务
systemctl restart sshd
5、本地 Windows 登陆
把私钥拷贝到本机上,Xshell 登陆
参考
分类:
Linux
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析