Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux SELinux 狀態查看與關閉完全指南:臨時與永久方案

編輯:關於 Linux

SELinux (Security-Enhanced Linux) 是 Linux 內核強制訪問控制 (MAC) 的實現,通過安全策略限制進程對文件、端口等資源的訪問。雖然大幅提升安全性,但在特定場景(如應用調試、兼容性測試)下可能需要關閉。本文詳述查看狀態、臨時關閉、永久關閉的標準操作流程。

一、查看 SELinux 當前狀態

推薦使用以下兩種命令,輸出直觀明確:

1. 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 為寬容模式(僅記錄不攔截)。

2. getenforce(快速查看模式)

# getenforce
Enforcing

返回值僅三種:Enforcing、Permissive、Disabled。

二、關閉 SELinux 方法

1. 臨時關閉(無需重啟,重啟後失效)

切換至 Permissive 模式,策略不再強制執行,但仍記錄違規日誌,便於排查:

# setenforce 0
# getenforce
Permissive
  • setenforce 0:設置為 Permissive
  • setenforce 1:恢復 Enforcing

注意:此操作需 root 權限,且僅影響當前運行內核,修改不寫入配置文件。

2. 永久關閉(需重啟生效)

編輯主配置文件 /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 重啟系統。

三、最佳實踐建議

  1. 優先嘗試調整策略而非關閉:使用 audit2allow 分析 /var/log/audit/audit.log 生成自定義策略模塊,解決誤攔截問題。
  2. 臨時排查用 Permissive:setenforce 0 可快速驗證是否為 SELinux 導致的問題,定位後再決定是否永久關閉。
  3. 生產環境慎用 Disabled:完全禁用會喪失 MAC 保護,建議僅在容器、特定嵌入式設備或絕對可信環境中使用。

四、驗證關閉結果

重啟後執行:

# getenforce
Disabled
# sestatus
SELinux status:                 disabled

即確認已完全禁用。

SELinux 配置示意圖

Copyright © Windows教程網 All Rights Reserved