温馨提示×

Centos context怎么查看

小樊
45
2026-07-09 01:04:35
栏目: 智能运维

在 CentOS(以及 RHEL 系列系统)里,“context”通常指 SELinux 上下文(Security Context)。下面按常见需求给你整理一下查看方法。


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

1️⃣ 使用 ls -Z

最常用:

ls -Z /path/to/file

示例:

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

输出类似:

unconfined_u:object_r:httpd_sys_content_t:s0 index.html

字段含义:

用户:角色:类型:级别

2️⃣ 查看目录本身

ls -Zd /var/www/html

二、查看进程的 SELinux Context

1️⃣ 使用 ps -Z

ps -Z aux

或只看某个进程:

ps -Z -C nginx

示例输出:

system_u:system_r:httpd_t:s0 root 1234 nginx

2️⃣ 查看当前 shell 的 context

id -Z

输出示例:

unconfined_u:unconfined_r:unconfined_t:s0

三、查看网络端口的 Context(常用)

semanage port -l

查看某个服务:

semanage port -l | grep http

四、查看 SELinux 当前状态(先确认是否开启)

getenforce

可能结果:

  • Enforcing
  • Permissive
  • Disabled

如果是 Disabled,context 基本无意义。

查看详细状态:

sestatus

五、查看策略或布尔值 Context

查看布尔值

getsebool -a

查看某个布尔:

getsebool httpd_can_network_connect

六、如果你说的 “context” 是 systemd 的 Context

查看 service 配置

systemctl show nginx

或:

systemctl cat nginx

总结(常用命令速查)

查看对象 命令
文件 context ls -Z
目录 context ls -Zd
进程 context ps -Z
当前 shell id -Z
SELinux 状态 getenforce
端口 context semanage port -l

如果你说的 “Centos context”不是 SELinux,而是:

  • Oracle / 数据库
  • Docker / Kubernetes
  • LXC / 容器
  • systemd unit file

可以再具体说一下,我可以按对应场景详细讲。

0