Skip to content

Intermediate · SELinux and SSH · topic 29 of 57 · 30 min

SELinux: what it is and its modes

Explanation

SELinux is an extra security layer in the kernel. Normal permissions ask "which user owns this?"; SELinux also asks "is this kind of program allowed to touch this kind of file?" using labels. It runs in enforcing mode (blocks and logs), permissive mode (only logs) or is disabled. Keep it enforcing.

Why another layer?

Normal Linux permissions are called DAC (discretionary access control): the owner of a file decides who may use it. The weakness: a program runs with the full rights of its user. If an attacker breaks into the web server process (user apache), they can read every file apache can read, and as root, everything.

SELinux (Security-Enhanced Linux) adds MAC (mandatory access control): rules written by the system, which even root cannot ignore in the moment. Every process and every file gets a label. The policy says which process labels may do what to which file labels. The web server runs with the label httpd_t and may read files labelled httpd_sys_content_t. Even if it is hacked, it cannot read /etc/shadow (shadow_t) or users' home directories (user_home_t).

Think of it like this: Normal permissions are your house keys: whoever holds the key gets into every room it opens. SELinux is like a hospital's access card system: a nurse's card opens wards and the pharmacy store, a canteen worker's card opens the kitchen. Even if someone steals the canteen card, it cannot open the operation theatre, because the card type, not the person, decides.

The targeted policy

RHEL uses the targeted policy: network-facing and system services (httpd, sshd, named, postgresql and hundreds more) run in confined domains with strict rules. Users' own programs run mostly unconfined. So you meet SELinux mostly when you put service files in unusual places or use unusual ports.

The three modes

ModeWhat happensWhen to use
enforcingRules are applied; denials are blocked and loggedAlways, in normal operation
permissiveNothing is blocked; would-be denials are loggedShort troubleshooting only
disabledSELinux is off completely; no labels are kept for new filesAvoid. Turning it back on needs a full relabel
bash
$ getenforce
Enforcing
$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
...
# setenforce 0        # permissive until reboot (or setenforce 1)
# getenforce
Permissive
setenforce switches between enforcing (1) and permissive (0) at run time only. It cannot disable SELinux.

The mode at boot

output
# /etc/selinux/config
SELINUX=enforcing
SELINUXTYPE=targeted
The mode the system boots into. Change it here for a permanent change, then reboot.

You can also set the mode for one boot with kernel arguments: enforcing=0 boots permissive. On RHEL 9 the supported way to disable SELinux completely is the kernel argument selinux=0, not only the config file. But in real work, and in exams, the right answer is almost always: leave it enforcing and fix the label, boolean or port.

Common mistakes

  • Following an internet guide that says "first, disable SELinux". This removes a major protection. Learn the three fixes in the next lessons instead.
  • Leaving a server in permissive after troubleshooting. Run setenforce 1 when done and check /etc/selinux/config.
  • Assuming SELinux is the problem every time. Check normal permissions and the firewall first; then look for AVC denials in the audit log.
  • Disabling SELinux and later enabling it without a relabel. Files created meanwhile have no labels; the system may fail to boot. Use touch /.autorelabel before rebooting into enforcing.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

SELinux: what it is and its modes · 1/7

process label + file label → policy → allow or deny

SELinux is an extra security layer in the kernel. It uses labels on every process and file.

Prefer a video by a teacher? Find free videos on “selinux explained for beginners enforcing permissive rhel” on YouTube ↗

In real life: A hacked plugin that could not go further

A media company in Mumbai ran a web CMS with a vulnerable plugin. An attacker got code running as the web server. Because SELinux was enforcing, the process (httpd_t) could not read SSH keys or /etc/shadow, could not connect out to the attacker's server on a random port, and could not write outside its upload folder. The audit log showed the blocked attempts, which is how the team found the break-in.

bash
# ausearch -m AVC -ts today | grep -c denied
37
# ausearch -m AVC -ts today | grep name_connect | head -1
type=AVC msg=audit(1729338502.611:2214): avc:  denied  { name_connect } for  pid=40121 comm="php-fpm" dest=4444 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:unreserved_port_t:s0 tclass=tcp_socket permissive=0

Practice (0/3 done)

Task 1 · Question

Which mode blocks nothing but logs what would have been denied?

Task 2 · Question

Which change survives a reboot?

Task 3 · Practice

On your lab VM: check the SELinux status, switch to permissive and back at run time, and confirm the config file says enforcing.

Quick quiz

Question 1

1.What does SELinux add to normal Linux permissions?

Question 2

2.Which policy does RHEL use by default?

Question 3

3.Can setenforce disable SELinux?

Question 4

4.What should you do after troubleshooting in permissive mode?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What does SELinux add to normal Linux permissions?

Answer: Mandatory, label-based rules that even root processes must follow

It is mandatory access control based on labels.

Which policy does RHEL use by default?

Answer: targeted

targeted confines services.

Can setenforce disable SELinux?

Answer: No, it only switches between enforcing and permissive

Disabling needs a boot-time change.

What should you do after troubleshooting in permissive mode?

Answer: setenforce 1 and confirm the config says enforcing

Return to enforcing.

A port already has a different label. Which semanage option changes it?Related

Answer: -m

-m modifies an existing record.

From SELinux booleans and port labels

Which command shows SELinux denials from the last few minutes?Related

Answer: ausearch -m AVC -ts recent

ausearch filters audit records by type and time.

From Troubleshooting SELinux denials

Which kernel argument replaces systemd with a bash shell for one boot?Related

Answer: init=/bin/bash

init= sets the first program the kernel runs.

From Recovering a lost root password

What happens to files written inside a container (not in a volume) after podman rm?Related

Answer: They are deleted with the container

The container layer is removed.

From Persistent storage and containers as systemd services

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 4 more topics in “SELinux and SSH” before its exam: Contexts and file labels: ls -Z, semanage fcontext and restorecon, SELinux booleans and port labels, Troubleshooting SELinux denials, SSH: keys, ssh-copy-id and hardening sshd.
  2. Module 6 exam: SELinux and SSH · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Automation basics: scheduling, archives and shell scripts. 57 topics left in the course before the final exam.

← Previous: Growing storage and adding swap