Log inSign up

Work with SELinux instead of switching it off

Rocky Linux 9 and AlmaLinux 9 boot with SELinux in enforcing mode, and it should stay that way. When a service is blocked, run sudo ausearch -m AVC -ts recent | audit2why to see the reason, then fix the label with semanage fcontext plus restorecon, add a port with semanage port, or enable a boolean with setsebool -P. Those three tools solve almost every denial on a typical web or app server.

Topic: Server basics · Tested on: Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What does SELinux add on top of normal permissions?

SELinux is a mandatory access control layer in the kernel. Ordinary file permissions decide what a user may do; SELinux additionally decides what each program may do, based on labels attached to processes, files and ports. Nginx running as the httpd_t domain may read files labelled httpd_sys_content_t and bind ports labelled http_port_t, and nothing else, even if it runs as root.

The value shows up when something goes wrong. A compromised web application cannot read your SSH keys or open a shell on a random port, because the policy never allowed the web server domain to do that. This is why the guide recommends learning a few commands rather than disabling the system. Both Rocky Linux 9 and AlmaLinux 9 images ship with the targeted policy active.

How do I check the current mode?

Use getenforce for a one word answer and sestatus for detail:

getenforce
sestatus

There are three modes. Enforcing blocks and logs violations. Permissive only logs them, which is useful for a short diagnosis. Disabled turns labelling off entirely. You can switch between enforcing and permissive at runtime without a reboot:

sudo setenforce 0    # permissive until reboot
sudo setenforce 1    # back to enforcing

The boot time mode is set by SELINUX= in /etc/selinux/config. Leave it at enforcing. On version 9 the value disabled in that file no longer fully switches SELinux off, which is one more reason to keep it on and fix labels instead.

Which tools should I install first?

The minimal image includes the core commands, but semanage, audit2why and audit2allow live in a separate package. Install it along with the troubleshooting helper:

sudo dnf install policycoreutils-python-utils setroubleshoot-server
semanage --help | head -n 5

The setroubleshoot-server package adds sealert, which turns raw audit entries into readable suggestions. The audit daemon auditd is already running on a standard install; check with systemctl status auditd.

How do I read security contexts?

Add -Z to familiar commands to show labels. A context has four parts: user, role, type and level. For the targeted policy only the type matters in daily work.

ls -Z /var/www/html
ps -eZ | grep nginx
id -Z
sudo ss -tlnpZ

A file under /var/www/html normally shows httpd_sys_content_t. If you created content in your home directory and then moved it with mv, it keeps the home label user_home_t, and the web server gets a permission error although the Unix modes look correct. Copying with cp picks up the label of the destination, moving does not.

How do I label a custom directory correctly?

Register a rule with semanage fcontext, then apply it with restorecon. The rule is stored in the policy, so it survives a full relabel. Here a site is served from /srv/app:

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

If the application must write to a directory, such as an uploads or cache folder, use the read write type for that subfolder only:

sudo semanage fcontext -a -t httpd_sys_rw_content_t "/srv/app/uploads(/.*)?"
sudo restorecon -Rv /srv/app/uploads

Avoid chcon for permanent fixes. It changes the label right away, but a later restorecon or relabel resets it. List your own rules with sudo semanage fcontext -l -C.

How do I allow a service on a non standard port?

Ports carry labels too. A web server on 8081 or SSH on 2222 is refused until the port has the right type. Inspect and add labels like this:

sudo semanage port -l | grep -E "^(http_port_t|ssh_port_t)"
sudo semanage port -a -t http_port_t -p tcp 8081
sudo semanage port -a -t ssh_port_t -p tcp 2222

If the port already belongs to another type, use -m instead of -a to modify it. Remember that the firewall is separate: open the same port with sudo firewall-cmd --permanent --add-port=8081/tcp and sudo firewall-cmd --reload, as covered in the firewalld guide.

What are booleans and when do I use them?

Booleans are on or off switches built into the policy for common needs, so you do not have to write rules. List them, read the description, then set one persistently with -P:

getsebool -a | grep httpd
sudo semanage boolean -l | grep httpd_can_network_connect
sudo setsebool -P httpd_can_network_connect on

Typical examples: httpd_can_network_connect lets Nginx or Apache proxy to an app on another port, httpd_can_network_connect_db allows a remote database, httpd_can_sendmail lets PHP send mail, and httpd_enable_homedirs serves user directories. Without -P the change is lost at the next boot.

How do I find out why something was denied?

Every denial is written to the audit log as an AVC record. Search recent ones and pass them to audit2why, which states whether a boolean, a label or a missing rule is the cause:

sudo ausearch -m AVC,USER_AVC -ts recent
sudo ausearch -m AVC -ts recent | audit2why
sudo sealert -a /var/log/audit/audit.log | less

Most results point to one of the fixes above. When nothing fits, for example a custom daemon in an unusual path, you can build a small local module from the denials. Read the generated rules before loading them, because they allow exactly what was blocked:

sudo ausearch -m AVC -ts recent | audit2allow -M myapp-local
cat myapp-local.te
sudo semodule -i myapp-local.pp

To debug one service without opening the whole system, make only its domain permissive with sudo semanage permissive -a httpd_t, and remove that again with -d once fixed.

What if labels are wrong across the whole disk?

After restoring files from an archive, or if SELinux was once disabled, labels may be missing everywhere. Ask for a complete relabel at the next boot:

sudo touch /.autorelabel
sudo reboot

The relabel takes a few minutes on a small server and then reboots again by itself. Plan it in a quiet moment, as explained in rebooting safely.

Questions about SELinux on Rocky Linux and AlmaLinux

Should I disable SELinux to get my app working?

No. Use audit2why to find the cause, then fix the label, port or boolean. Disabling removes a strong protection and makes later re-enabling slow, because every file must be relabelled.

Does SELinux slow the server down?

The checks run in the kernel and cost very little for normal workloads. You will not notice them on a web, database or app server.

Does Ubuntu or Debian use SELinux?

Ubuntu and Debian use AppArmor by default, which works with per program profiles instead of labels. This guide applies to Rocky Linux, AlmaLinux and CentOS Stream.

Why does my container volume give permission denied?

Podman and Docker need the volume relabelled. Add :Z for a private volume or :z for a shared one to the mount option, and the runtime sets a container label.

How do I undo a semanage rule?

Use the same command with -d, for example sudo semanage fcontext -d "/srv/app(/.*)?" or sudo semanage port -d -t http_port_t -p tcp 8081, then run restorecon on the path.