在 **CentOS 中“Context 恢复”**通常指的是 SELinux 安全上下文(Security Context)被改乱了,需要恢复成默认值。下面按常见场景给你完整说明。
ls -Z /path/to/file
示例:
ls -Z /var/www/html/index.html
输出类似:
unconfined_u:object_r:httpd_sys_content_t:s0 index.html
ps -eZ | grep httpd
restorecon(推荐)restorecon -v /path/to/file
restorecon -Rv /path/to/dir
restorecon -Rv /var/www/html
✅ 不会破坏数据,只修复 SELinux Context
restorecon -RFv /path/to/dir
semanage fcontext -l | grep /var/www
semanage fcontext -a -t httpd_sys_content_t "/data/www(/.*)?"
然后应用:
restorecon -Rv /data/www
touch /.autorelabel
reboot
系统重启后会自动重新标记所有文件。
⚠️ 注意
getenforce
setenforce 0
编辑:
vi /etc/selinux/config
SELINUX=disabled
然后重启。
restorecon -Rv /var/www
restorecon -Rv /home
restorecon -Rv /var/lib/mysql
setenforce 0
如果服务立刻恢复正常 → 90% 是 SELinux Context 问题
| 需求 | 命令 |
|---|---|
| 恢复文件 Context | restorecon -v file |
| 恢复目录 | restorecon -Rv dir |
| 添加规则 | semanage fcontext -a |
| 全系统修复 | touch /.autorelabel && reboot |
如果你能贴出:
ls -Z 输出我可以给你 精确到命令级别 的修复方案 ✅