迁移 SELinux 策略通常指把一台机器(或某个系统版本)上定义好的 SELinux 策略、模块、布尔值、文件上下文等,迁移到另一台机器或新系统中。下面按“常见场景 + 操作步骤”说明。
SELinux 相关内容包括:
.pp 文件(已编译).te / .fc / .if 源码getsebool -asemanage fcontextsemanage portsemanage user/loginsemanage export -f selinux-config.exp
该文件包含:
semodule -l | grep -v "^(base|unconfined)" > modules.list
.ppmkdir selinux-modules
for m in $(awk '{print $1}' modules.list); do
semodule -E $m
mv $m.pp selinux-modules/
done
semanage import -f selinux-config.exp
cd selinux-modules
for pp in *.pp; do
semodule -i $pp
done
restorecon -Rv /
不建议直接拷贝 /etc/selinux,原因:
✅ 推荐做法:
policy_source/
├── myapp.te
├── myapp.fc
├── myapp.if
编译并安装:
make -f /usr/share/selinux/devel/Makefile
semodule -i myapp.pp
适合:
⚠️ 不要直接复制:
/etc/selinux/targeted//var/lib/selinux/⚠️ 内核版本差异可能导致:
semodule -i 失败⚠️ 迁移后务必:
sestatus
ausearch -m avc -ts recent
✅ 布尔值一致
✅ fcontext 一致
✅ 模块已加载
✅ restorecon 已执行
✅ AVC 无异常
如果你能说明:
我可以给你更精确的迁移方案。