Founding prices until 15 December: 30% off Pro, locked for life. See pricing

SELinux for the RHCSA: contexts, ports and booleans, practised on a VM

The SELinux tasks the RHCSA expects, in the order you'd meet them: modes, file contexts, a web server on a new port, booleans and reading denials.

LinuxUpdated 27 November 20262 min read

SELinux questions on the RHCSA almost always look like this: a service is configured correctly but can't read its files or bind to its port, and the fix is a label, a port type or a boolean, never switching SELinux off. Practise each step below on a Rocky Linux 9 VM (how to build one). BrokenCluster's lab machines don't run SELinux, so this part needs your own VM.

1. Modes

getenforce                     # Enforcing
sestatus
sudo setenforce 0              # permissive until the next boot
sudo setenforce 1
sudo vim /etc/selinux/config   # SELINUX=enforcing  (what the next boot uses)

Moving from disabled to enforcing needs a reboot and a full relabel (touch /.autorelabel). Keep it enforcing for every exercise below.

2. File contexts: a new web root

Install Apache, then serve from /srv/web instead of /var/www/html:

sudo dnf install -y httpd
sudo mkdir /srv/web && echo hello | sudo tee /srv/web/index.html
sudo sed -i 's|/var/www/html|/srv/web|g' /etc/httpd/conf/httpd.conf
sudo systemctl enable --now httpd
curl localhost                       # 403 Forbidden
ls -Zd /srv/web                      # var_t: Apache may not read it

Change the default label for the path, then apply it:

sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
sudo restorecon -Rv /srv/web
curl localhost                       # hello

"/srv/web(/.*)?" covers the directory and everything below it. Check the rule with semanage fcontext -l | grep /srv/web.

Variation to practise: create a file in your home directory and mv it into /srv/web. It keeps its old label (user_home_t), and Apache can't read it until you run restorecon on it.

3. A service on a new port

sudo sed -i 's/^Listen 80$/Listen 82/' /etc/httpd/conf/httpd.conf
sudo systemctl restart httpd         # fails
sudo ausearch -m AVC -ts recent      # denied { name_bind } ... port 82
sudo semanage port -l | grep http_port_t
sudo semanage port -a -t http_port_t -p tcp 82
sudo systemctl restart httpd
sudo firewall-cmd --permanent --add-port=82/tcp && sudo firewall-cmd --reload

The port type lets the service bind; the firewall rule lets clients in. The exam usually needs both. You can practise the firewall half in the browser: Publish a web page through the firewall.

4. Booleans

Booleans switch prepared parts of the policy on or off:

getsebool -a | grep httpd
sudo setsebool -P httpd_enable_homedirs on      # -P: also after reboot
semanage boolean -l | grep homedirs             # what it does

Without -P the change is lost at the next boot. On the exam, assume every change must survive a reboot.

5. Reading denials

sudo ausearch -m AVC -ts recent
sudo journalctl -t setroubleshoot      # with setroubleshoot-server installed
sudo sealert -a /var/log/audit/audit.log

sealert explains each denial in plain words and suggests the semanage, restorecon or setsebool command. Read its suggestion, but never blindly run the audit2allow module it offers last: that writes a new policy rule, and on the exam it's almost never the intended fix.

Drill

Snapshot the VM, then repeat until each takes under five minutes: a new web root, a new port, a home-directory site with its boolean. Then answer the SELinux quiz questions on BrokenCluster to check the "why".

Questions

Should I set SELinux to permissive to make a task work?
No. On the exam, tasks are checked with SELinux enforcing, and permissive hides the problem instead of fixing it. Find the denial with ausearch and fix the label, port or boolean.
What is the difference between chcon and semanage fcontext?
chcon changes a file's label now; the next restorecon or relabel puts the default back. semanage fcontext changes the default itself, and restorecon applies it. For anything that must survive, use semanage fcontext and restorecon.
How do I know which port types exist?
semanage port -l lists every port type with its ports. For a web server look at http_port_t; for SSH, ssh_port_t.

Fix it yourself

A live machine in your browser, already broken. The grader checks the result.

Start the first lab

No account needed for your first tries.

More guides