在 CentOS 里提到 “context 无效”,通常是指 SELinux 的安全上下文(security context)没有生效 / 不生效,常见原因和排查方法如下:
getenforce
Enforcing ✅ 正常Permissive ⚠️ 只记录不拦截Disabled ❌ context 完全无效如果是 Disabled:
# 修改
vi /etc/selinux/config
SELINUX=enforcing
然后 重启系统
你修改了文件 context,但系统未刷新:
# 查看当前 context
ls -Z /path/file
# 恢复默认 context
restorecon -Rv /path
# 或临时设置
chcon -t httpd_sys_content_t /path/file
⚠️ 注意:
chcon 是临时,重启或 restorecon 会覆盖正确做法:
semanage fcontext -a -t httpd_sys_content_t "/data(/.*)?"
restorecon -Rv /data
常见错误:
semanage 没 restorecon例如:
检查:
ps -Z
如:
vfat / ntfs / nfs(默认无 context)context=mount | grep /data
解决:
mount -o context=system_u:object_r:httpd_sys_content_t:s0 /dev/sdb1 /data
getenforce
ls -Z 文件
ps -Z 进程
semanage fcontext -l | grep 路径
restorecon -Rv 路径
你可以补充:
我可以直接给你对应可用的命令。