以下是在Ubuntu上恢复SELinux备份的常见方法:
若之前使用tar命令备份了/etc/selinux目录,可通过以下命令恢复:
sudo tar -xzvf selinux-backup.tar.gz -C / # 解压备份文件到原路径
使用restorecon命令
递归恢复文件和目录的安全上下文到默认值:
sudo restorecon -Rv / # 恢复整个系统上下文
使用setfattr命令(若备份了上下文文件)
若通过getfattr备份了上下文文件(如selinux_contexts.txt),可执行:
sudo setfattr --restore=selinux_contexts.txt # 恢复指定文件/目录的上下文
若备份了/etc/selinux/config,直接复制回原路径:
sudo cp /path/to/backup/selinux.config /etc/selinux/config # 恢复配置文件
恢复后需重启系统或SELinux服务使更改生效:
sudo reboot # 重启系统
# 或
sudo systemctl restart selinux # 重启SELinux服务(部分场景需手动加载策略)
ls -lh检查备份文件大小。semodule -i安装模块。permissive模式避免权限问题:sudo setenforce 0,恢复后再切回enforcing。参考来源: