Most SELinux problems are fixed with five or six commands, yet the one administrators reach for first is often setenforce 0, which removes the protection instead of fixing the cause. This cheat sheet lists the commands for checking mode and status, viewing and changing file and process contexts, setting booleans, labelling ports, and reading and fixing denials, with the order in which to use them. Commands follow the Red Hat Enterprise Linux 9 documentation and the SELinux project's own manual pages, and apply to RHEL, CentOS Stream, Rocky Linux, AlmaLinux and Fedora with the targeted policy. It is written for Linux administrators who already know what SELinux does; for how policy decisions work, confined users and custom modules, see the SELinux Guide.
This is an independent guide from NHI Mgmt Group, based on Red Hat's public documentation and the SELinux project's manual pages 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
- Fix labels before anything else. Red Hat says troubleshooting should start with a check for a labelling problem;
semanage fcontextplusrestoreconfixes most denials. - Persistent means
-Pandsemanage.setseboolwithout-Pandchconchanges do not survive a reboot or relabel. - Read the denial, then decide.
ausearch,sealertandaudit2whytell you whether the cause is a label, a boolean, a port or missing policy. - audit2allow is a last step. Red Hat says not to use it to generate a local module as the first option; it allows exactly what was denied, including what an attacker tried.
- Relax one domain, not the host.
semanage permissive -a <domain>lets one service log without blocking while everything else stays enforced.
How to use this template
- Placeholders: <path>, <type>, <domain>, <boolean> and <port> are yours to replace. The
#prompt means run as root or withsudo. - Packages: on RHEL 8 and 9,
semanage,audit2allowandaudit2whycome frompolicycoreutils-python-utils, andsealertfromsetroubleshoot-server. Install them on build images so they are there during an incident. - Distribution differences: Debian and Ubuntu use AppArmor by default; if you run SELinux there, policy module names, default types and package names differ from RHEL, so check type names with
seinfoorsemanage fcontext -lrather than copying RHEL examples. For AppArmor, see the AppArmor Guide. - Read-only first: every section lists the inspection commands before the commands that change state. Run the inspection commands freely; change commands belong in change control and configuration management.
Checking SELinux status and mode
| Purpose | Command | Notes |
|---|---|---|
| Show current mode | getenforce | Returns Enforcing, Permissive or Disabled |
| Full status | sestatus | Shows loaded policy name, current mode and mode from the config file |
| Persistent mode and policy | grep -E '^SELINUX(TYPE)?=' /etc/selinux/config | SELINUX=enforcing|permissive|disabled; SELINUXTYPE=targeted|mls; takes effect after reboot |
| Check kernel boot overrides | cat /proc/cmdline | Look for selinux=0, enforcing=0 or autorelabel=1; none should be present in steady state |
| Switch mode until reboot | setenforce 1 / setenforce 0 | For a short test only; see What not to do |
| List permissive domains | semanage permissive -l | Shows built-in and customised permissive types |
| Make one domain permissive | semanage permissive -a <domain> | Example: semanage permissive -a httpd_t |
| Remove a customised permissive domain | semanage permissive -d <domain> | Works only on types added by semanage permissive |
| Schedule a full relabel | fixfiles -F onboot | Red Hat: switch to permissive first and make sure the next boot is permissive |
Red Hat's own guidance is to debug with permissive mode rather than disabling SELinux, and not to disable it except in specific cases such as performance-sensitive systems. The documented disable method on RHEL 9 is the selinux=0 kernel parameter, which Red Hat says not to use in production.
Contexts: viewing and changing
A context is user:role:type:level, for example system_u:object_r:httpd_sys_content_t:s0. Day to day, only the type matters.
| Purpose | Command | Notes |
|---|---|---|
| File and directory labels | ls -Z <path> / ls -dZ <dir> | -d shows the directory itself, not its contents |
| Process domains | ps -eZ / ps -efZ | grep <name> | Look for services in unconfined_service_t, which have no confining policy |
| Your own context | id -Z | Usually unconfined_u:unconfined_r:unconfined_t for admins by default |
| Socket and port contexts in use | ss -Ztlpn | Shows the process context bound to each listening port |
| What the policy expects for a path | matchpathcon <path> | Compare against ls -Z |
| Check a path without changing it | matchpathcon -V <path> or restorecon -Rnv <path> | -n reports what would change; nothing is written |
| List file context rules | semanage fcontext -l | grep <path or type> | Full rule set, policy plus local |
| List only local customisations | semanage fcontext -l -C | Your changes, the ones to carry to rebuilt hosts |
| Add a persistent rule | semanage fcontext -a -t <type> "<path>(/.*)?" | Writes the rule only; nothing on disk changes until restorecon |
| Label a path like an existing one | semanage fcontext -a -e /var/www /srv/www | Equivalence: /srv/www inherits every rule for /var/www |
| Change an existing local rule | semanage fcontext -m -t <type> "<path>(/.*)?" | Modify instead of adding a duplicate |
| Delete a local rule | semanage fcontext -d "<path>(/.*)?" | Then run restorecon to return files to the policy default |
| Apply rules to disk | restorecon -Rv <path> | Add -F to reset user, role and range as well as type |
| Temporary label for a test | chcon -t <type> <path> | Lost at the next restorecon or relabel; never the final fix |
The standard fix for content in a non-default location, from the semanage-fcontext manual page, is a pair: semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?" then restorecon -R -v /web. Moving files with mv keeps their old label; copying with cp gives them the label of the destination directory, which is a common cause of denials after a deployment.
Booleans
| Purpose | Command | Notes |
|---|---|---|
| List all booleans with state | getsebool -a | Filter with grep httpd, grep samba and so on |
| List with descriptions | semanage boolean -l | Shows current and default state plus a one-line description |
| List only booleans you changed | semanage boolean -l -C | Audit drift from the policy defaults |
| Read one boolean | getsebool <boolean> | Example: getsebool httpd_can_network_connect_db |
| Set until reboot | setsebool <boolean> on | Use to test the effect |
| Set persistently | setsebool -P <boolean> on | Red Hat notes -P rebuilds the policy and can take time |
Choose the narrowest boolean that solves the problem. httpd_can_network_connect_db lets the web server reach database ports; httpd_can_network_connect lets it connect to any port, which is far broader. Record every boolean you turn on, with the reason, in the host's build configuration.
Reading and fixing denials
| Purpose | Command | Notes |
|---|---|---|
| Recent denials and SELinux errors | ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -ts recent | The Red Hat starting point; -ts today for a longer window |
| Denials for one command or process | ausearch -m AVC -c <comm> -ts recent | comm is the process name shown in the AVC record |
| setroubleshoot messages | journalctl -t setroubleshoot | Needs setroubleshoot-server |
| Analyse all alerts with suggested fixes | sealert -l "*" | Suggestions are ranked; read them, do not run them blindly |
| Explain why access was denied | ausearch -m AVC -ts recent | audit2why | Same as audit2allow -w; often names the boolean that would allow it |
| Show hidden denials | semodule -DB | Disables dontaudit rules so every denial is logged |
| Hide them again | semodule -B | Always re-enable after the investigation; the extra noise is large |
| Preview rules for a denial | ausearch -m AVC -ts recent | audit2allow -R | Prints suggested rules using reference policy interfaces; changes nothing |
| Build a local module (last resort) | ausearch -m AVC -c <comm> -ts recent | audit2allow -M local_<name> | Creates .te and .pp files; review the .te before loading |
| Load, list and remove modules | semodule -i local_<name>.pp / semodule -l / semodule -r local_<name> | A local_ prefix makes your modules easy to find and remove |
Fix order for any denial:
- Read it: run
ausearchandaudit2why. Note the source domain (scontext), target type (tcontext), class and permission. - Label: if the target type is wrong for its path (
default_t,var_t,user_home_ton web content), fix withsemanage fcontextandrestorecon. - Boolean: if
audit2whyorsealertnames a boolean, set it withsetsebool -P. - Port: if the denial is
name_bindorname_connecton a non-standard port, label the port (next section). - Isolate: if you still need the service running while you investigate, make only its domain permissive with
semanage permissive -a. - Module: only for a genuine policy gap, generate a small module with
audit2allow -M, read every rule, remove anything the service should not need, then load it. - Verify and revert: repeat the action, confirm no new AVCs, and remove any temporary permissive domain.
The audit2allow caution matters because it turns every logged denial into an allow rule. If a denial was caused by a compromised process probing the system, the generated module grants the attacker what SELinux had just stopped. Red Hat says to be careful when tools suggest audit2allow, and not to use it to generate a local module as a first option.
Ports and file context rules
| Purpose | Command | Notes |
|---|---|---|
| List port labels | semanage port -l | Filter: semanage port -l | grep http_port_t |
| List only local port changes | semanage port -l -C | Your additions |
| Allow a service on a new port | semanage port -a -t <type> -p tcp <port> | Example: semanage port -a -t http_port_t -p tcp 3131 |
| Re-label a port already defined | semanage port -m -t <type> -p tcp <port> | Use -m when -a reports the port is already defined |
| Remove a local port label | semanage port -d -p tcp <port> | Policy-defined ports cannot be deleted, only local ones |
| Export all local customisations | semanage export -f selinux-local.mods | File contexts, ports, booleans, logins and permissive domains in one list |
| Apply them on another host | semanage import -f selinux-local.mods | The manual page warns this replaces all existing semanage customisations on the target |
Treat the export file as configuration: keep it in version control and apply it through your configuration management tool, so a rebuilt host gets the same file context, port and boolean rules. The -C lists for fcontext, port and boolean are the quickest drift check between hosts.
What not to do
setenforce 0as the fix: it turns off enforcement for every domain on the host. If you need a service to keep running during diagnosis, usesemanage permissive -a <domain>and remove it afterwards.SELINUX=disabledorselinux=0: disabling stops labelling, so files created meanwhile carry no label and a full relabel is needed to return. Red Hat says not to useselinux=0in production.chconas the permanent change: the label disappears at the nextrestoreconor relabel, often months later during a recovery.- Loading
audit2allow -Moutput unread: it may allow a domain to write toshadow_toretc_t, or to execute fromtmp_t. Delete any rule you cannot justify. - Leaving debug settings on:
semodule -DB, permissive domains andenforcing=0on the kernel command line are all easy to forget. Check them withsemanage permissive -landcat /proc/cmdline. - Broad booleans for narrow needs:
httpd_can_network_connectwhenhttpd_can_network_connect_dbwould do.
Detection worth having: alert when a host's mode is not Enforcing, when setenforce runs, or when MAC_STATUS and MAC_POLICY_LOAD audit records appear outside a change window. Turning off a host's security module is defence impairment in ATT&CK v20 (TA0112, technique T1685 Disable or Modify Tools). See the Linux Logging and auditd for Defenders Guide.
Framework mapping and measures
| Framework | Reference | What the commands give you |
|---|---|---|
| NIST SP 800-53 Rev. 5 | AC-3 Access Enforcement; CM-6 Configuration Settings | Mandatory access control enforced; mode and local rules as baseline settings |
| NIST SP 800-53 Rev. 5 | AU-6 Audit Record Review; SI-4 System Monitoring | AVC review and alerts on mode changes |
| MITRE ATT&CK v20 | T1685 Disable or Modify Tools | Detection of SELinux being switched off |
Useful measures: percentage of RHEL family hosts in Enforcing mode; number of customised permissive domains (target: zero in steady state); number of local policy modules, each with an owner and reason; and services running as unconfined_service_t.
Practitioner checklist
- Confirm
getenforcereturns Enforcing and/etc/selinux/configsaysSELINUX=enforcingon every RHEL family host. - Install
policycoreutils-python-utilsandsetroubleshoot-serverin build images. - Fix denials in order: label, boolean, port, then a reviewed local module.
- Use
semanage fcontextplusrestoreconfor every path change; keepchconfor tests. - Use
setsebool -Pwith the narrowest boolean, and record the reason. - Use
semanage permissive -afor one domain instead ofsetenforce 0, and remove it when done. - Keep
semanage exportoutput in version control and apply it through configuration management. - Alert on hosts leaving Enforcing mode and on unexplained
setenforceor policy loads.
Standards and references
- 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, Configuring SELinux for applications and services with non-standard configurations (RHEL 9)
- SELinux Project: semanage-fcontext manual page
- SELinux Project: semanage-permissive manual page
- SELinux Project: audit2allow manual page
- SELinux Project: semanage-export manual page
Related NHI Mgmt Group resources: SELinux Guide · Reference Monitors and Mandatory Access Control Guide · Linux Server Hardening Guide · AppArmor Guide