在CentOS系統中進行SELinux故障排查可以按照以下步驟進行:
檢查SELinux狀態:
使用命令 sestatus
檢查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
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 31
查看SELinux日志:
查看SELinux日志文件,了解具體的錯誤信息。日志文件通常位于 /var/log/audit/audit.log
。
sudo tail -f /var/log/audit/audit.log
示例輸出:
type=AVC msg=audit(1675407323.123:12345): avc: denied { read } for pid=1234 comm="httpd" name="index.html" dev="sda1" ino=123456 scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:admin_home_t:s0 tclass=file permissive=0
解析SELinux日志:
使用 ausearch
和 audit2allow
工具解析SELinux日志,生成允許規則。
sudo ausearch -m avc -ts today | audit2allow
示例輸出:
#============= httpd_t ==============
allow httpd_t admin_home_t:file { read };
臨時禁用SELinux: 如果需要臨時禁用SELinux以排除問題,可以使用以下命令:
sudo setenforce 0
這將把SELinux模式從 enforcing
切換到 permissive
,允許所有被拒絕的操作,但仍然記錄日志。
永久禁用SELinux:
如果決定永久禁用SELinux,可以編輯 /etc/selinux/config
文件,將 SELINUX
設置為 disabled
:
SELINUX=disabled
然后重啟系統以應用更改:
sudo reboot
調整SELinux策略:
如果需要調整SELinux策略,可以使用 semanage
工具。例如,將某個目錄的上下文更改為 httpd_sys_content_t
:
sudo semanage fcontext -a -t httpd_sys_content_t "/path/to/directory(/.*)?"
sudo restorecon -R /path/to/directory
檢查文件和目錄的上下文:
使用 ls -Z
命令檢查文件和目錄的SELinux上下文:
ls -Z /path/to/directory
示例輸出:
drwxr-xr-x. root root system_u:object_r:admin_home_t:s0 directory
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html
重新設置文件和目錄的上下文:
使用 restorecon
命令重新設置文件和目錄的SELinux上下文:
sudo restorecon -R /path/to/directory
檢查網絡端口的上下文:
使用 semanage
命令檢查和設置網絡端口的SELinux上下文:
sudo semanage port -l | grep http_port_t
sudo semanage port -a -t http_port_t -p tcp 8080
檢查用戶和角色的上下文:
使用 id -Z
命令檢查用戶的SELinux上下文:
id -Z
示例輸出:
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023