在 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 上下文。
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,從而繼承其父目錄的上下文。
注意:在執行這些命令之前,請確保你了解它們的作用,并在生產環境中謹慎操作。在進行更改之前,建議備份重要數據。