原文链接:http://blog.51cto.com/nightmoon/1383651
问题描述:
近日,在测试优化脚本的时候遇到一个问题。脚本命令如下
# close selinux
setenforce 0 &&
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g'/etc/sysconfig/selinux
当设置完毕后,查看/etc/sysconfig/selinux的确是关闭了,但是当我准备临时开启selinux时问题出现:
[slothbear@bear~]$ setenforce 1
setenforce:SELinux is disabled
临时开启失败了。经查询,网络大神说当selinux关闭时,临时开启是提示关闭的。因此我又修改/etc/sysconfig/selinux,重启系统后,发现selinux还是关闭状态的。查看日志提示
系统在运行的时候就将selinux关闭了。但是配置文件中明明是开启啊。
[slothbear@bear ~]$ cat /etc/selinux/config
# This file controls the state of SELinux on thesystem.
# SELINUX= can take one of these three values:
#enforcing- SELinux security policy is enforced.
#permissive- SELinux prints warnings instead of enforcing.
#disabled - No SELinux policy is loaded.
SELINUX=enforcing 此处设置开启
# SELINUXTYPE= can take one of these two values:
#targeted -Targeted processes are protected,
#mls -Multi Level Security protection.
SELINUXTYPE=targeted
绞尽脑汁,从开始设置脚本查起,在man sed命令后才发下如下提示
sed –i会破坏原有文件的的软链接或硬链接。
搞到这里才彻底明白,原来我在用脚本修改/etc/sysconfig/selinux文件后。它已经不在是/etc/selinux/config的链接文件,从而变成了一个普通文件,因此无论你在如何修改/etc/sysconfig/selinux和重启系统,它都不会生效。
解决办法:
[root@bear ~]# rm -rf /etc/sysconfig/selinux删除原软链接文件
[root@bear sysconfig]# ln -s /etc/selinux/config/etc/sysconfig/selinux重新创建软链接文件
[root@bear sysconfig]# ls -li |grep selinux 查看软链接文件是否生效
74 lrwxrwxrwx1 root root19 Mar 23 02:48 selinux -> /etc/selinux/config
[root@bear sysconfig]# cat /etc/sysconfig/selinux查看软链接文件内容是否和源文件一样
# This filecontrols the state of SELinux on the system.
# SELINUX= can takeone of these three values:
#enforcing - SELinux security policyis enforced.
#permissive - SELinux prints warningsinstead of enforcing.
#disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= cantake one of these two values:
#targeted - Targeted processes areprotected,
#mls - Multi Level Security protection.
SELINUXTYPE=targeted
然后修改SELINUX= enforcing,重启系统后插叙selinux状态
[slothbear@bear ~]$getenforce
Enforcing
selinux起来了,说明配置文件生效了。
重启时会出现如下提示:
***Warning -- SELinux targeted policy relabel is required.
***Relabeling could take a very long time, depending on file
***system size and speed of hard drives.
警告的意思是说,selinux的targeted策略要求重新打标签,应该是对系统所有的文件打上一个selinux标签,速度的快慢由系统文件的多少和硬盘的速度决定。