迁移到 Rocky Linux
本文将指导你使用 Rocky Linux 官方迁移工具 migrate2rocky.sh,从 CentOS 8、CentOS Stream、RHEL、AlmaLinux、Oracle Linux 等 EL 兼容发行版迁移到 Rocky Linux。
在开始迁移之前,请确认以下事项:
- 操作系统版本:CentOS 8、CentOS Stream 8/9、RHEL 8/9、AlmaLinux 8/9、Oracle Linux 8/9 等 EL8+ 兼容系统
- 磁盘空间:
/var分区至少有 5 GB 可用空间 - 网络连接:服务器能够访问外网以下载 Rocky Linux 软件包
- root 权限:迁移过程需要 root 或 sudo 权限
- 完整备份:务必在迁移前完成系统快照或完整备份
重要提示:
migrate2rocky.sh目前主要支持 EL8 和 EL9 系统。CentOS 7 用户需要先升级到 EL8 才能使用此工具。
记录当前系统状态
Section titled “记录当前系统状态”$ cat /etc/os-release$ rpm -qa --queryformat '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' | sort > ~/packages-before-migration.txt$ systemctl list-units --type=service --state=running > ~/services-before-migration.txt$ cp -a /etc/yum.repos.d/ ~/yum.repos.d.bak/$ sudo lvcreate --size 10G --snapshot --name pre_migration_snap /dev/mapper/centos-root下载 migrate2rocky.sh 工具
Section titled “下载 migrate2rocky.sh 工具”migrate2rocky.sh 由 Rocky Linux 官方维护,托管在 GitHub 上。
$ sudo dnf install -y curl git有两种方式获取迁移脚本:
方式一:直接下载脚本
Section titled “方式一:直接下载脚本”$ curl -O https://raw.githubusercontent.com/rocky-linux/rocky-tools/main/migrate2rocky/migrate2rocky.sh方式二:克隆整个仓库
Section titled “方式二:克隆整个仓库”$ git clone https://github.com/rocky-linux/rocky-tools.git$ cd rocky-tools/migrate2rocky/$ ls -la migrate2rocky.sh基本迁移(CentOS 8 / CentOS Stream 8)
Section titled “基本迁移(CentOS 8 / CentOS Stream 8)”$ sudo bash migrate2rocky.sh -r-r 参数表示执行完整迁移(包含 dnf distro-sync)。脚本会自动完成以下步骤:
- 检测当前操作系统和版本
- 检查是否存在已知的不兼容情况
- 下载 Rocky Linux GPG 密钥
- 替换系统标识包(
*-release、*-logos、*-gpg-keys等) - 执行
dnf distro-sync将所有软件包同步到 Rocky Linux 版本
EL9 系统的迁移
Section titled “EL9 系统的迁移”对于 CentOS Stream 9、AlmaLinux 9 等 EL9 系统:
$ curl -O https://raw.githubusercontent.com/rocky-linux/rocky-tools/main/migrate2rocky/migrate2rocky9.sh$ sudo bash migrate2rocky9.sh -rCentOS 7 的迁移路径
Section titled “CentOS 7 的迁移路径”CentOS 7 不能直接迁移到 Rocky Linux,需要借助 ELevate 项目先升级到 EL8。
$ sudo yum install -y http://repo.almalinux.org/elevate/elevate-release-latest-el7.noarch.rpm$ sudo yum install -y leapp-upgrade leapp-data-rocky$ sudo leapp preupgrade$ sudo cat /var/log/leapp/leapp-report.txt$ sudo leapp upgrade系统重启后将直接进入 Rocky Linux 8。
处理第三方仓库
Section titled “处理第三方仓库”第三方仓库是迁移过程中最常见的问题来源。建议在迁移前妥善处理。
迁移前禁用第三方仓库
Section titled “迁移前禁用第三方仓库”$ dnf repolist --enabled$ sudo dnf config-manager --set-disabled epel epel-modular 2>/dev/null$ sudo dnf config-manager --set-disabled remi* ius* rpmfusion* elrepo*迁移后恢复第三方仓库
Section titled “迁移后恢复第三方仓库”迁移完成后,需要安装对应 Rocky Linux 版本的第三方仓库包:
$ sudo dnf install -y epel-release$ sudo dnf install -y epel-release$ sudo dnf install -y https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm$ sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm$ sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm处理 Katello / Satellite 管理的系统
Section titled “处理 Katello / Satellite 管理的系统”如果系统使用 Katello 或 Red Hat Satellite 管理:
$ sudo subscription-manager unregister$ sudo subscription-manager clean迁移完成后根据需要重新配置管理工具。
确认系统版本
Section titled “确认系统版本”$ cat /etc/os-release预期输出:
NAME="Rocky Linux"VERSION="8.9 (Green Obsidian)"ID="rocky"$ rpm -qa | grep rocky-release$ rpm -qa --qf '%{NAME} %{VENDOR}\n' | grep -v -i rocky | grep -v "(none)" | sort$ rpm -qa | grep -i centos$ sudo dnf remove centos-logos centos-indexhtml centos-backgrounds 2>/dev/null同步所有软件包
Section titled “同步所有软件包”$ sudo dnf distro-sync -y重建 GRUB 引导
Section titled “重建 GRUB 引导”$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg$ sudo grub2-mkconfig -o /boot/efi/EFI/rocky/grub.cfg重启并最终验证
Section titled “重启并最终验证”$ sudo reboot$ uname -r$ systemctl list-units --type=service --state=failed$ rpm -qa --queryformat '%{NAME}\n' | sort > ~/packages-after-migration.txt$ diff ~/packages-before-migration.txt ~/packages-after-migration.txt脚本报错 “Unsupported OS”
Section titled “脚本报错 “Unsupported OS””migrate2rocky.sh 仅支持 EL8 系统。如果你运行的是 CentOS 7,需要使用 ELevate 项目先升级到 EL8。如果是 EL9,请使用 migrate2rocky9.sh。
$ rpm -E %{rhel}报错 “Could not download Rocky Linux GPG keys”
Section titled “报错 “Could not download Rocky Linux GPG keys””网络问题导致无法下载 GPG 密钥。
$ curl -I https://dl.rockylinux.org/$ sudo rpm --import https://dl.rockylinux.org/pub/rocky/RPM-GPG-KEY-Rocky-8如果服务器在国内,可以考虑使用镜像源加速。
dnf distro-sync 报错包冲突
Section titled “dnf distro-sync 报错包冲突”$ sudo dnf distro-sync -y --allowerasing 2>&1 | tee /tmp/distro-sync.log$ sudo dnf distro-sync -y --exclude=<问题包名>$ sudo dnf clean all$ sudo rm -rf /var/cache/dnf$ sudo dnf makecache$ sudo dnf distro-sync -y迁移后 SSH 连接断开
Section titled “迁移后 SSH 连接断开”如果迁移过程中 SSH 断开,可以通过控制台(IPMI / KVM / 云平台控制台)重新连接。
建议: 在
screen或tmux会话中执行迁移脚本,防止网络中断导致迁移过程被终止。
$ tmux new -s migration$ sudo bash migrate2rocky.sh -r如果 tmux 会话断开,重新连接:
$ tmux attach -t migration迁移后无法引导系统
Section titled “迁移后无法引导系统”参考 启动故障排查 页面。通常需要从救援模式重新安装内核和 GRUB。