CentOS7:复制文件并保留SELinux上下文

时间:2017-05-09 10:48:44

标签: linux phpmyadmin cp selinux

为了配置我的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上下文?

1 个答案:

答案 0 :(得分:0)

根据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

相关问题