如何在CentOS 8上禁用SELinux

时间:2020-03-05 15:24:27  来源:igfitidea点击:

安全性增强的Linux或者SELinux是内置在基于RHEL的发行版使用的Linux内核中的安全机制。

SELinux通过允许管理员和用户基于策略规则控制对对象的访问,为系统增加了一层安全保护。

SELinux策略规则指定进程和用户如何相互交互以及进程和用户如何与文件交互。
如果没有明确允许访问对象的规则(例如,打开文件的进程),则拒绝访问。

SELinux具有三种操作模式:

  • 实施:SELinux允许根据SELinux策略规则进行访问。
  • 允许的:SELinux仅记录在强制模式下运行将被拒绝的操作。此模式对于调试和创建新的策略规则很有用。
  • 禁用:未加载SELinux策略,并且不记录任何消息。

默认情况下,在CentOS 8中,启用了SELinux并处于强制模式。
强烈建议将SELinux保持为强制模式。
但是,有时它可能会干扰某些应用程序的功能,因此我们需要将其设置为许可模式或者将其完全禁用。

在本教程中,我们将说明在CentOS 8上禁用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模式更改为宽松

启用后,SELinux可以处于强制模式或者许可模式。
我们可以使用以下命令将模式从目标临时更改为许可:

sudo setenforce 0

但是,此更改仅对当前运行时会话有效,并且不会在两次重新引导之间持续存在。

要将SELinux模式永久设置为许可,请执行以下步骤:

  • 打开“/etc/selinux/config”文件,并将“ SELINUX” mod设置为“ permissive”:/etc/selinux/config
# 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”:/etc/selinux/config
# 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