在 CentOS 里,“context” 通常和 SELinux 有关(文件/进程的安全上下文,security context)。下面分常见情况说明怎么“修改 context”。
ls -Z /path/to/file
ps -Z
chconchcon -t httpd_sys_content_t /var/www/html/index.html
常用参数:
-t:类型(type)-u:用户(如 system_u)-r:角色(如 object_r)示例:
chcon -u system_u -r object_r -t httpd_sys_content_t /data/web
⚠️
chcon是临时改,系统做restorecon或 relabel 会还原。
semanage fcontext -a -t httpd_sys_content_t "/data/web(/.*)?"
restorecon -Rv /data/web
查看已定义规则:
semanage fcontext -l | grep web
删除规则:
semanage fcontext -d "/data/web(/.*)?"
systemctl start httpd
进程的 context 由 service 的 SELinux domain 决定。
runcon -t httpd_t /usr/sbin/httpd
chcon -R -t public_content_t /shared
或永久:
semanage fcontext -a -t public_content_t "/shared(/.*)?"
restorecon -Rv /shared
查看拒绝日志:
ausearch -m avc -ts recent
或
journalctl -t setroubleshoot
生成修复建议:
sealert -a /var/log/audit/audit.log
查看状态:
getenforce
临时:
setenforce 0 # permissive
setenforce 1 # enforcing
永久(需重启):
vi /etc/selinux/config
SELINUX=enforcing
如果你说的 “context”不是 SELinux,而是:
context(如 bash / systemd 环境)可以直接告诉我具体场景,我给你更精确的命令。