SELinux:手动更改 /etc/selinux/targeted/contexts/files/ 中的文件

SELinux:手动更改 /etc/selinux/targeted/contexts/files/ 中的文件

我正在设置一个 CentOS 7 服务器,其中/home目录必须位于另一个分区上,然后使用绑定挂载进行挂载。因此:/data/homes应该绑定挂载到/home

问题在于确保正确应用 SELinux 上下文。事实上,以下命令会产生相互矛盾的结果:

# Applies the rules for /home to all the files
restorecon -R -v /home
# Applies the generic rules (standard files) to all the files
restorecon -R -v /data/homes

如果系统必须重新标记文件,则会导致问题。

为了解决这个问题,我修改了策略文件,/etc/selinux/targeted/contexts/files/file_contexts.homedirs复制了以下所有规则/data/homes

$ sed -n '/^\/home/p' /etc/selinux/targeted/contexts/files/file_contexts.homedirs \
  | sed 's/^\/home/\/data\/homes/' \
  >> /etc/selinux/targeted/contexts/files/file_contexts.homedirs

然而,当用 重新构建策略时semodule -B,我的更改就丢失了。

我知道修改这些文件的推荐方法是使用semanage fcontext,但总共有近 200 条规则需要添加,而运行semanage for each不是一种选择。

我如何手动更改文件/etc/selinux/targeted/contexts/files/file_contexts并确保保留更改?

答案1

semanage fcontext -a -t <file_context> "<path>/<file>(/.*)?"
restorecon -R <path>/<file>

将允许您以递归和永久的方式向许多文件添加上下文。我不确定您是否尝试过此操作。您能否提供一些您尝试设置的规则示例以及针对哪些文件,以便我们了解哪些规则可满足您的需求?

相关内容