Lab 164 · security

SELinux Basics

Verify SELinux mode, inspect file contexts, toggle a boolean, and restore default labels using CLI-only workflows. Prove enforcement by applying a deliberate context change and then returning the system to a known-good baseline.

selinux security policy

Scenario

You are operating a web server where SELinux enforcement is required. A support ticket claims that “SELinux is blocking normal behavior,” but there is no proof of the current state, no context baseline, and no controlled test to validate policy changes. You will verify SELinux status, inspect and manipulate a file label in the web root, restore the default context, and then toggle a targeted boolean used by httpd.

Operator context

Do not disable SELinux to “make the problem go away.” Prove the state, collect context evidence, and apply changes that can be audited and reverted cleanly.

Objective

  • Confirm SELinux is enabled and identify the active mode.
  • Inspect the SELinux context on the web root directory.
  • Create a test file and apply a non-default SELinux type.
  • Verify the label change and restore the default context with restorecon.
  • Inspect and toggle the httpd_can_network_connect boolean persistently.
  • Return the system to baseline by resetting the boolean and removing the test file.

Concepts

  • SELinux runtime state and mode verification with sestatus and getenforce.
  • SELinux contexts: user, role, type, and level; why the type is often the operational control point.
  • Inspecting labels on files and directories with ls -Z.
  • Temporary context changes with chcon and why they are not policy.
  • Restoring default labels using restorecon against the policy’s file context mapping.
  • Booleans as tunable policy switches and persistent changes with setsebool -P.

Walkthrough

Step 1: Check SELinux status.
Command
sestatus
  SELinux status:                 enabled
  Loaded policy name:             targeted
  Current mode:                   Enforcing
  Mode from config file:          Enforcing

This is your authoritative baseline. Capture enabled/disabled and the current mode before you touch booleans, contexts, or service behavior.

Step 2: Display current enforcing mode.
Command
getenforce
  Enforcing

getenforce provides the quick operational view. It is useful in scripts and in incident response when you need a single-value truth.

Step 3: Show SELinux context for the web root directory.
Command
ls -Z /var/www/html
  drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html

This is the expected type for static web content. When a file under the document root has the wrong type, httpd can be blocked even if standard UNIX permissions look correct.

Step 4: Create a test file in the web root.
Command
touch /var/www/html/selinux_test.txt
  File created.

This file is a controlled test artifact. You will deliberately apply a wrong label to prove how SELinux labeling impacts access decisions.

Step 5: Change its SELinux type to user_home_t.
Safety note

chcon is a local label change and is not persistent policy. Use it for controlled testing, then restore defaults with restorecon.

Command
chcon -t user_home_t /var/www/html/selinux_test.txt
  Context modified.

You have now created a mismatch: a user-home type inside the web root. This is the kind of state that produces “it works on one box but not another” incidents if labels drift.

Step 6: Verify the new label.
Command
ls -Z /var/www/html/selinux_test.txt
  -rw-r--r--. root root system_u:object_r:user_home_t:s0 selinux_test.txt

Confirm the type changed to user_home_t. This is your “before restorecon” evidence.

Step 7: Restore the default context.
Command
restorecon -v /var/www/html/selinux_test.txt
  restorecon reset /var/www/html/selinux_test.txt context to httpd_sys_content_t

restorecon returns the file to the policy-defined label for that path. This is the standard operational recovery for label drift.

Step 8: Check boolean httpd_can_network_connect.
Command
getsebool httpd_can_network_connect
  httpd_can_network_connect --> off

Booleans are explicit policy switches. Before you change one, capture the current value and confirm you understand the intended behavior impact.

Step 9: Enable the boolean persistently.
Persistence note

-P writes the boolean setting so it survives reboot. Without persistence, a reboot can silently revert behavior and reintroduce the incident.

Command
setsebool -P httpd_can_network_connect on
  Boolean updated.

This change allows httpd to initiate outbound network connections where policy requires it. Treat boolean changes as configuration changes that must be tracked.

Step 10: Cleanup: disable the boolean and remove the test file.
Commands
setsebool -P httpd_can_network_connect off
rm -f /var/www/html/selinux_test.txt

This returns the system to baseline. You should not leave a modified boolean or stray artifacts behind after a controlled policy test.

Common breakpoints

SELinux is disabled or permissive

If sestatus shows disabled or permissive, you cannot treat SELinux as an enforcement layer. Confirm the operational requirement and reconcile runtime mode with the config file setting.

Context output does not include httpd_sys_content_t

If /var/www/html is not labeled httpd_sys_content_t, investigate label drift across the path. Restore the default directory context before troubleshooting application behavior.

restorecon does not change the label

If the label does not reset, confirm the path matches the expected file context mapping. Verify you targeted the correct file and rerun with verbose output.

setsebool -P takes a long time

Persistent boolean updates may trigger policy rebuild operations. Do not interrupt the process. Treat it as a change that can impact production timing.

Cleanup checklist

This lab modifies a boolean and creates a test file. Cleanup is restoring the boolean to its baseline state and removing the test artifact from the web root.

Commands
setsebool -P httpd_can_network_connect off
rm -f /var/www/html/selinux_test.txt
ls -Z /var/www/html
Success signal

The test file is removed, httpd_can_network_connect is back to the expected value, and the web root shows the correct SELinux type.

Reference

  • sestatus: Shows SELinux status, policy, and current mode.
  • getenforce: Prints the current SELinux mode.
  • ls -Z <path>: Displays SELinux context labels for files and directories.
    • -Z: Prints SELinux security context information.
  • touch <file>: Creates an empty file (or updates timestamps if it exists).
  • chcon -t <type> <path>: Changes the SELinux type on a file or directory.
    • -t: Sets the SELinux type field in the context.
    • user_home_t: Example type used to simulate an incorrect label.
  • restorecon -v <path>: Restores the default SELinux context based on policy mappings.
    • -v: Prints changes as contexts are reset.
  • getsebool <boolean>: Displays the current value of an SELinux boolean.
  • setsebool -P <boolean> on|off: Sets an SELinux boolean value persistently.
    • -P: Makes the change persistent across reboots.
    • httpd_can_network_connect: Boolean controlling outbound network connections for httpd.
  • rm -f <file>: Removes a file without prompting (even if it is missing).
    • -f: Forces removal and suppresses most errors.