7.2. Using the command-line assistant to troubleshoot SELinux issues
You can troubleshoot SELinux issues by using the command-line assistant. The example troubleshooting process demonstrates the diagnostic capabilities of the command-line assistant.
Prerequisites
- You have enabled the command-line assistant.
- You have root access to your system.
Procedure
On your terminal, enter the following command to list the
httpdpackage version that you have installed in your system:$ sudo rpm -qa httpd httpd-2.4.62-2.fc40.x86_64Query all
httpdpackages:$ sudo rpm -qa httpdIdentify the ports on which the web server accepts incoming requests:
$ 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 80Restart the
httpdservice:$ 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.Run the
journalctlcommand for more details on the failed service:$ sudo journalctl -xeu httpd.service
Use the command-line assistant to troubleshoot the issue and ask why the service is failing:
$ sudo c "why did httpd fail to start"Ask the command-line assistant about the
selinux httpdport:$ c "selinux httpd port"The assistant advises using the
sestatuscommand to check the current SELinux status and the content of the httpd services with the following command:$ 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: 33View the specific SELinux policy for the httpd services by running the following command:
$ sudo cat /usr/share/selinux/targeted/contexts/httpd_var_run_t No such file or directoryAsk the command-line assistant about contexts.
$ c "i don't have a httpd_var_run_t contexts"The command-line assistant takes some time to process the request, then provides several possible suggestions.
The assistant says that you might not have context and need to set it with the following command:
$ sudo chcon -R -t httpd_var_run_tAsk the CLA about the port:
$ c "selinux won't let httpd listen on port 12345"Try the following suggestion, run the command:
$ sudo semanage port -a -t httpd_port_t -p tcp 12345 ValueError: Type httpd_port_t is invalid, must be a port typeAsk the CLA about the error you see in the output:
$ c "how do I fix ValueError: Type httpd_port_t is invalid, must be a port type"
Run the steps provided by the CLA:
$ 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 1Restart the
httpdservice and check the status ofhttpd.service:$ sudo systemctl restart httpd Job failed $ sudo systemctl status httpd.service Failed to start the Apache Server
Ask the CLA how to enable
httpdto listen onport12345:$ c "how do I enable httpd to listen on port 12345 selinux"Run the command advised by the CLA:
$ sudo setsebool -P httpd_can_network_connect=1
Restart the
httpdservice and check the status ofhttpd.serviceagain:$ sudo systemctl status httpd $ sudo systemctl restart httpd Job failed, see journalctlCheck the
journalctlservice:$ journalctl -xeu httpd Output: An ExecStart= process belonging to unit httpd.service has exited.Use the output to ask the CLA to troubleshoot:
$ c "An ExecStart= process belonging to unit httpd.service has exited."Run the command that the CLA responds with:
$ 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"Copy the output of the previous command:
$ 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"Run the following command to resolve the error “SELinux is preventing Apache Server (httpd) from binding to port 12345".
$ sudo semanage port -a -t http_port_t -p tcp 12345
Verification
Restart the httpd service and check the status of
httpd.service:$ sudo systemctl restart httpd No error $ sudo systemctl status httpd.serviceThe server is configured, up and running, and listening on
port 443,port 12345.