Skip to main content
Now that you understand SELinux enforcement modes, the next key concept is file context. File contexts are SELinux labels attached to files and directories that determine which processes can access them—think of them as departmental labels on folders in an office. Even if UNIX permissions permit access, SELinux can deny it if the file’s context does not match the service trying to use it.
A diagram titled "File Context" illustrating SELinux access controls. It shows an HR folder accessible by the HR department but denied to Finance, and a web server allowed to access files for the same service but denied for a different service.

Viewing file contexts

Use ls -Z to display SELinux contexts for files and directories:
A file context has four colon-separated fields: Components summary:
When a process requests access to an object, SELinux compares the process context to the object context and consults the loaded policy to allow or deny the action—type is the most important field for those decisions.

Check and change SELinux mode

Quick commands to check SELinux status:
Temporarily toggle enforcement at runtime:
Persistently change the mode by editing /etc/selinux/config:
As an alternative to the config file, you can pass selinux=0 as a kernel argument to disable SELinux at boot. Use grubby to add or remove that kernel argument:

Demo: Apache blocked by wrong file context

This demo walks through a common scenario: Apache cannot serve files located in a non-standard directory because those files have the wrong SELinux file context.
  1. Install Apache and create a custom document root:
  1. Point Apache to the new document root (place these directives in the appropriate httpd configuration file):
  1. Start Apache and test:
  1. Request the page — on an enforcing system you may get a 403 Forbidden even though UNIX permissions allow access:
Confirm UNIX permissions are correct:
Switch to permissive to confirm SELinux is the cause:
This demonstrates that SELinux was blocking access due to file context.

Inspecting contexts: standard vs custom paths

Typical contexts for standard Apache content:
The custom directory still has an unconfined/default label:

Fixing file contexts with semanage and restorecon

To let Apache serve content from a non-standard directory, add a persistent file-context rule with semanage fcontext and then relabel files with restorecon.
You can consult the semanage fcontext manual for pattern examples; they help construct correct regular-expression paths for matching files and directories.
Example:
Verify the new context:
Return to enforcing mode and retest:
Because the file type is now httpd_sys_content_t, the httpd policy permits Apache to serve files from /webcontent/html.

Useful commands reference

Summary

  • SELinux file contexts are labels in the format user:role:type:level.
  • The type field is used by policy to allow or deny accesses (e.g., httpd_sys_content_t for web content).
  • Use ls -Z to inspect contexts; semanage fcontext and restorecon add and apply persistent labels.
  • Toggle enforcement temporarily with setenforce or persistently via /etc/selinux/config or kernel arguments.
  • When a service is denied access despite correct UNIX permissions, check SELinux contexts and policy.

Watch Video