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

Ita*_*Ale 6 linux redhat selinux centos rhel7

我正在设置一个 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
Run Code Online (Sandbox Code Playgroud)

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

为了解决这个问题,我/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
Run Code Online (Sandbox Code Playgroud)

但是,当使用 重新构建策略时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>
Run Code Online (Sandbox Code Playgroud)

将允许您递归且永久地向许多文件添加上下文。我不确定你是否已经尝试过这个。您能否提供一些您尝试设置的规则以及在哪些文件上设置的规则示例,以便我们了解哪些规则可以满足您的需求?