Enable SELinux to enforce mandatory access control, or disable it temporarily for debugging when its policies block trusted applications.
See What is SELinux?
Usage notes
The screenshots in this topic are based on the following images:
-
Alibaba Cloud Linux 3.2104 LTS 64-bit
-
CentOS 7.6 64-bit
For other Linux distributions such as Fedora, RHEL 9, Debian, or Ubuntu, refer to the official documentation. SELinux configuration methods vary by distribution.
Enable SELinux
-
Connect to the Linux ECS instance.
-
Check the SELinux status:
sestatusIf
disabledis returned forSELinux status, SELinux is disabled.[root@iZ***Z ~]# sestatus SELinux status: disabled [root@iZ***Z ~]# -
Open the SELinux
configfile:sudo vi /etc/selinux/config -
Find
SELINUX=disabledand pressIto enter Insert mode.SELinux supports the following modes:
Mode
Value
Description
Enforcing
SELINUX=
enforcingDenies all requests that violate SELinux policy.
Permissive
SELINUX=
permissiveLogs policy violations without denying requests.
The current configuration file shows
SELINUX=disabled, indicating that SELinux is disabled. Change this value to the target mode.# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted -
Press
Esc, enter:wq, and press Enter to save and close the file.ImportantAfter modifying the
configfile, you must restart the instance for changes to take effect. To prevent boot failure, create an.autorelabelfile before restarting. -
Create the
.autorelabelfile in the root directory:sudo touch /.autorelabel -
Restart the ECS instance. See Restart an instance.
NoteDuring restart, SELinux automatically relabels all system files. This process may take time depending on the number and size of files. Do not operate the instance until relabeling completes.
Disable SELinux
Disabling SELinux makes your system more vulnerable to attacks. Ensure other security measures are in place before proceeding.
-
Connect to the Linux ECS instance.
-
Check the SELinux status:
sestatusIf
enabledis returned forSELinux status, SELinux is enabled.[testuser@iZ xxx Z ~]$ sestatus SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Max kernel policy version: 31 -
Temporarily or permanently disable SELinux.
NoteTemporarily disabling SELinux affects only the current session; the original status restores after restart. To permanently disable SELinux, modify the configuration file.
Temporarily disable SELinux
Temporarily set SELinux from
enforcingtopermissive:sudo setenforce 0Permanently disable SELinux
-
Open the SELinux
configfile:sudo vi /etc/selinux/config -
Find
SELINUX=enforcingorSELINUX=permissive, pressIto enter Insert mode, and change it toSELINUX=disabled. The configuration file content is as shown below.# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted -
Press
Esc, enter:wq, and press Enter to save and close the file. -
Restart the ECS instance.
See Restart an instance.
-
Check the SELinux status:
sestatusIf
disabledis returned forSELinux status, SELinux is permanently disabled.
-
References
In permissive mode, review logs to identify SELinux policy violations and adjust rules before switching to enforcing. See View system logs and screenshots.