[英]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的這份出色的文檔,它以人類語言很好地解釋了這個主題:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.