Linux15-SELinux
目录
一、查看和配置SELinux生效
二、SELinux上下文
2.1、查看SELinux上下文
2.2、semanage+restorecon命令更改SELinux上下文
三、SELinux布尔值
四、SELinux故障排除
Security Enhanced Linux(SELinux)是一个额外的系统安全层,是一种强制访问控制(MAC)的访问控制手段,基于策略的方式来保护系统中进程。
一、查看和配置SELinux生效
使用命令getenforce查看SELinux的配置情况。使用命令setenforce只能临时修改SELinux配置,也就是修改内存中SELinux的配置,重启后失效。selinux本质是一个内核模块,要让修改永久生效,那么就要修改配置文件/etc/selinux/config中的SELINUX参数为,然后重启生效。
SELinux的三种状态:
- enforcing,强制模式。默认没有明确的允许就是拒绝。提供保护,同时记录日志。
- permissive,许可模式。访问跟策略冲突时允许用户去执行,会把错误信息发送到日志。
- disabled,禁用模式。不提供保护,也不记录日志。
setenforce 0 permissive | 1 enforcing
查看selinux的日志的方法:tail /var/log/audit/audit.log | grep avc。过滤avc可以看到错误信息。
[root@server0 ~]# getenforce
Enforcing
[root@server0 ~]# cat /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.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
二、SELinux上下文
selinux具有多种上下文:用户、角色、类型、敏感度。默认的目标策略(SELINUXTYPE=targeted)会根据第三个上下文——类型上下文来制定自己的规则。类型上下文通常以 _t 结尾。比如Web服务器的类型上下文是httpd_t,位于/var/www/html中的文件和目录的类型上下文是httpd_sys_content_t,Web服务器端口的类型上下文是http_port_t 等等。如果更改了目录、监听端口,而新的目录、端口不具备相应的类型上下文,那么selinux就禁止访问,从禁止恶意用户访问。
2.1、查看SELinux上下文
每个端口、每个文件都有自己的类型上下文。查看方法有 ls -lZ、ps -Z。
selinux保护系统中的进程:
- 进程需要监听某个端口
- 访问进程自己的家目录
[root@server0 ~]# ps axZ | grep httpd
system_u:system_r:httpd_t:s0 5998 ? Ss 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 5999 ? S 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 6000 ? S 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 6001 ? S 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 6002 ? S 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 6003 ? S 0:00 /usr/sbin/httpd -DFOREGROUND[root@server0 ~]# ll -dZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
2.2、semanage+restorecon命令更改SELinux上下文
虽然修改端口和文件的上下文有两个命令,但是为了保证重启依旧生效,只推荐使用semanage+restorecon命令,另一个不记也不要使用!
列出和修改端口的上下文,semanage port的 -l列出 -a增加 -t指定上下文 -p指定协议和端口。
semanage port -l # 列出端口上下文
semanage port -a -t http_port_t -p tcp 8888 # 端口8888增加web服务的端口上下文
列出和修改文件的上下文,和上面的选项意义一样。
注意(/.*)?正则表达式,表示“匹配后跟任意数量的字符”,它会匹配在表达式前面列出的目录并递归匹配该目录中的所有内容。
注意semanage只是改变了默认的设置,还要改一下已经生成的文件的文件域,用restorecon -Rv file,这样才永久生效。
semanage fcontext -l # 列出文件上下文semanage fcontext -a -t httpd_sys_content_t '/data/my/custom/dir(/.*)?' # 自己定义的web服务目录/data/my/custom/dir增加web服务的文件上下文
restorecon -Rv /data/my/custom/dir # 还要改一下已经生成的文件的文件域才永久生效
举例,我们把apache的目录改为/virtual,那么配置好以后启动apache,访问网页,也会有如下的报错。配置好上下文以后访问才正常。
[root@server0 ~]# systemctl status httpd
httpd.service - The Apache HTTP ServerLoaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)Active: active (running) since Sat 2021-03-27 14:43:19 CST; 54s agoProcess: 24773 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)Main PID: 24777 (httpd)Status: "Total requests: 5; Current requests/sec: 0; Current traffic: 0 B/sec"CGroup: /system.slice/httpd.service├─24777 /usr/sbin/httpd -DFOREGROUND├─24779 /usr/sbin/httpd -DFOREGROUND├─24780 /usr/sbin/httpd -DFOREGROUND├─24781 /usr/sbin/httpd -DFOREGROUND├─24782 /usr/sbin/httpd -DFOREGROUND├─24783 /usr/sbin/httpd -DFOREGROUND└─24790 /usr/sbin/httpd -DFOREGROUNDMar 27 14:43:18 server0.example.com systemd[1]: Starting The Apache HTTP Server...
Mar 27 14:43:19 server0.example.com systemd[1]: Started The Apache HTTP Server.
Mar 27 14:43:25 server0.example.com python[24785]: SELinux is preventing /usr/sbin/httpd from getattr access on the file .***** Plugin catchall_labels (83.8 confidence) suggests *******************...[root@server0 ~]# semanage fcontext -a -t httpd_sys_content_t '/virtual(/.*)?'
[root@server0 ~]# restorecon -Rv /virtual/
restorecon reset /virtual context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /virtual/index.html context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
[root@server0 ~]# systemctl restart httpd
三、SELinux布尔值
SELinux布尔值是更改SELinux策略行为的开关。可以启用或禁用。selinux-policy-devel包提供了很多手册(man 8 查看),解释不同布尔值的含义。man -k selinux可以列出这些文档。
yum install selinux-policy-devel.noarch
mandb
man -k selinux
查看当前布尔值设置。
[root@server0 ~]# semanage boolean -l | grep httpd | grep home
httpd_enable_homedirs (off , off) Allow httpd to read home directories
设置布尔值,-P永久生效。
[root@server0 ~]# setsebool httpd_enable_homedirs on
[root@server0 ~]# semanage boolean -l | grep httpd | grep home
httpd_enable_homedirs (on , off) Allow httpd to read home directories[root@server0 ~]# setsebool -P httpd_enable_homedirs on
[root@server0 ~]# semanage boolean -l | grep httpd | grep home
httpd_enable_homedirs (on , on) Allow httpd to read home directories
四、SELinux故障排除
selinux默认的日志/var/log/audit/audit.log,过滤avc字样来看报错。从日志/var/log/message里也可以发现有用的消息。
安装setroubleshoot-server包,然后使用命令 sealert -a <log>查看selinux报错的详细内容。
或者知道一个selinux UUID(selinux的一个事件号),用sealsert -l <UUID>也可以查看有用的信息。
yum install setroubleshoot-server # 装包
sealert -a /var/log/audit/audit.log # 分析一个日志less /var/log/messages # 从日志里找出UUID
Mar 27 03:23:35 localhost setroubleshoot: SELinux is preventing /usr/sbin/httpd from read access on the file . For complete SELinux messages. run sealert -l 36191ec4-827d-4018-b156-8924c3a992cc
sealert -l 36191ec4-827d-4018-b156-8924c3a992cc # 根据UUID查看详细信息
Linux15-SELinux相关推荐
- centos7下selinux永久关闭
----------------------------------------------selinux永久关闭------------------------------------------- ...
- 【linux】SELinux工具:semanage的安装和使用
1.安装 在ubuntu14.04上安装 sudo apt-get install policycoreutils 在CentOS7上安装 sudo yum -y install policycore ...
- 9.5. SELINUX
禁用SElinux编辑/etc/selinux/config,修改如下内容: SELINUX=disabled 使用命令 getenforce setenforce 0 lokkit --selinu ...
- SELinux与强制访问控制系统应用
几乎可以肯定每个人都听说过 SELinux (更准确的说,尝试关闭过),甚至某些过往的经验让您对 SELinux 产生了偏见.不过随着日益增长的 0-day 安全漏洞,或许现在是时候去了解下这个在 ...
- phpMyAdmin FTP iptables Selinux
四.修改配置 1.vi /usr/local/php/etc/php.ini 1) ; Directory in which the loadable extensions (modules) res ...
- linux防火墙伦堂,「linux专栏」自从看了这篇文章,我彻底搞懂了selinux和防火墙...
很多网友提到linux的防火墙就犯难,现在又多出一个selinux,愈加分不清了.曾几何时,小编也是如此,不过当你看完这篇文章之后,再也无需为此犯难了. 何为SELinux 简单来书,SELinux( ...
- context linux,使用selinux contexts
SELinux增强了Linux系统的安全,但是对于初学者来说很容易造成各种各样的错误,经常被搞的莫名其妙不知所措,最后只能祭出大招把SELinux一关了之.笔者也是初学者,也没有搞清楚其中的道理,只是 ...
- 提升篇——SELINUX相关介绍
1.SElinux简介 SELinux:Secure Enhanced Linux,是美国国家安全局(NSA=The National Security Agency)和SCC(Secure Comp ...
- centos selinux mysql 5.6_centos 6.4下安装mysql 5.6.11
今天有空来测试下最新的centos6.4下安装最新数据可mysql5.6.11 系统安装在这里不再叙述 centos6.4采用最小化安装 mysql5.6.11采用源码包安装 初始化系统:关掉ipta ...
- android+关闭selinux权限问题,快速解决Android中的selinux权限问题【转】
在Android开发的过程中,遇到关于selinux相关的东西,当时还一下子看不懂,现在好像有点眉目了. 比如,内核打印这个提示 type=1400 audit(32.939:25): avc: de ...
最新文章
- 平凡的世界,有感而发
- OpenCV使用dnn从图像中解析人体部位的实例(附完整代码)
- win7 CapsLooks键 转换成Ctrl键
- 启动Cognos时报0106错误
- HttpRequest 类
- 《Redis官方文档》Data types—数据类型
- 修改eclipse中java和xml页面的字体的样式
- 【转】浏览器缓存机制
- python编程广告_为什么铺天盖地都是Python的广告?
- “ +”(加号)CSS选择器是什么意思?
- 超简单友盟分享(微信、QQ)+ 原生微信分享
- 十进制数转换BCD码
- php 下载技巧,PHP 使用技巧集
- 继电保护原理1.1-电流、方向保护
- C#基础之vs2010安装与使用教程
- vue+node.js+mysql的数据库课程设计有感
- js获取当前时间写入HTML,html获取当前时间 js获取当前日期的前后4天
- [转] “嫁给我是你一生的赌注,我怎么舍得让你输”
- 自律型性格分析,自律型人格的职业规划
- 微软WHQL认证有哪些步骤?驱动程序签名及发布