Linux下selinux简单梳理

 

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

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的软连接,因此这个细节要留意,若是不是软连接关系,就最好两个文件都修改下!
相关文章
相关标签/搜索