4.2. 使用命令行助手对 SELinux 问题进行故障排除
下面的示例提供了在命令行助手中对 SELinux 问题进行故障排除的步骤。
先决条件
- 您已启用了命令行助手。
- 有对系统的 root 访问权限。
流程
在终端中,输入以下命令列出您在系统中安装的
httpd软件包版本:$ sudo rpm -qa httpd httpd-2.4.62-2.fc40.x86_64显示
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.查询所有
httpdq软件包:$ sudo rpm -qa httpdq识别 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重启
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.运行
journalctl命令,以获取有关失败服务的更多详细信息:$ sudo journalctl -xeu httpd.service
使用命令行助手来排除此问题,并询问服务失败的原因:
$ sudo c “why did httpd fail to start”助手中的其中一个建议是使用
ausearch工具查询审计日志,并将AVC和USER_AVC值用于消息类型参数。为此,请运行以下命令:$ sudo ausearch -m AVC,USER_AVC -ts recent询问有关
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运行以下命令,查看 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”命令行助手需要一些时间来处理请求,然后提供了几个可能的建议。
该助手表示您可能没有上下文,需要使用以下命令进行设置:
$ sudo chcon -R -t httpd_var_run_t询问有关端口的 CLA:
$ c “selinux won't let httpd listen on port 12345”尝试以下建议,运行以下命令:
$ sudo semage port -a -t httpd_port_t -p tcp 12345 ValueError: Type httpd_port_t is invalid, must be a port type询问有关您在输出中看到的错误的 CLA:
$ c “how do I fix ValueError: Type httpd_port_t is invalid, must be a port type”
运行 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重启
httpd服务并检查httpd.service的状态:$ sudo systemctl restart httpd Job failed $ sudo systemctl status httpd.service Failed to start the Apache Server
询问 CLA 如何启用
httpd侦听端口12345:$ c “how do I enable httpd to listen on port 12345 selinux”运行 CLA 推荐的命令:
$ sudo setsebool -P httpd_can_network_connect=1
重启
httpd服务并再次检查httpd.service的状态:$ sudo systemctl status htppd $ sudo systemctl restart httpd Job failed, see journalctl检查
journalctl服务:$ journalctl -xeu httpd Output: An ExecStart= process belonging to unit httpd.service has exited.使用输出来要求 CLA 进行故障排除:
$ c “An ExecStart= process belonging to unit httpd.service has exited.”运行 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"复制上一个命令的输出:
$ 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”运行以下命令解决"SELinux 正在阻止 Apache 服务器(httpd)绑定到端口 12345"的错误。
$ sudo semage 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。