在 CentOS 7 中,SELinux(Security-Enhanced Linux)默认是启用的,并且具有一些预设的上下文(context)
-
打开终端。
-
查看当前的 SELinux 上下文:
ls -lZ /
这将显示文件和目录的 SELinux 上下文。例如:
-rw-r--r--. root root system_u:object_r:user_home_t:s0 file.txt
在这个例子中,system_u:object_r:user_home_t:s0
是文件 file.txt
的 SELinux 上下文。
- 更改文件的 SELinux 上下文以继承其父目录的上下文。例如,如果父目录的上下文是
system_u:object_r:public_content_t:s0
,你可以使用semanage
和restorecon
命令来更改文件的上下文:
sudo yum install -y policycoreutils-python sudo semanage fcontext -a -t public_content_t "/path/to/parent_directory(/.*)?" sudo restorecon -Rv /path/to/file.txt
这将使 file.txt
的 SELinux 上下文变为 system_u:object_r:public_content_t:s0
,从而继承其父目录的上下文。
注意:在执行这些命令之前,请确保你了解它们的作用,并在生产环境中谨慎操作。在进行更改之前,建议备份重要数据。