在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
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:CentOS FTP Server如何进行故障排查