先厘清关键点
- Ubuntu 默认使用 APT/dpkg,并不使用 YUM/RPM。因此出现“Yum 仓库失效”的提示,多数是误用命令或把 YUM 配置文件放在了 /etc/yum.repos.d/ 而系统实际读取的是 /etc/apt/sources.list。正确做法是:日常用 APT;只有在你明确需要 RPM 生态时才在 Ubuntu 上额外安装并使用 YUM。
场景一 你其实在用 APT 但看到 YUM 报错
- 典型现象:运行 apt 相关命令时报错,或误执行了 yum 命令。
- 处理步骤:
- 备份当前源并改用可用镜像(以 Ubuntu 22.04 jammy 为例,其他版本把代号替换为你的系统代号,如 focal、jammy、noble):
- 备份:sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
- 写入官方源或国内镜像(二选一):
- 官方源:
deb https://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse
deb https://archive.ubuntu.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
- 清华镜像(示例):
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
- 更新索引并校验:
- sudo apt clean && sudo apt update
- 若仍提示找不到包或源不可用,检查网络连通性(如 ping archive.ubuntu.com),再换镜像重试。以上步骤可修复绝大多数“找不到源/源列表损坏”的问题。
场景二 你的系统版本已 EOL 官方源下线
- 典型现象:apt update 大量 404,或提示仓库不存在。
- 处理步骤:
- 查看版本代号:lsb_release -a(如 14.04 trusty、16.04 xenial 等)。
- 将源切换到 old-releases.ubuntu.com(以 trusty 为例):
- 备份:sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
- 替换为:
deb http://old-releases.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
需要源码时可加 deb-src,同步添加 -updates/-security/-backports 对应行
- 更新索引:sudo apt update
注意:过期的系统存在安全风险,建议尽快升级到受支持的版本。
场景三 你确实需要在 Ubuntu 上使用 YUM
- 说明与风险:Ubuntu 并非 RPM 系,强行使用 YUM 容易出现依赖与兼容性问题;仅在确有 RPM 场景(如特定容器/实验)时采用。
- 处理步骤:
- 安装 YUM(它会从 Ubuntu 仓库拉取 RPM 相关依赖,可能并不完整):
- sudo apt update
- sudo apt install yum
- 如需配置 YUM 仓库,编辑或新增文件于 /etc/yum.repos.d/*.repo,例如:
[ubuntu-base]
name=Ubuntu Base
baseurl=http://archive.ubuntu.com/ubuntu/
enabled=1
gpgcheck=1
gpgkey=https://archive.ubuntu.com/ubuntu/archive-key.gpg
- 刷新并验证:
- sudo yum clean all
- sudo yum makecache
- sudo yum repolist
- 建议优先使用 容器/虚拟机运行 CentOS/RHEL 来获得完整的 YUM/RHEL 生态体验。