当前位置:首页>Linux>CentOS 7 升级到 Rocky Linux 9(二):从 CentOS 7 升级到 Rocky 8

CentOS 7 升级到 Rocky Linux 9(二):从 CentOS 7 升级到 Rocky 8

  • 2026-10-11 07:15:16
CentOS 7 升级到 Rocky Linux 9(二):从 CentOS 7 升级到 Rocky 8

字数 1548,阅读大约需 8 分钟

2.1 查看当前系统版本

初始安装镜像为 CentOS 7.6 64 位版本。

# 查看内核版本
uname -r
# 
输出: 3.10.0-1160.71.1.el7.x86_64

# 
查看完整系统信息
uname -a
# 
输出: Linux VM-4-3-centos 3.10.0-1160.71.1.el7.x86_64 
#1 SMP Tue Jun 28 15:37:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

# 
查看详细版本信息
cat /proc/version
# 
输出: Linux version 3.10.0-1160.71.1.el7.x86_64 (<mockbuild@kbuilder.bsys.centos.org>) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Tue Jun 28 15:37:28 UTC 2022

2.2 安装 EPEL 仓库

# 安装 EPEL 仓库
yum -y install epel-release
# 
输出:
# 
Installed:
# 
  epel-release.noarch 0:7-14
# 
Complete!

2.3 设置终端语言为简体中文

# 查看可用的语言环境
locale -a

# 
安装中文字体
yum groupinstall "fonts"

# 
设置系统语言为中文
localectl set-locale LANG=zh_CN.UTF-8

# 
退出终端并重新登录后界面语言将显示为中文

2.4 升级到 CentOS 最新版本并重启

# 升级所有软件包到最新版本
yum -y update

# 
升级完成后重启系统
reboot

# 
重启后系统版本应升级为 CentOS 7.9

2.5 安装升级依赖工具

# 安装 rpmconf 和 yum-utils
yum -y install rpmconf yum-utils

# 
执行 rpmconf 检查配置文件
# 
如有交互提示,输入 Y 并回车继续;若无提示则进行下一步
rpmconf -a

# 
安装 dnf 包管理器
yum -y install dnf

# 
移除 yum 及相关组件
dnf -y remove yum yum-metadata-parser

2.6 配置 Rocky Linux 8 软件源

# 删除 CentOS 7 的软件源
rpm -e --nodeps `rpm -qa|grep centos-`

# 
安装 Rocky Linux 8 的软件源
rpm -ivh --nodeps --force https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/r/rocky-gpg-keys-8.10-1.9.el8.noarch.rpm
rpm -ivh --nodeps --force https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/r/rocky-release-8.10-1.9.el8.noarch.rpm
rpm -ivh --nodeps --force https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/r/rocky-repos-8.10-1.9.el8.noarch.rpm

# 
升级 EPEL 源到版本 8
dnf -y upgrade https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

# 
清理缓存
dnf clean all

2.7 卸载旧内核并配置 Python 编码

2.7.1 卸载 CentOS 7 内核

# 卸载 CentOS 7 的内核包
rpm -e --nodeps `rpm -qa|grep -i kernel`

2.7.2 配置 Python 2 编码

编辑 Python 站点配置文件:

vi /usr/lib/python2.7/site-packages/sitecustomize.py

添加以下内容:

# encoding=utf8

import
 sys

reload(sys)
sys.setdefaultencoding('utf8')

2.8 执行系统升级

此步骤可能遇到错误,按以下步骤逐一解决。

2.8.1 第一次尝试升级及错误处理

# 执行系统升级
dnf -y --releasever=8 --allowerasing --setopt=deltarpm=false distro-sync

可能出现的错误:

运行事务检查
错误:事务检查与依赖解决错误:
  (gcc >= 8 with gcc < 9) 被 annobin-10.67-3.el8.x86_64 需要
  rpmlib(RichDependencies) <= 4.12.0-1 被 annobin-10.67-3.el8.x86_64 需要
  (NetworkManager >= 1.20 or dhclient) 被 dracut-network-049-218.git20221019.el8_7.x86_64 需要
  rpmlib(RichDependencies) <= 4.12.0-1 被 dracut-network-049-218.git20221019.el8_7.x86_64 需要
  (annobin if gcc) 被 redhat-rpm-config-130-1.el8.noarch 需要
  (gcc-plugin-annobin if gcc) 被 redhat-rpm-config-130-1.el8.noarch 需要
  rpmlib(RichDependencies) <= 4.12.0-1 被 redhat-rpm-config-130-1.el8.noarch 需要

解决方案:

# 查找冲突的软件包位置
find /var/cache/dnf/ -name *annobin-10.67-3.el8.x86_64*
# 
输出: /var/cache/dnf/appstream-62ae9a0bbea44fbe/packages/annobin-10.67-3.el8.x86_64.rpm

find /var/cache/dnf/ -name *dracut-network-049-218.git20221019.el8_7.x86_64*
# 
输出: /var/cache/dnf/baseos-3e608afeebc9a90b/packages/dracut-network-049-218.git20221019.el8_7.x86_64.rpm

find /var/cache/dnf/ -name *redhat-rpm-config-130-1.el8.noarch*
# 
输出: /var/cache/dnf/appstream-62ae9a0bbea44fbe/packages/redhat-rpm-config-130-1.el8.noarch.rpm

# 
强制安装冲突的软件包
rpm -ivh --nodeps --force /var/cache/dnf/appstream-62ae9a0bbea44fbe/packages/annobin-10.67-3.el8.x86_64.rpm
rpm -ivh --nodeps --force /var/cache/dnf/baseos-3e608afeebc9a90b/packages/dracut-network-049-218.git20221019.el8_7.x86_64.rpm
rpm -ivh --nodeps --force /var/cache/dnf/appstream-62ae9a0bbea44fbe/packages/redhat-rpm-config-130-1.el8.noarch.rpm

2.8.2 第二次尝试升级及错误处理

# 再次尝试系统升级
dnf -y --releasever=8 --allowerasing --setopt=deltarpm=false distro-sync

可能出现的错误:

运行事务测试
错误:事务检查错误:
  file /usr/sbin/pidof from install of procps-ng-3.3.15-9.el8.x86_64 conflicts with file from package sysvinit-tools-2.88-14.dsf.el7.x86_64
  file /usr/bin/last from install of util-linux-2.32.1-39.el8_7.x86_64 conflicts with file from package sysvinit-tools-2.88-14.dsf.el7.x86_64
  file /usr/bin/mesg from install of util-linux-2.32.1-39.el8_7.x86_64 conflicts with file from package sysvinit-tools-2.88-14.dsf.el7.x86_64
  file /usr/bin/wall from install of util-linux-2.32.1-39.el8_7.x86_64 conflicts with file from package sysvinit-tools-2.88-14.dsf.el7.x86_64
  file /usr/share/man/man1/last.1.gz from install of util-linux-2.32.1-39.el8_7.x86_64 conflicts with file from package sysvinit-tools-2.88-14.dsf.el7.x86_64
  file /usr/share/man/man1/mesg.1.gz from install of util-linux-2.32.1-39.el8_7.x86_64 conflicts with file from package sysvinit-tools-2.88-14.dsf.el7.x86_64
  file /usr/share/man/man1/wall.1.gz from install of util-linux-2.32.1-39.el8_7.x86_64 conflicts with file from package sysvinit-tools-2.88-14.dsf.el7.x86_64

解决方案:

# 删除冲突的软件包
rpm -e --nodeps sysvinit-tools-2.88-14.dsf.el7.x86_64

2.8.3 第三次尝试升级

# 第三次执行升级
dnf -y --releasever=8 --allowerasing --setopt=deltarpm=false distro-sync

# 
升级成功完成

2.9 清理和重新配置软件仓库

卸载旧的 EPEL 仓库,重新安装并清理冲突软件包,然后重装 rpmconf 并执行配置检查。

# 进入仓库配置目录
cd /etc/yum.repos.d

# 
删除旧的 EPEL 仓库文件
rm -rf /etc/yum.repos.d/CentOS-Base.repo.rpmsave \
        /etc/yum.repos.d/CentOS-Epel.repo \
        /etc/yum.repos.d/epel-modular.repo \
        /etc/yum.repos.d/epel.repo \
        /etc/yum.repos.d/epel-testing-modular.repo \
        /etc/yum.repos.d/epel-testing.repo

# 
重新安装 EPEL 仓库
dnf -y reinstall epel-release

# 
删除冲突的软件包
rpm -e --nodeps python36-rpmconf-1.1.7-1.el7.1.noarch

# 
重装 rpmconf
dnf -y install rpmconf yum-utils

# 
执行 rpmconf 检查配置文件
# 
在交互界面中一直输入 Y 并回车即可
rpmconf -a

执行 rpmconf -a 后会看到类似以下交互界面:

Configuration file '/etc/pam.d/fingerprint-auth'
-rw-r--r--. 1 root root 701 Jul  3 04:47 /etc/pam.d/fingerprint-auth.rpmnew
lrwxrwxrwx. 1 root root  19 Aug  6 10:32 /etc/pam.d/fingerprint-auth -> fingerprint-auth-ac

 ==> Package distributor has shipped an updated version.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      M     : merge configuration files
      Z     : background this process to examine the situation
      S     : skip this file
 The default action is to keep your current version.
*** aliases (Y/I/N/O/D/M/Z/S) [default=N] ?
Your choice:

2.10 安装 Rocky Linux 8 内核

安装 Rocky Linux 8 内核及相关引导组件。如果升级后无法启动,可以使用 Rocky Linux 8 ISO 镜像的救援模式安装内核。

# 安装内核核心组件
dnf -y install kernel kernel-core

# 
安装 GRUB 引导相关组件
dnf -y install shim grub2-tools-extra grubby grub2-common grub2-pc
dnf -y install grub2-tools-efi grub2-tools-minimal grub2-efi grub2-pc-modules grub2-tools

2.11 修复系统引导

执行以下命令修复引导配置,防止迁移完成后重启无法启动。根据分区表类型选择对应命令。

# 查看分区表类型
fdisk -l

MBR 分区(传统 BIOS、DOS、MSDOS)

适用于传统 BIOS 系统。注意 /dev/vda 需根据实际情况替换为正确的磁盘路径。

# 生成 GRUB 配置文件
grub2-mkconfig -o /boot/grub2/grub.cfg

# 
安装 GRUB 到磁盘
grub2-install /dev/vda

GPT 分区(UEFI BIOS)

适用于 UEFI BIOS 系统。

# 查找 Rocky 引导目录
export grubcfg=`find /boot/ -name rocky`

# 
生成 GRUB 配置文件
grub2-mkconfig -o $grubcfg/grub.cfg

# 
删除 CentOS 引导残留
rm -rf `find /boot/ -name centos`

添加 UEFI 启动项。注意:

  • • /dev/nvme0n1 为 EFI 分区所在磁盘,需根据实际情况替换
  • • -p 后的数字为分区位置,默认为 1
    • • 若 EFI 分区为 /dev/nvme0n1p1,值为 1
    • • 若 EFI 分区为 /dev/nvme0n1p2,值为 2
efibootmgr -c -w -L "RockyLinux" -d /dev/nvme0n1 -p 1 -l \\EFI\\Boot\\bootx64.efi

2.12 安装最小化环境并重启

# 清理旧配置(可选,可解决某些 yum 报错问题)
rm -rf /etc/yum

# 
安装最小化基础环境
dnf -y groupinstall "Minimal Install"

# 
重启系统
reboot

重启完成后,系统已成功从 CentOS 7 升级到 Rocky Linux 8。

相关阅读:
CentOS 7 升级到 Rocky Linux 9 新手完全指南(一):升级前准备与环境检查

最新文章

随机文章