温馨提示×

CentOS Context 如何查看

小樊
72
2026-08-04 07:28:49
栏目: 智能运维

在 CentOS(以及 RHEL 系列) 中,“Context” 通常指 SELinux 的安全上下文(Security Context)。下面按常见场景说明如何查看。


一、查看文件/目录的 SELinux Context

1️⃣ 使用 ls -Z

ls -Z /path/to/file

示例:

ls -Z /var/www/html/index.html

输出示例:

system_u:object_r:httpd_sys_content_t:s0 index.html

字段含义:

用户:角色:类型:级别

2️⃣ 查看目录及内容

ls -Zd /var/www/html
ls -Z /var/www/html

二、查看进程的 SELinux Context

使用 ps -Z

ps -Z

或查看指定进程:

ps -Z -p $(pidof httpd)

示例输出:

system_u:system_r:httpd_t:s0 1234 httpd

三、查看端口的 SELinux Context

使用 semanage port -l

semanage port -l

查看某个端口:

semanage port -l | grep 80

四、查看当前用户的 SELinux Context

id -Z

示例:

unconfined_u:unconfined_r:unconfined_t:s0

五、查看系统 SELinux 状态

sestatus

或:

getenforce
  • Enforcing:强制模式
  • Permissive:宽容模式
  • Disabled:关闭

六、查看某个服务的 SELinux Context(示例)

ls -Z /usr/sbin/httpd
ps -Z | grep httpd

七、常见 Context 类型说明

类型 说明
httpd_sys_content_t Apache 可访问的网页文件
httpd_exec_t Apache 可执行文件
mysqld_exec_t MySQL 可执行文件
unconfined_t 不受限制(常见于桌面)

八、如果你指的是“上下文”但不是 SELinux

如果你指的是:

  • Shell 上下文(bash / environment)
  • 系统语言/区域(locale)
  • 容器上下文(Docker / Podman)
  • 网络命名空间 / systemd context

可以告诉我具体场景,我可以给更精确的命令。


✅ 总结一句话

在 CentOS 中,查看 SELinux Context 最常用的是:
ls -Z(文件) / ps -Z(进程) / id -Z(用户)

如果你有具体报错或想解决某个 SELinux 拒绝问题,也可以直接贴出来。

0 踩