温馨提示×

怎样查看centos context

小樊
62
2026-09-09 04:35:47
栏目: 智能运维

在 CentOS(以及一般的 Linux 系统)里,“context” 最常见的含义是 SELinux 安全上下文(Security Context)。下面分几种常见场景说明如何查看。


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

1. 使用 ls -Z

ls -Z /etc/passwd

输出示例:

system_u:object_r:passwd_file_t:s0 /etc/passwd

2. 查看目录及其内容

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

二、查看进程的 SELinux context

1. 使用 ps -Z

ps -Z
ps -Z -C nginx

示例:

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

2. 查看当前 shell 的 context

id -Z

或

echo $SELINUX_ROLE

三、查看用户 SELinux context

semanage login -l

四、查看端口的 SELinux context

semanage port -l

查看某端口:

semanage port -l | grep 80

五、查看系统 SELinux 状态

getenforce
sestatus

六、如果“context”指其他含义

如果你说的 context 是指:

  • systemd 单元上下文 → systemctl show <service>
  • Docker 容器 context → docker inspect <container>
  • Shell 环境变量/上下文 → env、printenv
  • Kubernetes context → kubectl config get-contexts

可以告诉我你具体指的是哪一种,我可以给你更精确的命令。


✅ 总结一句话:
在 CentOS 中,查看 SELinux context 最常用的是 ls -Z、ps -Z 和 id -Z。

如果你愿意,可以告诉我你是在排错、配置服务,还是学习 SELinux,我可以按场景详细讲。

0 踩