Home Rocky LinuxSELinux on Rocky Linux 9: Modes, Contexts, Booleans and Troubleshooting

SELinux on Rocky Linux 9: Modes, Contexts, Booleans and Troubleshooting

by Knowledge Base Linux
SELinux on Rocky Linux 9 guarding a server with shield layers

SELinux is the security layer that most often frustrates new RHEL-family administrators — and the one most often disabled in frustration, which is exactly the wrong move. This guide explains SELinux on Rocky Linux 9 the practical way: what the modes do, how file contexts and port labels work, how booleans let services do more, and how to diagnose a denial from the audit log instead of turning the whole system off.

What SELinux on Rocky Linux 9 Actually Does

Standard Linux permissions (DAC) decide access by user and group. SELinux adds Mandatory Access Control (MAC): every process and file carries a label such as system_u:object_r:httpd_sys_content_t:s0, and a policy decides which process types may access which file types. Even if an attacker compromises your web server, SELinux confines the process to web content — it cannot read /etc/shadow or your home directories because the policy simply has no rule for it.

Modes: Enforcing, Permissive, Disabled – SELinux on Rocky Linux 9

getenforce
sudo setenforce 0     # permissive until reboot (diagnostics only)
sudo setenforce 1     # back to enforcing
sestatus
  • Enforcing: policy is applied and violations are denied and logged. This is the default and where you should stay.
  • Permissive: violations are logged but allowed — the debugging mode.
  • Disabled: set only in /etc/selinux/config; files stop being labelled, and re-enabling later requires a full relabel. Avoid it.

To make a mode permanent, edit SELINUX= in /etc/selinux/config and reboot.

File Contexts: Labels Are Everything – SELinux on Rocky Linux 9

Most “SELinux broke my site” stories are really “the files have the wrong label”. Inspect and fix labels with:

ls -Z /var/www/html
sudo restorecon -Rv /var/www/html

restorecon resets labels to the policy defaults. When you move content to a non-standard path, teach the policy the intended label permanently:

sudo semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"
sudo restorecon -Rv /srv/www

Use chcon only for quick tests — its changes do not survive a relabel. The semanage tool lives in the policycoreutils-python-utils package if it is missing. This workflow is exactly what we use in the LEMP on Rocky Linux 9 and Apache PHP-FPM guides.

Access control checkpoint in SELinux on Rocky Linux 9

Booleans: Policy Switches for Services

Booleans are on/off switches in the policy that let a service do extra things without writing custom rules:

getsebool -a | grep httpd
sudo setsebool -P httpd_can_network_connect 1
sudo setsebool -P httpd_enable_homedirs 1

The -P flag makes the change persistent across reboots. Examples: web apps that call external APIs need httpd_can_network_connect; apps that connect to databases on other hosts need httpd_can_network_connect_db.

Port Labels: Why a Custom SSH Port Fails

SELinux also labels network ports. If you move SSH to port 2222 and the daemon refuses to start, the port simply is not labelled for SSH:

sudo semanage port -l | grep ssh
sudo semanage port -a -t ssh_port_t -p tcp 2222
sudo systemctl restart sshd

The same pattern applies to running a web server on port 8080 (http_port_t) or a database on a custom port. We use this exact fix in the Rocky Linux 9 server setup guide.

Reading Denials in the Audit Log

When something is blocked, the answer is in /var/log/audit/audit.log:

sudo ausearch -m avc -ts recent
sudo ausearch -m avc -ts recent | audit2why

audit2why translates the raw denial into plain language and usually tells you the exact boolean or context to fix. Generating a custom policy module with audit2allow is possible but should be the last resort — it can silently open holes you did not intend.

Relabelling After Major Changes

If labels are badly damaged — for example after restoring files from a backup without contexts — force a full relabel at next boot:

sudo touch /.autorelabel
sudo reboot

The reboot takes longer while every file is relabelled from policy. Afterwards, check that the system returned to enforcing with getenforce.

Troubleshooting Checklist

  • Service fails only when SELinux is enforcing: read the AVC denial first — it names the process type, the target type and the missing permission.
  • It works in permissive mode: that proves SELinux is the blocker; fix the label or boolean rather than staying in permissive.
  • Labels keep reverting: something (often a deployment script or rsync without -X) is copying files without contexts; re-run the semanage fcontext + restorecon pair after deployments.

Users, Roles and Your Login Context

Files are only half of the story; users and processes carry contexts too. Check your own login context and the context of running processes:

id -Z
ps -eZ | head
sudo semanage user -l

On a default Rocky Linux 9 server, interactive users map to the unconfined_u SELinux user, while system services run confined under types such as httpd_t or mysqld_t. That split is the whole point: the confined service types are where SELinux earns its keep, so you rarely need to touch user mappings on a typical web or database server.

Conclusion

SELinux on Rocky Linux 9 stops being mysterious once you think in labels: match the file context, open the right boolean, label your custom ports, and read the audit log when something is denied. The reward is a genuine second wall around every service. The Rocky Linux documentation and the guides at kblinux.com give you plenty of next steps for putting these skills to work.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.