运行 getenforce 命令可查看当前 selinux 模式,输出 enforcing、permissive 或 disabled;更全面的信息需用 sestatus,它同时显示启用状态、当前模式、配置文件设定模式及策略类型等关键信息。

如何查看当前 SELinux 模式
SELinux 有三种运行模式:enforcing(强制执行策略)、permissive(记录但不阻止违规行为)、disabled(完全关闭)。最直接的判断方式是运行:
getenforce
它只输出一行结果,比如 Enforcing 或 Permissive。注意大小写敏感,且不带引号。
如果返回 Disabled,说明 SELinux 已在内核启动时被禁用,此时 setenforce 命令无效,必须改配置文件再重启。
临时切换 enforcing/permissive 模式
用 setenforce 可以立即切换前两种模式,无需重启:
-
setenforce 1→ 切换到enforcing -
setenforce 0→ 切换到permissive
这个操作只在当前运行周期生效;系统重启后会恢复为 /etc/selinux/config 中设定的默认模式。
常见错误:在 disabled 模式下执行 setenforce 0,会报错 setenforce: SELinux is disabled,此时不能靠这条命令“启用”SELinux。
永久修改 SELinux 模式(需编辑配置文件)
真正持久生效的方式是改 /etc/selinux/config 文件中的 SELINUX= 行:
-
SELINUX=enforcing→ 强制执行(默认生产环境推荐) -
SELINUX=permissive→ 排查问题常用,日志全但不拦截 -
SELINUX=disabled→ 彻底关闭(不建议,部分发行版如 RHEL 9+ 启动时可能报警告甚至失败)
改完保存后必须重启系统才生效。仅运行 reboot 不够——某些旧内核或 init 系统可能缓存状态,建议用 shutdown -r now 确保干净重启。
注意:SELINUXTYPE=targeted 这行一般不动,它是策略类型,不是运行模式;改错这里不会影响 enforce/permissive 切换,但可能让服务无法加载策略。
为什么 sestatus 比 getenforce 更有用
sestatus 输出更完整,能一眼看出是否真“启用”、当前模式、策略类型、以及关键路径是否存在:
$ sestatus SELinux status: enabled SELinuxfs mount: /sys/fs/selinux Current mode: enforcing Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Max kernel policy version: 33
重点看 SELinux status 和 Current mode 两行。如果 status 是 disabled,哪怕 config 文件写的是 enforcing,也说明内核没加载模块——这时候得检查启动参数有没有 selinux=0 或 security=selinux 缺失。
很多排查卡在“明明改了 config 却不生效”,其实是因为 grub 启动项里硬编码关掉了 SELinux,这种优先级高于配置文件。










