啟用SELinux時遇到的問題
一臺CentOS5.7的機(jī)器,原來只是在公司內(nèi)部訪問,SELinux是禁用的狀態(tài),現(xiàn)需要搭建成FTP服務(wù)器并發(fā)布到外網(wǎng)上去,為了安全,啟用SELinux。編輯/etc/selinux/config文件,將SELINUX=disabled改為SELINUX=permissive,保存后重啟系統(tǒng)。(SELinux從禁用狀態(tài)轉(zhuǎn)變?yōu)閱⒂脮r,需要重啟系統(tǒng)以重新標(biāo)記文件的上下文,紅帽建議先將SELinux的值設(shè)置為permissive,重新標(biāo)記完成后再設(shè)置為enforcing。)
按道理說,重新對整個文件系統(tǒng)進(jìn)行標(biāo)記應(yīng)該是比較費時間的,至少需要幾分鐘,但在這臺機(jī)器上敲入reboot命令后,發(fā)現(xiàn)它很快就啟動起來的。登錄后使用命令getenforce,返回permissive,說明SELinux的狀態(tài)正常,于是把SELinux的狀態(tài)設(shè)置為Enforcing,設(shè)置完成后重啟了一下vsftpd和httpd服務(wù),這下問題大了,服務(wù)無法正常啟動,提示“errorwhile loading shared libraries: libssl.so.6: failed to map segment from sharedobject: Permission denied”,直覺告訴我是因為重啟時沒有對整個文件系統(tǒng)進(jìn)行正確的relabeling引起的,于是執(zhí)行命令touch /.autorelabel ,在/下創(chuàng)建一個.autorelabel文件,有這個文件存在,系統(tǒng)在重啟時就會對整個文件系統(tǒng)進(jìn)行relabeling,但奇怪的是重啟又很快完成,看來還是沒有完成relabeling。
***google了一下,發(fā)現(xiàn)一個解決方案,執(zhí)行下面三條命令:
fixfiles -f relabel
touch /.autorelabel
reboot
但在執(zhí)行***條命令時就報錯,提示“/etc/selinux/targeted/contexts/files/file_contexts.homedirs: line 18 hasinvalid context user_u:object_r:user_mozilla_home_t:s0”,報錯信息有多條類似的,不止這一條。
再次google,***在紅帽的網(wǎng)站上找到了原因,之所以會出現(xiàn)這個問題因為在SELinux是disabled的狀態(tài)下升級了操作系統(tǒng),參見https://bugzilla.redhat.com/show_bug.cgi?id=449420
解決方法:
在啟用SELinux并重啟后,執(zhí)行下列命令:
genhomedircon
touch /.autorelabel
reboot
這次在重啟的時候就停在relabeling的地方,大概花了5分鐘左右,完成后進(jìn)入系統(tǒng),將SELinux設(shè)置為Enforcing模式,執(zhí)行命令ps -eZ 查看進(jìn)程的SELinux上下文,一切正常。
附:
系統(tǒng)啟動時,運行/etc/rc.d/rc.sysinit腳本,在這個腳本中判斷了/.autorelabel文件是否存在,如果存在,則會調(diào)用fixfiles命令對整個文件系統(tǒng)進(jìn)行relabeling,相關(guān)代碼如下:
If [ -f /.autorelabel ] || strstr"$cmdline" autorelabel ; then
relabel_selinux
fi
relabel_selinux() {
if [ -x/usr/bin/rhgb-client ] && /usr/bin/rhgb-client --ping ; then
chvt 1
fi
# if /sbin/init isnot labeled correctly this process is running in the
# wrong context,so a reboot will be reuired after relabel
REBOOTFLAG=`restorecon -v /sbin/init`
AUTORELABEL=
./etc/selinux/config
if ["$AUTORELABEL" = "0" ]; then
rm -f /.autorelabel
echo
echo$"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required. "
echo $"*** /etc/selinux/configindicates you want to manually fix labeling"
echo$"*** problems. Dropping you to a shell; the system will reboot"
echo$"*** when you leave the shell."
echo"0" > $selinuxfs/enforce
sulogin
echo$"Unmounting file systems"
umount -a
mount -n -oremount,ro /
echo$"Automatic reboot in progress."
reboot -f
else
echo
echo$"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required."
echo$"*** Relabeling could take a very long time, depending on file"
echo$"*** system size and speed of hard drives."
echo"0" > $selinuxfs/enforce
/sbin/fixfiles restore > /dev/null 2>&1
rm -f /.autorelabel
if [ ! -z "$REBOOTFLAG" ]; then
echo$"Automatic reboot in progress."
reboot -f
fi
echo$SELINUX_STATE > $selinuxfs/enforce
if [ -x/usr/bin/rhgb-client ] && /usr/bin/rhgb-client --ping ; then
chvt 8
fi
fi
}
【編輯推薦】