1. 程式人生 > >基礎運維:SELINUX入門到掌握

基礎運維:SELINUX入門到掌握

什麽 裝系統 http 程序 reboot nco lec cati util

一、介紹

1.官方意思
SELinux是一種基於 域-類型 模型(domain-type)的強制訪問控制(MAC)安全系統,它由NSA編寫並設計成內核模塊包含到內核中,相應的某些安全相關的應用也被打了SELinux的補丁,最後還有一個相應的安全策略。任何程序對其資源享有完全的控制權。假設某個程序打算把含有潛在重要信息的文件扔到/tmp目錄下,那麽在DAC情況下沒人能阻止他。SELinux提供了比傳統的UNIX權限更好的訪問控制。

2.簡單意思
selinux就像是一種系統的保護機制,如果關閉它的話,也沒什麽問題,操作上不會因為selinux而感到阻礙和困難,但是你開啟之後,你會感覺做什麽都是錯的,寸步難行。舉個樂子:”當你打開了HTTP服務,內容能正常訪問,但是你開啟selinux之後就報錯,那你會覺得是selinux的問題,然後你就設置好這個內容的selinux的上下文(類似用戶權限),又可以正常訪問了,然後你開開心心地將一個項目代碼上傳到網頁根目錄,不能打開了,你會想到selinux ~~ 首頁能打開,但是其他jpg、gif等等不能正常顯示,你又改權限,劈裏啪啦地一頓操作猛如虎,都可以了,但是跳轉到別人的網站又不行了,emmm ~~ 直接重裝系統“

3.有點意思
selinux畢竟是保護機制,所以肯定能起到保護作用,保護著系統不被任意修改,保護著站點的權限又嚴格訪問限制,你的文件都是等於數據,數據是錢買不了的,想好好地存在於互聯網,就要做相對的安全(沒有絕對的安全),通過各種手段保護你的數據不被修改等等,安全意識是非常重要的。

4.沒什麽意思
selinux是非常好的一種保護機制,但在企業中,大多數是沒有使用這個安全機制的,畢竟都是直接關閉的,比如雲服務器,你買的時候就已經默認是關閉selinux這個保護機制了。所以在這裏演示,後期的軟件服務和部署都不會開啟。

二、Selinux入門--基本設置

1.關於selinux
selinux在系統的分布

[root@leo ~]# find / -name selinux
/sys/fs/selinux
/etc/sysconfig/selinux
/etc/selinux
/usr/lib64/python2.7/site-packages/selinux
/usr/share/selinux
/usr/libexec/selinux
[root@leo ~]# 

2.分析

selinux目錄組成:
/sys/:系統全局設備管理目錄,比如cgroup、pstore、selinux、xfs都存在這個目錄。
/etc/:服務的配置文件目錄。
/usr/:存放二進制文件、共享文件、函數庫文件、服務執行文件的目錄。

3.命令式寬容模式或嚴格模式(以下設置全局有效)

查看當前selinux的狀態
[root@leo ~]# getenforce
Enforcing
[root@leo ~]# 

設置寬容模式
[root@leo ~]# setenforce 0
[root@leo ~]# getenforce
Permissive
[root@leo ~]# 

設置拒絕模式
[root@leo ~]# setenforce 1
[root@leo ~]# getenforce
Enforcing
[root@leo ~]# 

4.配置文件說明(可以設置關閉、寬容、嚴格模式)
/etc/sysconfig/selinux 是 /etc/selinux/config 文件的軟鏈接
所以修改這兩個其中一個,文件內容都會改變。

5.配置文件

[root@leo ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# 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     #設置selinux的狀態模式
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,    #目標模式
#     minimum - Modification of targeted policy. Only selected processes are protected.   #最小化權限控制
#     mls - Multi Level Security protection.    #多種selinux模式,根據文件的上下文設置而改變訪問權限
SELINUXTYPE=targeted      #設置selinux的類型,默認目標模式

6.更新配置文件

[root@leo ~]# vim /etc/selinux/config 
[root@leo ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# 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=disabled    #修改配置文件,需要重啟系統以生效
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

[root@leo ~]# 

7.重啟系統
重啟系統方式有很多:
第一種方法:
[root@leo ~]# reboot

第二種方法:
[root@leo ~]# init 6

這是系統啟動級別命令,參考我另一個博客文章:
http://blog.51cto.com/leoheng/2161336

第三種方法:

虛擬機--》鼠標右擊--》電源--》重新啟動客戶機
技術分享圖片

8.查看selinux狀態

[root@leo ~]# getenforce
Disabled

三、Selinux進階---上下文

提醒:如果想使用上下文,必須開啟selinux才能有效,如果設置了上下文,卻沒有開啟selinux,那等於白忙了。

這裏舉個例子:其他設置,類比就好

1.安裝Apache服務
[root@leo ~]# yum install -y httpd #提供網頁服務

2.安裝網絡工具
[root@leo ~]# yum install -y net-tools #提供查詢系統網絡狀態的工具

3.啟動網頁服務並檢查網站端口(默認端口為80)

[root@leo ~]# systemctl start httpd && netstat -tunlp |grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      1633/httpd          
[root@leo ~]# 

4.訪問網頁(默認網頁)
技術分享圖片

5.開啟selinux

[root@leo ~]# getenforce
Enforcing
[root@leo ~]# 

6.設置自定義網頁
技術分享圖片

7.查看web上下文

[root@leo html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[root@leo html]# 

上下文為:httpd_sys_content_t

8.在家目錄創建網頁文件,並移動到網站根目錄

[root@leo html]# cd
[root@leo ~]# vim leo.html
[root@leo ~]# cat leo.html 
<h1 align=center>test2 web</h1>
<h2 align=center>leo</h2>
[root@leo ~]# mv leo.html /var/www/html/
[root@leo ~]# cd /var/www/html/
[root@leo html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 leo.html
[root@leo html]# 

9.訪問新創建的網頁文件(顯示沒權限訪問該文件)
技術分享圖片

10.設置文件selinux,並訪問網頁(可正常訪問該網頁文件)
技術分享圖片

11.chcon命令掌握

[root@leo html]# chcon --help
Usage: chcon [OPTION]... CONTEXT FILE...
  or:  chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...
  or:  chcon [OPTION]... --reference=RFILE FILE...

選項如下:
--reference     #引用其他文件上下文
--dereference    #不引用其他文件上下文    
-h, --no-dereference     #影響符號鏈接而不是引用文件
-u, --user=USER      #指定用戶
-r, --role=ROLE     #指定角色
-t, --type=TYPE     #指定上下文
-l, --range=RANGE      #指定上下文的範圍
--no-preserve-root      #不區分對待根目錄
--preserve-root    #區分對待根目錄
--reference=RFILE      #直接引用該文件的上下文
-R, --recursive        #遞歸,一般用在目錄
-v, --verbose          #對每個文件輸出信息

12.顯示一下版本信息

[root@leo html]# chcon --version
chcon (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Russell Coker and Jim Meyering.
[root@leo html]# 

13.selinux高級

這個後期會在RHCE認證專欄做詳細解說

基礎運維:SELINUX入門到掌握