Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

SELinux 常用指令工具完整整理

SELinux 的相關工具不少,但真正日常會用到的就那幾個。以下依序說明。

工具速覽

sestatus:查詢 SELinux 目前狀態
selinuxenabled:查詢系統是否啟用 SELinux 支援
setenforce:設定 SELinux 運作模式
getsebool:列出所有 boolean 數值
setsebool:設定 boolean 數值
chcon:變更檔案目錄的 security context
restorecon:恢復檔案目錄預設的 security context
fixfiles:修正預設 security context
semanage:SELinux policy 管理程式
secon:檢視行程、檔案等項目的 SELinux context
audit2why:檢視 audit 訊息內容
sealert:SELinux 訊息診斷用戶端程式

1. sestatus

root@monitor:~# sestatus
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: permissive
Mode from config file: permissive
Policy version: 21
Policy from config file: targeted

2. selinuxenabled

透過返回值判斷是否啟動:0 表示已啟動,1 表示已關閉。

selinuxenabled ; echo $?

3. setenforce

切換運行狀態,前提是 SELinux 已開啟;這種切換只對當前有效,重啟即失效(若要永久關閉,必須改 /etc/selinux/config)。

setenforce [ Enforcing | Permissive | 1 | 0 ]

enforcing 或 1:強制模式
permissive 或 0:僅警告、不限制

root@monitor:~# sestatus | grep -i mod
Current mode: permissive
Mode from config file: permissive
root@monitor:~# setenforce 1
root@monitor:~# sestatus | grep -i mod
Current mode: enforcing
Mode from config file: permissive

4. getsebool 與 setsebool

SELinux 把許多控制項做成 boolean 開關,值存放在 /selinux/booleans/ 目錄下,只有 1(啟用)與 0(關閉)兩種。

getsebool ftpd_disable_trans
ftpd_disable_trans --> off

getsebool -a
NetworkManager_disable_trans --> off
allow_cvs_read_shadow --> off
allow_daemons_dump_core --> on
allow_daemons_use_tty --> off
allow_execheap --> off
allow_execmem --> on
allow_execmod --> off

設定方式:

setsebool [ -P ] boolean value
setsebool ftpd_disable_trans=on
setsebool -P ftpd_disable_trans=off

-P 表示永久套用,少了 -P 重啟就會被還原。

5. chcon

chcon [OPTION]... CONTEXT FILE...
chcon [OPTION]... --reference=RFILE FILE...

參數:-u 設定 user、-r 設定 role、-t 設定 type。

chcon -t var_t /etc/vsftpd/vsftpd.conf
chcon --reference=/var/www/html index.html

注意:若變更的是目錄,後續在該目錄內新增的檔案會套用目錄本身的 type。

6. restorecon

恢復預設 security context,規則來源是 /etc/selinux/<POLICY>/contexts/files/ 下的 file_contexts 與 file_contexts.local。

常用參數:-R 包含子目錄、-F 強制套用預設值、-v 顯示過程。

restorecon [FRrv] [-e excludedir] pathname...
restorecon /etc/ntp.conf
restorecon -v /etc/ntp.conf
restorecon -v -F /etc/ntp.conf

手動新增恢復規則:編輯 /etc/selinux/<POLICYTYPE>/contexts/files/file_contexts.local,加入例如:

/var/ftp(/.*)? system_u:object_r:public_content_t

用 semanage 維護會更方便。

7. fixfiles

fixfiles { check | restore | [-F] relabel } [[dir] ...]
fixfiles -R rpmpackage[,rpmpackage...] { check | restore }

-R 使用指定 rpm 套件提供的檔案清單。

fixfiles check /etc
fixfiles restore /etc
fixfiles -F relabel /
fixfiles -R setup check

8. audit2why

檢視 /var/log/audit/audit.log 的記錄並說明原因,需搭配 auditd 服務。

audit2why < /var/log/audit/audit.log

9. sealert

參數:-H 以網頁格式輸出、-l 檢視指定 ID 的警示。

sealert -l xxxxx-xxxxx-xxxx
sealert -H -l xxxxx-xxxxx-xxxx > output.html

需搭配 setroubleshoot 服務;該服務啟動後會依 audit 提供的資訊診斷問題,輸出到 /var/log/messages 供除錯。

10. semanage

semanage { login | user | port | interface | fcontext | translation } -l [-n]
semanage login -l
semanage user -l
semanage port -l
semanage port -a -t http_port_t -P tcp 81
semanage fcontext -a -t httpd_sys_content_t "/home/users/(.+)/public_html(/.*)?"

11. secon

secon [-hVurtscmPRfLp] [CONTEXT]
secon [--file] FILE | [--link] FILE | [--pid] PID

參數:-u 顯示 user、-r 顯示 role、-t 顯示 type、-f 從檔案取 context、-p 從行程 PID 取 context。

secon -u
secon -r
secon -t
secon --file /etc/passwd
secon --pid <pid>
Copyright © Linux教程網 All Rights Reserved