首页 > 编程笔记 > Linux笔记 阅读:2

SELinux开启和关闭的2种方式(附带实例)

SELinux可以永久地启动或者关闭,也可以临时启动或关闭。

永久启动或关闭SELinux

如果需要永久启动或关闭 SELinux,就需要通过 SELinux 配置文件来进行。打开并查看 SELinux 配置文件,命令如下:
[root@localhost ~]# vi /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.
# See also:
#     https://docs.fedoraproject.org/en-US/quick-docs/getting-started-with-selinux/#getting-started-with-selinux-selinux-states-and-modes
#
# NOTE: In earlier Fedora kernel builds, SELinux=disabled would also
# fully disable SELinux during boot. If you need a system with SELinux
# fully disabled instead of SELinux running with no policy loaded, you
# need to pass selinux=0 to the kernel command line. You can use grubby
# to persistently set the bootloader to boot with selinux=0:
#
#     grubby --update-kernel ALL --args selinux=0
#
# To revert back to SELinux enabled:
#
#     grubby --update-kernel ALL --remove-args selinux
#
SELinux=enforcing
#指定 SELinux 的运行模式,存在 enforcing(强制模式)、permissive(宽容模式)、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
#指定 SELinux的默认策略,存在有targeted(针对性保护策略,是默认策略)和mls(多级安全保护策略)两种策略)两种策略
要想关闭和启动 SELinux,只需修改 SELinux 的运行模式即可。三种模式的区别如下:
  1. SELinux=enforcing:强制模式,代表 SELinux 正常运行,所有的策略已经生效;
  2. SELinux=permissive:宽容模式,代表 SELinux 已经启动,但是只会显示警告信息,并不会实际限制进程访问文件或目录资源;
  3. SELinux=disable:关闭,代表 SELinux 被禁用了。

注意,如果从强制模式(enforcing)、宽容模式(permissive)切换到关闭模式(disabled),或者从关闭模式切换到其他两种模式,就必须重启 Linux 系统才能生效,但是如果是强制模式和宽容模式这两种模式互相切换,那么不用重启 Linux 系统就可以生效。因为 SELinux 是整合到 Linux 内核中的,所以必须重启才能正确关闭和启动。而且,如果从关闭模式切换到强制模式,那么重启 Linux 系统的速度会比较慢,因为需要重新写入安全上下文信息。

临时启动或关闭SELinux

我们也可以使用命令来进行 SELinux 的运行模式的查询和修改,查询命令如下:
[root@localhost ~]# getenforce
#查询 SELinux 的运行模式
Enforcing
#当前的 SELinux 是强制模式

除了可以查询 SELinux 的运行模式,还可以修改 SELinux 的运行模式。不过需要注意,setenforce 命令只能让 SELinux 在强制模式和宽容模式两种模式之间进行切换。如果从强制模式切换到关闭模式,或者从关闭模式切换到强制模式,就只能修改配置文件,setenforce 命令就无能为力了。

这种修改只是临时修改,系统重启之后就会不起作用,具体格式如下:
[root@localhost ~]# setenforce 选项
选项:
例如:
[root@localhost ~]# setenforce 0
#切换成宽容模式
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# setenforce 1
#切换成强制模式
[root@localhost ~]# getenforce
Enforcing

现在已经知道了 SELinux 的启动和关闭方法,那么,如何查询当前 SELinux 的策略呢?这就要使用 sestatus 命令了,具体如下:
[root@localhost ~]# sestatus
SELinux status: enabled
#SELinux 是启动的
SELinuxfs mount: /sys/fs/selinux
#SELinux 相关数据的挂载位置
SELinux root directory: /etc/selinux
#SELinux 的根目录位置
Loaded policy name: targeted
#目前的策略是针对性保护策略
Current mode: enforcing
#运行模式是强制模式
Mode from config file: enforcing
#配置文件内指定的运行模式也是强制模式
Policy MLS status: enabled
#是否支持 MLS 模式
Policy deny_unknown status: allowed
#是否拒绝未知进程
Max kernel policy version: 333
#策略版本

相关文章