在当今网络环境下,确保系统安全是每个系统管理员的首要任务。CentOS 7作为一款流行的Linux操作系统,其超级用户登录安全尤为重要。本文将详细介绍如何在CentOS 7上实施一系列安全加固措施,以有效防范非法访问。
一、账号密码策略优化
1. 查看系统登录日志
lastlog
:查看登录用户、端口和登录时间等信息。
last -x
:查看系统关闭、用户登录和退出的信息。
lastb
:查看尝试登录的失败信息。
2. 设置口令复杂度
通过修改
/etc/pam.d/system-auth
文件,可以设置口令复杂度:
password required pam_cracklib.so try_first_pass retry=3 dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1 minlen=8
这行配置意味着密码至少包含一个数字、一个小写字母、一个大写字母、一个特殊字符,且密码长度大于8。
二、限制root用户登录
1. 创建普通用户
使用
useradd
命令创建一个普通用户,例如
gzprg
。
useradd gzprg
2. 为普通用户添加su授权
编辑
/etc/sudoers
文件,为普通用户添加su授权:
gzprg ALL=(ALL) ALL
3. 修改root用户禁止SSH登录系统
编辑
/etc/ssh/sshd_config
文件,将
PermitRootLogin
设置为
no
。
PermitRootLogin no
三、SSH限制IP和用户登录
1. 允许某个IP登录
编辑
/etc/hosts.allow
文件,添加如下配置:
sshd:192.168.10.1:allow
2. 允许某个网段的IP登录
编辑
/etc/hosts.allow
文件,添加如下配置:
sshd:192.168.10.:allow
3. 只允许特定IP以特定用户身份登录
编辑
/etc/hosts.allow
文件,添加如下配置:
AllowUsers admin@192.168.0.222 root@192.168.1.135
四、其他安全加固措施
1. 禁用root之外的超级用户
编辑
/etc/passwd
文件,禁用root之外的超级用户。
2. 设置SSH服务安全配置
编辑
/etc/ssh/sshd_config
文件,设置以下配置:
Port 22
MaxAuthTries 3
PermitRootLogin no
RSAAuthentication yes
PubkeyAuthentication yes
ClientAliveInterval 300
ClientAliveCountMax 0
LogLevel INFO
3. 设置用户登录超时
编辑
/etc/profile
文件,设置用户登录超时:
export TMOUT=180
4. 使用密钥登录
禁用密码登录,使用密钥登录:
PermitRootLogin no
PasswordAuthentication no
五、总结
通过以上安全加固措施,可以有效提高CentOS 7超级用户登录的安全性,防范非法访问。在实际操作过程中,还需根据实际情况调整和优化配置,以确保系统安全。