在 CentOS(以及 RHEL 系列)里,context 失效通常指 SELinux 安全上下文(security context)没有按预期生效,比如文件/进程 context 不对、策略不生效、重启后丢失等。下面按常见原因 + 对应现象 + 解决办法说明。
getenforce 显示 DisabledSELinux 根本没运行,context 只是“标签”,不发挥作用。
getenforce
sestatus
编辑:
vi /etc/selinux/config
确保:
SELINUX=enforcing
重启系统(Disabled → Enforcing 必须重启)。
chcon 成功,但 ls -Z 看不到 contextmount | grep nfs
df -T
context="system_u:object_r:httpd_sys_content_t:s0"
chcon 后生效restorecon 后失效SELinux 以 policy + semanage 数据库 为准,不是你手改的 context。
chcon -t httpd_sys_content_t /web
restorecon -Rv /web
# 又变回 default_t
semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
restorecon -Rv /web
✅ 这样永久生效。
新目录继承了父目录 context,不符合服务要求。
restorecon -Rv /var/www/html
或重新打标签:
fixfiles relabel
semodule -l 里缺模块semodule -l | grep httpd
semodule -e httpd
:Z 或 :zdocker run -v /data:/data:Z
或:
chcon -Rt svirt_sandbox_file_t /data
runcon 手动跑可以systemd 有独立 SELinux 配置。
systemctl edit httpd
加:
[Service]
SELinuxContext=system_u:system_r:httpd_t:s0
getenforce
sestatus
ls -Z /path
ps -Z | grep service
semanage fcontext -l | grep path
restorecon -Rv /path
如果你能告诉我:
avc: denied)我可以直接帮你定位是哪一条原因。