天天看點

SELinux安全上下文初探

SELinux安全上下文初探

   開啟我們的SELinux,我在安裝linux系統時将它禁用了,但是開啟selinux後,可能會對我們的一些服務有些限制功能,比如你可能不能正常通路網頁、ftp等等,下面就對這些問題通過設定SELinux來解決這些問題

1、如果你的SELinux沒用啟動的話,有以下幾種啟動的方法

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143524623.png"></a>

不可用狀态

開啟SELinux

Vim  /etc/sysconfig/selinux  如下:

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143606353.png"></a>

或者用圖形化界面修改指令為 system-config-selinux 或 system-cofig-securitylevel  将SELinux設定為“強制”

以上三種方法必須重新啟動計算機後SELinux才能生效。

2、當我們将SELinux設定為“強制”模式後,我們更改httpd.conf裡的根目錄以後,會發現,httpd重新開機不了,老失敗、、、、

這裡将根目錄改為如下:

 DocumentRoot "/www"

mkdir /www

echo "123" &gt; /www/index.html

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143618967.png"></a>

重新開機httpd時,報錯了,重新開機不了,檢視日志發現如下:

Dec 23 11:33:03 localhost setroubleshoot: SELinux is preventing access to files with the default label, default_t. For complete SELinux messages. run sealert -l 511c4eda-797d-4b79-a1e3-51477afed4e6

按照日志的提示我們允許sealert -l 511c4eda-797d-4b79-a1e3-51477afed4e6指令 有以下資訊

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143646636.png"></a>

源上下文和目标上下文的類型 不同,httpd 無法對default_t類的檔案及目錄的讀取,是以啟動不了,修改為httpd能夠讀取的類型

httpd能夠讀取什麼樣的類型呢,通過如下一些指令檢視 

指令 ll -Z 目錄或檔案

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143706555.png"></a>

或者是指令 semanage  fcontext  -l   | grep http

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143716982.png"></a>

從上邊我們可以看到httpd 能讀取的類型是 httpd_sys_context_t的類型

好了知道了這個類型,我們就把/www這個目錄改成這個類型 指令有chcon(change context)或者restorecon 用法如下:

chcon  -R --reference=/var/www/html/  /www/ 或者chcon -t httpd_sys_content_t  /www/

改回原來的類型用 restorecon -v -R /www

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143731396.png"></a>

Httpd正常啟動了,并且也能通路到index.html網頁

重新開機httpd報錯時。除了參看日志外還可用以下指令排錯

sealert -b  或者是圖形界面下右上角的

<a href="http://blog.51cto.com/attachment/201212/142457850.png" target="_blank"></a>

單擊小星星

關于web這裡用的是預設的80端口,如果我們要建基于端口的虛拟主機時,能否通過SELinux呢。。。 假如我們把端口改為8001,

Listen 8001

重新開機httpd服務

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143744750.png"></a>

出現錯誤了,不能綁定8001端口

通過上述的排錯方式找到了如下

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143757589.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143815182.png"></a>

SELinux安全政策庫裡沒有關于httpd的8001端口,是以我們要添加上去

semanage port  -a  -t http_port_t -p tcp 8001

重新開機httpd服務就沒問題了。

ftp關于SELinux的一些問題

登入ftp以後dir一下看不到自己家目錄裡的任何檔案及檔案夾如下:

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143834864.png"></a>

通過上述的排錯方法得知如下:

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143845133.png"></a>

先看一下ftp的布爾值

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143859785.png"></a>

預設SELinux不允許的,需要改為ON指令如下:

setsebool -P ftp_home_dir=ON 或者setsebool -P ftp_home_dir=1

再次dir

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143933779.png"></a>

Samba關于SELinux的一些問題

共享目錄為/wsm   

        [public]

        comment = Public

        path = /wsm

        public = yes

但是卻通路不了/wsm  和自己的家目錄  日志如下:

Dec 23 14:35:15 localhost smbd[13169]: [2012/12/23 14:35:15, 0] smbd/service.c:make_connection_snum(1003) 

Dec 23 14:35:15 localhost smbd[13169]:   '/wsm' does not exist or permission denied when connecting to [public] Error was 權限不夠 

要設定一下SELinux了, smb.conf裡就有關于SELinux的配置說明

執行如下指令

chcon -R  -t samba_share_t  /wsm/

<a target="_blank" href="http://blog.51cto.com/attachment/201212/143945473.png"></a>

開啟自己的家目錄

setsebool -P samba_enable_home_dirs=ON

一切OK 

本文轉自 abc16810 51CTO部落格,原文連結:http://blog.51cto.com/abc16810/1102086

繼續閱讀