1.selinux的启动、关闭与查看

目前支持三种模式

enforcing //强制模式,运行中

permissive //宽容模式,运作中,debug模式

disabled // 关闭

显示目前selinux的模式

getenforce

修改配置文件

vim /etc/selinux/config

实现selinux模式在enforcing与permissive之间切换

setenforce [0|1] //0转换为宽容模式1转换为强制模式

2.selinux type的修改

范例:将/etc/hosts 复制到root用户主目录,并查看相关的selinux的变化

cp /etc/hosts /root

ls -dZ /etc/hosts /root/hosts /root

范例:将/root/hosts 移动到/tmp下,并查看相关selinux类型文化

mv /root/hosts /tmp

ls -dZ /tmp /tmp/hosts

说明:单纯的复制,selinux的type字段会继承自目标目录,移动的话类型也会被移动过去

2.1 chcon

chcon [-R] [-t type] [-u user] [-r role] 文件

chcon [-R] --reference=范例文件 文件

选项与参数:

-R:遍历子目录也同时修改

-t:后面接安全性环境的类型字段,如httpd_sys_content_t

-u:后面接身份识别,如system_u

-r:后面接角色,例如system_r

--reference=范例文件,拿某个文件当范例来修改后续接的文件类型

范例:将刚刚的/tmp/hosts类型改为etc_t的类型

chcon -t net_conf_t /tmp/hosts

范例:以/var/spool/mail为依据,将/tmp/hosts修改成该类型

chcon --reference=/var/spool/mail /tmp/hosts

2.2 restorecon

restorecon [-Rv] 文件或目录

选项与参数:

-R:连同子目录一起修改

-v:将过程显示在屏幕上

范例

restorecon -Rv /root

2.3 semanage

系统记录在 /etc/selinux/targeted/contexts

semanage {login|user|port|interface|fcontext|translation} -l

semanage fcontext -{a|d|m} [-frst] file_spec

选项与参数:

fcontext:主要用在安全性环境方便,-l为查询的意思

-a:增加,可以增加一些目录的默认安全性环境类型设置

-m:修改

-d:删除

范例:查询一下/var/www的默认安全性环境设置为何

yum install policycoreutils-python

semanage fcontext -l | grep '/var/www'

范例:利用semanage设置/srv/action目录的默认安全性环境为public_content_t

mkdir /srv/action

ll -dZ /srv/action

semanage fcontext -l |grep '/srv'

semanage fcontext -a -t public_content_t "/srv/action(/.*)?"

semanage fcontext -l |grep '/srv/action'

cat /etc/selinux/targeted/contexts/files/file_contexts/local

restorecon -Rv /srv/action //尝试恢复默认值

ll -dZ /srv/action

3.selinux策略内的规则布尔值修订

3.1策略查询

yum install setools-console

seinfo [-Atrub]

选项与参数:

-A:列出selinux的状态,规则布尔值、身份识别、角色、类别等所有消息

-t:列出selinux的所有类别(type)种类

-r:列出selinux的所有角色(role)种类

-u:列出selinux的所有身份识别(user)种类

-b:列出所有规则的种类(布尔值)

范例一:列出selinux在此策略下的统计状态

seinfo

范例二:列出与httpd有关的规则(booleans)有哪些

seinfo -b | grep httpd

如果想要知道详细规则

sesearch [--all] [-s 主体类别] [-t 目标类别] [-b 布尔值]

选项与参数:

--all:列出该类别或布尔值的所有相关信息

-t:后面还要接类别,例如 -t httpd_t

-b:后面要接布尔值的规则,例如-b httpd_enable_ftp_server

范例一:找出目标文件资源类别为httpd_sys_content_t的相关信息

sesearch --all -t httpd_sys_content_t

范例二:查看httpd_enable_homedirs 这个布尔值规范了什么

sesearch -b httpd_enable_homedirs --all

3.2布尔值的查询与修改

getsebool [-a] [布尔值条款]

选项与参数:

-a:列出目前系统上面的所有布尔值条款设置为开启或关闭值

范例一查询本系统所有的布尔值设置情况

getsebool -a

setsebool [-P] 布尔值=[0|1]

选项与参数:

-P:直接将设置值写入配置文件,该设置数据未来会生效的

范例一:查询httpd_enable_homedirs 是否为on,如果不为on,则启动它

getsebool httpd_enable_homedirs

setsebool -P httpd_enable_homedirs=1

getsebool httpd_enable_homedirs

4.selinux日志文件记录所需的服务

4.1 setroubleshoot 将错误信息写入/var/log/messages/

setroubleshoot服务启动,centos6.x将 auditd与setroubleshoot整合在了auditd

yum install setroubleshoot setroubleshoot-server

/etc/init.d/auditd restart

cat /var/log/messages/ |grep setroubleshoot

4.2 用email或在命令列上面直接提供setroubleshoot

vim /etc/setroubleshoot/setroubleshoot.cfg

修改

recipients = /var/lib/setroubleshoot/email_alert_recipients //大约81行

console = true //大约147行

vim /var/lib/setroubleshoot/email_alert_recipients

加入

root@localhost

your@email.address

/etc/init.d/auditd restart