CentOS 7 与 CentOS 8/ CentOS Stream 8 已经 EOL,然而作为 Red Hat Enterprise Linux 的免费社区版,一直以来在服务器操作系统领域占据着重要地位。
本文将介绍如何将 CentOS 7 升级到 CentOS Stream 9,如果你公司的 CTO 并没有计划更换到其他的 Linux 发行版,本文可能对你有所帮助。
Important
开始操作前,请确保对重要数据已经完成了非本机的备份。
由于发行版升级可能造成网卡名字的变动,所以请做好准备使用 VNC 或是显示器处理此问题。
由于 openssh-server 的升级,可能会造成过于陈旧的密钥格式无法使用,请做好相关预案。
Note
本文中的 mirrors.tuna.tsinghua.edu.cn 可以替换为 mirror.rackspace.com 或是其他的镜像源
更新 CentOS 7
在开始升级到 CentOS 9 之前,我们需要确保当前的 CentOS 7 系统已经更新到最新版本。
-
启用 CentOS 的归档源
由于默认的 mirrorlist 已经停止解析,所以我们需要手动启用 CentOS 的归档源。
sed -e 's|^mirrorlist=|#mirrorlist=|g' \ -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos|g' \ -i /etc/yum.repos.d/*.repo
-
清理 YUM 缓存并重新生成缓存
yum clean all yum makecache
-
安装 yum-utils 工具包来增强 YUM 的功能
yum install yum-utils -y
-
安装 EPEL(Extra Packages for Enterprise Linux)仓库,以便能够访问更多的软件包
yum install http://mirrors.tuna.tsinghua.edu.cn/epel/epel-release-latest-7.noarch.rpm -y
-
修改 EPEL 的镜像源文件
sed -e 's|^metalink=|#metalink=|g' \ -e 's|^#baseurl=|baseurl=|g' \ -e 's|https\?://download\.fedoraproject\.org/pub/epel|http://mirrors.tuna.tsinghua.edu.cn/epel|g' \ -e 's|https\?://download\.example/pub/epel|http://mirrors.tuna.tsinghua.edu.cn/epel|g' \ -i /etc/yum.repos.d/epel{,-testing}.repo
-
重新生成 YUM 缓存并进行系统升级
yum makecache yum upgrade -y
准备系统环境
在更新完 CentOS 7 之后,我们安装一些必要工具,为升级到 CentOS 8 做准备。
-
安装 rpmconf 工具,用于处理配置文件
yum install rpmconf -y
-
运行 rpmconf,检查并合并所有配置文件
在执行此命令时,系统会询问你是否要合并不同版本的配置文件,你需要根据实际情况来做出决定。
rpmconf -a
-
使用 package-cleanup 工具清理系统
在执行 package-cleanup --leaves 和 package-cleanup --orphans 命令时,系统会列出一些叶子包(无其他包依赖的包)和孤立包(不再被任何包依赖的包)。你需要根据具体情况来决定是否要移除这些包:
- 如果这些包对你的应用和服务没有影响,可以选择删除它们,以减少系统的冗余。
- 如果不确定某些包的作用,建议保留它们,以免影响系统运行。
package-cleanup --leaves package-cleanup --orphans
-
切换到 DNF 并移除 YUM
YUM 是 CentOS 7 使用的包管理器,而在 CentOS 8 及以后的版本中,DNF 取代了 YUM。所以我们需要安装 DNF 并移除 YUM。
yum install dnf -y
-
移除 YUM 及其相关组件,并删除 YUM 配置文件夹
dnf -y remove yum yum-metadata-parser && rm -rf /etc/yum
-
使用 DNF 升级现有的软件包
dnf upgrade -y
-
重新启动服务器
reboot
升级到 CentOS 8-Stream
-
安装 CentOS 8 核心包
dnf install http://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/BaseOS/x86_64/os/Packages/{centos-linux-repos-8-3.el8.noarch.rpm,centos-linux-release-8.5-1.2111.el8.noarch.rpm,centos-gpg-keys-8-3.el8.noarch.rpm} -y
-
修改镜像源配置文件
sed -e 's|^mirrorlist=|#mirrorlist=|g' \ -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos|g' \ -i /etc/yum.repos.d/CentOS-*.repo
-
移除 EPEL 7
dnf remove epel-release
-
安装 EPEL 8
dnf install http://mirrors.tuna.tsinghua.edu.cn/epel/epel-release-latest-8.noarch.rpm -y
-
修改 EPEL 镜像源配置文件
sed -e 's|^metalink=|#metalink=|g' \ -e 's|^#baseurl=https\?://download.fedoraproject.org/pub/epel/|baseurl=http://mirrors.tuna.tsinghua.edu.cn/epel/|g' \ -e 's|^#baseurl=https\?://download.example/pub/epel/|baseurl=http://mirrors.tuna.tsinghua.edu.cn/epel/|g' \ -i /etc/yum.repos.d/epel*.repo
-
再次运行 rpmconf 以处理配置文件
rpmconf -a
-
删除备份的镜像源配置文件
rm -rf /etc/yum.repos.d/CentOS-*.repo.rpmsave
-
删除旧的内核包
rpm -e $(rpm -q kernel)
-
清理 DNF 缓存
dnf clean all
-
移除不兼容的软件包
dnf remove dracut-network sysvinit-tools dracut-network
-
移除旧版本的 rpmconf
dnf remove python36-rpmconf
-
同步发行版软件包
这里将会将当前系统的包升级到 CentOS 8.5.2111
dnf -y --releasever=8.5.2111 --allowerasing --setopt=deltarpm=false distro-sync
-
安装内核包
dnf -y --releasever=8.5.2111 install kernel-core
-
更新核心和最小安装组
dnf -y --releasever=8.5.2111 groupupdate "Core" "Minimal Install"
-
安装最新版本的 rpmconf 和 yum-utils
dnf --releasever=8.5.2111 install rpmconf yum-utils
-
再次运行 rpmconf 处理配置文件
rpmconf -a
-
移除不需要的软件包和额外的软件包
这些软件包理论上都应该移除掉,但是请务必逐个确认是否已经有备份,以及后续是否有替代的工具。
dnf --releasever=8.5.2111 repoquery --unneeded dnf --releasever=8.5.2111 remove -y $(dnf --releasever=8.5.2111 repoquery --unneeded) dnf --releasever=8.5.2111 repoquery --extras dnf --releasever=8.5.2111 remove -y $(dnf --releasever=8.5.2111 repoquery --extras)
-
自动移除未使用的软件包
dnf --releasever=8.5.2111 autoremove -y
-
使用 package-cleanup 工具进一步清理系统
package-cleanup --leaves package-cleanup --orphans
-
列出仍然安装的 CentOS 7 软件包
这些软件包理论上都应该移除掉,但是请务必逐个确认是否已经有备份,以及后续是否有替代的工具。
dnf --releasever=8.5.2111 list --installed | grep el7
-
刷新并更新软件包:
dnf --releasever=8.5.2111 upgrade --refresh -y
-
安装 CentOS Stream 发布包
dnf --releasever=8.5.2111 install centos-release-stream
-
修改 CentOS Stream 镜像源配置文件
sed -e 's|^mirrorlist=|#mirrorlist=|g' \ -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos|g' \ -i /etc/yum.repos.d/CentOS-Stream-*.repo
-
切换到 CentOS Stream 仓库
dnf --releasever=8.5.2111 swap centos-{linux,stream}-repos
-
再次运行 rpmconf 处理配置文件
rpmconf -a
-
删除旧的 CentOS Linux 镜像源备份
rm -rf /etc/yum.repos.d/CentOS-Linux-*.repo.rpmsave
-
再一次修改 CentOS Stream 镜像源配置文件
sed -e 's|^mirrorlist=|#mirrorlist=|g' \ -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos-vault/centos|g' \ -i /etc/yum.repos.d/CentOS-Stream-*.repo
-
再次刷新并更新软件包
这里将会将当前系统的包升级到 CentOS Stream 8
dnf upgrade --refresh -y
-
同步发行版软件包
dnf -y distro-sync
-
重新启动服务器
reboot
升级到 CentOS Stream 9
-
禁用特定模块
dnf module disable python36 virt
-
安装 Perl,后续的步骤 5 需要
dnf install perl
-
安装 CentOS Stream 9 核心包
dnf install http://mirrors.tuna.tsinghua.edu.cn/centos-stream/9-stream/BaseOS/x86_64/os/Packages/{centos-stream-repos-9.0-26.el9.noarch.rpm,centos-stream-release-9.0-26.el9.noarch.rpm,centos-gpg-keys-9.0-26.el9.noarch.rpm}
-
再次运行 rpmconf 处理配置文件
rpmconf -a
-
替换镜像源
视网络情况的可选步骤,建议参考 http://mirrors.tuna.tsinghua.edu.cn/help/centos-stream/ 进行。
-
移除旧的 EPEL 包
dnf remove epel-release
-
删除备份的镜像源配置文件
rm -rf /etc/yum.repos.d/*.repo.rpmsave
-
安装 EPEL 9 和 EPEL Next 9
dnf install http://mirrors.tuna.tsinghua.edu.cn/epel/{epel-release-latest-9.noarch.rpm,epel-next-release-latest-9.noarch.rpm}
-
修改 EPEL 9 镜像源文件
sed -e 's!^metalink=!#metalink=!g' \ -e 's!^#baseurl=!baseurl=!g' \ -e 's!https\?://download\.fedoraproject\.org/pub/epel!http://mirrors.tuna.tsinghua.edu.cn/epel!g' \ -e 's!https\?://download\.example/pub/epel!http://mirrors.tuna.tsinghua.edu.cn/epel!g' \ -i /etc/yum.repos.d/{epel-next.repo,epel-next-testing.repo,epel.repo,epel-testing.repo}
-
清理 DNF 缓存并重新生成缓存
dnf clean all dnf makecache
-
删除旧的内核包
rpm -e $(rpm -q kernel)
-
卸载 CentOS Stream 9 中不受支持的安装包
dnf remove iptables-ebtables initscripts
-
同步发行版软件包
dnf -y --releasever=9 --allowerasing --setopt=deltarpm=false distro-sync
-
强制重启服务器
systemctl --force --force reboot
-
清理 DNF 缓存
dnf clean all
-
重建 RPM 数据库
rm -f /var/lib/rpm/__db* rpm --rebuilddb dnf makecache
-
重置特定模块
实际情况可能不同,因此需要重置的模块也可能有所不同。请根据具体情况进行调整。
dnf module reset perl perl-IO-Socket-SSL perl-libwww-perl satellite-5-client mysql
-
更新核心和最小安装组:
dnf -y groupupdate "Core" "Minimal Install"
-
同步发行版软件包
dnf -y distro-sync