SELinux is an extra protection layer of AlmaLinux and Rocky Linux: every service may only use the files, ports and connections meant for it. If an attacker takes over your web server, SELinux stops them from reading your SSH keys or other private files. The price: some steps from guides written for Ubuntu need one more command here. This page shows whether SELinux is on, how to switch it on, how to recognise a block and the usual fixes. Everything was tested on a dedicated server with AlmaLinux 9 and in test machines with AlmaLinux 10 and Rocky Linux 9; run the commands as root.


Is SELinux On?

getenforce
  • Enforcing: SELinux is on and blocks what is not allowed.
  • Permissive: SELinux only writes down what it would block.
  • Disabled: SELinux is off. A dedicated server installed with AlmaLinux 9 from the Client Area has it off after the installation. Nothing on this page then applies; the SELinux commands in our other guides change nothing and give no error.

Switch SELinux On

When SELinux has been off, every file first needs its label. Switch it on in the mode that only writes down, let the system label all files, and restart:

sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
touch /.autorelabel
reboot

During this start the server labels all its files and then restarts once more by itself. With a fresh installation, it answers SSH again after about 9 minutes; with many files it takes longer. Then check whether SELinux would have blocked anything:

getenforce
ausearch -m avc -ts boot

The first line answers Permissive. If the second line shows no denied lines (for example <no matches>), switch blocking on, at once and for every later start:

setenforce 1
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config

SSH, firewalld, Docker and the network keep working with SELinux on, also after a restart. If denied lines appear, fix their causes with the steps below before you switch blocking on.

1. Is SELinux the Cause?

Typical signs: a program reports Permission denied although the file permissions are right, a website shows 403 Forbidden or 502 Bad Gateway, or a service does not listen on the port you set. If getenforce answers Enforcing, look at the audit log, where every block is written. Show the blocks of the last ten minutes:

ausearch -m avc -ts recent

Look for denied, the program after comm= and the file or port. Examples:

Line in the log (shortened)What happenedFix
denied { read } comm="nginx" name="moved.html" … tcontext=…admin_home_tNginx may not read a file that was moved from /rootStep 2
denied { name_bind } comm="sshd" src=2222SSH may not use port 2222Step 3
denied { name_connect } comm="nginx" dest=8000Nginx may not connect to a program on port 8000Step 4
denied { execute } comm="(run.sh)" … tcontext=…user_home_tA service may not start a script from a home folderStep 5

No denied lines at all? Then SELinux did not cause the problem; look in the program's own log, for example with journalctl -u nginx.

2. Files Have the Wrong Label

Every file has an SELinux label, shown by ls -Z. A web server may read only files labelled httpd_sys_content_t. New files usually get the label of their folder, but a file moved with mv keeps its old one. Give a folder the labels that belong there:

restorecon -Rv /var/www/mysite

restorecon prints every file it relabels. For your own folder outside the usual places, for example /srv/mysite, first tell SELinux once what the folder is, then relabel it:

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

A website in /srv/mysite gives 403 Forbidden before these two lines and works after them. If semanage is not found: dnf install policycoreutils-python-utils.

3. A Service on Another Port

Each kind of service may use only its own ports. semanage port -l | grep ssh_port_t lists those of SSH, semanage port -l | grep http_port_t those of web servers. Add a port to the list:

semanage port -a -t ssh_port_t -p tcp 2222

The same for a website on port 8090: semanage port -a -t http_port_t -p tcp 8090. Nginx with listen 8090; does not start before this line (bind() to 0.0.0.0:8090 failed (13: Permission denied)) and works after it. The whole procedure for SSH: Change the SSH Port on AlmaLinux and Rocky Linux. Remove the port again with -d instead of -a.

4. Switches for Common Needs

SELinux has on/off switches (booleans) for things many people need. getsebool -a | grep httpd lists those of web servers. The one you need most often lets Nginx or Apache connect to other programs, for example to an app behind Nginx:

setsebool -P httpd_can_network_connect 1

-P keeps the setting after a restart. See Website With HTTPS on AlmaLinux and Rocky Linux.

5. Scripts Started by a Service

A systemd service may not start a program or script that lies in a home folder. A service with ExecStart=/home/alex/myapp/run.sh fails at once, with the execute block shown above. Keep such scripts in /usr/local/bin:

mv /home/alex/myapp/run.sh /usr/local/bin/myapp-run
restorecon -v /usr/local/bin/myapp-run

and write ExecStart=/usr/local/bin/myapp-run in the service. A service that runs a program from /usr/bin, for example python3, may keep its data and working folder in a home folder: a service with WorkingDirectory=/home/alex/myapp and ExecStart=/usr/bin/python3 -m http.server 8000 works. How to write a service: Keep a Program Running on Linux.

Do Not Switch SELinux Off

To find out quickly whether SELinux is the cause, you can let it only log for a moment instead of blocking:

setenforce 0
setenforce 1

Run the first line, try again, and run the second line right after. If it works only with SELinux in this mode (getenforce shows Permissive), fix the cause with one of the steps above. Do not switch SELinux off for good in /etc/selinux/config: you lose its protection for every service on the server.


Related Guides

View all AlmaLinux and Rocky Linux guides

Was this answer helpful? 0 Users Found This Useful (0 Votes)