在CentOS中测试SELinux可按以下步骤进行:
检查SELinux状态
getenforce 或 sestatus 查看当前模式(Enforcing/Permissive/Disabled)。/etc/selinux/config 确认默认模式。测试访问控制
echo "test" > /tmp/testfile
sudo chcon -t httpd_sys_content_t /tmp/testfile # 示例:设置为httpd可访问类型
查看SELinux日志
ausearch -m avc -ts recent 查看最近的拒绝日志。audit2why 分析日志,获取拒绝原因及解决方案。调整策略并验证
sudo setsebool -P httpd_can_network_connect on # 示例:允许httpd联网
getsebool -a | grep httpd_can_network_connect # 验证设置
audit2allow 生成模块并加载:sudo ausearch -m avc -ts recent > deny.log
sudo audit2allow -M mypolicy -i deny.log
sudo semodule -i mypolicy.pp
切换模式测试
sudo setenforce 0
sudo setenforce 1
注意:生产环境谨慎操作,建议提前备份配置文件,测试后及时恢复默认策略。