繁体   English   中英

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

[英]CentOS7: copy file and preserve SELinux context

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM