CentOS7.4安装使用SELinux防护系统的简要说明 (centos7.4 selinux)

SELinux是安全增强型Linux(Security Enhanced Linux)的缩写,是一种在Linux操作系统中实现Mandatory access control(MAC,强制性访问控制)的机制,能够有效地防止未授权的访问和访问的结果。在CentOS7.4发行版中,SELinux已经默认安装并开启,但是由于其较为复杂的配置和使用,导致很多用户不知道如何使用SELinux防护自己的系统,本文将简要说明CentOS7.4安装使用SELinux防护系统的方法。

一、SELinux的安装

由于CentOS7.4默认安装了SELinux,所以无需再次安装。如果要安装其他版本的SELinux,可以使用yum命令进行安装:

yum install selinux-policy

二、SELinux的开关

在CentOS7.4中,SELinux默认开启。可以使用以下命令检查SELinux的状态:

sestatus

若显示如下信息,则表示SELinux处于enforcing状态:

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

Max kernel policy version: 31

如果需要关闭SELinux,可以使用以下命令:

setenforce 0

如果需要重新打开SELinux,可以使用以下命令:

setenforce 1

三、SELinux的配置

1.用selinuxconfig工具修改SELinux配置文件

SELinux的配置文件位于/etc/selinux/config中。可以使用selinuxconfig工具修改此文件的内容:

sudo selinuxconfig

在弹出的配置窗口中,可以选择SELinux的三种模式:Enforcing(强制模式)、Permissive(宽容模式)和Disable(关闭模式)。

Enforcing模式:在该模式下,SELinux是启用的,对系统的访问进行强制限制,不符合规则的访问行为将被阻止。

Permissive模式:在该模式下,SELinux处于宽容模式,只会发出警告消息,不会阻止访问行为。

Disable模式:在该模式下,SELinux处于关闭状态,和传统的Linux系统一样工作。

2.手动编辑SELinux配置文件

可以手动编辑SELinux配置文件/etc/selinux/config进行配置。

SELINUX=enforcing # SELinux默认开启,可以改为permissive或disabled进行设置

SELINUXTYPE=targeted # SELinux策略类型,目前有targeted和mls两种

SETLOCALDEFS=0 # 是否允许修改本地定义,一般为0

四、SELinux的使用

1.安装auditd,用于监测系统日志

在CentOS7.4中,auditd是SELinux的默认日志记录工具,可以使用以下命令进行安装:

yum install audit

安装完成后,需要启动auditd服务:

systemctl start auditd

2.添加SELinux策略

使用audit2allow工具来添加SELinux策略。audit2allow可以根据系统日志中的记录生成一些被SELinux拒绝的操作,并将这些操作转换为SELinux策略。

如下示例,在CentOS7.4中安装了Nginx服务,在启动Nginx时,SELinux会拒绝访问端口80号:

[root@localhost ~]# systemctl start nginx

Job for nginx.service fled because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for detls.

[root@localhost ~]# systemctl status nginx.service

nginx.service – The nginx HTTP and reverse proxy server

Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)

Active: fled (Result: exit-code) since Mon 2023-10-14 09:40:29 CST; 1min 39s ago

Process: 25198 ExecStart=/usr/in/nginx (code=exited, status=1/FLURE)

Oct 14 09:40:29 localhost.localdomn systemd[1]: Starting The nginx HTTP and reverse proxy server…

Oct 14 09:40:29 localhost.localdomn nginx[25198]: nginx: [emerg] bind() to 0.0.0.0:80 fled (13: Permission denied)

Oct 14 09:40:29 localhost.localdomn nginx[25198]: nginx: configuration file /etc/nginx/nginx.conf test fled

Oct 14 09:40:29 localhost.localdomn systemd[1]: nginx.service: control process exited, code=exited status=1

Oct 14 09:40:29 localhost.localdomn systemd[1]: Fled to start The nginx HTTP and reverse proxy server.

Oct 14 09:40:29 localhost.localdomn systemd[1]: Unit nginx.service entered fled state.

Oct 14 09:40:29 localhost.localdomn systemd[1]: nginx.service fled.

我们需要手动添加SELinux策略来允许Nginx访问端口80号:

grep nginx /var/log/audit/audit.log | audit2allow -M mynginx

然后再使用semodule命令将刚刚生成的mynginx.pp策略文件装载到SELinux中:

semodule -i mynginx.pp

最后再次启动Nginx,就可以正常访问端口80号了。

SELinux是一种实现强制性访问控制的机制,在保护系统安全方面发挥了重要作用。本文简要介绍了在CentOS7.4中安装使用SELinux进行系统防护的方法,包括SELinux的开关、配置和使用。了解并掌握SELinux的使用,可以让我们更好地保护系统安全。

相关问题拓展阅读:

CentOS 8 怎么禁用 SELinux 呢?

这表明SELinux已启动并正在运行。

要暂时禁用SELinux,请运行命令。

# setenforce 0

另外,您可以运行命令。

# setenforce Permissive

这些命令中的任何一个都将暂时禁用SELinux,直到下次重启为止。

 在CentOS 8上永久禁用SELinux

现在,让我们看看如何永久

禁用SELinux

。 SElinux的配置文件位于 /etc/selinux/config。 因此,我们需要对该文件进行一些修扰喊改。

现在嫌扒,使用命令检查SELinux的状态。

$ sestatus

SELinux status:  disabled

centos7.4 selinux的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于centos7.4 selinux,CentOS7.4安装使用SELinux防护系统的简要说明,CentOS 8 怎么禁用 SELinux 呢?的信息别忘了在本站进行查找喔。


数据运维技术 » CentOS7.4安装使用SELinux防护系统的简要说明 (centos7.4 selinux)