温馨提示×

如何安装ubuntu selinux

小樊
47
2026-09-07 10:43:38
栏目: 智能运维

Ubuntu 上安装和启用 SELinux 与在 RHEL/CentOS 上不同,因为 Ubuntu 默认使用的是 AppArmor。下面我按从零开始、可操作的方式给你说明。


一、确认当前安全模块

先看看系统现在用的是哪种:

cat /proc/self/attr/current

或者:

sestatus

如果是:

  • command not found → 没装 SELinux
  • 显示 AppArmor → 默认安全模块是 AppArmor

二、安装 SELinux 相关软件包

1. 更新系统

sudo apt update
sudo apt upgrade -y

2. 安装 SELinux 基础组件

sudo apt install selinux selinux-utils selinux-basics -y

selinux-basics 会自动处理一些配置


三、从 AppArmor 切换到 SELinux(重要)

Ubuntu 默认启用 AppArmor,必须禁用它才能用 SELinux。

1. 禁用 AppArmor

sudo systemctl disable apparmor
sudo systemctl stop apparmor

2. 激活 SELinux

sudo selinux-activate

这个命令会:

  • 修改 GRUB 启动参数
  • 设置 SELinux 为 enforcingpermissive

四、选择 SELinux 模式

编辑配置文件:

sudo nano /etc/selinux/config

内容示例:

SELINUX=permissive
SELINUXTYPE=default

建议第一次用 permissive,只记录不拦截,避免系统无法启动


五、重启系统

sudo reboot

六、验证 SELinux 是否生效

重启后执行:

sestatus

正常输出示例:

SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
Current mode:                   permissive
Mode from config file:          permissive

七、从 permissive 切换到 enforcing(可选)

确认系统运行正常后:

sudo setenforce 1

并修改配置文件:

SELINUX=enforcing

八、常见问题(重要)

❌ Ubuntu + SELinux 的风险

  • 很多 Ubuntu 官方文档和软件 未针对 SELinux 测试
  • 容易出现登录失败、服务无法启动
  • 服务器环境 不建议新手使用

✅ 推荐方案

  • 生产服务器:用 RHEL / Rocky / AlmaLinux
  • Ubuntu 用户:使用 AppArmor(默认、稳定)

九、如果你只是“想学 SELinux”

建议:

  • CentOS / Rocky Linux 虚拟机
  • 或 Ubuntu + permissive 模式做实验

如果你告诉我:

  • Ubuntu 版本(20.04 / 22.04 / 24.04)
  • 桌面还是服务器
  • 目的是 学习 / 生产 / 安全加固

我可以给你更精确的配置方案。

0