linux

SELinux状态查询方法有哪些

小樊
37
2025-10-31 01:29:13
栏目: 智能运维

SELinux状态查询的常用方法

1. 使用getenforce命令

getenforce是最直接的状态查询命令,用于快速获取SELinux的当前运行模式(无需root权限)。执行后会返回以下三种结果之一:

[root@localhost ~]# getenforce
Enforcing

2. 使用sestatus命令

sestatus(SELinux Status)提供详细的SELinux状态信息,包括配置状态、运行模式、策略版本等。常用选项:

[root@localhost ~]# sestatus -v
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
...

3. 查看/etc/selinux/config配置文件

SELinux的默认运行模式(重启后生效)记录在该配置文件中。通过cat命令查看文件中的SELINUX=参数值:

[root@localhost ~]# cat /etc/selinux/config
...
SELINUX=enforcing
SELINUXTYPE=targeted

4. 使用lsenforce命令(libselinux库工具)

lsenforcelibselinux库提供的工具,用于查询SELinux的运行状态值(需root权限)。返回结果为数字:

[root@localhost ~]# lsenforce
1

5. 使用selinuxenabled命令

selinuxenabled用于检查SELinux是否启用(返回状态码而非文字描述)。执行后:

[root@localhost ~]# selinuxenabled
[root@localhost ~]# echo $?  # 输出0表示启用
0

以上方法覆盖了从快速检查到详细信息的不同需求,其中getenforcesestatus是最常用的日常查询工具,而配置文件查看则用于确认默认状态。

0
看了该问题的人还看了