Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux setenforce command

Linux setenforce command

Linux Command Reference Linux Command Reference


setenforce is a command-line tool in Linux systems used to modify the running mode of SELinux (Security-Enhanced Linux). SELinux is a mandatory access control (MAC) security mechanism, and setenforce the command allows administrators to temporarily change the SELinux enforcement policy without restarting the system.


SELinux mode overview

Before delving into setenforce , we need to first understand the three running modes of SELinux:

  1. Enforcing mode: Enforce the SELinux policy, denying unauthorized access
  2. Permissive mode: Only log policy violations but do not block, used for troubleshooting
  3. Disabled mode: Completely disable SELinux (not recommended, reduces system security)

setenforce The command is used to switch between Enforcing and Permissive modes.


Command syntax

setenforce The basic syntax of the command is as follows:

setenforce [Enforcing|Permissive|1|0]


Parameter Description

Parameter options Numeric equivalent Description
Enforcing 1 Set SELinux to enforcing mode
Permissive 0 Set SELinux to permissive mode, only log violations without blocking
(no parameters) - Display the current SELinux status (supported in some versions)

Usage examples

Example 1: View the current SELinux status

getenforce

The output might be:

Example 2: Set SELinux to Permissive mode

Example

sudo

setenforce

0

or

sudo

setenforce Permissive

Example 3: Set SELinux to Enforcing mode

Example

sudo

setenforce

1

or

sudo

setenforce Enforcing


Notes

  1. Permission requirements: When using setenforce Requires root privileges, usually need to add sudo
  2. Temporary changes:setenforce changes are only valid for the current session; after a restart, the settings in the configuration file are restored
  3. Permanent configuration: To permanently change the SELinux mode, you need to modify /etc/selinux/config file
  4. Disabled mode:setenforce cannot be used to enable/disable SELinux; it can only switch between Enforcing and Permissive modes

Practical Application Scenarios

Scenario 1: Troubleshooting

When an application cannot run due to SELinux policy issues, you can temporarily set it to Permissive mode for testing:

Example

sudo

setenforce

0

Test the application

If the problem is resolved, it is a SELinux policy issue

sudo

setenforce

1

 

Restore after testing is complete

Scenario 2: Policy development

When developing a new SELinux policy, use Permissive mode to collect violation logs:

Example

sudo

setenforce

0

Perform operations to generate SELinux logs

sudo

grep

AVC

/

var

/

log

/

audit

/

audit.log  

View violation records


Frequently Asked Questions

Q1: Why is the setenforce command ineffective?

Possible reasons:

Q2: How to permanently disable SELinux?

Completely disabling SELinux is not recommended, but if a permanent change is needed:

  1. Editing /etc/selinux/config file
  2. data to the child component via SELINUX= change the line to SELINUX=disabled
  3. Reboot the system

Q3: The difference between setenforce and getenforce?


Summary

setenforce is a simple yet powerful SELinux management tool that allows administrators to quickly switch between Enforcing and Permissive modes. Understanding and correctly using this command is essential for Linux system administration and security configuration. Remember, production environments should keep Enforcing mode as much as possible to ensure system security.


Linux Command Reference Linux Command Reference