歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> CentOS 5.7啟用SELinux時遇到的問題

CentOS 5.7啟用SELinux時遇到的問題

日期:2017/2/28 15:50:54   编辑:Linux教程

一台CentOS5.7的機器,原來只是在公司內部訪問,SELinux是禁用的狀態,現需要搭建成FTP服務器並發布到外網上去,為了安全,啟用SELinux。編輯/etc/selinux/config文件,將SELINUX=disabled改為SELINUX=permissive,保存後重啟系統。(SELinux從禁用狀態轉變為啟用時,需要重啟系統以重新標記文件的上下文,紅帽建議先將SELinux的值設置為permissive,重新標記完成後再設置為enforcing。)

按道理說,重新對整個文件系統進行標記應該是比較費時間的,至少需要幾分鐘,但在這台機器上敲入reboot命令後,發現它很快就啟動起來的。登錄後使用命令getenforce,返回permissive,說明SELinux的狀態正常,於是把SELinux的狀態設置為Enforcing,設置完成後重啟了一下vsftpd和httpd服務,這下問題大了,服務無法正常啟動,提示“errorwhile loading shared libraries: libssl.so.6: failed to map segment from sharedobject: Permission denied”,直覺告訴我是因為重啟時沒有對整個文件系統進行正確的relabeling引起的,於是執行命令touch /.autorelabel ,在/下創建一個.autorelabel文件,有這個文件存在,系統在重啟時就會對整個文件系統進行relabeling,但奇怪的是重啟又很快完成,看來還是沒有完成relabeling。

最後google了一下,發現一個解決方案,執行下面三條命令:

fixfiles -f relabel

touch /.autorelabel

reboot

但在執行第一條命令時就報錯,提示“/etc/selinux/targeted/contexts/files/file_contexts.homedirs: line 18 hasinvalid context user_u:object_r:user_mozilla_home_t:s0”,報錯信息有多條類似的,不止這一條。

再次google,最後在紅帽的網站上找到了原因,之所以會出現這個問題因為在SELinux是disabled的狀態下升級了操作系統,參見https://bugzilla.redhat.com/show_bug.cgi?id=449420

解決方法:

在啟用SELinux並重啟後,執行下列命令:

genhomedircon

touch /.autorelabel

reboot

這次在重啟的時候就停在relabeling的地方,大概花了5分鐘左右,完成後進入系統,將SELinux設置為Enforcing模式,執行命令ps -eZ 查看進程的SELinux上下文,一切正常。

附:

系統啟動時,運行/etc/rc.d/rc.sysinit腳本,在這個腳本中判斷了/.autorelabel文件是否存在,如果存在,則會調用fixfiles命令對整個文件系統進行relabeling,相關代碼如下:

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

}

Copyright © Linux教程網 All Rights Reserved