在 Ubuntu 上配置 SELinux 需要注意:
Ubuntu 默认使用的是 AppArmor,而不是 SELinux。如果你确实需要使用 SELinux(例如为了兼容 RHEL/CentOS 环境或安全合规),可以按下面的步骤来。
cat /proc/self/attr/current
或者:
sestatus
如果显示:
Disabled 或命令不存在 → 当前未启用 SELinuxEnforcing / Permissive → 已启用sudo apt update
sudo apt install selinux selinux-utils policycoreutils checkpolicy
可选(推荐):
sudo apt install selinux-basics
sudo selinux-activate
该命令会:
编辑 GRUB 配置:
sudo nano /etc/default/grub
在 GRUB_CMDLINE_LINUX 中添加:
security=selinux selinux=1
更新 GRUB:
sudo update-grub
编辑配置文件:
sudo nano /etc/selinux/config
示例(推荐先 Permissive):
SELINUX=permissive
SELINUXTYPE=default
模式说明:
enforcing:强制模式(会拦截违规)permissive:宽容模式(只记录不拦截)disabled:关闭sudo reboot
重启后检查:
sestatus
# 查看状态
sestatus
# 临时切换模式
sudo setenforce 0 # permissive
sudo setenforce 1 # enforcing
# 查看文件上下文
ls -Z
# 修改文件上下文
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www(/.*)?"
sudo restorecon -Rv /var/www
# 查看审计日志
sudo ausearch -m AVC
sudo sealert -a /var/log/audit/audit.log
✅ Ubuntu 对 SELinux 支持 不如 RHEL/CentOS 完善
✅ 很多 Ubuntu 服务默认 没有 SELinux policy
✅ 如果只是为了安全,AppArmor 更简单稳定
✅ 生产环境启用前务必在测试机验证
| 场景 | 建议 |
|---|---|
| 普通 Ubuntu 用户 | 使用 AppArmor |
| 学习 SELinux | 用 CentOS / Rocky / AlmaLinux |
| 必须 Ubuntu + SELinux | Permissive 模式起步 |
如果你愿意,我可以:
你现在的 Ubuntu 版本是多少?