CentOS安全指令:保障系统稳定运行的关键
系统初始化安全设置

更新系统
确保系统软件包是最新的,以修复已知的安全漏洞,使用以下命令更新系统:
sudo yum update
重置root密码
为了提高安全性,建议在安装后立即重置root密码,使用以下命令重置:
sudo passwd root
禁用root用户登录
为了防止未经授权的访问,建议禁用root用户登录,编辑/etc/ssh/sshd_config文件,找到PermitRootLogin行,将其值修改为no,然后重启SSH服务:
sudo nano /etc/ssh/sshd_config sudo systemctl restart sshd
配置防火墙
安装防火墙
安装firewalld防火墙,使用以下命令:
sudo yum install firewalld
开启防火墙
启用并启动firewalld服务:

sudo systemctl enable firewalld sudo systemctl start firewalld
设置防火墙规则
根据需要设置防火墙规则,以下是一个示例:
| 服务 | 端口 | 动作 |
|---|---|---|
| HTTP | 80 | 允许 |
| HTTPS | 443 | 允许 |
| SSH | 22 | 允许 |
使用以下命令添加规则:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --permanent --add-service=ssh sudo firewall-cmd --reload
配置SELinux
查看SELinux状态
使用以下命令查看SELinux状态:
sestatus
修改SELinux模式
如果需要修改SELinux模式,可以使用以下命令:
sudo setenforce 0
将0替换为1可以启用SELinux。
配置SSH服务
修改SSH配置

编辑/etc/ssh/sshd_config文件,进行以下修改:
#禁用密码登录 PasswordAuthentication no #启用密钥登录 PubkeyAuthentication yes #禁用X11转发 X11Forwarding no #禁用空密码登录 PermitEmptyPasswords no
重启SSH服务
重启SSH服务以应用修改:
sudo systemctl restart sshd
FAQs
问题:如何查看系统是否已安装firewalld防火墙?
解答:使用以下命令查看:
yum list installed | grep firewalld
如果输出结果中包含firewalld,则表示已安装。
问题:如何查看SELinux的状态?
解答:使用以下命令查看:
sestatus
如果输出结果中显示SELinux已启用,则表示SELinux正在运行。

