Chapter 2. Changing SELinux states and modes
When enabled, SELinux can run in one of two modes: enforcing or permissive. The following sections show how to permanently change into these modes.
2.1. Permanent changes in SELinux states and modes
As discussed in SELinux states and modes, SELinux can be enabled or disabled. When enabled, SELinux has two modes: enforcing and permissive.
				Use the getenforce or sestatus commands to check in which mode SELinux is running. The getenforce command returns Enforcing, Permissive, or Disabled.
			
				The sestatus command returns the SELinux status and the SELinux policy being used:
			
When systems run SELinux in permissive mode, users and processes might label various file-system objects incorrectly. File-system objects created while SELinux is disabled are not labeled at all. This behavior causes problems when changing to enforcing mode because SELinux relies on correct labels of file-system objects.
					To prevent incorrectly labeled and unlabeled files from causing problems, SELinux automatically relabels file systems when changing from the disabled state to permissive or enforcing mode. Use the fixfiles -F onboot command as root to create the /.autorelabel file containing the -F option to ensure that files are relabeled upon next reboot.
				
					Before rebooting the system for relabeling, make sure the system will boot in permissive mode, for example by using the enforcing=0 kernel option. This prevents the system from failing to boot in case the system contains unlabeled files required by systemd before launching the selinux-autorelabel service. For more information, see RHBZ#2021835.
				
2.2. Changing SELinux to permissive mode
When SELinux is running in permissive mode, SELinux policy is not enforced. The system remains operational and SELinux does not deny any operations but only logs AVC messages, which can be then used for troubleshooting, debugging, and SELinux policy improvements. Each AVC is logged only once in this case.
Prerequisites
- 
						The selinux-policy-targeted,libselinux-utils, andpolicycoreutilspackages are installed on your system.
- 
						The selinux=0orenforcing=0kernel parameters are not used.
Procedure
- Open the - /etc/selinux/configfile in a text editor of your choice, for example:- vi /etc/selinux/config - # vi /etc/selinux/config- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Configure the - SELINUX=permissiveoption:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Restart the system: - reboot - # reboot- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
Verification
- After the system restarts, confirm that the - getenforcecommand returns- Permissive:- getenforce - $ getenforce Permissive- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
2.3. Changing SELinux to enforcing mode
When SELinux is running in enforcing mode, it enforces the SELinux policy and denies access based on SELinux policy rules. In RHEL, enforcing mode is enabled by default when the system was initially installed with SELinux.
Prerequisites
- 
						The selinux-policy-targeted,libselinux-utils, andpolicycoreutilspackages are installed on your system.
- 
						The selinux=0orenforcing=0kernel parameters are not used.
Procedure
- Open the - /etc/selinux/configfile in a text editor of your choice, for example:- vi /etc/selinux/config - # vi /etc/selinux/config- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Configure the - SELINUX=enforcingoption:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Save the change, and restart the system: - reboot - # reboot- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - On the next boot, SELinux relabels all the files and directories within the system and adds SELinux context for files and directories that were created when SELinux was disabled. 
Verification
- After the system restarts, confirm that the - getenforcecommand returns- Enforcing:- getenforce - $ getenforce Enforcing- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
Troubleshooting
After changing to enforcing mode, SELinux may deny some actions because of incorrect or missing SELinux policy rules.
- To view what actions SELinux denies, enter the following command as root: - ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -ts today - # ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -ts today- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Alternatively, with the - setroubleshoot-serverpackage installed, enter:- grep "SELinux is preventing" /var/log/messages - # grep "SELinux is preventing" /var/log/messages- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- If SELinux is active and the Audit daemon ( - auditd) is not running on your system, then search for certain SELinux messages in the output of the- dmesgcommand:- dmesg | grep -i -e type=1300 -e type=1400 - # dmesg | grep -i -e type=1300 -e type=1400- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
See Troubleshooting problems related to SELinux for more information.
2.4. Enabling SELinux on systems that previously had it disabled
To avoid problems, such as systems unable to boot or process failures, when enabling SELinux on systems that previously had it disabled, resolve Access Vector Cache (AVC) messages in permissive mode first.
When systems run SELinux in permissive mode, users and processes might label various file-system objects incorrectly. File-system objects created while SELinux is disabled are not labeled at all. This behavior causes problems when changing to enforcing mode because SELinux relies on correct labels of file-system objects.
To prevent incorrectly labeled and unlabeled files from causing problems, SELinux automatically relabels file systems when changing from the disabled state to permissive or enforcing mode.
					Before rebooting the system for relabeling, make sure the system will boot in permissive mode, for example by using the enforcing=0 kernel option. This prevents the system from failing to boot in case the system contains unlabeled files required by systemd before launching the selinux-autorelabel service. For more information, see RHBZ#2021835.
				
Procedure
- Enable SELinux in permissive mode. For more information, see Changing to permissive mode.
- Restart your system: - reboot - # reboot- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Check for SELinux denial messages. For more information, see Identifying SELinux denials.
- Ensure that files are relabeled upon the next reboot: - fixfiles -F onboot - # fixfiles -F onboot- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - This creates the - /.autorelabelfile containing the- -Foption.Warning- Always switch to permissive mode before entering the - fixfiles -F onbootcommand.- By default, - autorelabeluses as many threads in parallel as the system has available CPU cores. To use only a single thread during automatic relabeling, use the- fixfiles -T 1 onbootcommand.
- If there are no denials, switch to enforcing mode. For more information, see Changing SELinux modes at boot time.
Verification
- After the system restarts, confirm that the - getenforcecommand returns- Enforcing:- getenforce - $ getenforce Enforcing- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
Next steps
To run custom applications with SELinux in enforcing mode, choose one of the following scenarios:
- 
						Run your application in the unconfined_service_tdomain.
- Write a new policy for your application. See the Writing a custom SELinux policy section for more information.
2.5. Disabling SELinux
When you disable SELinux, your system does not load your SELinux policy. As a result, the system does not enforce the SELinux policy and does not log Access Vector Cache (AVC) messages. Therefore, all benefits of running SELinux are lost.
Do not disable SELinux except in specific scenarios, such as performance-sensitive systems where the weakened security does not impose significant risks.
If your scenario requires to perform debugging in a production environment, temporarily use permissive mode instead of permanently disabling SELinux. See Changing to permissive mode for more information about permissive mode.
Prerequisites
- The - grubbypackage is installed:- rpm -q grubby - $ rpm -q grubby grubby-<version>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
Procedure
- Configure your boot loader to add - selinux=0to the kernel command line:- sudo grubby --update-kernel ALL --args selinux=0 - $ sudo grubby --update-kernel ALL --args selinux=0- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Restart your system: - reboot - $ reboot- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
Verification
- After the reboot, confirm that the - getenforcecommand returns- Disabled:- getenforce - $ getenforce Disabled- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
2.6. Changing SELinux modes at boot time
On boot, you can set the following kernel parameters to change the way SELinux runs:
- enforcing=0
- Setting this parameter causes the system to start in permissive mode, which is useful when troubleshooting issues. Using permissive mode might be the only option to detect a problem if your file system is too corrupted. Moreover, in permissive mode, the system continues to create the labels correctly. The AVC messages that are created in this mode can be different than in enforcing mode. - In permissive mode, only the first denial from a series of the same denials is reported. However, in enforcing mode, you might get a denial related to reading a directory, and an application stops. In permissive mode, you get the same AVC message, but the application continues reading files in the directory and you get an AVC for each denial in addition. 
- selinux=0
- This parameter causes the kernel to not load any part of the SELinux infrastructure. The init scripts notice that the system booted with the - selinux=0parameter and touch the- /.autorelabelfile. This causes the system to automatically relabel the next time you boot with SELinux enabled.Important- Do not use the - selinux=0parameter in a production environment. To debug your system, temporarily use permissive mode instead of disabling SELinux.
- autorelabel=1
- This parameter forces the system to relabel similarly to the following commands: - touch /.autorelabel reboot - # touch /.autorelabel # reboot- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - If a file system contains a large amount of mislabeled objects, start the system in permissive mode to make the autorelabel process successful.