主题
SELinux配置和系统加固
课程介绍
SELinux(Security-Enhanced Linux)是Linux系统的安全增强模块,用于提供强制访问控制(MAC)。本课程将详细讲解SELinux配置、系统加固、安全策略等核心功能,帮助你搭建安全的Linux系统。
1. SELinux概述
1.1 什么是SELinux
SELinux是Linux系统的安全增强模块,提供强制访问控制(MAC)。
SELinux的特点:
| 特点 | 说明 |
|---|---|
| 强制访问控制 | 提供强制访问控制 |
| 最小权限原则 | 遵循最小权限原则 |
| 策略管理 | 支持多种安全策略 |
| 审计功能 | 提供审计功能 |
| 模块化设计 | 采用模块化设计 |
1.2 SELinux的工作模式
SELinux有三种工作模式。
工作模式:
| 模式 | 说明 |
|---|---|
| Enforcing | 强制模式,强制执行SELinux策略 |
| Permissive | 宽容模式,不强制执行SELinux策略,只记录日志 |
| Disabled | 禁用模式,禁用SELinux |
1.3 SELinux的安全上下文
SELinux使用安全上下文来标识资源。
安全上下文格式:
user:role:type:level安全上下文示例:
system_u:object_r:httpd_sys_content_t:s0安全上下文说明:
| 部分 | 说明 |
|---|---|
| user | 用户标识 |
| role | 角色标识 |
| type | 类型标识 |
| level | 级别标识 |
2. SELinux安装
2.1 检查SELinux状态
检查SELinux状态的步骤。
检查SELinux状态:
bash
# 查看SELinux状态
sestatus
# 查看SELinux模式
getenforce
# 查看SELinux配置
cat /etc/selinux/config2.2 安装SELinux
安装SELinux的步骤。
在CentOS上安装SELinux:
bash
# 安装SELinux
sudo yum install selinux-policy-targeted -y
# 启用SELinux
sudo vim /etc/selinux/config配置文件内容:
bash
SELINUX=enforcing
SELINUXTYPE=targeted重启系统:
bash
# 重启系统
sudo reboot2.3 禁用SELinux
禁用SELinux的步骤。
临时禁用SELinux:
bash
# 临时禁用SELinux
sudo setenforce 0
# 查看SELinux状态
getenforce永久禁用SELinux:
bash
# 编辑SELinux配置文件
sudo vim /etc/selinux/config配置文件内容:
bash
SELINUX=disabled重启系统:
bash
# 重启系统
sudo reboot3. SELinux配置
3.1 SELinux模式切换
SELinux模式切换的步骤。
切换到Enforcing模式:
bash
# 切换到Enforcing模式
sudo setenforce 1
# 查看SELinux状态
getenforce切换到Permissive模式:
bash
# 切换到Permissive模式
sudo setenforce 0
# 查看SELinux状态
getenforce3.2 SELinux策略
SELinux策略用于定义安全规则。
查看SELinux策略:
bash
# 查看SELinux策略
sestatus | grep "Loaded policy"
# 查看SELinux策略类型
sestatus | grep "Policy from config file"切换SELinux策略:
bash
# 编辑SELinux配置文件
sudo vim /etc/selinux/config配置文件内容:
bash
SELINUXTYPE=targeted重启系统:
bash
# 重启系统
sudo reboot3.3 SELinux布尔值
SELinux布尔值用于控制SELinux策略的行为。
查看SELinux布尔值:
bash
# 查看所有布尔值
getsebool -a
# 查看特定布尔值
getsebool httpd_enable_cgi设置SELinux布尔值:
bash
# 临时设置布尔值
sudo setsebool httpd_enable_cgi on
# 永久设置布尔值
sudo setsebool -P httpd_enable_cgi on4. SELinux安全上下文
4.1 查看安全上下文
查看安全上下文的步骤。
查看文件安全上下文:
bash
# 查看文件安全上下文
ls -Z /var/www/html
# 查看目录安全上下文
ls -Zd /var/www/html
# 查看进程安全上下文
ps -Z查看端口安全上下文:
bash
# 查看端口安全上下文
semanage port -l | grep http4.2 修改安全上下文
修改安全上下文的步骤。
修改文件安全上下文:
bash
# 修改文件安全上下文
sudo chcon -t httpd_sys_content_t /var/www/html/index.html
# 递归修改目录安全上下文
sudo chcon -R -t httpd_sys_content_t /var/www/html恢复默认安全上下文:
bash
# 恢复默认安全上下文
sudo restorecon -R /var/www/html4.3 添加端口安全上下文
添加端口安全上下文的步骤。
添加端口安全上下文:
bash
# 添加端口安全上下文
sudo semanage port -a -t http_port_t -p tcp 8080
# 查看端口安全上下文
sudo semanage port -l | grep http删除端口安全上下文:
bash
# 删除端口安全上下文
sudo semanage port -d -t http_port_t -p tcp 80805. SELinux故障排查
5.1 SELinux拒绝访问
SELinux拒绝访问的原因和解决方法。
故障现象:
Permission denied排查步骤:
步骤1:检查SELinux日志
bash
# 查看SELinux日志
sudo ausearch -m avc -ts recent
# 查看SELinux审计日志
sudo cat /var/log/audit/audit.log | grep AVC步骤2:分析SELinux拒绝
bash
# 分析SELinux拒绝
sudo audit2allow -w -a
# 生成SELinux策略
sudo audit2allow -a步骤3:解决SELinux拒绝
bash
# 方法1:修改安全上下文
sudo chcon -t httpd_sys_content_t /var/www/html/index.html
# 方法2:添加SELinux策略
sudo audit2allow -a -M mypolicy
sudo semodule -i mypolicy.pp5.2 SELinux布尔值问题
SELinux布尔值问题的原因和解决方法。
故障现象:
Service not working排查步骤:
步骤1:检查SELinux布尔值
bash
# 查看SELinux布尔值
getsebool -a | grep httpd步骤2:设置SELinux布尔值
bash
# 设置SELinux布尔值
sudo setsebool -P httpd_enable_cgi on6. 系统加固
6.1 系统加固概述
系统加固是指通过配置和优化,提高系统安全性。
系统加固的目标:
| 目标 | 说明 |
|---|---|
| 最小化安装 | 只安装必要的软件包 |
| 最小化服务 | 只运行必要的服务 |
| 最小化权限 | 只授予必要的权限 |
| 强化认证 | 强化认证机制 |
| 强化审计 | 强化审计机制 |
6.2 最小化安装
最小化安装可以提高系统安全性。
最小化安装步骤:
bash
# 选择最小化安装
# 只安装必要的软件包
# 查看已安装的软件包
rpm -qa
# 卸载不必要的软件包
sudo yum remove package_name -y6.3 最小化服务
最小化服务可以提高系统安全性。
最小化服务步骤:
bash
# 查看运行中的服务
sudo systemctl list-units --type=service --state=running
# 停止不必要的服务
sudo systemctl stop service_name
# 禁用不必要的服务
sudo systemctl disable service_name6.4 最小化权限
最小化权限可以提高系统安全性。
最小化权限步骤:
bash
# 禁止root远程登录
sudo vim /etc/ssh/sshd_config
# 配置文件内容
PermitRootLogin no
# 重启SSH服务
sudo systemctl restart sshd6.5 强化认证
强化认证可以提高系统安全性。
强化认证步骤:
bash
# 设置密码策略
sudo vim /etc/login.defs
# 配置文件内容
PASS_MAX_DAYS 90
PASS_MIN_DAYS 1
PASS_MIN_LEN 8
PASS_WARN_AGE 7
# 设置密码复杂度
sudo vim /etc/pam.d/system-auth
# 配置文件内容
password requisite pam_cracklib.so try_first_pass retry=3 type=
password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok
password required pam_deny.so6.6 强化审计
强化审计可以提高系统安全性。
强化审计步骤:
bash
# 安装auditd
sudo yum install auditd -y
# 启动auditd
sudo systemctl start auditd
# 设置auditd开机自启
sudo systemctl enable auditd
# 查看auditd状态
sudo systemctl status auditd
# 配置审计规则
sudo vim /etc/audit/audit.rules
# 配置文件内容
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/group -p wa -k identity
# 重启auditd
sudo systemctl restart auditd7. 系统加固最佳实践
7.1 定期更新系统
定期更新系统可以修复安全漏洞。
更新系统步骤:
bash
# 更新系统
sudo yum update -y
# 查看可更新的软件包
sudo yum check-update
# 安装安全更新
sudo yum update --security -y7.2 配置防火墙
配置防火墙可以提高系统安全性。
配置防火墙步骤:
bash
# 启动firewalld
sudo systemctl start firewalld
# 设置firewalld开机自启
sudo systemctl enable firewalld
# 允许SSH服务
sudo firewall-cmd --permanent --add-service=ssh
# 允许HTTP服务
sudo firewall-cmd --permanent --add-service=http
# 重载防火墙
sudo firewall-cmd --reload7.3 配置SSH安全
配置SSH安全可以提高系统安全性。
配置SSH安全步骤:
bash
# 编辑SSH配置文件
sudo vim /etc/ssh/sshd_config
# 配置文件内容
Port 2222
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
# 重启SSH服务
sudo systemctl restart sshd7.4 配置SELinux
配置SELinux可以提高系统安全性。
配置SELinux步骤:
bash
# 启用SELinux
sudo vim /etc/selinux/config
# 配置文件内容
SELINUX=enforcing
SELINUXTYPE=targeted
# 重启系统
sudo reboot8. 实战案例
案例1:Web服务器系统加固
场景:加固Web服务器系统。
加固步骤:
步骤1:最小化安装
bash
# 选择最小化安装
# 只安装必要的软件包步骤2:最小化服务
bash
# 停止不必要的服务
sudo systemctl stop postfix
sudo systemctl disable postfix步骤3:配置防火墙
bash
# 允许HTTP服务
sudo firewall-cmd --permanent --add-service=http
# 允许HTTPS服务
sudo firewall-cmd --permanent --add-service=https
# 重载防火墙
sudo firewall-cmd --reload步骤4:配置SSH安全
bash
# 禁止root登录
sudo vim /etc/ssh/sshd_config
# 配置文件内容
PermitRootLogin no
# 重启SSH服务
sudo systemctl restart sshd步骤5:配置SELinux
bash
# 启用SELinux
sudo vim /etc/selinux/config
# 配置文件内容
SELINUX=enforcing
# 重启系统
sudo reboot案例2:数据库服务器系统加固
场景:加固数据库服务器系统。
加固步骤:
步骤1:最小化安装
bash
# 选择最小化安装
# 只安装必要的软件包步骤2:最小化服务
bash
# 停止不必要的服务
sudo systemctl stop postfix
sudo systemctl disable postfix步骤3:配置防火墙
bash
# 允许MySQL服务
sudo firewall-cmd --permanent --add-service=mysql
# 重载防火墙
sudo firewall-cmd --reload步骤4:配置SSH安全
bash
# 禁止root登录
sudo vim /etc/ssh/sshd_config
# 配置文件内容
PermitRootLogin no
# 重启SSH服务
sudo systemctl restart sshd步骤5:配置SELinux
bash
# 启用SELinux
sudo vim /etc/selinux/config
# 配置文件内容
SELINUX=enforcing
# 重启系统
sudo reboot课程总结
这节课我们学习了SELinux配置和系统加固。
核心内容:
- SELinux概述
- SELinux安装
- SELinux配置
- SELinux安全上下文
- SELinux故障排查
- 系统加固
- 系统加固最佳实践
- 实战案例
重要命令:
sestatus:查看SELinux状态getenforce:查看SELinux模式setenforce:设置SELinux模式getsebool:查看SELinux布尔值setsebool:设置SELinux布尔值chcon:修改安全上下文restorecon:恢复默认安全上下文semanage:管理SELinux策略
SELinux是Linux系统安全的重要组成部分,掌握这些知识后,你将能够搭建安全的Linux系统。
课后练习
练习1(基础)
检查SELinux状态,并切换SELinux模式。
练习2(进阶)
修改文件安全上下文,并恢复默认安全上下文。
练习3(拓展)
配置系统加固,包括最小化安装、最小化服务、配置防火墙、配置SSH安全、配置SELinux等。