温馨提示×

CentOS Context 如何检查

小樊
36
2026-08-04 08:04:48
栏目: 智能运维

如果你说的 “CentOS Context” 指的是 SELinux 的安全上下文(Security Context),下面是最常用、最实用的检查方法。


一、检查 SELinux 是否启用

getenforce
  • Enforcing:启用并强制执行
  • Permissive:仅记录,不拦截
  • Disabled:已关闭

查看详细状态:

sestatus

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

1️⃣ 查看文件上下文

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

三、查看进程(Process)的 SELinux Context

ps -eZ

或查看某个进程:

ps -eZ | grep httpd

示例输出:

system_u:system_r:httpd_t:s0  1234 ? 00:00:01 httpd

四、查看端口的 SELinux Context

semanage port -l

查看某个端口:

semanage port -l | grep 80

五、查看用户 SELinux Context

id -Z

示例:

unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

六、查看策略中的 Context 定义

seinfo -t

(需安装 setools-console

yum install setools-console

七、临时修改文件 Context(重启可能失效)

chcon -t httpd_sys_content_t /var/www/html/test.html

八、永久修改文件 Context(推荐)

semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
restorecon -Rv /var/www/html

九、常见 Context 类型速查

类型 用途
httpd_sys_content_t Apache 可访问
httpd_sys_rw_content_t Apache 可读写
ssh_home_t SSH 密钥
user_home_t 用户主目录
etc_t 配置文件
bin_t 可执行程序

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

请补充说明,例如:

  • systemd 上下文
  • 网络 namespace / 容器上下文
  • Shell 环境变量上下文
  • CentOS 版本(7 / 8 / Stream)

我可以给你更精确的命令 ✅

0