在Linux上保留SELinux上下文的同时复制文件
SElinux现在已经成为任何体面的Linux系统的重要组成部分。在配置各种服务期间,文件SELinux上下文起着重要作用。有时,您需要使用预定义的SELinux上下文复制或备份文件以供以后使用,或者您尝试模仿当前配置。要在保留SELinux上下文的同时复制文件,请使用
例如,让我们显示文件的SELinux文件上下文
cp
带有--preserve=context
option选项的命令。例如,让我们显示文件的SELinux文件上下文
/etc/services
:[[email protected] ]# ls -Z /etc/services -rw-r--r--. root root system_u:object_r:etc_t:s0 /etc/services在复制过程中
cp
,默认情况下,一条命令将创建一个新的SELinux文件上下文:[[email protected] ]# cp /etc/services /tmp/ [[email protected] ]# ls -Z /tmp/services -rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/servicesusing
preserve=context
将指示cp
命令保留SELinux上下文:[[email protected] ]# cp --preserve=context /etc/services /tmp/ cp: overwrite ‘/tmp/services’? y [[email protected] ]# ls -Z /tmp/services -rw-r--r--. root root system_u:object_r:etc_t:s0 /tmp/services上面的SELinux保存上下文过程也适用于目录:
[[email protected] ]# ls -Zd /etc/ drwxr-xr-x. root root system_u:object_r:etc_t:s0 /etc/ [[email protected] ]# cp -r /etc/ /tmp/ [[email protected] ]# ls -Zd /tmp/etc/ drwxr-xr-x. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/etc/ [[email protected] ]# rm -fr /tmp/etc/ [[email protected] ]# cp -r --preserve=context /etc/ /tmp/ [[email protected] ]# ls -Zd /tmp/etc/ drwxr-xr-x. root root system_u:object_r:etc_t:s0 /tmp/etc/