SELinux is the mandatory access control layer that contains a compromised service on Red Hat family systems, and the most common response to its first denial is to switch it off. This guide explains how SELinux makes decisions, how to read and fix denials without weakening policy, how to confine administrators and service accounts, and how to monitor that enforcement stays on. It covers RHEL, CentOS Stream, Rocky Linux, AlmaLinux and Fedora with the targeted policy, with commands taken from the Red Hat Enterprise Linux 9 documentation. It is written for Linux administrators, platform engineers and security teams who own server baselines. For the wider host baseline see the Linux Server Hardening Guide, and for the theory behind label based access control see the Reference Monitors and Mandatory Access Control Guide.
This is an independent guide from NHI Mgmt Group, based on Red Hat's public documentation as of November 2026. It is not sponsored or reviewed by Red Hat. Product features change, so confirm settings against Red Hat's current documentation.
Key takeaways
- Permissive is the debugging mode, disabled is not. Red Hat recommends permissive mode for production troubleshooting; disabling skips labelling, so re-enabling later needs a full relabel and a careful reboot.
- Most denials are labelling or configuration problems. Check file contexts, booleans and port labels before writing any policy; Red Hat explicitly says not to reach for audit2allow first.
- Per-domain permissive beats global permissive.
semanage permissive -a httpd_trelaxes one service while the rest of the host stays enforced. - Users are unconfined by default. The
__default__login maps tounconfined_u; mapping administrators tostaff_uwith a sudo role transition puts real limits on interactive root. - AVC records are a detection signal. Sudden denials from a web or database domain, or a host leaving enforcing mode, are worth alerting on, not just ticketing.
How SELinux decides access: contexts, types and policy
SELinux is a Linux Security Module (LSM). The kernel LSM documentation states that the capabilities module always runs first, any number of minor modules (such as Yama) follow, and at most one major module such as SELinux is active; the active list is visible in /sys/kernel/security/lsm. SELinux checks run after normal discretionary permissions, so a file that is world readable can still be denied to a process whose domain has no rule for it.
Every process and object carries a context with four fields: user, role, type and level, written as user:role:type:level (for example system_u:system_r:httpd_t:s0). The type is the field that matters day to day. Policy is default deny: a process type (a domain) may only touch object types an allow rule names. Apache runs as httpd_t, its content is labelled httpd_sys_content_t under /var/www/html, and it may bind only to ports labelled http_port_t. If Apache is exploited, the attacker inherits httpd_t, not the full rights of the account.
| Concept | What it is | Where you see it |
|---|---|---|
| Mode | Enforcing, permissive (log only) or disabled | getenforce, sestatus, SELINUX= in /etc/selinux/config |
| Policy type | targeted (default, confines selected services) or mls | SELINUXTYPE= in /etc/selinux/config |
| Domain | The type of a running process, for example httpd_t, mysqld_t | ps -eZ |
| File context | Label on a file, for example httpd_sys_content_t, mysqld_db_t | ls -Z, matchpathcon -V |
| Boolean | Switch that turns optional policy rules on or off | getsebool -a, semanage boolean -l |
| Port label | Type assigned to a TCP or UDP port | semanage port -l |
| Level and categories | MLS sensitivity and MCS categories such as s0:c270,c963, used to keep containers apart | ps -efZ on container processes |
Unconfined is not the same as off. A daemon started from a binary that has no policy runs as unconfined_service_t. It is labelled and logged, but it has broad rights. On a hardened host the number of processes in unconfined domains should be small and known.
Modes and how to change them safely
- Runtime switch:
setenforce 0andsetenforce 1change the mode until reboot only. Use it for a minutes long test, not as a fix. - Persistent mode: edit
SELINUX=in/etc/selinux/configand reboot. In permissive mode each distinct AVC is logged once, so you collect the full set of denials a workload would hit. - Disabling on RHEL 9: the documented method is the kernel parameter
selinux=0, set withgrubby --update-kernel ALL --args selinux=0. With it, the kernel loads no SELinux infrastructure and init touches/.autorelabelso the next enabled boot relabels the file system. Red Hat advises against production use. - Boot overrides:
enforcing=0boots permissive;autorelabel=1forces a relabel. Both are recovery tools and should be absent from the steady state kernel command line.
Confined users and privileged access
The targeted policy confines services well, but by default people are not confined. semanage login -l shows that __default__ maps every unmapped Linux user to unconfined_u at range s0-s0:c0.c1023. A compromised admin session therefore has the full rights of root. Mapping users to confined SELinux users changes that.
| SELinux user | Default role | su / sudo | Typical use |
|---|---|---|---|
unconfined_u | unconfined_r | Allowed | Default for all users; avoid for production admins |
user_u | user_r | Not allowed | Ordinary users and application operators who never need root |
staff_u | staff_r (can enter sysadm_r) | sudo only | Administrators: log in confined, elevate through sudo |
sysadm_u | sysadm_r | Allowed | Dedicated admin accounts; SSH login governed by ssh_sysadm_login |
guest_u, xguest_u | guest_r, xguest_r | Not allowed | Kiosk or restricted shells; no networking for guest_r |
system_u | system_r | Not applicable | System processes only; Red Hat says never assign it to a Linux user |
- Change the default:
semanage login -m -s user_u -r s0 __default__maps all unmapped users touser_u; Red Hat documents this as a way to meet a STIG requirement. New sessions then run asuser_u:user_r:user_t:s0, which cannot run setuid tools such as sudo. - Map admins explicitly:
useradd -Z staff_u <user>orusermod -G wheel -Z sysadm_u <user>, thenrestorecon -R -F -v /home/<user>. - Role transition through sudo: a rule in
/etc/sudoers.d/withROLE=sysadm_randTYPE=sysadm_tmoves astaff_u:staff_r:staff_tsession tostaff_u:sysadm_r:sysadm_tonly for the elevated command. Pair this with the controls in the sudo and sudoers Security Guide. - Execution from writable paths: the
*_exec_contentbooleans control whether a role can run content from home and/tmp. Red Hat notesguest_exec_contentandxguest_exec_contentshould be set off.
Decision guidance: small teams with a handful of servers often start by confining only non-admin users to user_u, which is low risk. Larger estates with jump hosts and PAM tooling gain more from staff_u plus sudo transitions for admins, because it limits what a stolen admin session can do outside an approved command. Test confined admin roles on a pilot group first: backup agents, configuration management and monitoring scripts run through sudo may need extra roles such as logadm_r or dbadm_r.
Services, containers and custom policy modules
Service accounts are where SELinux earns its keep. A daemon running as a dedicated Linux account with a confined domain is limited twice: by its UID and by its type. When third party agents, databases or in-house daemons run as unconfined_service_t, that second layer is missing. The account side of this is covered in the Linux Service Accounts and Keytab Management Guide.
Fix order for a denial
- Labels: if content moved to a non default path, add a rule and apply it:
semanage fcontext -a -t httpd_sys_content_t "/srv/myweb(/.*)?", thenrestorecon -R -v /srv/myweb.semanage fcontextalone changes nothing on disk;chconchanges do not survive a relabel. - Booleans: look for an existing switch before writing rules, for example
setsebool -P httpd_can_network_connect_db on. Without-Pthe change is lost at reboot. - Ports: a service on a non standard port needs the port labelled:
semanage port -a -t http_port_t -p tcp 9876. - Policy bug or genuinely new behaviour: only now generate rules. Red Hat advises using rules it provides (Known Issues, Solutions) in a small
.cilmodule with alocal_prefix, installed withsemodule -iand removed withsemodule -r.
Writing policy for your own daemon
For in-house services, sepolicy generate --init /usr/local/bin/mydaemon produces a type enforcement file (.te), interface file (.if), file contexts (.fc), a spec file and a build script. After loading, ps -efZ should show the process in its own mydaemon_t domain rather than unconfined_service_t. Run that domain permissive with semanage permissive -a mydaemon_t while you exercise the service, review ausearch -m AVC -ts recent | audit2allow -R output, prefer interface macros (for example logging_write_generic_logs(mydaemon_t)) over raw allow rules, then remove the permissive flag with semanage permissive -d mydaemon_t.
Review every generated rule. audit2allow writes whatever the denial needs. A rule letting a web domain write shadow_t or execute tmp_t is a request to approve the attack, not a fix. Keep local modules in version control and review them like code.
Containers
Podman containers run in a generic container domain with unique MCS categories per container (visible as s0:cNNN,cNNN in ps -efZ), which stops one container reading another's files even when both run as the same type. Red Hat notes the generic domain can be too strict or too loose for a given workload; udica builds a per-container policy from podman inspect output, and the container is started with --security-opt label=type:my_container.process. Never mount host paths with relabelling options onto system directories such as /home or /usr. See the Container Runtime Security Guide.
Logging and detection
SELinux denials are written by auditd to /var/log/audit/audit.log as AVC records (kernel message type 1400). The Red Hat search filter is ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -ts recent; if auditd is not running, use dmesg | grep -i -e type=1300 -e type=1400. The key fields are scontext (the process), tcontext (the target), tclass (file, tcp_socket, process) and the permission in braces. sealert -l "*" from setroubleshoot-server explains each denial in plain language, and journalctl -t setroubleshoot shows its summaries. Some denials are hidden by dontaudit rules; semodule -DB disables them temporarily and semodule -B restores them. Shipping audit logs is covered in the Linux Logging and auditd for Defenders Guide.
| Signal | Why it matters | Suggested handling |
|---|---|---|
AVC from httpd_t, mysqld_t or another internet facing domain trying to execute, connect out or read shadow_t | Post exploitation behaviour blocked by policy (ATT&CK T1190 followed by attempts at T1068) | High priority alert; investigate the service, not the policy |
Mode change: MAC_STATUS audit record, setenforce 0 in shell history, selinux=0 or enforcing=0 on the kernel command line | Defence tampering (ATT&CK T1685 Disable or Modify Tools) | Alert and require a change ticket |
MAC_POLICY_LOAD, new modules in semodule -lfull, boolean changes | Policy weakened, legitimately or not | Compare to an approved module and boolean list |
| Spike in AVCs after a deployment | Labelling or path change in the release | Route to the owning team; fix labels in the package or automation |
| Container process with an unexpected type or no categories | Container run privileged or with labelling disabled, raising escape risk (T1611) | Block at admission or in the run configuration |
Metrics that show it is working
- Enforcing coverage: percentage of Linux hosts where
getenforcereturns Enforcing (target 100% outside documented exceptions). - Unconfined services: count of processes in
unconfined_service_tper host, trending down. - Permissive domains: number of domains in
semanage permissive -l, each with an owner and expiry date. - Local modules: count of local policy modules and the share reviewed in the last 12 months.
- Time to resolve denials: median days from first AVC on a new release to a label, boolean or reviewed module fix.
- Confined admins: percentage of admin accounts mapped to
staff_uorsysadm_urather thanunconfined_u.
Hardening baseline
Rollout plan
- Inventory: collect
sestatus,semanage permissive -l,semodule -lfull | grep local_,semanage login -land the kernel command line from every host. Group hosts into enforcing, permissive and disabled. - Disabled hosts first move to permissive, not enforcing: remove
selinux=0withgrubby --update-kernel ALL --remove-args selinux=0, setSELINUX=permissive, runfixfiles -F onbootand reboot. Red Hat warns to confirm the system boots in permissive mode before relabelling. - Collect denials for a full business cycle: include month end jobs, backups and patching. Fix with labels, booleans and port types in configuration management, not by hand.
- Pilot ring to enforcing: switch a small ring, watch AVCs and service health for a week, then widen. Keep any stubborn service in a per-domain permissive exception with an owner and end date.
- Confine people: map
__default__touser_uand admins tostaff_uwith sudo role transitions on a pilot group before estate wide rollout. - Rollback: for a single service,
semanage permissive -a <domain>; for a host,setenforce 0immediately andSELINUX=permissivepersistently. Do not roll back by disabling, because recovery then costs a relabel.
Framework mapping
| Framework | Reference | How SELinux supports it |
|---|---|---|
| NIST SP 800-53 Rev. 5 | AC-3(3) Mandatory Access Control; AC-6 Least Privilege; SC-39 Process Isolation | Type enforcement confines domains regardless of the account's rights |
| NIST SP 800-53 Rev. 5 | CM-6 Configuration Settings; CM-7 Least Functionality; AU-12 Audit Record Generation | Mode, booleans and modules as baseline settings; AVC records as audit events |
| NIST CSF 2.0 | PR.PS-01, PR.AA-05, DE.CM-09 | Configuration management, enforced access permissions, monitoring of runtime environments |
| CIS Controls v8.1 | Control 4 Secure Configuration of Enterprise Assets and Software | CIS Benchmarks for RHEL family systems check SELinux is installed, not disabled and enforcing |
| ISO/IEC 27001:2022 | Annex A 8.9 Configuration management; 8.2 Privileged access rights | Documented MAC baseline and confined admin roles |
| MITRE ATT&CK v20 | T1190, T1068, T1611, T1685 | Limits post exploitation reach; mode changes are a tampering signal |
Compliance profiles for CIS and STIG can be scanned automatically; see the OpenSCAP and Linux Compliance Scanning Guide and the Red Hat Enterprise Linux Security Hardening Guide.
Common misconfigurations
- Disabling to fix one service: a single denial on install leads to
SELINUX=disabledin a golden image, and every later host inherits it. Use per-domain permissive and fix the label. - Blind audit2allow: piping all denials into a module and loading it, which can grant a compromised domain exactly the access policy just stopped. Review each rule against the fix order above.
- chcon instead of semanage fcontext: labels look right until the next relabel or
restoreconrun reverts them, and the outage returns months later. - Booleans set without -P: the fix works until the next patch reboot. Manage booleans in configuration management.
- Permanent permissive exceptions:
semanage permissive -aused in an incident and never removed. Review the list monthly. - Vendor agents left unconfined: monitoring, backup and security agents installed under
/optrun asunconfined_service_twith root. Ask vendors for SELinux policy modules or confine them yourself, and treat their credentials as privileged non-human identities, as the Service Account Security Guide describes.
Practitioner checklist
- Confirm every RHEL family host reports Enforcing with the targeted policy, and alert on any change.
- Remove
selinux=0,enforcing=0andautorelabel=1from steady state kernel command lines. - Fix denials in order: file contexts, booleans, port labels, then reviewed local modules.
- Use
semanage fcontextplusrestorecon, neverchcon, and set booleans with-Pfrom configuration management. - Keep per-domain permissive exceptions on a register with owner and expiry.
- Map
__default__touser_uand admins tostaff_uwith sudo role transitions. - Give in-house daemons their own domain with
sepolicy generate; keep local modules in version control. - Ship AVC, MAC_STATUS and MAC_POLICY_LOAD records to the SIEM and alert on internet facing domains.
- Run containers with labelling enabled and use
udicawhere the generic domain is wrong for the workload.
Standards and references
- Linux kernel documentation: Linux Security Module Usage
- Red Hat: Using SELinux, Getting started with SELinux (RHEL 9)
- Red Hat: Using SELinux, Changing SELinux states and modes (RHEL 9)
- Red Hat: Using SELinux, Troubleshooting problems related to SELinux (RHEL 9)
- Red Hat: Using SELinux, Managing confined and unconfined users (RHEL 9)
- Red Hat: Using SELinux, Writing a custom SELinux policy (RHEL 9)
- Red Hat: Using SELinux, Creating SELinux policies for containers (RHEL 9)
- NIST: SP 800-53 Rev. 5
Related NHI Mgmt Group resources: Linux Server Hardening Guide · AppArmor Guide · Linux Kernel Hardening and sysctl Guide · Linux Privilege Escalation Techniques Guide