Skip to content
Visual explainer 5h to learn 6 steps

SELinux basics

Diagnose and fix SELinux denials with contexts, booleans and port labels.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 11 minutes to read.

Topic 29 of 42

In short

SELinux adds mandatory access control on top of normal permissions: every process and file has a security context, and policy decides which process types may touch which file types. Most problems come from wrong file labels, a disabled boolean or a non-standard port. The fix is to correct the label or policy setting, never to disable SELinux.

Explained simply

Normal Linux permissions ask one question: is this user allowed to touch this file? SELinux adds a second, independent question: is this kind of program allowed to touch this kind of file? Both answers must be yes.

SELinux puts a label on every process, file and network port. The web server runs with the label httpd_t. Web pages carry the label httpd_sys_content_t. A rule in the policy says httpd_t may read httpd_sys_content_t. Files in home folders carry a different label, so the web server cannot read them, even with mode 777.

This matters when a program is hacked. An attacker who takes over the web server is still stuck inside httpd_t. They cannot read SSH keys, change passwords or install services, because the policy has no rule allowing it.

When something breaks with Permission denied but the file modes look right, SELinux is a likely reason. The fix is almost always one of three things: give the files the correct label, turn on a boolean (a policy switch), or label a new port. Turning SELinux off is never the answer.

Press Play on a diagram to walk through it one step at a time.

MapAnatomy of a context
system_u:object_r:httpd_sys_content_t:s0
  • User: system_u
    • SELinux user
  • Role: object_r
    • Role for files
  • Type: httpd_sys_content_t
    • What targeted policy checks
    • httpd_t may read this type
  • Level: s0
    • MLS/MCS sensitivity
FlowFixing a denial
  1. 1
    Symptom

    403 or permission denied despite correct modes.

  2. 2
    Confirm

    getenforce; ausearch -m AVC -ts recent.

  3. 3
    Find the mismatch

    Wrong file type, boolean off, or unlabelled port.

  4. 4
    Fix persistently

    semanage fcontext + restorecon, setsebool -P, or semanage port.

  5. 5
    Verify

    Retry with SELinux still enforcing.

FlowTwo checks for every access
  1. 1
    httpd opens a file

    Runs as user apache, type httpd_t

  2. 2
    Normal permissions

    Owner, group, other bits and ACLs

  3. 3
    SELinux policy

    Is httpd_t allowed to read this type?

  4. 4
    Both say yes

    Access granted

  5. 5
    Either says no

    Permission denied; SELinux logs an AVC

In depth

The three modes and how to switch

Enforcing blocks and logs anything the policy does not allow. Permissive allows everything but still logs what would have been blocked, which is useful for testing. Disabled turns SELinux off completely, and turning it back on later needs a full relabel of every file.

setenforce 0 and setenforce 1 switch between permissive and enforcing until the next reboot. The boot-time mode lives in /etc/selinux/config as SELINUX=enforcing. You can also make a single domain permissive while the rest stays enforcing: semanage permissive -a httpd_t. That is a much safer way to test one service. On RHEL 9, fully disabling SELinux is done with the kernel option selinux=0, not the config file alone.

  • getenforce prints Enforcing, Permissive or Disabled
  • sestatus shows mode, policy name and config file mode
  • semanage permissive -d httpd_t removes a per-domain exception

Where file labels come from

The policy holds a list of path patterns and the label each should have, for example /var/www(/.*)? gets httpd_sys_content_t. restorecon reads that list and fixes files to match. semanage fcontext -a adds your own patterns to the list, so they survive future relabels.

New files inherit the type of the folder they are created in. The big trap is mv: moving a file keeps its old label. If you create index.html in your home folder and mv it to /var/www/html, it keeps user_home_t and the web server gets a 403. cp creates a new file, so it gets the label of the destination. After any mv into a service folder, run restorecon.

  • semanage fcontext -l | grep /srv/web shows rules for a path
  • restorecon -Rnv /srv/web previews changes without making them
  • touch /.autorelabel and reboot relabels the whole filesystem
Comparechcon vs semanage fcontext + restorecon
chcon -t TYPE file
  • Changes the label right now
  • Not stored in policy
  • Lost on restorecon or relabel
  • Only for quick tests
semanage fcontext + restorecon
  • Adds a rule to the policy list
  • restorecon applies it
  • Survives relabels and reboots
  • The right way, and what exams check

Reading an AVC denial

Every block is logged as an AVC (access vector cache) message in /var/log/audit/audit.log. A typical one says: avc: denied { read } for comm="httpd" name="index.html" scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file. Read it as a sentence: the httpd process (source type httpd_t) tried to read a file whose target type is user_home_t, and was denied.

The target type tells you what to fix. If it is a wrong-looking type, fix the label. If the type is right, look for a boolean. ausearch -m AVC -ts recent filters the log, and sealert -a /var/log/audit/audit.log (from setroubleshoot-server) explains each denial in plain words with suggested commands.

MapParts of an AVC message
avc: denied { read }
  • comm="httpd"
    • Program that tried
  • scontext ... httpd_t
    • Source type: the process
  • tcontext ... user_home_t
    • Target type: the object
    • Usually the clue
  • tclass=file
    • Kind of object: file, dir, tcp_socket
  • permissive=0
    • 0 means it was really blocked

Booleans and port labels

Booleans are on or off switches built into the policy for common needs. httpd_can_network_connect lets the web server open outgoing connections, for example to an app server behind it. httpd_enable_homedirs lets it serve ~/public_html. getsebool -a lists them all, and semanage boolean -l adds a short description. setsebool -P name on changes it permanently; without -P it reverts at reboot.

Ports are labelled too. httpd may bind only ports labelled http_port_t, which by default include 80, 81, 443, 8008, 8009, 8443 and 9000. semanage port -l | grep http_port_t shows the list. To run a site on 82, add it with semanage port -a -t http_port_t -p tcp 82. If the port already has another label, use -m instead of -a.

When nothing is logged, and audit2allow

Some denials are hidden on purpose by dontaudit rules, which silence harmless noise. If something fails but ausearch shows nothing, run semodule -DB to disable dontaudit rules, reproduce the problem, read the log, then semodule -B to restore them.

audit2allow can turn denials into a custom policy module (audit2allow -a -M mypol, then semodule -i mypol.pp). Treat it as a last resort. It allows exactly what was blocked, which may be far too much, and it often hides a simple label problem. Always try restorecon, a boolean or a port label first. audit2why explains why each denial happened.

Learn it in 6 short steps

About 50m each. Track them with a timer in your plan.

  1. 1
    Switch enforcing and permissive modes~50m
  2. 2
    Read contexts with ls -Z and ps -Z~50m
  3. 3
    Restore contexts with restorecon~50m
  4. 4
    Set persistent rules with semanage fcontext~50m
  5. 5
    Toggle booleans with setsebool -P~50m
  6. 6
    Read denials with ausearch~50m

Key points

  • getenforce shows the mode; setenforce 0 switches to permissive only until reboot.
  • chcon changes are lost on relabel; use semanage fcontext then restorecon for persistence.
  • Booleans toggle policy features; -P makes setsebool persistent.
  • Non-standard ports need a label, such as semanage port -a -t http_port_t -p tcp 8080.
  • sealert, from setroubleshoot, explains denials and suggests fixes.

Key terms

Tap a card to flip it. 0/6 known

Examples

bash
getenforce
ls -Zd /srv/web; ps -eZ | grep httpd
sudo ausearch -m AVC -ts recent
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
sudo restorecon -Rv /srv/web
sudo setsebool -P httpd_can_network_connect on
sudo semanage port -a -t http_port_t -p tcp 8080

Serve content from /srv/web and on port 8080 with SELinux enforcing.

A web page moved from home gives 403

You wrote index.html in your home folder and moved it to /var/www/html with mv. The modes are 644, yet the browser shows 403 Forbidden. Step 1: compare labels with ls -Z; the file still has user_home_t. Step 2: confirm with ausearch. Step 3: run restorecon, which applies the default label for that path. No new rule is needed because /var/www already has one in the policy.

bash
ls -Z /var/www/html/index.html
# unconfined_u:object_r:user_home_t:s0 /var/www/html/index.html
sudo ausearch -m AVC -ts recent | grep index.html
sudo restorecon -v /var/www/html/index.html
# Relabeled ... from user_home_t to httpd_sys_content_t
curl -I http://localhost/    # 200 OK

httpd as a reverse proxy cannot reach the app

Apache proxies requests to an app on 127.0.0.1:8000, but users get 503 and the Apache log says permission denied on connect. Step 1: find the denial; it shows name_connect on a tcp_socket. Step 2: this is the exact case a boolean covers, so look it up. Step 3: turn it on persistently with -P. No labels or custom modules needed.

bash
sudo ausearch -m AVC -ts recent | grep name_connect
sudo semanage boolean -l | grep httpd_can_network_connect
sudo setsebool -P httpd_can_network_connect on
getsebool httpd_can_network_connect
# httpd_can_network_connect --> on
curl -I http://localhost/

Quick reference

  • ls -Z files, ps -eZ processes, id -Z your own context
  • Modes: setenforce 0 or 1 until reboot; SELINUX= in /etc/selinux/config at boot
  • semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?" then restorecon -Rv /srv/web
  • mv keeps the old label, cp takes the destination label: run restorecon after mv
  • setsebool -P name on makes a boolean permanent; getsebool -a lists all
  • semanage port -a -t http_port_t -p tcp 82 lets httpd bind port 82
  • ausearch -m AVC -ts recent finds denials; sealert -a /var/log/audit/audit.log explains them
  • semanage permissive -a httpd_t tests one service without disabling SELinux

Common mistakes

  • Disabling SELinux instead of fixing the label.
  • Using chcon, which a later relabel silently reverts.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A file was moved with mv from /home/ana to /var/www/html. What label does it have?

Question 2

2Which change survives a full filesystem relabel?

Question 3

3In an AVC message, which field usually tells you what to fix?

Question 4

4What does setsebool httpd_can_network_connect on do without -P?

Question 5

5httpd fails to start after you set Listen 82. Which command is the fix?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Show the current SELinux mode, the context of the httpd processes and the context of /var/www/html.

  2. Exercise 2medium

    Serve a website from /srv/web with SELinux enforcing, in a way that survives a relabel.

  3. Exercise 3hard

    Something is denied but ausearch shows no AVC. Find the hidden denial, then restore normal logging.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I just disable SELinux to make things work?
No. Almost every problem is a wrong label, a boolean or a port label, and each is a one-line fix. Disabling it removes a strong layer of protection and is a failed task on the RHCSA.
What is the difference between permissive and disabled?
Permissive keeps labelling files and logs what it would block, so you can switch back at any time. Disabled stops everything, and enabling it again needs a full relabel.
What does the dot at the end of ls -l permissions mean?
A dot, as in -rw-r--r--., means the file has an SELinux label and no ACL. A plus means an ACL is set.
Where are SELinux denials logged?
In /var/log/audit/audit.log when auditd runs, which is the default on RHEL. Search them with ausearch -m AVC, or read summaries with sealert.

Go deeper

ShareXLinkedInWhatsApp