SELinux 模式

SELinux 为系统中的资源提供了另外的安全层。
与 DAC(自由访问控制)相反,它提供 MAC(强制访问控制)。
在我们深入设置 SELinux 模式之前,让我们看看有哪些不同的 SELinux 操作模式以及它们是如何工作的。
SELinux 可以在以下 3 种模式中的任何一种下运行:

  1. Enforced:阻止违反策略的操作,并在审核日志中记录相应的事件。

  2. Permissive : 违反政策的行为只记录在审计日志中。

  3. Disabled:SELinux 完全禁用。

禁用 SELinux

有时当我们不熟悉 SELinux 功能时,最好将其禁用。
我们无法在不重新启动的情况下禁用 SELinux。
另一种选择是将 SELinux 设置为 Permissive 模式。
要完全禁用 SELinux,请编辑配置文件 /etc/sysconfig/selinux 或者 /etc/selinux/config,它是 /etc/sysconfig/selinux 文件的软链接。

# ls -l /etc/sysconfig/selinux
lrwxrwxrwx. 1 root root 17 Mar  2 13:03 /etc/sysconfig/selinux -> ../selinux/config
# cat /etc/sysconfig/selinux
# 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 two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

编辑此文件并重新启动系统以使更改生效。

使用内核引导参数禁用 SELinux
另一种永久禁用 SELinux 的方法是编辑内核引导参数。
编辑 /etc/grub.conf 文件并将 selinux=0 选项添加到引导选项以在引导时禁用 SELinux。
在这种情况下,/etc/sysconfig/selinux 中的设置将被忽略。

# cat /etc/grub.conf
........
	root (hd0,0)
	kernel /vmlinuz-2.6.32-279.el6.x86_64 root=/dev/md3 selinux=0
	initrd /initramfs-2.6.32-279.el6.x86_64.img
.........
欢迎 on it road

永久更改 SELinux 模式

使用 /etc/sysconfig/selinux 文件
将 SELinux 模式永久更改为 Enforcing 或者 Permissive 的一种方法是编辑 /etc/sysconfig/selinux 文件并将 SELINUX 参数值设置为 enforcing 或者 permissive。

# ls -l /etc/sysconfig/selinux
lrwxrwxrwx. 1 root root 17 Mar  2 13:03 /etc/sysconfig/selinux -> ../selinux/config
# cat /etc/sysconfig/selinux
# 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 two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

编辑此文件并重新启动系统以使更改生效。

使用内核引导参数
我们也可以在启动时使用 Kernel boot 参数来设置 SELinux 模式。
为此,请编辑 /etc/grub.conf 文件并将选项“selinux=1 enforcing=[0|1]”添加到引导参数中。

# cat /etc/grub.conf
........
	root (hd0,0)
	kernel /vmlinuz-2.6.32-279.el6.x86_64 root=/dev/md3 selinux=1 enforcing=0
	initrd /initramfs-2.6.32-279.el6.x86_64.img
.........

selinux=1 -> 启用 SELinux
enforcing=0 -> 许可模式
enforcing=1 -> 强制模式

如何在 RHEL/CentOS 中启用/禁用 SELinux 模式

暂时切换 SELinux 模式

要临时在 SELinux 模式之间切换,我们可以使用 setenforce 命令,如下所示:

# setenforce [ Enforcing | Permissive | 1 | 0 ]

0 -> Permissive
1 -> Enforcing

或者我们可以简单地将值回显到伪文件 /sys/fs/selinux/enforce 或者 /selinux/enforce 中。

# echo [0|1] > /sys/fs/selinux/enforce

要检查 SELinux 的当前模式:

# getenforce
Enforcing

或者我们也可以使用 sestatus 命令获取详细状态:

# sestatus
SELinux status:                 enabled         
SELinuxfs mount:                /selinux        --> virtual FS similar to /proc
Current mode:                   enforcing       --> current mode of operation 
Mode from config file:          permissive      --> mode set in the /etc/sysconfig/selinux file.
Policy version:                 24
Policy from config file:        targeted
日期:2020-09-17 00:11:51 来源:oir作者:oir