Linux SELinux 使用操做

Linux SELinux 使用操做linux


  • # 修改 SELinux 啓動模式、臨時生效
  • 命令:setenforce [0|1]
  • 0:轉成 permissive 寬容模式;
  • 1:轉成 Enforcing 強制模式;

  • # 查看 當前 SELinux 模式
  • 命令:getenforce

  • # 修改 SElinux 啓動模式、永久生效 重啓生效
  • 命令:vim /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
    # SELINUXTYPE= can take one of three values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    配置文件

  • # 查看 指定主體程序type類型
  • 命令:ps -eZ | grep cron
  • 解析:ps -eZ | grep 篩選主體

  •  # 查看 系統系統所有 SELinux 規則的啓動關閉
  • 命令:getsebool -a
  • -a:列出目前系統上面全部 SELinux 規則的布林值爲開放或者關閉
  • 同功能命令semanage boolean -l

  • # 查看 SELinux 總信息統計狀態、安裝包:setools-console-*
  • 命令:seinfo
  • 格式:seinfo [-trub]
  • --all:列出 SELinux 的狀態、規則布林值、身份識別、角色、類別等全部信息
  • -u:列出 SELinux 的全部身份識別 uesr 種類
  • -r:列出 SELinux 的全部身份 role 種類
  • -t:列出 SELinux 的全部身份 type 種類
  • -b:列出全部規則的種類 布林值
  • Statistics for policy file: /sys/fs/selinux/policy
    Policy Version & Type: v.28 (binary, mls)
    
       Classes:            83    Permissions:       255
       Sensitivities:       1    Categories:       1024
       Types:            4620    Attributes:        357
       Users:               8    Roles:              14
       Booleans:          295    Cond. Expr.:       346
       Allow:          102249    Neverallow:          0
       Auditallow:        160    Dontaudit:        8413
       Type_trans:      16863    Type_change:        74
       Type_member:        35    Role allow:         30
       Role_trans:        412    Range_trans:      5439
    ....(底下省略)....
    # 從上面我們能夠看到這個政策是 targeted ,此政策的安全本文類別有 4620 個;
    # 而各種 SELinux 的規則 (Booleans) 共制訂了 295 條!
    輸出信息

  • # 查看 指定type類型再 SELinux 中被賦予的權限規則
  • 命令1:sesearch -A -s crond_t | grep spool
  • 解析:sesearch -A -s 主體type | grep 塞選字段
  • 命令2:sesearch -A -b httpd_enable_homedirs
  • 解析:sesearch -A -b 布林值
  • 格式:sesearch [-A] [-s 主體類別=程序type] [-t 目標類別] [-b 布林值=文件目錄type]
  • -A:列出讀取或放行的相關信息
  •    allow crond_t system_cron_spool_t : file { ioctl read write create getattr ..
       allow crond_t system_cron_spool_t : dir { ioctl read getattr lock search op..
       allow crond_t user_cron_spool_t : file { ioctl read write create getattr se..
       allow crond_t user_cron_spool_t : dir { ioctl read write getattr lock add_n..
       allow crond_t user_cron_spool_t : lnk_file { read getattr } ;
    輸出信息

  • # 查看 文件目錄或程序的 SELinux 權限信息
  • 命令:ll -Z 文件/目錄
  • 命令:ls -Z 文件/目錄

  • # 關閉/啓動 SElinux type 規則
  • 命令:setsebool -P httpd_enable_homedirs  1 
  • 解析:setsebool -P 規則 
  • 格式:setsebool [-P] 『規則名稱』 [0|1]
  • -P:直接將設定值寫入文件,永久生效。
  • 1:打開
  • 0:關閉

  • # 修改 SELinux 文件主體,角色,類型、經過示例文件實現
  • 命令:chcon -v --reference=/etc/shadow  /etc/cron.d/checktime
  • 解析:chcon -v --reference=示例文件目錄  賦值文件目錄
  • 格式1:chcon [-R] [-t type] [-u user] [-r role] 目錄文件
  • 格式2:chcon [-R] --reference=示例目錄文件 目錄文件
  • -R:連同子目錄同時修改。
  • -t:後面接type類型。列如:httpd_sys_content_t。
  • -u:後面接身份識別。列如:system_u。
  • -r:後面跟角色,列如:system_r。
  • -v:如有變化成功,請將變更的結果列出來
  • --reference=示例目錄文件:拿某個文件目錄來修訂改動後續接的文件目錄類型。

  • # 恢復 SELinux 賦值的默認預設權限及目錄
  • 命令:restorecon -Rv /etc/cron.d
  • 解析:restorecon -Rv 文件目錄
  • 格式:restorecon [-Rv] 檔案或目錄
  • -R:連同子目錄同事修改。
  • -v:將過程打印到終端。

  • # 查看 篩選 SELinux 下指定類型規則的做用範圍
  • 命令:semanage fcontext -l | grep '^/srv/mycron'
  • 解析:semanage fcontext -l | grep 做用範圍
  • # 添加 SELinux 預設type類型
  • 命令:semanage fcontext -a -t system_cron_spool_t "/srv/mycron(/.*)?"
  • 解析:semanage fcontext -a -t type規則  做用範圍
  • 格式1:semanage {login|user|port|interface|fcontext|translation} -l
  • 格式2:semanage fcontext -{a|d|m} [-frst] file_spec
  • fcontext:主要用在安全性文本方面的用途。
  • -l:爲查詢的意思。
  • -a:增長的意思,你能夠增長一些目錄的預設的安全性文本預設。
  • -m:修改的意思。
  • -d:刪除的意思。

  • # 查看 SElinux 日誌服務監控是否安裝
  • 命令:rpm -qa | grep setroubleshoot
  • 安裝服務名稱:auditd、setroubleshootd
相關文章
相關標籤/搜索