?
在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 //顯示出目前的模式為 Enforcing3)查看 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 與 strict5)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 ~]# getenforce2)永久關閉 [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的軟鏈接,所以這個細節要留意,如果不是軟鏈接關系,就最好兩個文件都修改下!