在 CentOS 8 上禁用 SELinux
在本教程中,我们将说明在 CentOS 8 上禁用 SELinux 。
安全性增强的 Linux 或 SELinux 是内置在基于 RHEL 的发行版使用的 Linux 内核中的安全性机制。
SELinux 通过允许管理员和用户基于策略规则控制对对象的访问,为系统增加了一层安全保护。
SELinux 策略规则指定进程和用户如何相互交互以及进程和用户如何与文件交互。如果没有明确允许访问对象的规则(例如,打开文件的进程),则拒绝访问。
SELinux 具有三种操作模式:
- Enforcing: SELinux 允许根据 SELinux 策略规则进行访问。
- Permissive: SELinux 仅记录在强制模式下运行将被拒绝的操作。此模式对于调试和创建新的策略规则很有用。
- Disabled: 未加载 SELinux 策略,并且不记录任何消息。
默认情况下,在 CentOS 8 中,启用了 SELinux 并处于强制模式。强烈建议将 SELinux 保持为强制模式。但是,有时它可能会干扰某些应用程序的功能,因此您需要将其设置为许可模式或完全禁用它。
先决条件
只有 root 用户或具有 sudo 特权的用户才能更改 SELinux 模式。
检查 SELinux 模式
使用以下 sestatus
命令检查 SELinux 的状态和运行方式:
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
Memory protection checking: actual (secure)
Max kernel policy version: 31
上面的输出显示 SELinux 已启用并将其设置为强制模式。
将 SELinux 模式更改为 permissive
启用后, SELinux 可以处于强制模式或许可模式。您可以使用以下命令将模式临时更改:
sudo setenforce 0
但是,此更改仅对当前运行时会话有效,并且不会在两次重新引导之间持续存在。
要将 SELinux 模式永久设置为 permissive,请执行以下步骤:
-
打开
/etc/selinux/config
文件并将SELINUX
设置为permissive
:# 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=permissive # SELINUXTYPE= can take one of these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
-
保存文件并运行
setenforce 0
命令以更改当前会话的 SELinux 模式:sudo shutdown -r now
禁用 SELinux
强烈建议不要禁用 SELinux ,而应将模式更改为宽松模式。仅在需要使应用程序正常运行时才禁用 SELinux 。
执行以下步骤永久禁用 CentOS 8 系统上的 SELinux :
-
打开
/etc/selinux/config
文件并将其SELINUX
值更改为disabled
:# 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 these three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
-
保存文件并重新启动系统:
sudo shutdown -r now
-
引导系统后,使用以下
sestatus
命令来验证 SELinux 已被禁用:sestatus
输出应如下所示:
SELinux status: disabled
结论
SELinux 是一种通过实施强制访问控制 (MAC) 来保护系统安全的机制。 SELinux 在 CentOS 8 系统上默认为启用,但是可以通过编辑配置文件并重新引导系统来禁用 SELinux 。
要了解有关 SELinux 强大功能的更多信息,请访问 CentOS SELinux 指南。