1. SELinux 简介
SELinux(Security-Enhanced Linux)是一个由美国国家安全局(NSA)开发的安全模块,用于实现 Linux 操作系统的强制访问控制(MAC)。SELinux 通过限制程序对资源的访问来增强系统的安全性。
主要概念:
- 策略(Policy):定义了如何限制程序的行为。SELinux 使用策略来决定哪些操作是允许的,哪些操作是禁止的。
- 类型(Type):每个文件和进程都有一个类型标签,SELinux 通过这些标签控制资源访问。
- 上下文(Context):每个文件、进程或对象都拥有与其相关的上下文,格式为
<user>:<role>:<type>:<level>
,其中<type>
是访问控制的关键。
2. SELinux 模式
SELinux 有三种运行模式:
- Enforcing(强制模式):SELinux 会执行策略,拒绝不符合规则的操作,并记录日志。
- Permissive(宽容模式):SELinux 不会拒绝操作,而是允许所有操作,只记录日志。
- Disabled(禁用模式):SELinux 完全禁用,所有访问控制都不生效。
查看 SELinux 当前状态:
[root@localhost ~]# getenforce
设置 SELinux 模式:
- 临时更改模式(不重启):
- 启用:
setenforce 1
- 禁用:
setenforce 0
- 永久更改模式(需重启): 编辑
/etc/selinux/config
文件,修改
SELINUX
配置项:
SELINUX=enforcing # 或 permissive 或 disabled
重启系统使更改生效。
3. 查看 SELinux 状态
- 查看当前 SELinux 状态:
[root@localhost ~]# sestatus
- 查看文件的 SELinux 上下文:
[root@localhost ~]# ls -Z <file>
ls -Z
显示文件的 SELinux 上下文。
- 查看进程的 SELinux 上下文:
[root@localhost ~]# ps -eZ
4. SELinux 上下文管理
SELinux 使用上下文来标识进程、文件、网络端口等资源的安全属性。以下是一些常用的命令来管理 SELinux 上下文。
更改文件的 SELinux 上下文
- 更改文件上下文 :
[root@localhost ~]# chcon -t <type> <file>
例如,将文件
test.txt
的类型设置为
httpd_sys_content_t
:
[root@localhost ~]# chcon -t httpd_sys_content_t test.txt
- 恢复默认上下文 (对于目录及其文件):
[root@localhost ~]# restorecon -v <file>
该命令会将文件的上下文恢复为默认设置。
5. SELinux 配置
- 查看 SELinux 配置:
[root@localhost ~]# cat /etc/selinux/config
该文件包含 SELinux 的默认模式和策略设置。
- 修改 SELinux 策略: SELinux 支持三种类型的策略:
- targeted(默认):只对系统中指定的服务应用 SELinux 策略。
- mls(Multi-Level Security):使用更细粒度的访问控制,适用于高度敏感的系统。 在
/etc/selinux/config
中设置SELINUXTYPE
以指定所使用的策略类型:
SELINUXTYPE=targeted
6. SELinux 日志
SELinux 会将违反策略的行为记录到系统日志中。查看 SELinux 日志的常用方法:
- 查看
/var/log/audit/audit.log
日志:
[root@localhost ~]# tail -f /var/log/audit/audit.log
- 查看 SELinux 违规记录:
[root@localhost ~]# sealert -a /var/log/audit/audit.log
7. SELinux 安全策略管理
- 查看和管理 SELinux Booleans : SELinux 使用 Booleans 来启用或禁用某些策略。
- 列出所有 SELinux Booleans :
[root@localhost ~]# getsebool -a
- 修改 SELinux Boolean (如启用 HTTPD 访问网络):
[root@localhost ~]# setsebool -P httpd_can_network_connect 1
8. SELinux 错误排查
如果应用程序因 SELinux 设置而无法正常运行,可以通过以下方式排查:
- 查看 SELinux 错误日志:
[root@localhost ~]# ausearch -m avc -ts recent
该命令会显示最近的 SELinux 拒绝记录。
- 使用
sealert
查看详细信息:
[root@localhost ~]# sealert -a /var/log/audit/audit.log
9. 禁用 SELinux(不推荐)
如果需要禁用 SELinux,方法如下:
- 修改
/etc/selinux/config
文件,设置SELINUX=disabled
。 - 重启系统使设置生效。
禁用 SELinux 会降低系统的安全性,不推荐在生产环境中禁用。
10. SELinux 常见问题与解决方案
- 应用程序无法访问文件:
- 查看是否是 SELinux 拒绝了访问:
ausearch -m avc -ts recent
- 使用
sealert
或setsebool
修改相关策略。
- 配置 HTTPD 或其他服务的 SELinux 权限:
- 如果 HTTPD 服务不能访问某个目录或文件,使用
chcon
设置文件的类型。 - 也可以设置相关的 SELinux Boolean,如
httpd_can_network_connect
。
11. SELinux 命令总结
命令 | 说明 |
---|---|
getenforce | 查看 SELinux 当前模式(Enforcing/Permissive) |
setenforce <mode> | 设置 SELinux 模式(1 为 Enforcing,0 为 Permissive) |
sestatus | 查看 SELinux 状态和策略 |
ls -Z <file> | 查看文件的 SELinux 上下文 |
chcon -t <type> <file> | 修改文件的 SELinux 上下文 |
restorecon -v <file> | 恢复文件的默认 SELinux 上下文 |
sealert -a /var/log/audit/audit.log | 分析 SELinux 拒绝日志并给出建议 |
getsebool -a | 列出所有 SELinux Booleans |
setsebool -P <boolean> <value> | 修改 SELinux Boolean 设置 |
ausearch -m avc | 搜索 SELinux 违规访问控制记录 |
Comments NOTHING