CentOS下SELinux安全设置

CentOS下SELinux安全设置

centoscentos 系统安全防护 2017年12月14日html

529 0 0linux

Linux就该这么学

1、SELinux配置文件web

在CentOS 7系统中部署SELinux很是简单,因为SELinux已经做为模块集成到内核中,默认SELinux已经处于激活状态。对管理员来讲,更多的是须要配置与管理SELinux,CentOS 7系统中SELinux全局配置文件为/etc/sysconfig/selinux,内容以下:vim

 

[root@centos7 ~]# vim /etc/sysconfig/selinuxcentos

 

# This file controls the state of SELinux on the system.安全

# SELINUX= can take one of these three values:服务器

#     enforcing - SELinux security policy is enforced.app

#     permissive - SELinux prints warnings instead of enforcing.dom

#     disabled - No SELinux policy is loaded.tcp

SELINUX=enforcing

# SELINUXTYPE= can take one of these two values:

#     targeted - Targeted processes are protected,

#     mls - Multi Level Security protection.

SELINUXTYPE=targeted

 

SELinux=enforcing为SELinux总开关,有效值能够是enforcing、permissive或disabled。

其中,disabled表明禁用SELinux功能,因为SELinux是内核模块功能,因此若是设置禁用,须要重启计算机。permissive表明仅警告模式,处于此状态下时,当主题程序试图访问无权限的资源时,SELinux会记录日志但不会拦截该访问,也就是最终访问是成功的,只是在SELinux日志中记录而已。enforcing模式表明强制开启,SELinux会拦截非法的资源访问并记录相关日志。

 

使用setenforce能够临时在enforcing模式与permissive模式之间切换,切换会被马上应用于当前系统,计算机重启后无效,永久修改模式须要修改配置文件。

 

[root@centos7 ~]# setenforce 0           #设置SELinux为permissive模式

[root@centos7 ~]# setenforce 1           #设置SELinux为enforcing模式

 

 

2、SELinux安全上下文

SELinux会为进程与文件添加安全信息标签,如:SELinux用户、角色、类型以及可选的级别。当运行SELinux后全部这些信息都是访问控制的依据。下面经过一个实例文件查看SELinux安全上下文,使用ls -Z命令就能够看到文件或目录的这些上下文信息,而ps aux –Z则能够查看进程的安全上下文信息:

[root@centos7 ~]# ls -Z anaconda-ks.cfg 

-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg

[root@centos7 ~]# ps aux -Z

 

SELinux的安全上下文包括

用户:角色:类型:级别

 

3、SELinux排错

无论SELinux策略是容许仍是拒绝资源的访问请求行为,都会记录日志,也就是AVC(Access Vector Cache)。全部SELinux拒绝的消息都会被记录进日志,根据系统中安装运行的服务进程不一样,拒绝日志消息会被记录到不一样的文件中,表6-2列出了进程与日志文件的关系。

表6-2

日志文件                      进程

/var/log/audit/audit.log           auditd服务开启

/var/log/messages                auditd服务关闭,rsyslogd服务开启

/var/log/audit/audit.log,/var/log/messages安装setroubleshoot相关软件包

autitd与rsyslogd同时开启

 

对于大多数生产环境中的服务器而言,更多的是没有部署安装图形界面的Linux系统,咱们须要手动查看日志文件。在此建议管理员安装setroubleshoot相关的软件包,这样能够将本来生涩的AVC拒绝日志转换为可读性比较高的setroubleshoot日志。查看日志可使用以下两种方法:

[root@centos7 ~]# grep setroubleshoot /var/log/messages

[root@centos7 ~]# grep denied /var/log/audit/audit.log

 

查看messages日志会提示,根据黑体字提示运行sealert命令才能够看到人性化报错信息。

setroubleshoot: SELinux is preventing /usr/sbin/httpd from read access on the file index.html. For complete SELinux messages. run sealert -l 7082b8b4-70f4-42fb-92ea-08a51299d080

 

[root@centos7 ~]# sealert -l 7082b8b4-70f4-42fb-92ea-08a51299d080

 

 

4、修改安全上下文

有多种方式能够修改与管理SELinux安全上下文,如:chcon、semanage、fcontext以及restorecon命令。

1. chcon命令

描述:修改文件SELinux安全上下文。

用法:chcon [选项] [-u SELinux用户] [-r 角色] [-l 范围] [-t 类型] 文件

chcon [选项] --reference=参考文件 文件

选项:-u 修改用户属性

-r 修改角色属性

-l 修改范围属性

-t 修改类型属性

示例:

(1)修改文件安全上下文。

[root@centos7 ~]# cp --preserve=all /etc/passwd /root/ #复制文件(保留上下文信息)

[root@centos7 ~]# ls -Z /root/passwd             #查看文件SELinux安全上下文

[root@centos7 ~]# chcon -t admin_home_t /root/passwd #修改文件安全上下文中的类型

[root@centos7 ~]# ls -Z /root/passwd

 

(2)修改目录安全上下文。

 

[root@centos7 ~]# chcon -R -t admin_home_t /root/   #递归修改目录安全上下文

 

(3)根据参考文件修改目标文件安全上下文。

 

[root@centos7 ~]# chcon --reference=/etc/passwd /root/passwd

 

经过chcon修改的安全上下文并非SELinux预设的安全上下文,当文件系统重置SELinux安全标签或使用restorecon命令重置指定目录的安全标签后,全部文件与目录的安全标签会被还原为系统预设值,若是须要修改SELinux默认的预设安全上下文,须要使用semanage命令添加或修改。

5、semanage命令

描述:SELinux策略管理工具。

用法:semanage fcontext [-S store] -{a|d|m|l|n|D} [-frst] file_spec

选项:-a,--add添加预设安全上下文

-d,--delete删除指定的预设安全上下文

-D,--deleteall    删除全部的预设自定义上下文

-m,--modify修改指定的预设安全上下文

-l,--list     显示预设安全上下文

-n,--noheading     不显示头部信息

示例:

(1)查看SELinux策略默认的预设安全上下文信息,系统将列出策略中定义的全部目录与安全上下文信息。

 

[root@centos7 ~]#semanage fcontext -l

 

(2)修改策略,添加一条新的预设安全上下文信息。

[root@centos7 ~]# semanage fcontext -a -t samba_share_t /test/test.txt

[root@centos7 ~]# mkdir /test; touch /test/test.txt

[root@centos7 ~]# ls -Z /test/test.txt

 

 

(3)使用restorecon命令,还原test.txt文件的安全上下文为预设值。

[root@centos7 ~]# restorecon /test/test.txt

[root@centos7 ~]# ls -Z /test/test.txt

 

 

(4)递归设置目录的预设安全上下文。

[root@centos7 ~]# semanage fcontext -a -t httpd_sys_content_t "/site/www(/.*)?"

[root@centos7 ~]# mkdir -p /site/www/{web1,web2}

[root@centos7 ~]# touch /site/www/{web1,web2}/index.html

[root@centos7 ~]# ls -RZ /site/www

[root@centos7 ~]# restorecon -R /site/

 

 

(5)删除预设安全上下文。

 

[root@centos7 ~]# semanage fcontext -d /test/ test.txt

 

(6)检查预设SELinux安全上下文。

 

[root@centos7 ~]# matchpathcon /site/www/

 

6.2.6 查看与修改布尔值

SELinux布尔值能够实时被修改。如,你能够在不从新加载或编译SELinux策略的状况下容许服务访问NFS文件系统。getsebool是用来查看SELinux布尔值的命令,用法比较简单,-a选项用来查看全部的布尔值。通常建议管理员经过管道过滤本身须要的布尔值参数,如getsebool -a |grep ftp过滤与FTP相关的布尔值信息,显示效果中左侧为关键词,右侧为开关,on表明开,off表明关,具体命令以下。

[root@centos7 ~]# getsebool -a

abrt_anon_write  off

abrt_handle_event  off

allow_console_login  on

allow_cvs_read_shadow  off

allow_daemons_dump_core  on

allow_daemons_use_tcp_wrapper  off

allow_daemons_use_tty  on

allow_domain_fd_use  on

… …部份内容省略… …

 

 

修改SELinux布尔值状态也很是简单,使用setsebool name X便可实现。其中,name是布尔值名称,X表明on或off。默认setsebool命令修改的布尔值参数会当即生效,但计算机重启后会被还原,若是但愿永久修改,须要使用-p参数。

[root@centos7 ~]# setsebool ftp_home_dir on

[root@centos7 ~]# setsebool -p ftp_home_dir on

相关文章
相关标签/搜索