Join our Newsletter — 33% off our NHI Course
Home› Guides› SELinux Commands Cheat Sheet: Contexts, Booleans and Denial…
Template Endpoint Security

SELinux Commands Cheat Sheet: Contexts, Booleans and Denial Fixes

PDF for newsletter subscribers · free to read online

← All guides
By NHI Mgmt Group, under the editorial direction of Lalit Choda Updated 27 November 2026 12 min read
On this page

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 fcontext plus restorecon fixes most denials.
  • Persistent means -P and semanage. setsebool without -P and chcon changes do not survive a reboot or relabel.
  • Read the denial, then decide. ausearch, sealert and audit2why tell 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 with sudo.
  • Packages: on RHEL 8 and 9, semanage, audit2allow and audit2why come from policycoreutils-python-utils, and sealert from setroubleshoot-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 seinfo or semanage fcontext -l rather 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

PurposeCommandNotes
Show current modegetenforceReturns Enforcing, Permissive or Disabled
Full statussestatusShows loaded policy name, current mode and mode from the config file
Persistent mode and policygrep -E '^SELINUX(TYPE)?=' /etc/selinux/configSELINUX=enforcing|permissive|disabled; SELINUXTYPE=targeted|mls; takes effect after reboot
Check kernel boot overridescat /proc/cmdlineLook for selinux=0, enforcing=0 or autorelabel=1; none should be present in steady state
Switch mode until rebootsetenforce 1 / setenforce 0For a short test only; see What not to do
List permissive domainssemanage permissive -lShows built-in and customised permissive types
Make one domain permissivesemanage permissive -a <domain>Example: semanage permissive -a httpd_t
Remove a customised permissive domainsemanage permissive -d <domain>Works only on types added by semanage permissive
Schedule a full relabelfixfiles -F onbootRed 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.

PurposeCommandNotes
File and directory labelsls -Z <path> / ls -dZ <dir>-d shows the directory itself, not its contents
Process domainsps -eZ / ps -efZ | grep <name>Look for services in unconfined_service_t, which have no confining policy
Your own contextid -ZUsually unconfined_u:unconfined_r:unconfined_t for admins by default
Socket and port contexts in usess -ZtlpnShows the process context bound to each listening port
What the policy expects for a pathmatchpathcon <path>Compare against ls -Z
Check a path without changing itmatchpathcon -V <path> or restorecon -Rnv <path>-n reports what would change; nothing is written
List file context rulessemanage fcontext -l | grep <path or type>Full rule set, policy plus local
List only local customisationssemanage fcontext -l -CYour changes, the ones to carry to rebuilt hosts
Add a persistent rulesemanage fcontext -a -t <type> "<path>(/.*)?"Writes the rule only; nothing on disk changes until restorecon
Label a path like an existing onesemanage fcontext -a -e /var/www /srv/wwwEquivalence: /srv/www inherits every rule for /var/www
Change an existing local rulesemanage fcontext -m -t <type> "<path>(/.*)?"Modify instead of adding a duplicate
Delete a local rulesemanage fcontext -d "<path>(/.*)?"Then run restorecon to return files to the policy default
Apply rules to diskrestorecon -Rv <path>Add -F to reset user, role and range as well as type
Temporary label for a testchcon -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

PurposeCommandNotes
List all booleans with stategetsebool -aFilter with grep httpd, grep samba and so on
List with descriptionssemanage boolean -lShows current and default state plus a one-line description
List only booleans you changedsemanage boolean -l -CAudit drift from the policy defaults
Read one booleangetsebool <boolean>Example: getsebool httpd_can_network_connect_db
Set until rebootsetsebool <boolean> onUse to test the effect
Set persistentlysetsebool -P <boolean> onRed 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

PurposeCommandNotes
Recent denials and SELinux errorsausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -ts recentThe Red Hat starting point; -ts today for a longer window
Denials for one command or processausearch -m AVC -c <comm> -ts recentcomm is the process name shown in the AVC record
setroubleshoot messagesjournalctl -t setroubleshootNeeds setroubleshoot-server
Analyse all alerts with suggested fixessealert -l "*"Suggestions are ranked; read them, do not run them blindly
Explain why access was deniedausearch -m AVC -ts recent | audit2whySame as audit2allow -w; often names the boolean that would allow it
Show hidden denialssemodule -DBDisables dontaudit rules so every denial is logged
Hide them againsemodule -BAlways re-enable after the investigation; the extra noise is large
Preview rules for a denialausearch -m AVC -ts recent | audit2allow -RPrints 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 modulessemodule -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:

  1. Read it: run ausearch and audit2why. Note the source domain (scontext), target type (tcontext), class and permission.
  2. Label: if the target type is wrong for its path (default_t, var_t, user_home_t on web content), fix with semanage fcontext and restorecon.
  3. Boolean: if audit2why or sealert names a boolean, set it with setsebool -P.
  4. Port: if the denial is name_bind or name_connect on a non-standard port, label the port (next section).
  5. Isolate: if you still need the service running while you investigate, make only its domain permissive with semanage permissive -a.
  6. 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.
  7. 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

PurposeCommandNotes
List port labelssemanage port -lFilter: semanage port -l | grep http_port_t
List only local port changessemanage port -l -CYour additions
Allow a service on a new portsemanage port -a -t <type> -p tcp <port>Example: semanage port -a -t http_port_t -p tcp 3131
Re-label a port already definedsemanage port -m -t <type> -p tcp <port>Use -m when -a reports the port is already defined
Remove a local port labelsemanage port -d -p tcp <port>Policy-defined ports cannot be deleted, only local ones
Export all local customisationssemanage export -f selinux-local.modsFile contexts, ports, booleans, logins and permissive domains in one list
Apply them on another hostsemanage import -f selinux-local.modsThe 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 0 as the fix: it turns off enforcement for every domain on the host. If you need a service to keep running during diagnosis, use semanage permissive -a <domain> and remove it afterwards.
  • SELINUX=disabled or selinux=0: disabling stops labelling, so files created meanwhile carry no label and a full relabel is needed to return. Red Hat says not to use selinux=0 in production.
  • chcon as the permanent change: the label disappears at the next restorecon or relabel, often months later during a recovery.
  • Loading audit2allow -M output unread: it may allow a domain to write to shadow_t or etc_t, or to execute from tmp_t. Delete any rule you cannot justify.
  • Leaving debug settings on: semodule -DB, permissive domains and enforcing=0 on the kernel command line are all easy to forget. Check them with semanage permissive -l and cat /proc/cmdline.
  • Broad booleans for narrow needs: httpd_can_network_connect when httpd_can_network_connect_db would 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

FrameworkReferenceWhat the commands give you
NIST SP 800-53 Rev. 5AC-3 Access Enforcement; CM-6 Configuration SettingsMandatory access control enforced; mode and local rules as baseline settings
NIST SP 800-53 Rev. 5AU-6 Audit Record Review; SI-4 System MonitoringAVC review and alerts on mode changes
MITRE ATT&CK v20T1685 Disable or Modify ToolsDetection 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 getenforce returns Enforcing and /etc/selinux/config says SELINUX=enforcing on every RHEL family host.
  • Install policycoreutils-python-utils and setroubleshoot-server in build images.
  • Fix denials in order: label, boolean, port, then a reviewed local module.
  • Use semanage fcontext plus restorecon for every path change; keep chcon for tests.
  • Use setsebool -P with the narrowest boolean, and record the reason.
  • Use semanage permissive -a for one domain instead of setenforce 0, and remove it when done.
  • Keep semanage export output in version control and apply it through configuration management.
  • Alert on hosts leaving Enforcing mode and on unexplained setenforce or policy loads.

Standards and references

Related NHI Mgmt Group resources: SELinux Guide · Reference Monitors and Mandatory Access Control Guide · Linux Server Hardening Guide · AppArmor Guide

PDF for subscribers

Take this guide with you

Every section, table and checklist in one PDF, with links to the sources and related guides. Subscribe to our weekly NHI & AI Identity Journal and we’ll email you a download link.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

PDF Downloads for Newsletter Subscribers

Get the guide

Enter your details and we’ll email you a download link.

The full guide is free to read online. The PDF is a thank you for subscribing.

Fill in all fields marked * and tick both boxes to continue.