How To Allow Apache To Bind To Non-Standard Ports In SELinux?

Published July 14, 2024

Problem: Apache Binding to Non-Standard Ports with SELinux

SELinux can stop Apache from binding to non-standard ports, which limits server setup options. This security feature can be a problem when you want to set up Apache to listen on custom ports for specific web applications or services.

Solving the Apache Port Binding Problem

Using semanage to Modify SELinux Port Rules

To solve the Apache port binding problem with SELinux, use the semanage tool. This tool lets you modify SELinux port rules and add new ports to the SELinux configuration.

Install the needed tools:

sudo yum -y install policycoreutils-python

This command installs the policycoreutils-python package, which includes the semanage tool.

To add a new port to the SELinux configuration, use this command:

sudo semanage port -a -t http_port_t -p tcp [PORT_NUMBER]

Replace [PORT_NUMBER] with the port you want to allow for Apache.

Tip: Verify Port Addition

After adding a new port, you can verify if it was successfully added to the SELinux configuration by running:

sudo semanage port -l | grep http_port_t

This command will list all the ports associated with the http_port_t type, including your newly added port.

Step-by-Step Solution

  1. Check current SELinux port settings: To see the current SELinux port settings for Apache, run:

    sudo semanage port -l | grep http_port_t

    This command lists all the ports allowed for Apache.

  2. Add the new port to SELinux rules: Use the semanage command to add the new port. For example, to add port 88:

    sudo semanage port -a -t http_port_t -p tcp 88
  3. Restart the Apache service: After adding the new port, restart Apache to apply the changes:

    sudo systemctl restart httpd

Additional Considerations

Temporary vs Permanent SELinux Rule Changes

Using semanage for permanent changes: The semanage command makes permanent changes to SELinux rules. These changes stay active after system reboots. This method works well for long-term setups where Apache needs to bind to specific non-standard ports.

Options for temporary changes: For temporary changes, you can use the 'setenforce' command to switch SELinux to permissive mode:

sudo setenforce 0

This command turns off SELinux enforcement for a short time, letting Apache bind to any port. This setting goes back to the default enforcing mode after a system reboot.

Tip: Security Risk

Switching SELinux to permissive mode lowers system security. Use this method only for testing and avoid it in production.

Security Implications

Balancing ease and security: Adding non-standard ports to SELinux rules for Apache gives more options but can reduce security. Each added port increases the risk of threats. Always compare the benefits of using non-standard ports with the higher security risk.

Best practices for non-standard port usage:

  1. Add only ports that your applications need.
  2. Check your SELinux port settings often to remove unused ports.
  3. Use strong firewall rules to limit access to non-standard ports.
  4. Check logs for unusual activity on these ports.
  5. Keep your Apache server and related software up to date to reduce potential risks.

Alternative Approaches

Using Apache with Different SELinux Contexts

Changing the SELinux context for Apache: You can change the SELinux context for Apache instead of modifying SELinux port rules. This method involves giving a different SELinux context to the Apache process, letting it bind to different ports.

To change the SELinux context for Apache:

  1. Create a custom SELinux policy module:

    ausearch -c 'httpd' --raw | audit2allow -M my-httpd
  2. Load the new policy module:

    semodule -i my-httpd.pp

Pros of this method:

  • Apache can bind to any port without changing individual port rules.
  • It offers a flexible solution for complex setups.

Cons of this method:

  • It may give more permissions than needed, which could reduce security.
  • It needs advanced knowledge of SELinux policies.
  • Fixing issues can be harder if problems occur.

Tip: Verify SELinux Context

After changing the SELinux context for Apache, verify the new context using the following command:

ps auxZ | grep httpd

This will show the SELinux context associated with the Apache process, helping you confirm that the changes were applied correctly.