Rocky Linux 10 Tutorial / SELinux custom paths, ports and booleans

To allow new paths, ports, and connections for services while maintaining SELinux on Rocky Linux 10, set the default type for file paths, the type for receiving ports, and the required conditional policies respectively. File path mappings are registered with semanage fcontext and then applied with restorecon, receiving ports are managed with semanage port, and policy booleans are adjusted with setsebool after confirming their purpose.

Distinguish file, receiving port, and connection permissions

SELinux Mode·Context·AVC Diagnosis distinguished between regular file permissions and SELinux types. This time, the web content is placed in /srv/lf-web18, and Apache is configured to receive on 127.0.0.1:18081. Afterwards, some requests are forwarded to the backend 127.0.0.1:18082 on the same VM.

Three separate actions require permission: binding a listening port, reading content files, and opening a connection to the backend. Allowing one action does not automatically allow the others. Firewall permissions are also separate from these settings.

Configuration Target of this practice Actual results to check
Port type mapping TCP 18081 that Apache will listen on Whether the service starts and listens on the given address
File path mapping and labels /srv/lf-web18 and its subfiles Check if 200 and the test string appear in the web file request
Conditional policy boolean httpd_can_network_connect Check if Apache can connect to a normal backend

A boolean is a policy condition that can be turned on or off, like true or false. Don’t just enable it by name; check what actions and scope it affects. The connection boolean in this article is not a rule that only allows one test backend address, so in actual operation, you should also consider narrower policy options according to service purposes.

Prepare tools and sources on a dedicated VM

Only proceed on a practice VM that does not have an existing web service. This VM had Apache installed in the previous lecture, which was stopped and set to not start automatically, and SELinux was Enforcing. All web requests are executed inside the VM without adding external firewall ports or host port forwarding. Requesting 127.0.0.1:18081 from a Windows terminal is not the same test.

getenforce
systemctl is-active httpd
systemctl is-enabled httpd
sudo ss -lntp
sudo dnf install policycoreutils-python-utils
rpm -q httpd policycoreutils-python-utils selinux-policy-targeted
sudo semanage port -l -C
sudo semanage fcontext -l -C
sudo semanage boolean -l -C
getsebool httpd_can_network_connect

The source lab used Rocky Linux 10.2, Apache 2.4.63-13.el10_2.8, policycoreutils-python-utils-3.10-2.el10_2, selinux-policy-targeted-42.1.18-4.el10_2.4. The source installation added the management tools and seven dependency packages. -l -C only retrieves items that were added or modified locally. There were no local custom items in this VM at the beginning, and the connection boolean was off.

If there is already a configuration or active web service using the same path, port, and policy, stop this procedure and use a separate dedicated environment. The port number must not only be empty but also not conflict with existing SELinux mappings. Backup paths also do not overwrite existing items.

sudo bash <<'BASH'
set -eu
backup=/root/selinux-lab18-before
test ! -e "$backup"
test ! -e /srv/lf-web18
test ! -e /etc/httpd/conf.d/lf-course18.conf
mkdir -m 700 "$backup"
cp -a /etc/httpd "$backup/httpd"
semanage export > "$backup/local-policy.txt"
getsebool httpd_can_network_connect > "$backup/network-boolean.txt"
BASH

This backup is for comparison and selective restoration purposes. Do not blindly import a whole policy backup during recovery, because that can overwrite changes made by other administrators. In the source verification, SELinux configuration files, firewall files, and runtime policies were also separately preserved, organized, and compared.

Configure the Apache path and loopback receiving address.

Open the default settings with sudoedit /etc/httpd/conf/httpd.conf and change the active Listen 80 line to the following value. Listen Existing service configurations with multiple of these or other virtual hosts are not the target of this example.

Listen 127.0.0.1:18081

The following is the full content of the new file /etc/httpd/conf.d/lf-course18.conf. It will be written as sudoedit. Also, predefine the reverse proxy that forwards requests to the backend, but until the backend practice, only request the default path /.

ServerName localhost
DocumentRoot "/srv/lf-web18"
<Directory "/srv/lf-web18">
    AllowOverride None
    Require all granted
</Directory>
ProxyRequests Off
ProxyPass "/backend/" "http://127.0.0.1:18082/" retry=0
ProxyPassReverse "/backend/" "http://127.0.0.1:18082/"

Require all granted is Apache's access configuration for this path and does not bypass SELinux or standard permissions. A reverse proxy is a feature where Apache forwards requests to a specified backend. As explained in the official Apache mod_proxy documentation, there is no need to create ProxyRequests On just for this function. retry=0 specifies not to delay backend retries in this comparison.

sudo bash <<'BASH'
set -eu
test ! -e /srv/lf-web18
mkdir -m 755 /srv/lf-web18
printf 'selinux-path18-ok\n' > /srv/lf-web18/index.html
chmod 644 /srv/lf-web18/index.html
restorecon -v /etc/httpd/conf.d/lf-course18.conf
BASH
sudo httpd -t
sudo httpd -M | grep -E 'proxy(_http)?_module'
ls -ldZ /srv/lf-web18 /srv/lf-web18/index.html

Check that the configuration syntax Syntax OK and the required modules for proxy proxy_module and proxy_http_module are loaded. If a module is missing or there is a syntax error, resolve it before changing the policy. The general permissions for the new path and file are 755 and 644, but the initial type in this environment was var_t.

Map the incoming port type and compare the startup results.

First, read the list of ports allowed by Apache and the current receiving status. The TCP http_port_t list of this VM was 80, 81, 443, 488, 8008, 8009, 8443, 9000, and 18081 was not included. Do not change numbers that already belong to other port types as they are.

sudo semanage port -l | grep '^http_port_t'
sudo systemctl start httpd
sudo systemctl status httpd --no-pager
sudo journalctl -u httpd --since '5 minutes ago' --no-pager
sudo ausearch --input-logs -m AVC,USER_AVC -ts recent -c httpd -i

The actual start before mapping failed. The journal showed could not bind to address 127.0.0.1:18081, and AVC recorded name_bind denial, source service httpd_t, destination unreserved_port_t, and port 18081. Even if the configuration syntax check succeeds, it does not mean that port usage is allowed during execution.

Add one number that did not have a separate service mapping used in this practice to http_port_t. semanage port official manual explains the mapping of port numbers to SELinux port types.

sudo semanage port -a -t http_port_t -p tcp 18081
sudo semanage port -l -C
sudo systemctl start httpd
sudo ss -lntp '( sport = :18081 )'
curl --noproxy '*' --max-time 5 -sS -o /dev/null \
  -w 'HTTP %{http_code}\n' http://127.0.0.1:18081/

Now the service has started and received on 127.0.0.1:18081. However, the web response is still 403. This is because only the port binding issue has been resolved, and the file path type has not yet been matched. semanage port is not a command to open firewall ports and does not change the receiving configuration of the server program.

If an error appears that it is already defined when adding it, first check other mappings. To eliminate the error, do not immediately change -a to -m or reassign the port for the existing service.

Register the default path and apply it to the actual label.

Register only /srv/lf-web18 and its subpaths as read-only web content types. The regular expression (/.*)? includes the directory itself and the paths under it. Enclose it in single quotes so the shell does not interpret the pattern.

sudo semanage fcontext -a -t httpd_sys_content_t '/srv/lf-web18(/.*)?'
sudo semanage fcontext -l -C
matchpathcon /srv/lf-web18/index.html
ls -ldZ /srv/lf-web18 /srv/lf-web18/index.html
curl --noproxy '*' --max-time 5 -sS -o /dev/null \
  -w 'HTTP %{http_code}\n' http://127.0.0.1:18081/

This step only registers the default mapping for the path. It does not automatically change existing file labels. In the source test, matchpathcon showed the new expected type, but the file still had var_t and requests continued to return 403.

sudo restorecon -Rv /srv/lf-web18
ls -ldZ /srv/lf-web18 /srv/lf-web18/index.html
curl --noproxy '*' --max-time 5 -sS -w 'HTTP %{http_code}\n' \
  http://127.0.0.1:18081/

After applying the label, the type became httpd_sys_content_t, and the responses included selinux-path18-ok and HTTP 200. Since the registered path rules still remain, subsequent restorecon will also use this default. In practice, when only the test file was changed back to var_t and restorecon was executed, it returned to the new web type and a 200 response was confirmed.

The official semanage fcontext manual explains that local mappings take precedence over policy-module mappings, and local entries are evaluated newest first. Adding an unnecessarily broad pattern such as /srv(/.*)? can affect other services. Do not grant a writable type to content that only needs to be read.

Check the backend status and compare the connection boolean.

Open another terminal for a regular user on the same VM and run the following example. If the practice folder in the home directory already exists, stop. Keep this terminal open while the backend server is running, and press Ctrl+C to exit. The Python 3.12 default HTTP server is for this local test and is not configured to be deployed as a production web server.

bash <<'BASH'
set -eu
dir="$HOME/lf-web18-backend"
test ! -e "$dir"
mkdir -m 755 "$dir"
printf 'backend18-ok\n' > "$dir/index.html"
chmod 644 "$dir/index.html"
python3 -m http.server 18082 --bind 127.0.0.1 --directory "$dir"
BASH

From another VM terminal, make a direct request to the backend. If the result is not 200, check the process, address, port, and files before changing the boolean.

curl --noproxy '*' --max-time 5 -sS -w 'HTTP %{http_code}\n' \
  http://127.0.0.1:18082/
getsebool httpd_can_network_connect
curl --noproxy '*' --max-time 5 -sS -o /dev/null \
  -w 'HTTP %{http_code}\n' http://127.0.0.1:18081/backend/
sudo ausearch --input-logs -m AVC,USER_AVC -ts recent -c httpd -i

In verification, a direct request returned backend18-ok and 200, but the Apache-passed request with the boolean off was 503. The AVC recorded a name_connect denial for the destination port 18082. This behavior is different from the denial on the receiving port name_bind.

In this private practice, where the original value is off, the current boolean is turned on for comparison and then immediately turned off. This value expands the range of network connections allowed by Apache, so it is not used as a universal switch to fix static file 403 errors.

sudo setsebool httpd_can_network_connect on
getsebool httpd_can_network_connect
curl --noproxy '*' --max-time 5 -sS -w 'HTTP %{http_code}\n' \
  http://127.0.0.1:18081/backend/
sudo setsebool httpd_can_network_connect off
getsebool httpd_can_network_connect
curl --noproxy '*' --max-time 5 -sS -o /dev/null \
  -w 'HTTP %{http_code}\n' http://127.0.0.1:18081/backend/
curl --noproxy '*' --max-time 5 -sS -w 'HTTP %{http_code}\n' \
  http://127.0.0.1:18082/

When on, the proxy response changed to backend18-ok and 200, and when restored to off, it returned to 503. The last direct request was still 200, which allowed us to differentiate it from a backend-down failure. In the source automated verification, the same Python server was run and stopped using a one-time systemd unit dedicated to the practice, instead of the foreground terminal.

The setsebool manual explains that without -P, the change affects the current value and is not saved for boot. Consider persistence only after reviewing the required scope. For example, sudo setsebool -P httpd_can_network_connect on saves the setting, but the source comparison did not execute that permanent change. Preserve the original value and local customizations on an existing system.

Remove only the policies that were changed and compare with the original state.

Stop the backend server with Ctrl+C in its terminal, then stop Apache. The source environment originally had no local path or port mappings and its connection boolean was off. The commands below remove only entries added by this exercise . Do not use --deleteall to delete all local customizations.

sudo systemctl stop httpd
getsebool httpd_can_network_connect
sudo semanage fcontext -d '/srv/lf-web18(/.*)?'
sudo restorecon -Rv /srv/lf-web18
sudo semanage port -d -p tcp 18081
sudo semanage port -l -C
sudo semanage fcontext -l -C
sudo semanage boolean -l -C

After removing the path rules, you also need to restore the actual labels so that existing files align with the new defaults. This path has now returned to var_t. For port deletion, only remove the local mapping you added at 18081. If the environment had modifications to existing items, you should restore the original mapping you recorded, rather than just delete it.

Check the contents and paths of files before removing them. Make sure the new Apache configuration file matches the practice content described above before deleting, and verify whether the differences in the main configuration are only a single line in Listen.

cat /srv/lf-web18/index.html
cat "$HOME/lf-web18-backend/index.html"
sudo cat /etc/httpd/conf.d/lf-course18.conf
sudo diff -u /root/selinux-lab18-before/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf

Execute the following cleanup only after confirming that all new files are for this practice and there are no other changes in the main configuration. rmdir must have an empty directory to succeed; if there are unexpected other files, stop as is and check.

sudo cp -a /root/selinux-lab18-before/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf
sudo rm -- /etc/httpd/conf.d/lf-course18.conf
sudo rm -- /srv/lf-web18/index.html
sudo rmdir /srv/lf-web18
rm -- "$HOME/lf-web18-backend/index.html"
rmdir "$HOME/lf-web18-backend"
sudo diff -r --no-dereference /root/selinux-lab18-before/httpd /etc/httpd
sudo semanage export | sudo cmp - /root/selinux-lab18-before/local-policy.txt
sudo ss -lntp '( sport = :18081 or sport = :18082 )'
systemctl is-active httpd
systemctl is-enabled httpd
getenforce

After cleanup in the source lab, the Apache configuration matched its backup, including symbolic link targets, and the complete local policy export matched the original. Ports 18081·18082 and the practice files disappeared, and Apache was stopped with automatic start disabled. The added management tool packages were left, and SELinux was in Enforcing mode, with SSH, audit services, and the original firewall maintained. --no-dereference was used to compare without following the relative symbolic links in the backup to their actual targets.

Use file labels for the custom content path, port types for listening permission and the relevant connection policy for the backend. Match the failed operation to its AVC action, change only the required policy, and repeat the same request to establish what resolved the failure.