Linux SELinux 更新与维护指南
一 版本与平台差异
二 日常维护与更新流程
getenforce、sestatussetenforce 0|1(仅在当前会话生效,重启后按配置文件生效)ls -Z、ps -Z、chcon(临时)、semanage fcontext(永久)、restorecon -Rv(重打标签)semanage port -l | grep <svc> 查看;semanage port -a -t <type> -p <proto> <port> 添加;-d 删除getsebool -a、setsebool -P <bool> on|off(持久化)三 故障排查与策略优化
ausearch -m avc -ts recent 快速检索最近拒绝;aureport 生成汇总报告grep <关键词> /var/log/audit/audit.log | audit2why(解释原因)、audit2allow(生成允许规则)audit2allow -M <name> 生成 semodule -i <name>.pp 加载;如需仅“隐藏”拒绝而不放行,可用 -D 生成 dontaudit 规则semodule -r <name> 卸载模块。四 变更管理与回退
sestatus、semodule -l、semanage fcontext -l、semanage port -l;必要时导出自定义模块与本地策略源文件semodule -r <name> 卸载或 setenforce 0 回退到宽容模式。semodule -d <module>;恢复:semodule -e <module>(重启后仍然有效)。适用于排查发行版策略组件引发的问题。五 附录 常用命令速查
| 目标 | 命令示例 |
|---|---|
| 查看状态 | getenforce、sestatus |
| 切换模式 | `setenforce 0 |
| 查看/修复上下文 | ls -Z、ps -Z、chcon -t <type> <path>、semanage fcontext -a -t <type> '<path>(/.*)?'、restorecon -Rv <path> |
| 端口管理 | `semanage port -l |
| 布尔值 | getsebool -a、`setsebool -P |
| 日志分析 | ausearch -m avc -ts recent、aureport、audit2why、audit2allow -M <name> |
| 模块管理 | semodule -l、semodule -i <name>.pp、semodule -r <name>、`semodule -d |
| 全量重标记 | touch /.autorelabel && reboot |
| 以上命令覆盖日常检查、修复与策略微调的高频场景,建议结合变更记录与回退方案使用。 |