Operating Systems / CentOS
How-To Guide

How to Configure SELinux Enforcing Mode in CentOS 7

Learn how to enable SELinux enforcing mode in CentOS 7, verify the current state, make the setting persistent, validate policy behavior, and roll back safely if a service is blocked.

How to Configure SELinux Enforcing Mode in CentOS 7

Quick version

If your goal is simply to put CentOS 7 into SELinux enforcing mode, the practical path is straightforward:

  1. Check the current SELinux state.
  2. Set SELINUX=enforcing in /etc/selinux/config.
  3. Apply enforcing mode immediately with setenforce 1.
  4. Reboot or re-login only if your environment requires it for services or automation to pick up the persistent setting.
  5. Verify that the system reports Enforcing after the change.

Use this approach when SELinux is already installed and the system is in permissive mode or disabled. If you need help interpreting denials after the switch, keep a troubleshooting reference such as the CentOS SELinux Troubleshooting Guide for Policy Enforcement Issues nearby so you can distinguish policy denial from an actual service fault.

Prerequisites

Before changing enforcement, confirm that you have console or out-of-band access, especially on remote servers. Turning on SELinux enforcing mode can block access to services that were previously operating with weak or incorrect labels, so you need a recovery path if a critical daemon stops responding.

You should also know which services must remain available after the change. Common examples are SSH, web servers, database services, and configuration management agents. If those services already depend on custom paths, non-default ports, or unusual file contexts, plan to verify their SELinux labels before enforcing mode is made permanent.

Run as a user with sudo privileges, and make sure you can reboot the host if the persistent configuration changes do not behave as expected.

Check the current SELinux state

Start by confirming how SELinux is currently configured and whether the kernel is already enforcing policy.

sestatus

Expected output will include a line similar to:

SELinux status:                 enabled
Current mode:                   permissive
Mode from config file:          permissive

You can also check the runtime mode directly:

getenforce

Typical outputs are Enforcing, Permissive, or Disabled.

These checks matter because runtime mode and configuration file settings are not always the same. A server can be permissive now but set to boot in enforcing mode later, or vice versa.

Set SELinux to enforcing mode permanently

The persistent setting is controlled in /etc/selinux/config. Edit the file and set the policy to enforcing:

sudo vi /etc/selinux/config

Use values similar to the following:

SELINUX=enforcing
SELINUXTYPE=targeted

A few practical notes:

  • SELINUX=enforcing makes the system enforce policy at boot.
  • SELINUXTYPE=targeted is the common default on CentOS 7 and should only be changed if you have a deliberate policy requirement.
  • If the file already contains SELINUX=permissive, replace it rather than adding a second entry.
  • If SELinux is disabled, changing this file may not be sufficient on its own, because the boot state also depends on how the system was installed and what the kernel is told to do at boot.

For systems that are already enabled, this file is the main persistence point. For systems being transitioned from permissive mode, it is the setting that ensures the change survives a reboot.

Apply enforcing mode immediately

If you want the change to take effect now, without waiting for a reboot, switch the runtime mode with setenforce:

sudo setenforce 1

Then confirm the result:

getenforce

Expected output:

Enforcing

This immediate switch is useful for maintenance windows and staged changes. It lets you validate policy behavior before you commit to a reboot cycle.

If setenforce 1 fails, it usually means one of two things: SELinux is disabled at boot, or your session does not have sufficient privileges. In that case, verify the boot-time state with sestatus and inspect how the system was configured during installation.

Verify the change after editing the config file

After you update /etc/selinux/config, confirm that the file reflects the intended state:

grep '^SELINUX=' /etc/selinux/config

Expected output:

SELINUX=enforcing

Then check both the runtime mode and the configuration file setting again:

sestatus
getenforce

A healthy result after the switch is usually:

  • Current mode: enforcing
  • Mode from config file: enforcing
  • getenforce returns Enforcing

If the system reports Permissive even after the runtime change, recheck whether the command succeeded and whether another automation tool or boot process is reverting the state.

Validate that services still work under enforcement

Once SELinux is enforcing, do not assume the host is ready for production until you validate the services that matter most.

At minimum, test the applications that expose network services, access custom directories, or interact with mounted volumes. A service can still start while quietly logging denials, so functional validation must include real requests, not just systemctl status.

Useful checks include:

  • Confirming the service is active:

bash systemctl status <service>

  • Checking for recent denials in audit logs:

bash sudo ausearch -m avc -ts recent

  • Reviewing contextual denials after a test request:

bash sudo grep -i denied /var/log/audit/audit.log

If you see denials, determine whether they are expected policy controls or mislabeling. For example, a web server reading files from a non-standard path may need the correct file context rather than a policy relaxation. In many cases, the correct fix is relabeling or adjusting the service’s SELinux context, not disabling enforcement.

If you also manage network exposure, make sure firewall rules and SELinux port labels align. An enforcing SELinux policy can block access even when the firewall is open, so it is useful to validate alongside a network control baseline such as CentOS FirewallD Configuration for Secure Network Access Control.

What to do when a service is blocked

When enforcement causes a service to fail, use the smallest safe correction that matches the root cause.

Typical decision rules are:

  • If the service cannot read a file, check file labels first.
  • If the service cannot bind to a port, verify the SELinux port context.
  • If the service needs access to a custom directory, verify the directory’s SELinux context and persistent labeling.
  • If a denial appears legitimate and unavoidable, evaluate a policy adjustment rather than disabling SELinux globally.

You can inspect file contexts with:

ls -Z /path/to/resource

And review service-related contexts with commands such as:

ps -Z -C <process>

A common operational mistake is to respond to the first denial by switching back to permissive mode. That may restore functionality temporarily, but it also hides policy gaps and leaves the host less protected than intended. Keep enforcing mode in place whenever possible and correct the underlying labeling or policy issue.

Roll back safely if enforcing mode breaks production

If you need to restore service quickly, use the runtime switch first:

sudo setenforce 0

Confirm the rollback:

getenforce

Expected output:

Permissive

This reduces risk immediately while keeping SELinux active enough to continue collecting denial information. That makes it easier to troubleshoot without fully disabling policy.

If you must make permissive mode persistent, edit /etc/selinux/config again and set:

SELINUX=permissive

Then reboot only if you need the boot-time state to match the file. Remember that permissive is a temporary operational fallback, not the preferred steady state for a security-hardened server.

Optional improvements before production use

Once enforcing mode is stable, there are a few hardening checks worth doing before you call the system ready.

First, review the audit log after a normal workload cycle. The goal is to catch denials that only appear under real traffic, scheduled jobs, or backup operations. A short validation window is often not enough for services with asynchronous tasks.

Second, confirm that custom content and configuration files have the correct labels. This is especially important after migrations, manual file copies, or restores from backup. Incorrect labels are one of the most common causes of avoidable SELinux denials.

Third, document any exceptions you had to make. If a host needs non-default policy rules or local adjustments, those changes should be tracked so the next rebuild or patch cycle does not undo them.

Finally, if you are standardizing SELinux across multiple CentOS systems, compare the CentOS 7 configuration with your newer baseline. A companion procedure such as How to Install and Configure SELinux on CentOS 8 can help you spot differences in how you validate or stage enforcement during a later platform upgrade.

Final verification checklist

Before you treat the host as production-ready, confirm the following:

  • getenforce returns Enforcing
  • sestatus shows SELinux is enabled and in enforcing mode
  • /etc/selinux/config contains SELINUX=enforcing
  • Critical services start and respond as expected
  • Recent audit logs do not contain unexplained denials
  • Any required file, port, or process contexts have been reviewed

If those checks pass, you have more than a configuration change—you have a validated enforcement state that is much less likely to surprise you after the next reboot.

Configuring SELinux enforcing mode in CentOS 7 is not just about flipping a setting. The operational goal is to enable policy, validate the workloads that matter, and keep a safe rollback path in case a service depends on incorrect labels or an unapproved exception.

Use this guidance together with Ubuntu login activity audit to connect the workflow with related operational context already available on the site.

Continue learning

Related content