您的位置:首页 > 运维架构 > Linux

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

2014-09-30 09:46 337 查看
文件的复制、移动、压缩等对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

本文出自 “沈工鬼斧” 博客,请务必保留此出处http://stlong.blog.51cto.com/5144113/1559628
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: