Linux SELinux 使用操作
- # 修改 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 規則 1
- 格式: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