1

我面临以下情况。出于 Web 开发目的,我已经设法使用 VirtualBox 设置了一个 CentOS 7 来宾 VM。我已经安装了 LAMP 堆栈并配置了 Apache(vhost,添加了组 vboxsf 的 apache 成员,添加了防火墙规则)以访问 VirtualBox 共享文件夹。

GUEST CentOS 7 VM Guest机器的配置设置:

Virtual machine hostname: dickwan.dev
Shared Folders:
    Name    |   Read-only   | Auto-mount
    ------------------------------------
    dickwan |   no          | yes
    ------------------------------------


Networking: NAT (with port forwarding rules)
Port Forwarding Rules:
    Name    |   Protocol    |   Host IP     |   Host Port   |   Guest IP    |   Guest Port
    --------------------------------------------------------------------------------------
    HTTP    |   TCP         |   . . .       |   8080        |   . . .       |   80
    --------------------------------------------------------------------------------------
    MariaDB |   TCP         |   . . .       |   9306        |   . . .       |   3306
    --------------------------------------------------------------------------------------
    SSH     |   TCP         |   . . .       |   2222        |   . . .       |   22

现在在我的主机中,我打开一个浏览器并导航到(让我们说):

http://dickwan.dev:8080/server-status

我收到消息:

Forbidden

You don't have permission to access /server-status on this server.

我已经将问题归结为 SELinux 安全上下文类型问题。当 SELinux 被禁用时,一切正常(嗯......很好,嗯嗯)。

但在我看来,仅仅关闭安全功能是一种不好的做法。我尝试更改共享文件夹的上下文,但无法执行操作

是否有机会在不停用 SELinux 的情况下通过 Apache 访问共享文件夹?

4

2 回答 2

2

由于 VBox 共享文件夹的安全上下文无法更改,您可以修改 SELinux 安全策略以允许 Apache 使用该上下文。这类似于在防火墙中打开一个端口以将某个端口公开给应用程序。

首先,确保您的 apache 用户是拥有共享文件夹的组的一部分,如果不是,您可以使用如下所示的命令添加它(用户/组名称在您的系统上可以不同):

usermod -aG vboxsf apache

然后,您可以使用audit2allow生成新的安全策略来解决您的问题。这是一个很好的教程

如果您很懒惰并且只想允许 Apache 对您的 VBox 共享文件夹进行读取访问,您可以调整以下my_httpd_t.te策略文件并使用包含的命令将其应用到您的系统上。

module my_httpd_t 1.0;

require {
        type httpd_t;
        type vmblock_t;
        class dir read;
        class file { read getattr open };
}

#============= httpd_t ==============
allow httpd_t vmblock_t:dir read;
allow httpd_t vmblock_t:file { getattr open read };

# Generated by audit2allow

# To apply this policy:
## checkmodule -M -m -o my_httpd_t.mod my_httpd_t.te
## semodule_package -o my_httpd_t.pp -m my_httpd_t.mod
## semodule -i my_httpd_t.pp
## systemctl restart httpd
于 2017-07-26T23:39:44.467 回答
1

我有一个类似的问题(除了 Fedora 20 作为主机和来宾操作系统)。我做了什么:

sudo mount -t vboxsf shared_folder /media/shared_folder

sudo ln -s /media/shared_folder/ /var/www/

sudo chcon -R --reference=/var/www /var/www/shared_folder

这对我有用:)

在我尝试将安全上下文设置为自动挂载的共享文件夹(通过 VirtualBox)但没有成功之前,我手动挂载它

于 2014-12-05T13:16:09.923 回答