Ubuntu開啟SELinux的詳細步驟
SELinux并非Ubuntu默認組件,需先通過APT包管理器安裝核心組件及依賴。打開終端,依次執行以下命令更新軟件包列表并安裝:
sudo apt update
sudo apt install selinux-basics selinux-policy-default auditd selinux-utils
selinux-basics:提供SELinux基礎服務;selinux-policy-default:默認安全策略;auditd:審計守護進程(記錄SELinux拒絕事件);selinux-utils:包含sestatus等實用工具。安裝完成后,通過selinux-activate命令激活SELinux,系統會在下次啟動時加載SELinux模塊:
sudo selinux-activate
該命令會自動配置GRUB引導參數(添加security=selinux),確保SELinux內核模塊被加載。
SELinux有三種運行模式,需通過修改配置文件設置默認模式:
sudo nano /etc/selinux/config
SELINUX=行,將disabled改為enforcing(強制模式,阻止違規操作)或permissive(寬容模式,僅記錄違規日志,不阻止)。例如:SELINUX=enforcing # 強制模式(推薦生產環境使用)
# SELINUX=permissive # 寬容模式(測試環境使用)
Ctrl+O保存,Ctrl+X退出編輯器。修改配置后,必須重啟系統以加載SELinux模塊并應用模式設置:
sudo reboot
系統重啟后,通過以下命令確認SELinux是否啟用及當前模式:
sestatus
正常輸出應包含以下關鍵信息(以強制模式為例):
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 # 配置文件中設置的默認模式
也可通過getenforce命令快速查看當前模式(輸出Enforcing表示強制模式)。
enforcing)會嚴格攔截違規操作,適合對安全性要求高的場景;寬容模式(permissive)僅記錄日志,適合調試階段。semanage(sudo apt install policycoreutils-python-utils)或audit2allow工具生成自定義規則。