首页 > 解决方案 > Vagrant:设置同步文件夹的上下文以防止 SELinux 抱怨

问题描述

Vagrant 2.1.1
主机操作系统:Windows 7 Pro
客户操作系统:Centos 7.4

使用默认的 Vagrant 同步文件夹或 nfs vagrant 同步文件夹,我不断收到 SELinux 错误,告诉我需要将上下文更改为httpd_sys_content_t. 默认同步文件夹中的所有文件都设置为default_t上下文,所有使用 nfs 同步的文件都设置为nft_t.

文件始终是 default_t 上下文
config.vm.synced_folder '.', '/vagrant'
文件始终是 nfs_t 上下文
config.vm.synced_folder ".", "/vagrant", type: "nfs"

我尝试使用fcontextand以正常方式更改文件上下文restorecon,但restorecon只是默默地失败了。

如何设置同步文件/文件夹的上下文以避免 SELinux 错误?

我试过使用:mount_options,但它们被忽略了。
例子:
config.vm.synced_folder "./www", "/var/www", type: "nfs", create: true, id: "sites", mount_options: ['vers=3', :udp, :nolock, :noatime, 'context=system_u:object_r:httpd_sys_content_t:s0']

也许我以错误的方式在挂载选项中添加上下文?

标签: linuxvagrantselinuxvagrant-windows

解决方案


您必须将 rsync 用于 vagrant 同步文件夹,因为不支持通过 sync_folders 设置 SELContext,因为它是 vboxfs 的限制。所以在你的 vagrantfile 中做这样的事情:

config.vm.synced_folder "guest/sync/location", "/host/sync/location", :owner => "root", :group => "root", type: "rsync", rsync__exclude: [".git", ".DS_Store"], rsync__args:["-avz", "--rsync-path='sudo rsync'"]

此解决方法假定您已根据本文使用 semanage 和 restorecon 持久且递归地设置httpd_sys_content_t

已编辑:11/3/2018 遗憾的是,上述解决方案实际上并未同步文件,因为 rsync 并不能真正与寡妇主机一起使用。以下是我对解决方法的发现和结论:

https://github.com/hashicorp/vagrant/issues/9827#issuecomment-435622888


推荐阅读