SELinux (Security-Enhanced Linux) 是 Linux 內核強制訪問控制 (MAC) 的實現,通過安全策略限制進程對文件、端口等資源的訪問。雖然大幅提升安全性,但在特定場景(如應用調試、兼容性測試)下可能需要關閉。本文詳述查看狀態、臨時關閉、永久關閉的標準操作流程。
推薦使用以下兩種命令,輸出直觀明確:
sestatus -v(詳細模式)# /usr/sbin/sestatus -v
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
關鍵字段:SELinux status: enabled 表示啟用;Current mode: enforcing 表示強制模式(拒絕違規並記錄日誌);permissive 為寬容模式(僅記錄不攔截)。
getenforce(快速查看模式)# getenforce
Enforcing
返回值僅三種:Enforcing、Permissive、Disabled。
切換至 Permissive 模式,策略不再強制執行,但仍記錄違規日誌,便於排查:
# setenforce 0
# getenforce
Permissive
setenforce 0:設置為 Permissivesetenforce 1:恢復 Enforcing注意:此操作需 root 權限,且僅影響當前運行內核,修改不寫入配置文件。
編輯主配置文件 /etc/selinux/config(或 /etc/sysconfig/selinux,兩者為符號鏈接):
# vi /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 these two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
將 SELINUX=enforcing 修改為 SELINUX=disabled,保存退出,執行 reboot 重啟系統。
audit2allow 分析 /var/log/audit/audit.log 生成自定義策略模塊,解決誤攔截問題。setenforce 0 可快速驗證是否為 SELinux 導致的問題,定位後再決定是否永久關閉。重啟後執行:
# getenforce
Disabled
# sestatus
SELinux status: disabled
即確認已完全禁用。
