4.2. 使用命令行助手对 SELinux 问题进行故障排除


下面的示例提供了在命令行助手中对 SELinux 问题进行故障排除的步骤。

先决条件

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

流程

  1. 在终端中,输入以下命令列出您在系统中安装的 httpd 软件包版本:

    $ sudo rpm -qa httpd
    httpd-2.4.62-2.fc40.x86_64
  2. 显示 httpd.conf 文件内容。

    $ sudo cat /etc/httpd/conf/httpd.conf
    # This is the main Apache HTTP server configuration file.  It contains the
    # configuration directives that give the server its instructions.
    # See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
    # In particular, see<URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
    # for a discussion of each configuration directive.
  3. 查询所有 httpdq 软件包:

    $ sudo rpm -qa httpdq
  4. 识别 Web 服务器接受传入请求的端口:

    $ cat /etc/httpd/conf/httpd.conf | grep Listen
    # Listen: Allows you to bind Apache to specific IP addresses and/or
    # Change this to Listen on a specific IP address, but note that if
    #Listen 12.34.56.78:80
    Listen 80
  5. 重启 httpd 服务:

    $ systemctl restart httpd
    
    Job for httpd. Service failed because the control process exited with error code.
    See “systemctl status httpd.service" and “journalctl -xeu httpd.service” for details.
    1. 运行 journalctl 命令,以获取有关失败服务的更多详细信息:

      $ sudo journalctl -xeu httpd.service
  6. 使用命令行助手来排除此问题,并询问服务失败的原因:

    $ sudo c “why did httpd fail to start”

    助手中的其中一个建议是使用 ausearch 工具查询审计日志,并将 AVCUSER_AVC 值用于消息类型参数。为此,请运行以下命令:

    $ sudo ausearch -m AVC,USER_AVC -ts recent
    1. 询问有关 selinux httpd 端口的命令行助手:

      $ c “selinux httpd port”

      助手建议使用 sestatus 命令检查当前的 SELinux 状态以及 httpd 服务的内容:

      $ sudo 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
      Memory protection checking:     actual (secure)
      Max kernel policy version:      33
    2. 运行以下命令,查看 httpd 服务的具体 SELinux 策略:

      $ sudo cat /usr/share/selinux/targeted/contexts/httpd_var_run_t
      No such file or directory
    3. 询问有关上下文的命令行助手。

      $ c “i don't have a httpd_var_run_t contexts”

      命令行助手需要一些时间来处理请求,然后提供了几个可能的建议。

    4. 该助手表示您可能没有上下文,需要使用以下命令进行设置:

      $ sudo chcon -R -t httpd_var_run_t
    5. 询问有关端口的 CLA:

      $ c “selinux won't let httpd listen on port 12345”
    6. 尝试以下建议,运行以下命令:

      $ sudo semage port -a -t httpd_port_t -p tcp 12345
      ValueError: Type httpd_port_t is invalid, must be a port type
    7. 询问有关您在输出中看到的错误的 CLA:

      $ c “how do I fix ValueError: Type httpd_port_t is invalid, must be a port type”
  7. 运行 CLA 提供的步骤:

    $  sudo getenforce
    Enforcing
    $ setenforce 0
    $ sudo systemctl restart httpd
    $ sudo systemctl status httpd
    
    $ sudo ls -Z /usr/sbin/httpd
    system_u:object_r:httpd_exec_t:s0 /usr/sbin/httpd
    
    $ chcon -t httpd_exec_t /usr/sbin/httpd
    
    $ sudo setenforce 1
    1. 重启 httpd 服务并检查 httpd.service 的状态:

      $ sudo systemctl restart httpd
      Job failed
      
      $ sudo systemctl status httpd.service
      Failed to start the Apache Server
  8. 询问 CLA 如何启用 httpd 侦听端口 12345:

    $ c “how do I enable httpd to listen on port 12345 selinux”
    1. 运行 CLA 推荐的命令:

      $ sudo setsebool -P httpd_can_network_connect=1
  9. 重启 httpd 服务并再次检查 httpd.service 的状态:

    $ sudo systemctl status htppd
    $ sudo systemctl restart httpd
    Job failed, see journalctl
  10. 检查 journalctl 服务:

    $ journalctl -xeu httpd
    Output: An ExecStart= process belonging to unit httpd.service has exited.
  11. 使用输出来要求 CLA 进行故障排除:

    $ c “An ExecStart= process belonging to unit httpd.service has exited.”
    1. 运行 CLA 响应的命令:

      $ sudo ausearch -m AVC,USER_AVC -ts recent
      Output: “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 tclas=tcp_socket permissive=0"
    2. 复制上一个命令的输出:

      $ sudo 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 tclas=tcp_socket permissive=0”
    3. 运行以下命令解决"SELinux 正在阻止 Apache 服务器(httpd)绑定到端口 12345"的错误。

      $ sudo semage port -a -t http_port_t -p tcp 12345
  12. 重启 httpd 服务并检查 httpd.service 的状态:

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

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

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat
返回顶部