2017-05-09 150 views
0

为了配置我的CentOS系统,我想覆盖一个任意的文件,保留几乎所有的属性,但时间戳和内容。CentOS7:复制文件并保留SELinux上下文

举个例子,我将使用/etc/phpMyAdmin/config.inc.php

注:正在复制文件是其它文件系统

[[email protected] 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 
[[email protected] 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~») 
[[email protected] 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~ 
[[email protected] phpMyAdmin]# systemctl restart httpd 

当我试图http://localhost/phpmyadmin,拳头时间我得到一个SELinux的警告,Apache可以” t访问文件。

[[email protected] phpMyAdmin]# chcon --reference /etc/phpMyAdmin/config.inc.php.~1~ /etc/phpMyAdmin/config.inc.php 
[[email protected] 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~ 
[[email protected] phpMyAdmin]# systemctl restart httpd 

现在apache可以读取文件。

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

回答

0

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

请看看来自红帽这个优秀的文档,它解释了奇妙的在人类语言的话题:

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

+0

我已经尝试了许多CP的选项(在链接的指示), - preserve = context和--no-preserve = context,但结果总是相同的,apache不能使用该文件并设置** unlabeled_t **属性。复制apache后才能使用chcon才能使用该文件。也许是cp中的一个bug? –

+0

嗨Carlos,是不同文件系统上的目标文件?哪种文件系统类型?你在哪里安装音量? –

+0

源文件系统是ext4,挂载在/ mnt/shared上。目标(根)文件系统是ext3。 –

相关问题