温馨提示×

centos selinux故障排查步骤

小樊
89
2025-02-12 15:20:44
栏目: 智能运维
Linux服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在CentOS系统中进行SELinux故障排查可以按照以下步骤进行:

  1. 检查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
    
  2. 查看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 
    
  3. 解析SELinux日志: 使用 ausearchaudit2allow 工具解析SELinux日志,生成允许规则。

    sudo ausearch -m avc -ts today | audit2allow
    

    示例输出:

    #============= httpd_t ==============
    allow httpd_t admin_home_t:file { read };
    
  4. 临时禁用SELinux: 如果需要临时禁用SELinux以排除问题,可以使用以下命令:

    sudo setenforce 0
    

    这将把SELinux模式从 enforcing 切换到 permissive,允许所有被拒绝的操作,但仍然记录日志。

  5. 永久禁用SELinux: 如果决定永久禁用SELinux,可以编辑 /etc/selinux/config 文件,将 SELINUX 设置为 disabled

    SELINUX=disabled
    

    然后重启系统以应用更改:

    sudo reboot
    
  6. 调整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
    
  7. 检查文件和目录的上下文: 使用 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 
    
  8. 重新设置文件和目录的上下文: 使用 restorecon 命令重新设置文件和目录的SELinux上下文:

    sudo restorecon -R /path/to/directory
    
  9. 检查网络端口的上下文: 使用 semanage 命令检查和设置网络端口的SELinux上下文:

    sudo semanage port -l | grep http_port_t
    sudo semanage port -a -t http_port_t -p tcp 8080
    
  10. 检查用户和角色的上下文: 使用 id -Z 命令检查用户的SELinux上下文:

    id -Z
    

    示例输出:

    unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:CentOS FTP Server如何进行故障排查

0