Rocky Linux 7 是一个开源的类 Red Hat Enterprise Linux (RHEL) 发行版,它为用户提供了稳定、安全且功能丰富的操作系统。在本文中,我们将深入了解 Rocky Linux 7 的安全设置,帮助用户轻松守护系统安全。
1. 系统更新和补丁管理
1.1 使用
dnf
进行系统更新
Rocky Linux 7 使用
dnf
作为包管理器,它可以帮助用户轻松地更新系统。以下是一个基本的更新命令:
sudo dnf update
1.2 设置自动更新
为了确保系统始终保持最新状态,可以配置
dnf
进行自动更新。编辑
/etc/yum.repos.d/rocky-release.repo
文件,找到
[baseos]
和
[appstream]
部分,将
enabled
设置为
1
:
[baseos]
name=Rocky Linux $releasever – BaseOS
# …
enabled=1
[appstream]
name=Rocky Linux $releasever – AppStream
# …
enabled=1
然后,运行以下命令以使更改生效:
sudo dnf makecache
2. 配置防火墙
2.1 安装
firewalld
Rocky Linux 7 默认不安装防火墙,但可以使用
firewalld
进行配置。安装
firewalld
:
sudo dnf install firewalld
2.2 启动和使能
firewalld
启动
firewalld
服务:
sudo systemctl start firewalld
使能
firewalld
在启动时自动运行:
sudo systemctl enable firewalld
2.3 配置防火墙规则
使用
firewall-cmd
命令行工具来配置防火墙规则。以下是一些示例:
允许 SSH 访问:
sudo firewall-cmd –permanent –add-service=ssh
允许特定端口:
sudo firewall-cmd –permanent –add-port=8080/tcp
查看当前规则:
sudo firewall-cmd –list-all
3. 配置 SSH 安全
3.1 修改 SSH 配置
编辑
/etc/ssh/sshd_config
文件,进行以下设置:
-禁用 root 用户登录:
PermitRootLogin no
-要求使用密钥认证:
PasswordAuthentication no
-限制 SSH 版本:
# Use these options to control the version of the SSH protocol used by the server.
# Possible values are 2 and 1; default is 2. See ssh(1) for more information.
Protocol 2
3.2 重启 SSH 服务
重启 SSH 服务以应用更改:
sudo systemctl restart sshd
4. 配置系统账户
4.1 禁用不必要的账户
删除或禁用不必要的系统账户,以减少潜在的安全风险。
4.2 设置密码策略
编辑
/etc/pam.d/common-password
文件,设置密码策略。以下是一些示例:
密码最小长度:
password requisite pam_pwquality.so retry=3 minlen=8
密码历史:
password requisite pam_pwquality.so retry=3 remember=5
5. 配置日志记录
5.1 启用系统日志
启用系统日志记录,以便跟踪系统事件。编辑
/etc/rsyslog.conf
文件,添加以下行:
*.info;*. notice;*. warning;*. emerg /var/log/messages
5.2 配置日志轮转
使用
logrotate
工具配置日志轮转。编辑
/etc/logrotate.d/syslog
文件,设置日志轮转策略:
/var/log/messages {
daily
rotate 7
compress
missingok
notifempty
create 640 root root
}
6. 总结
通过以上步骤,您可以有效地提高 Rocky Linux 7 的安全性。请定期检查系统安全状态,及时更新系统和软件,以确保系统始终保持最佳安全状态。