Intermediate · SELinux and SSH · topic 29 of 57 · 30 min
SELinux: what it is and its modes
Explanation
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).
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
| Mode | What happens | When to use |
|---|---|---|
| enforcing | Rules are applied; denials are blocked and logged | Always, in normal operation |
| permissive | Nothing is blocked; would-be denials are logged | Short troubleshooting only |
| disabled | SELinux is off completely; no labels are kept for new files | Avoid. Turning it back on needs a full relabel |
$ 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
PermissiveThe mode at boot
# /etc/selinux/config
SELINUX=enforcing
SELINUXTYPE=targetedYou 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
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.
# 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=0Practice (0/3 done)
Which mode blocks nothing but logs what would have been denied?
Which change survives a reboot?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Contexts and file labels: ls -Z, semanage fcontext and restoreconSELinux and SSH · same module
- SELinux booleans and port labelsSELinux and SSH · same module
- Troubleshooting SELinux denialsSELinux and SSH · same module
- Recovering a lost root passwordBoot, recovery and containers
- Persistent storage and containers as systemd servicesBoot, recovery and containers
- Linux security basicsCyber Security: from zero to SOC analyst · Systems and cryptography
Questions people ask about this
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
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.
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.
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.
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
- 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.
- Module 6 exam: SELinux and SSH · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Automation basics: scheduling, archives and shell scripts. 57 topics left in the course before the final exam.