SELinux日志分析故障的实用流程
一、定位与收集日志
systemctl status auditd,确保 auditd 正在运行;若未运行,SELinux 拒绝可能被记录到 /var/log/messages。grep -i "denied" /var/log/audit/audit.logausearch -m avcaureport -m avcjournalctl | grep AVCsemodule -DB(排查完成后用 semodule -B 恢复)。二、读懂关键字段与典型场景
type=AVC msg=audit(...): avc: denied { getattr } for pid=... comm="httpd" path="/var/www/html/file1" scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:var_t:s0 tclass=file permissive=0sealert -a /var/log/audit/audit.log 生成可读报告并给出修复建议(如 restorecon、策略模块)audit2why 解释拒绝原因并提示是否可用 audit2allow 生成策略模块audit2allow -a /var/log/audit/audit.log 汇总并生成建议规则三、按图索骥的排查与修复
ls -Z 查看客体标签;2) 用 sealert 或 restorecon -v <path> 按建议恢复默认标签;3) 若是自定义目录,先用 semanage fcontext 注册正确类型,再 restorecon -R -v <path> 应用。semanage fcontext -a -t httpd_sys_content_t "/abc/www/html(/.*)?"restorecon -R -v /abc/www/htmlsetenforce 0(Permissive),验证问题是否消失;修复后再 setenforce 1(Enforcing)。semanage permissive -a mysqld_t(排查完 semanage permissive -d mysqld_t 恢复)。ausearch -c 'httpd' --raw | audit2allow -M my-httpdsemodule -i my-httpd.pp(必要时使用较高优先级:semodule -X 300 -i my-httpd.pp)四、验证与恢复
ausearch -m avc 或 aureport -m avc 验证。systemctl restart httpd),确认业务恢复正常。semanage fcontext 与 restorecon 固化下来的标签修复。五、无法登录或系统无法正常启动的紧急处置