文件的SELinux属性
1 .临时更改文件的类型属性
文件类型属性不正确是常见SELinux拒绝访问的主要原因
1 )更改文件的SELinux属性:
[ root @ localhost~] # touch test.file # #新文件
显示[ root @ localhost~] # ls-ztest.file # #文件的SELinux属性
- rw-r-- r--.rootrootunconfined _ u : object _ r : admin _ home _ t : s0 test.file
[ root @ localhost~] # chcon-t更改samba _ share _ ttest.file # #文件的默认SELinux属性
[ root @ localhost~] # ls-ztest.file
- rw-r-- r--.rootrootunconfined _ u : object _ r : samba _ share _ t : s0 test.file
[ root @ localhost~] # restorecon-f-vt est.file # #将更改的SELinux属性还原为默认属性
restorecon reset/root/test.filecontextunconfined _ u : object _ r : samba _ share _ t : s0-system _ u : obobobject
[ root @ localhost~] # ls-ztest.file
- rw-r-- r--.rootrootsystem _ 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.rootrootunconfined _ u : object _ r : default _ t : s0/web
[ root @ localhost~] # ls-zxd Yb/web/# #显示目录中文件的SELinux属性
- rw-r---r---rootrootunconfined _ u : object _ r : default _ t : s0 file 1
- rw-r---r---rootrootunconfined _ u : object _ r : default _ t : s0 file 2
[ root @ localhost~] # chcon-r-thttpd_sys_content_t/web/# #临时修改目录的SELinux属性为httpd _ sys _ content _ t
[root@localhost ~]# ls -dZ /web/
dwxr-xr-x.rootrootunconfined _ u : object _ r : httpd _ sys _ content _ t : s0/web /
[ root @ localhost~] # ls-zxd Yb/web /
- rw-r-- r--.rootrootunconfined _ u : object _ r : httpd _ sys _ content _ t 3360 s0 file 1
- rw-r-- r--.rootrootunconfined _ u : object _ r : httpd _ sys _ content _ t 3360 s0 file 2
[ root @ localhost~] # restorecon-r-v/web/# #恢复默认SELinux属性
restorecon reset/webcontextunconfined _ u : object _ r : httpd _ sys _ content _ t : s0-un confined _ u 3360 object
restorecon reset/web/file2contextunconfined _ u : object _ r : httpd _ sys _ content _ t : s0-_ nconfined _ r
restorecon reset/web/file1contextunconfined _ u : object _ r : httpd _ sys _ content _ t : s0-_ nconfined _ r
[ root @ localhost~] # ls-zxd Yb/web /
- rw-r---r---rootrootunconfined _ u : object _ r : default _ t : s0 file 1
-rw-r----r----rootrootunconf
ined_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 -zxdyb /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 -zxdyb /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 -zxdyb /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配置文件定义的模式不匹配
两种的类型的文件或目录,受限制的域的程序均不能访问