如何在 CentOS 8 上禁用 SELinux

Security Enhanced Linux 或者 SELinux 是一个安全机制,广泛内置于 RHEL 系列版本的 Linux kernel 中。

SELinux 给系统添加了一个额外的安全层,它允许管理员和用户基于政策规则控制对对象的访问。

SELinux 政策规则指定进程和用户如何相互交互,以及进程和用户如何与文件交互。 如果没有明显的规则允许对一个对象的访问,例如:一个进程要打开一个文件,这个访问是被禁止的。

SELinux 有三个操作模式:

  • Enforcing: SELinux 允许基于 SELinux 政策规则的访问
  • Permissive: 如果 SELinux 运行在 enforcing 模式,那么 SELinux 仅仅记录被禁止动作的日志。这个模式在调试和创建新政策规则的时候很有用处。
  • Disabled: 不加载任何 SELinux 政策,不记录任何信息。

在 CentOS 8 中,SELinux 默认被启用,并且处于 enforcing 模式。强烈推荐将 SELinux 保持在 enforcing 模式。当然,有时候它可能打扰到一些应用的运行,并且你需要将它设置成 permissive 模式或者完全禁用它。

在这篇教程中,我们将会讲解如何在 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 被启用,并且设置在 enforcing 模式。

三、 修改 SELinux 模式到 Permissive

当被启用的时候,SELinux 可以被设置成 enforcing 或者 permissive 模式。通过下面的命令,你可以临时将模式修改为 permissive:

sudo setenforce 0

不管怎样,这个修改仅仅对于当前运行的会话有效,并且不会持久化,重启后失效。

想要永久的将 SELinux 模式设置成 permissive 模式,请按照下面的步骤来:

01.打开/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

02.保存文件,并且运行setenforce 0命令,将当前 SELinux 模式修改成 permissive。

sudo shutdown -r now

四、禁用 SELinux

相比于禁用 SELinux,我们强烈你推荐将模式修改为 permissive。 仅仅在你的应用良好运行的情况下,才能禁用 SELinux。

执行下面的步骤,才能在你的 CentOS 8 系统上永久的禁用 SELinux:

01.打开/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

2.保存文件,并且重启系统:

sudo shutdown -r now

03.当系统重启后,使用sestatus命令来验证 SELinux 是否已经被禁用了:

sestatus

输出将会像这样:

SELinux status:                 disabled

五、总结

SELinux 是一个通过实现强制访问控制(MAC)来保证系统安全的机制。SELinux 在 CentOS 8 系统上默认被启用,但是它也能通过编辑配置文件并且重启系统进行禁用。

想要了解更多关于 SELinux 的强大特性,请浏览:CentOS SELinux guide

评论区
头像
    头像

    Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://www.binance.com/ro/register?ref=V3MG69RO

    头像

    This article opened my eyes, I can feel your mood, your thoughts, it seems very wonderful. I hope to see more articles like this. thanks for sharing.