Linux下selinux简单梳理

 

在linux环境下执行某些程序时,偶尔会遇到来一个关于selinux的强制模式不可执行的情况,这种情况下需要关闭selinux或者将enforcing改为permissive模式后才能进行执行。selinux是Linux内核中提供的强制访问控制(MAC)系统,下面就对selinux的几种模式及其转换进行总结:

selinux的启动、关闭与查看
1)目前selinux支持三种模式,分别如下:
•enforcing:强制模式,代表selinux运作中,且已经正确的开始限制 domain/type 了;
•permissive:宽容模式:代表selinux运作中,不过仅会有警告讯息并不会实际限制 domain/type 的存取。这种模式可以运来作为 selinux 的 debug 之用;
•disabled:关闭,selinux 并没有实际运作。
 
2)查看selinux的模式
[root@localhost ~]# getenforce 
Enforcing       //显示出目前的模式为 Enforcing
 
3)查看 selinux的政策 (Policy)?
[root@localhost ~]# sestatus
selinux status: enabled      //是否启动 selinux
selinuxfs mount: /selinux     //selinux 的相关文件资料挂载点
Current mode: enforcing           //目前的模式
Mode from config file: enforcing     //设定档指定的模式
Policy version: 21
Policy from config file: targeted     //目前的政策为何?
 
4)通过配置文件调整selinux的参数
[root@localhost ~]# vim /etc/selinux/config
selinux=enforcing           //调整 enforcing|disabled|permissive
selinuxTYPE=targeted        //目前仅有 targeted 与 strict
 
5)selinux的启动与关闭
上面是预设的政策与启动的模式!需要注意的是,如果改变了政策则需要重新开机;如果由enforcing或permissive改成disabled,或由disabled改成其他两个,那也必须要重新开机。这是因为 selinux 是整合到核心里面去的, 你只可以在 selinux 运作下切换成为强制 (enforcing) 或宽容 (permissive) 模式,不能够直接关闭 selinux 的!
同时,由selinux关闭 (disable) 的状态到开启的状态也需要重新开机!
 
查看selinux状态:
a)/usr/sbin/sestatus -v    //如果selinux status参数为enabled即为开启状态
selinux status: enabled
b)getenforce       //也可以用这个命令检查
 
 
关闭selinux:
a)临时关闭(不用重启机器):
setenforce 0      //设置selinux 成为permissive宽容模式
setenforce 1      //设置selinux 成为enforcing强制模式
 
b)修改配置文件需要重启机器:
修改/etc/selinux/config 文件
将selinux=enforcing改为selinux=disabled
重启机器即可
 
如果你要启动selinux的话,请将上述的selinux=enforcing设定妥当,并且指定selinuxTYPE=targeted 这一个设定,并且到/boot/grub/menu.lst这个文件去,看看核心有无关闭selinux。
[root@localhost ~]# vi /boot/grub/menu.lst
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-92.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-92.el5 ro root=LABEL=/1 rhgb quiet selinux=0
initrd /initrd-2.6.18-92.el5.img
 
如果要启动selinux ,则不可以出现 selinux=0的字样在kernel后面!

=======================================================
温馨提示一个坑点:
一般而言,要关闭服务器的selinux功能,常用下面两种方法:
1)临时关闭
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce

2)永久关闭
[root@localhost ~]# cat /etc/sysconfig/selinux 
SELINUX=disabled
[root@localhost ~]# reboot

之前碰到一个坑,如上设置SELINUX为disabled,并reboot重启服务器后,selinux并没有关闭!

最后发现原因是:
/etc/selinux/config文件里面的SELINUX值没有改过来,所以重启不生效!
一般而言,服务器系统安装后,/etc/sysconfig/selinux会默认为/etc/selinux/config的软链接,所以只需修改/etc/sysconfig/selinux文件中的SELINUX为disabled即可!
因为系统启动后默认会以/etc/selinux/config为主。

[root@localhost ~]# ll /etc/sysconfig/selinux 
lrwxrwxrwx. 1 root root 17 Apr 27 02:41 /etc/sysconfig/selinux -> ../selinux/config

但之前碰到过的现象是: /etc/sysconfig/selinux 并不是/etc/selinux/config的软链接,所以这个细节要留意,如果不是软链接关系,就最好两个文件都修改下!
posted @ 2016-08-14 15:10  散尽浮华  阅读(2615)  评论(0编辑  收藏  举报