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.
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_connectboolean persistently. - Return the system to baseline by resetting the boolean and removing the test file.
Concepts
-
SELinux runtime state and mode verification with
sestatusandgetenforce. -
SELinux contexts: user, role, type, and level; why the
typeis often the operational control point. -
Inspecting labels on files and directories with
ls -Z. -
Temporary context changes with
chconand why they are not policy. -
Restoring default labels using
restoreconagainst the policy’s file context mapping. -
Booleans as tunable policy switches and persistent changes with
setsebool -P.
Walkthrough
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.
getenforce
Enforcing
getenforce provides the quick operational view. It is useful in scripts and in incident response when you
need a single-value truth.
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.
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.
user_home_t.
chcon is a local label change and is not persistent policy. Use it for controlled testing, then
restore defaults with restorecon.
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.
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.
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.
httpd_can_network_connect.
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.
-P writes the boolean setting so it survives reboot. Without persistence, a reboot can silently revert
behavior and reintroduce the incident.
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.
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
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.
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.
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.
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.
setsebool -P httpd_can_network_connect off
rm -f /var/www/html/selinux_test.txt
ls -Z /var/www/html
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.