文件的复制、移动、压缩等对SELinux属性关系详解

1.临时修改文件的类型属性

文件的类型属性不正确是常见的SELinux拒绝访问的主要原因

1)修改文件的SELinux属性:

[root@localhost ~]# touch test.file   ##新建文件

[root@localhost ~]# ls -Z test.file   ##查看文件的SELinux属性

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 test.file

[root@localhost ~]# chcon -t samba_share_t test.file  ##修改文件的默认SELinux属性

[root@localhost ~]# ls -Z test.file

-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 test.file

[root@localhost ~]# restorecon -F -v test.file   ##恢复修改过的SELinux属性为默认属性

restorecon reset /root/test.file context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:admin_home_t:s0

[root@localhost ~]# ls -Z test.file

-rw-r--r--. root root system_u:object_r:admin_home_t:s0 test.file

2)修改目录的SELinux属性(所有的操作与文件对比多了一个“-R”代表递归):

[root@localhost ~]# mkdir /web

[root@localhost ~]# touch /web/file{1,2}

[root@localhost ~]# ls -dZ /web  ##查看目录的SELinux属性

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /web

[root@localhost ~]# ls -lZ /web/   ##查看目录下文件的SELinux属性

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file2

[root@localhost ~]# chcon -R -t httpd_sys_content_t /web/    ##临时修改目录的SELinux属性为httpd_sys_content_t

[root@localhost ~]# ls -dZ /web/

drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/

[root@localhost ~]# ls -lZ /web/

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file2

[root@localhost ~]# restorecon -R -v /web/   ##恢复为默认SELinux属性

restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:default_t:s0

restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:default_t:s0

restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:default_t:s0

[root@localhost ~]# ls -lZ /web/

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file2

[root@localhost ~]# ls -dZ /web/

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /web/

2.永久修改文件的类型属性

永久修改文件及目录的类型属性使用下列命令:

semanage fcontext -{a|d|l|m} [-frst] filespec  ##-a增加、-d删除、-l显示、-m修改

restorecon -v filespec    ##由于“semanage fcontext”命令只是将属性定义项加载到

“/etc/selinux/targeted/contexts/files/file_contexts.local”文件中,

使用该命令才是将文件的selinux属性永久地修改.

1)文件的selinux属性修改

[root@localhost tmp]# touch test.file

[root@localhost tmp]# ls -Z test.file

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.file

[root@localhost tmp]# yum install policycoreutils-python  ##安装semanage管理工具提供软件

[root@localhost tmp]# semanage fcontext -a -t samba_share_t /tmp/test.file  ##将测试文件的selinux属性设置为“samba_share_t”

[root@localhost tmp]# ls -Z /tmp/test.file   ##测试文件的selinux属性未发生变化

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/test.file

[root@localhost tmp]# restorecon -v /tmp/test.file    ##使semanage设置的selinux属性永久的生效

restorecon reset /tmp/test.file context unconfined_u:object_r:user_tmp_t:s0->unconfined_u:object_r:samba_share_t:s0

[root@localhost tmp]# ls -Z /tmp/test.file   ##测试文件的selinux属性已改

-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 /tmp/test.file

2)目录的selinux属性修改:(完成后目录下新建的文件自动继承selinux属性)

[root@localhost ~]# mkdir /html

[root@localhost ~]# touch /html/file{1,2}

[root@localhost ~]# ls -dZ /html

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /html

[root@localhost ~]# ls -lZ /html/

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file2

[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t "/html(/.*)?"  ##正则表达式“/html(/.*)?”表示/html目录及其中

的任何文件或子目录

[root@localhost ~]# restorecon -R -v /html/  ##使semanage设置的selinux属性永久的生效

restorecon reset /html context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

restorecon reset /html/file2 context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

restorecon reset /html/file1 context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

[root@localhost ~]# ls -lZ /html/   ##验证修改结果

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file2

[root@localhost ~]# ls -dZ /html/

drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /html/

3)如果要恢复/html的文件属性可使用下列命令:

[root@localhost ~]# semanage fcontext -d "/html(/.*)?"   ##删除自定义的selinux属性

[root@localhost ~]# restorecon -F -R -v /html/  ##永久生效

restorecon reset /html context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0

restorecon reset /html/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0

restorecon reset /html/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0

[root@localhost ~]# ls -lZ /html  ##验证文件的selinux属性已经从“httpd_sys_content_t”改为默认的"default_t"

-rw-r--r--. root root system_u:object_r:default_t:s0   file1

-rw-r--r--. root root system_u:object_r:default_t:s0   file2

[root@localhost ~]# ls -dZ /html

drwxr-xr-x. root root system_u:object_r:default_t:s0   /html

4)file_t与default_t

file_t:文件没有selinux属性

default_t:文件或目录的selinux属性与file-context配置文件定义的模式不匹配

两种的类型的文件或目录,受限制的域的程序均不能访问

3.移动文件对selinux属性的影响

在SELinux环境中,文件和目录移动之后保持原有SELinux属性不变

准备测试文件:

[root@localhost ~]# touch test.file

[root@localhost ~]# ls -Z test.file

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 test.file

[root@localhost ~]# ls -dZ /var/www/html/

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

移动测试:

[root@localhost ~]# mv test.file /var/www/html/

[root@localhost ~]# ls -Z /var/www/html/test.file   ##移动后selinux属性依然是“admin_home_t”,并未继承“httpd_sys_content_t”

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /var/www/html/test.file

注意:

移动和复制对比中,复制更有利于保持文件的selinux属性,以便能正常访问。

4.检测文件的默认属性

利用matchpathcon,可以验证目录下的文件selinux属性标记是否正确。

准备测试文件:

[root@localhost ~]# touch /var/www/html/file{1,2,3}  ##创建三个文件

[root@localhost ~]# ls -Z /var/www/html/file*

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file1

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file2

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file3

修改测试:

[root@localhost ~]# chcon -t samba_share_t /var/www/html/file1  ##临时修改selinux属性

[root@localhost ~]# ls -Z /var/www/html/file1

-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 /var/www/html/file1

[root@localhost ~]# matchpathcon -V /var/www/html/file?   ##检测selinux属性的正确性,提示file1为“samba_share_t”应

为“httpd_sys_content_t”

/var/www/html/file1 has context unconfined_u:object_r:samba_share_t:s0, should be system_u:object_r:httpd_sys_content_t:s0

/var/www/html/file2 verified.

/var/www/html/file3 verified.

[root@localhost ~]# touch test.file

[root@localhost ~]# ls -Z

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 test.file

[root@localhost ~]# mv test.file /var/www/html/  ##移动新文件测试

mv:是否覆盖"/var/www/html/test.file"? y

[root@localhost ~]# ls /var/www/html/test.file  -Z

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /var/www/html/test.file

[root@localhost ~]# restorecon -v /var/www/html/*   ##修复selinux属性

restorecon reset /var/www/html/file1 context unconfined_u:object_r:samba_share_t:s0-

>unconfined_u:object_r:httpd_sys_content_t:s0

restorecon reset /var/www/html/test.file context unconfined_u:object_r:admin_home_t:s0-

>unconfined_u:object_r:httpd_sys_content_t:s0

[root@localhost ~]# matchpathcon -V /var/www/html/*   ##提示verified表示校验成功

/var/www/html/file1 verified.

/var/www/html/file2 verified.

/var/www/html/file3 verified.

/var/www/html/test.file verified.

5.tar文件与selinux属性标记

tar命令不会保存属于扩展属性的selinux属性,使用“--selinux”或“--xattrs”可保存selinux属性信息。

准备对比步骤:

[root@localhost ~]# touch file{1..3}  ##创建文件

[root@localhost ~]# ls -Z

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file2

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file3

[root@localhost ~]# tar zcf test.tar.gz ./file*   ##不加“--selinux”选项

[root@localhost ~]# cp test.tar.gz /tmp/  ##拷贝文件

[root@localhost ~]# cd /tmp/   ##进入/tmp目录

[root@localhost tmp]# tar zxvf test.tar.gz   ##解压

./file1

./file2

./file3

[root@localhost tmp]# ls -Z   ##验证,selinux属性丢失

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file2

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file3

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.tar.gz

保留selinux属性:

[root@localhost tmp]# cd

[root@localhost ~]# touch file{4..5}

[root@localhost ~]# ls -Z file{4..5}

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file4

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file5

[root@localhost ~]# tar zcf test.se.tar.gz ./file{4..5} --selinux   ##压缩是保留selinux属性标记

[root@localhost ~]# cp test.se.tar.gz /tmp/

[root@localhost ~]# cd /tmp/

[root@localhost tmp]# tar zxvf test.se.tar.gz

./file4

./file5

[root@localhost tmp]# ls -Z  ##验证file1、2、3丢失了原有的selinux属性,file4、5保留了“admin_home_t”的selinux属性;请思考:

test开头的两压缩文件为什么是这样的selinux属性?

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file2

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file3

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file4

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file5

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.se.tar.gz

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.tar.gz

linux 移动压缩包 命令,文件的复制、移动、压缩等对SELinux属性关系详解相关推荐

  1. 文件的复制、移动、压缩等对SELinux属性关系详解

    文件的复制.移动.压缩等对SELinux属性关系详解 1.临时修改文件的类型属性 文件的类型属性不正确是常见的SELinux拒绝访问的主要原因 1)修改文件的SELinux属性: [root@loca ...

  2. 嵌入式linux防复制,嵌入式linux /dev 下的文件无法复制

    嵌入式linux /dev 下的文件无法复制 关注:234  答案:4  mip版 解决时间 2021-02-06 06:27 提问者一段小時光 2021-02-06 00:38 root下也有文件不 ...

  3. linux支行压缩的文件系统,Linux第八章:文件,文件系统的压缩,打包备份

    压缩:gzip  -v  文件名 1:压缩后成  文件名.gz 的压缩文件,原文件消失 2:压缩的文件可以直接使用zcat  文件名.gz 读取里面的内容 解压缩: gunzip   文件名.gz 替 ...

  4. Linux下zip格式文件的解压缩与压缩操作命令详解

    < Linux下zip格式文件的解压缩与压缩操作命令详解 > * 声明:网上找来比较凌乱,整理了一下,方便自己查看 zip格式文件的解压缩与压缩操作: 1.把/home目录下面的huaza ...

  5. Unity 分享 功能 用Unity Native Share Plugin 实现链接、图片、视频等文件的分享+ 安卓 Ios 都可以,代码图文详解

    Unity 分享 功能 用Unity Native Share Plugin 实现链接.图片.视频等文件的分享+ 安卓 Ios 都可以,代码图文详解 前言 环境 效果 一.Unity Native S ...

  6. Linux和GNU系统的关系详解

    今天广泛使用的 GNU 版本通常被称为"Linux",而它的许多用户并不知道 它基本上是由GNU 项目开发的 GNU 系统 . Linux内核 和 GNU 系统简介 确实有一个 L ...

  7. linux 移动压缩包 命令,Linux命令,复制,移动,删除,创建,解压缩,修改权限

    cp命令 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中,同MSDOS下的copy命令一样,功能十分强大. 语法: cp [选项] 源文件或目录目标文件或目录 说明:该命令把指定的源文件复制到 ...

  8. Linux的压缩包命令

    针对tar命令 Linux中常用的压缩包,大多是.tar,.tar.gz,tgz的 解压压缩包 针对压缩包后缀的情况,采用不同的参数,否则可能会损失文件 tar [-zxvf] 压缩包名称 [-C 路 ...

  9. linux 移动压缩包 命令,linux常用命令

    1.linux 目录结构 借用一张图,Linux 中,一切皆文件,所以,文件的根目录为/ centos 系统cd / 到根目录,ls ,查看所有文件如下: bin boot dev etc home ...

  10. Linux下解压缩包命令

    各种压缩文件的解包与打包命令 .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName --------------- .tar .xz ...

最新文章

  1. 中波磁棒天线在接收150kHz导航信号方向性
  2. 程序员2004下载地址
  3. 查找文件命令find总结以及查找大文件
  4. asp判断是否移动端_asp判断用户端是电脑访问还是移动设备方法
  5. 二进制包 mysql_二进制包安装MySQL数据库
  6. Kinect+OpenNI学习笔记之2(获取kinect的颜色图像和深度图像)
  7. Android 如何全局获取Context
  8. Python从头开始的演变策略
  9. 实况足球2020修改服务器,实况足球2020指令一览,最近的指令更改你知道吗
  10. 详解C/C++预处理器
  11. OV5640_MIPI_1LEN调试记录
  12. perfectmoney php接口_Perfect Money完美货币注册和用支付宝提现方法
  13. 计算机音乐数字大全抖音,抖音歌曲大全100首,抖音最火的100首音乐
  14. filebox管理php,filebox.php
  15. 关于Eureka的自我保护模式
  16. 升级Win10注意事项个人总结
  17. 关于Gary Marcus与Yann LeCun讨论AI现状及发展
  18. 三维计算机动画的特征是真实性,3D动画电影的应用特点及制作管理内容
  19. 运动蓝牙耳机什么牌子好?性价比高的无线蓝牙耳机
  20. 神经网络训练常见坑-新手如何优化调整训练神经网络

热门文章

  1. opencv3计算机视觉+Python(四)
  2. java统计中英文字数 Java问题通用解决代码
  3. 备份数据 宝塔linux_华为云服务器安装宝塔Linux面板及宝塔面板数据库备份导入体验...
  4. 无法显示jinglingzhoushou-2.0.4.AppImage
  5. 计算机存储地址如何,计算机内存地址只有5种表现形式吗,为什么?
  6. python连接MongoDB,以及常用操作
  7. pytorch加载自己的数据集图片格式
  8. Centos7下ping通ip但是ping不通域名+firefox无法打开网页
  9. 总结 | 四篇图网络综述文章提出的Future Directions
  10. Caffe学习系列(16):caffemodel可视化