跳到主要内容
Linux脚本库

国内服务器换源完全指南:apt / dnf / apk 一次讲清

更新于 #Linux 换源#Ubuntu#Debian#EPEL#Alpine

国内服务器装包慢,多数时候不是带宽不够,而是源不对:官方源在境外,延迟高、还可能被限速。这篇面向需要手动换源或排查源报错的运维,按 apt、dnf、apk 三类包管理器给出可直接复制的配置。文中地址以清华 TUNA 和阿里云为例,换用其他镜像站时目录结构可能不同,以该镜像站的帮助页为准。换源会改动系统文件,先备份,再动手。

换源能改善下载速度,但镜像同步天然有延迟:普通软件源可以换,安全更新建议保留官方源,下面 Debian 一节会展开。

换源前先备份

按你的系统执行对应的一行,文件不存在就跳过。恢复时把备份复制回原路径即可。

Terminal window
sudo cp -a /etc/apt/sources.list "/etc/apt/sources.list.bak.$(date +%F)"
sudo cp -a /etc/apt/sources.list.d/ubuntu.sources "/etc/apt/sources.list.d/ubuntu.sources.bak.$(date +%F)"
sudo cp -a /etc/yum.repos.d "/etc/yum.repos.d.bak.$(date +%F)"
sudo cp -a /etc/apk/repositories "/etc/apk/repositories.bak.$(date +%F)"

apt:Ubuntu 24.04 起默认用 deb822

从 Ubuntu 24.04(代号 noble)起,源配置默认放在 /etc/apt/sources.list.d/ubuntu.sources,采用 deb822 格式,/etc/apt/sources.list 不再承载配置。替换为下面内容(镜像站以清华 TUNA 为例):

Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

第一段是主源,第二段保留官方安全源。其他版本换代号即可,例如 22.04 是 jammy。ARM 服务器(aarch64)的 Ubuntu 官方源在 ports.ubuntu.com,要换到镜像站的 ubuntu-ports 目录,不能照抄 ubuntu 路径。旧版系统仍用一行式 /etc/apt/sources.list:

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 http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

普通安装的 Debian 12/13 通常还是一行式,把套件换成 bookworm 或 trixie,组件包含 main contrib non-free non-free-firmware(Debian 12 起 non-free-firmware 是独立组件)。从 Debian 12 起,容器镜像默认改用 /etc/apt/sources.list.d/debian.sources 的 deb822 格式;以文件的实际形态为准,两种配置不要同时生效。

Debian security 源不要替换

安全更新对时效敏感,镜像站同步有延迟,建议 security 继续指向官方的 security.debian.org / security.ubuntu.com,只替换主源、updates 与 backports。Debian 的 security 位于独立的 debian-security 路径,写成 .../debian/ bookworm-security 会 404;确实要用镜像时,URI 必须写到 debian-security 目录。

RHEL 系:.repo 与 $releasever / $basearch

RHEL、Rocky、AlmaLinux、CentOS Stream 的源在 /etc/yum.repos.d/*.repo。$releasever 是发行版主版本(如 9),$basearch 是架构(x86_64、aarch64),换源时保留变量,镜像站才能跟着系统版本走。以 Rocky 9 的 BaseOS 为例(阿里云镜像):

[baseos]
name=Rocky Linux $releasever - BaseOS
baseurl=https://mirrors.aliyun.com/rockylinux/$releasever/BaseOS/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9

AppStream、Extras、CRB 等同理,只换路径中的仓库名;gpgkey 保持发行版自带的 key 路径,不要删。只改 baseurl 时,记得注释掉原文件里的 metalink=,否则 dnf 仍会走官方 metalink。CentOS 7 已 EOL,源需指向 vault 归档路径,不建议在新机器上继续使用。

EPEL 单独配置

EPEL 是独立的附加仓库,和系统源分开处理。先安装 epel-release,再把 epel.repo 与 epel-testing.repo 的 metalink 注释掉、启用镜像 baseurl:

Terminal window
sudo dnf install -y epel-release
sudo sed -e 's!^metalink=!#metalink=!g' \
-e 's!^#baseurl=!baseurl=!g' \
-e 's!https\?://download\.fedoraproject\.org/pub/epel!https://mirrors.aliyun.com/epel!g' \
-e 's!https\?://download\.example/pub/epel!https://mirrors.aliyun.com/epel!g' \
-i /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel-testing.repo

改完后 baseurl 形如 https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch/,gpgkey 仍用 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-$releasever(由 epel-release 提供)。部分镜像站不同步 EPEL 的 Cisco OpenH264 仓库,用不到时可把 epel-cisco-openh264.repo 里的 enabled 改为 0。

Alpine:/etc/apk/repositories

Alpine 的仓库配置是一行一个地址,直接编辑 /etc/apk/repositories:

https://mirrors.tuna.tsinghua.edu.cn/alpine/latest-stable/main
https://mirrors.tuna.tsinghua.edu.cn/alpine/latest-stable/community

latest-stable 跟随最新稳定版;需要固定版本时换成镜像站里实际的 v3.xx 目录。改完执行 sudo apk update。

验证

按系统执行其中一条:

Terminal window
sudo apt update
sudo dnf clean all && sudo dnf makecache
sudo apk update

能正常拉取元数据、没有 GPG 或 404 报错,才算换源成功;再挑一个体积小的包试装一次更稳妥。RHEL 系可用 dnf repolist -v 查看解析后的 baseurl,确认 $releasever、$basearch 符合预期。

常见错误

  • GPG 报错:NO_PUBKEY 或签名无法验证,通常是 Signed-By 指向的 keyring 不存在、误用了其他发行版的配置,或镜像尚未同步完。Ubuntu 的 keyring 是 ubuntu-archive-keyring.gpg,Debian 是 debian-archive-keyring.gpg;不要再用已废弃的 apt-key。
  • 404:套件名或代号写错(24.04 是 noble,22.04 是 jammy)、组件不存在、发行版已 EOL 被移入归档,或 security 套件写到了主源路径。
  • releasever 不对:系统升级过或容器环境缺少版本信息,导致 $releasever 解析结果与 baseurl 不匹配;用 dnf repolist -v 看实际 URL。
  • 混用源:sources.list 与 ubuntu.sources / debian.sources 同时生效,或旧 .repo 没清理,出现重复条目警告,甚至装错版本;只保留一套配置。
  • HTTPS 失败:Debian 10(buster)之前的系统默认不支持 HTTPS 源,先安装 apt-transport-https ca-certificates。

换源脚本要选可信来源

换源脚本以 root 身份改系统配置,风险和直接执行 root 代码相同。使用前至少确认:固定版本与 sha256、支持 --dry-run、会备份原文件、能回滚、源码可读。来源不明的“一键换源”不要在生产上直接跑,更不要 curl | bash。本站的换源脚本即将上线,会按这个标准提供版本化产物与校验文件。

相关

输入关键词开始搜索。脚本名、发行版、分类、文档标题都会被索引。

↑↓ 选择 · Enter 打开浏览全部脚本