【问题标题】:CentOS7: copy file and preserve SELinux contextCentOS7:复制文件并保留 SELinux 上下文
【发布时间】:2017-10-07 15:27:20
【问题描述】:

为了配置我的 CentOS 系统,我想覆盖一个任意文件,该文件几乎保留了除时间戳和内容之外的所有属性。

例如,我将使用/etc/phpMyAdmin/config.inc.php

注意:被复制的文件在其他文件系统中

[root@localhost phpMyAdmin]# ls -laZ /etc/phpMyAdmin/
drwxr-x---. root apache system_u:object_r:etc_t:s0       .
drwxr-xr-x. root root   system_u:object_r:etc_t:s0       ..
-rw-r-----. root apache system_u:object_r:etc_t:s0       config.inc.php
[root@localhost phpMyAdmin]# /bin/cp -frv --backup=numbered --preserve=mode,ownership,context,xattr config.inc.php /etc/phpMyAdmin/config.inc.php 
«config.inc.php» -> «/etc/phpMyAdmin/config.inc.php» (respaldo: «/etc/phpMyAdmin/config.inc.php.~1~»)
[root@localhost phpMyAdmin]# ls -laZ /etc/phpMyAdmin/
drwxr-x---. root apache system_u:object_r:etc_t:s0       .
drwxr-xr-x. root root   system_u:object_r:etc_t:s0       ..
-rw-r-----. root apache system_u:object_r:unlabeled_t:s0 config.inc.php
-rw-r-----. root apache system_u:object_r:etc_t:s0       config.inc.php.~1~
[root@localhost phpMyAdmin]# systemctl restart httpd

当我尝试http://localhost/phpmyadmin 时,我第一次收到 SELinux 警告,而 Apache 无法访问该文件。

[root@localhost phpMyAdmin]# chcon --reference /etc/phpMyAdmin/config.inc.php.~1~ /etc/phpMyAdmin/config.inc.php
[root@localhost phpMyAdmin]# ls -laZ /etc/phpMyAdmin/
drwxr-x---. root apache system_u:object_r:etc_t:s0       .
drwxr-xr-x. root root   system_u:object_r:etc_t:s0       ..
-rw-r-----. root apache system_u:object_r:etc_t:s0       config.inc.php
-rw-r-----. root apache system_u:object_r:etc_t:s0       config.inc.php.~1~
[root@localhost phpMyAdmin]# systemctl restart httpd

现在 apache 可以读取文件了。

如何让 cp 保留原始文件的 SELinux 上下文?

【问题讨论】:

    标签: linux phpmyadmin cp selinux


    【解决方案1】:

    根据 cp 文档,开关“--preserve=context”允许在此过程中复制 Selinux 上下文。

    请看看这个来自 redhat 的优秀文档,它用人类语言精彩地解释了这个主题:

    https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security-Enhanced_Linux/sect-Security-Enhanced_Linux-Working_with_SELinux-Maintaining_SELinux_Labels_.html

    【讨论】:

    • 我尝试了很多cp选项(链接中指出的那些),--preserve=context和--no-preserve=context,但是结果总是一样,apache不能用文件和 unlabeled_t 属性已设置。只有在复制apache之后使用chcon才能使用该文件。也许是 cp 中的错误?
    • 嗨 Carlos,目标文件是在不同的文件系统上吗?哪种文件系统类型?你在哪里安装卷?
    • 源文件系统是 ext4,挂载在 /mnt/shared 上。目标(根)文件系统是 ext3。
    • 源文件系统是 ext4,挂载在 /mnt/shared,根(目标)文件系统是 ext3。
    • 在复制文件之前,我开发了一个脚本来设置从目标到源的上下文。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多