总体思路 在 CentOS 上,将 SELinux 保持在 Enforcing 模式,以 最小权限原则约束进程与文件访问;即便 root 也要遵循 SELinux 策略,从而在误操作发生时及时阻断并告警,降低对系统的影响范围。
快速防护配置
getenforce(返回 Enforcing/Permissive/Disabled)setenforce 0(Permissive,仅记录不阻断)或 setenforce 1(Enforcing)SELINUX=enforcing,重启后保持ls -Z /pathchcon -R -t <type> /pathsemanage fcontext -a -t <type> "/path(/.*)?" 后执行 restorecon -R /pathsetsebool -P <boolean> on/off 控制细粒度行为(如 FTP 家目录写入、NFS 导出等),变更前评估影响并尽量使用持久化 -P 参数故障排查与回退
ausearch -m avc -ts recent 或 aureport -aaudit2why 解释拒绝原因,必要时用 audit2allow -M <modname> 生成最小化自定义模块并通过 semodule -i <modname>.pp 安装setenforce 0(进入 Permissive 模式仅告警不阻断),故障修复后再切回 Enforcingchcon 直接改标签,优先用 semanage fcontext + restorecon 保证重启与策略更新后的持久一致性常见场景与命令清单
| 场景 | 关键命令 | 要点 |
|---|---|---|
| Web 目录迁移后 403 | ls -Z /var/www/html; semanage fcontext -a -t httpd_sys_content_t "/webdata(/.*)?"; restorecon -R /webdata |
让自定义目录获得 httpd 可读类型 |
| FTP 匿名写入 | setsebool -P allow_ftpd_anon_write 1 |
按需开启布尔值,变更后验证 |
| 查看进程域 | `ps auxZ | grep |
| 分析拒绝原因 | ausearch -m avc -ts recent; audit2why |
先研判再决定是否放行或写策略 |
| 临时放行排障 | setenforce 0 |
仅排障用,修复后务必恢复 Enforcing |
运维建议