SElinux是一个linux安全增强功能,其允许用户和管理员对访问控制有更多的控制权。
访问控制可以做到哪个用户可能访问哪些资源的级别。这些资源可以来自文件。标准的Linux存取控制,如文件权限(-rwxr-xr-x)由用户或用户运行的程序修改。相反的,SELinux 的存取权限由系统加载时的policy确定,这个policy不能随意更改。
SELinux同样提供了细粒度的存取控制,不仅仅是对文件的读写执行权限,还可以指定指定谁可以unlink,仅仅append以及移动文件等操作。SELinux同样可以指定除了文件意外的访问权限,如网络资源和进程间通信等。
SELinux是linux上的MAC(mandatory access control)强制访问控制。
selinux 安全模块
永久设定:
[root@desktop4 ~]# vim /etc/sysconfig/selinux //修改配置文件
三种状态:
enforcing - SELinux security policy is enforced.
permissive - SELinux prints warnings instead of enforcing.
disabled - No SELinux policy is loaded.
SELINUX开启状态:
临时设定 (0/1):
[root@desktop4 ~]# setenforce 0
[root@desktop4 ~]# getenforce //查看
Permissive
实验(selinux开启的情况下):
在桌面创建一个文件hello.html,输入hello
[root@desktop4 html]# cd
[root@desktop4 ~]# cd ~/Desktop/
[root@desktop4 Desktop]# echo hello >hello.html
[root@desktop4 Desktop]# ls
hello.html
在/var/www/html下创建文件
[root@desktop4 Desktop]# cd /var/www/html/
[root@desktop4 html]# ls
ks.cfg rhel7
[root@desktop4 html]# echo "<h1>westos<h1>" > westos.html
[root@desktop4 html]# ls
ks.cfg rhel7 westos.html
将桌面的hello文件mv到/var/www/html/下
在selinux开启的情况下用浏览器打开两个文件hello和westos
用浏览器查看
hello.html报错
而westos.html可以看到内容!
原因:selinux对于文件有着严格的控制,每一个文件创建时会生成一个标签,也叫做安全上下文,mv不能改变这个文件的安全上下文。
下面我们来看一下这两个文件的安全上下文。
[root@desktop4 html]# ls -Z /var/www/html/
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 hello.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 ks.cfg
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 westos.html
可以看到上下文是不一样的
我们通过把hello.html的安全上下文改成和westos.html一样的就可以了
[root@desktop4 html]# chcon -t httpd_sys_content_t hello.html
[root@desktop4 html]# ls -Z /var/www/html/
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 hello.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 ks.cfg
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 westos.html
用浏览器打开hello.html就可以查看到拉!
restorecon 刷新后还原原来文件的上下文
永久修改文件的上下文的话需要一条其他的命令 semanage fcontext