7.2. 使用命令行助手排除 SELinux 问题


您可以使用命令行助手对 SELinux 问题进行故障排除。示例故障排除过程演示了命令行助手的诊断功能。

先决条件

  • 您已启用了命令行助手。
  • 您有对系统的 root 访问权限。

流程

  1. 检查 httpd 软件包版本,并确定 Web 服务器接受传入请求的端口:

    $ sudo rpm -qa httpd
    httpd-2.4.62-2.fc40.x86_64
    $ cat /etc/httpd/conf/httpd.conf | grep Listen
    Listen 80
  2. 重启 httpd 服务并检查错误:

    $ systemctl restart httpd
    $ sudo journalctl -xeu httpd.service
    Job for httpd. Service failed because the control process exited with error code.
  3. 使用命令行助手来排除服务故障并检查 SELinux 状态:

    $ c "why did httpd fail to start"
    $ c "selinux httpd port"
    $ sudo sestatus
  4. 检查 httpd 的 SELinux 上下文,并询问命令行助手获取指导信息:

    $ sudo cat /usr/share/selinux/targeted/contexts/httpd_var_run_t
    No such file or directory
    $ c "i don't have a httpd_var_run_t contexts"
  5. 设置 SELinux 上下文,并根据命令行助手建议配置端口:

    $ sudo chcon -R -t httpd_var_run_t
    $ c "selinux won't let httpd listen on port 12345"
    $ sudo semanage port -a -t httpd_port_t -p tcp 12345
  6. 如果您收到 ValueError,请询问命令行助手并应用推荐的修复:

    $ c "how do I fix ValueError: Type httpd_port_t is invalid, must be a port type"
    $ sudo ls -Z /usr/sbin/httpd
    $ chcon -t httpd_exec_t /usr/sbin/httpd
    $ sudo setenforce 1
  7. 重启 httpd 并在服务仍然失败时启用网络连接:

    $ sudo systemctl restart httpd
    $ c "how do I enable httpd to listen on port 12345 selinux"
    $ sudo setsebool -P httpd_can_network_connect=1
  8. 如果服务再次失败,请检查日志并搜索 SELinux 拒绝:

    $ journalctl -xeu httpd
    $ c "An ExecStart= process belonging to unit httpd.service has exited."
    $ sudo ausearch -m AVC,USER_AVC -ts recent
  9. 在命令行助手中输入 AVC 拒绝输出进行分析:

    $ c "avc: denied {name_bind} for pid=7184 comm="httpd" src=12345 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:unreserved_port_t:s0 tclass=tcp_socket permissive=0"
  10. 将端口添加到正确的 SELinux 类型中以解决绑定错误:

    $ sudo semanage port -a -t http_port_t -p tcp 12345

验证

  • 重启 httpd 服务,并检查 httpd.service 的状态:

    $ sudo systemctl restart httpd
    $ No error
    $ sudo systemctl status httpd.service

    服务器已配置、启动并在运行,并侦听 端口 443端口 12345

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部