设为首页 收藏本站
查看: 280|回复: 0

[经验分享] 文件的SELinux属性

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-9-30 08:53:48 | 显示全部楼层 |阅读模式
文件的SELinux属性
1.临时修改文件的类型属性
文件的类型属性不正确是常见的SELinux拒绝访问的主要原因

1)修改文件的SELinux属性:
[iyunv@localhost ~]# touch test.file   ##新建文件
[iyunv@localhost ~]# ls -Z test.file   ##查看文件的SELinux属性
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 test.file
[iyunv@localhost ~]# chcon -t samba_share_t test.file  ##修改文件的默认SELinux属性
[iyunv@localhost ~]# ls -Z test.file   
-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 test.file
[iyunv@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
[iyunv@localhost ~]# ls -Z test.file
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 test.file

2)修改目录的SELinux属性(所有的操作与文件对比多了一个“-R”代表递归):
[iyunv@localhost ~]# mkdir /web
[iyunv@localhost ~]# touch /web/file{1,2}
[iyunv@localhost ~]# ls -dZ /web  ##查看目录的SELinux属性
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /web
[iyunv@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
[iyunv@localhost ~]# chcon -R -t httpd_sys_content_t /web/    ##临时修改目录的SELinux属性为httpd_sys_content_t
[iyunv@localhost ~]# ls -dZ /web/
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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属性修改
[iyunv@localhost tmp]# touch test.file
[iyunv@localhost tmp]# ls -Z test.file
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.file
[iyunv@localhost tmp]# yum install policycoreutils-python  ##安装semanage管理工具提供软件
[iyunv@localhost tmp]# semanage fcontext -a -t samba_share_t /tmp/test.file  ##将测试文件的selinux属性设置为“samba_share_t”
[iyunv@localhost tmp]# ls -Z /tmp/test.file   ##测试文件的selinux属性未发生变化
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/test.file
[iyunv@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
[iyunv@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属性)
[iyunv@localhost ~]# mkdir /html
[iyunv@localhost ~]# touch /html/file{1,2}
[iyunv@localhost ~]# ls -dZ /html
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /html
[iyunv@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
[iyunv@localhost ~]# semanage fcontext -a -t httpd_sys_content_t "/html(/.*)?"  ##正则表达式“/html(/.*)?”表示/html目录及其中的任何文件或子目录
[iyunv@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
[iyunv@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
[iyunv@localhost ~]# ls -dZ /html/
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /html/

3)如果要恢复/html的文件属性可使用下列命令:
[iyunv@localhost ~]# semanage fcontext -d "/html(/.*)?"   ##删除自定义的selinux属性
[iyunv@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
[iyunv@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
[iyunv@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配置文件定义的模式不匹配
两种的类型的文件或目录,受限制的域的程序均不能访问


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-25567-1-1.html 上篇帖子: 配置SELinux 下篇帖子: Linux文件系统只读Read-only file system
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表