CentOS7禁止 root 直接登陆,只能通过 SSH key 登陆

本文介绍了在Linux系统中如何禁止Root用户直接登录以及通过密码登录,转而使用SSHkey进行身份验证。步骤包括创建新用户、修改sshd配置、生成SSH密钥对、导入公钥、设置禁止密码登录并重启服务。这增强了系统的安全性。
摘要由CSDN通过智能技术生成

一、禁止 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 登陆

image

参考

https://blog.csdn.net/mshxuyi/article/details/100517924

https://www.cnblogs.com/yzgblogs/p/15191338.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值