树莓派镜像源替换 — 超级详细新手教程

树莓派镜像源替换 — 超级详细新手教程

适用系统:Raspberry Pi OS(Debian 12 bookworm / Debian 13 trixie)
面向读者:零基础新手小白


通过这篇教程,你可以学会:

  • 什么是"镜像源",为什么要换
  • 如何测试哪个镜像源最快
  • 如何正确替换树莓派的所有镜像源(包括apt和pip)
  • 如何验证镜像源是否生效

一、什么是"镜像源"?为什么要换?

1.1 简单理解

你的树莓派需要安装软件时,会去一个"软件仓库"下载。这个仓库的地址就是镜像源

打个比方:

  • 官方源 = 海外的仓库,东西全但运过来很慢
  • 国内镜像源 = 国内建的"分仓库",从海外同步过来,你从国内取货就快多了

1.2 为什么要换?

树莓派默认的软件源在国外(欧洲),从国内访问速度很慢。换成国内镜像源后:

对比项官方源国内镜像源
下载速度50~200 KB/s500~1500 KB/s
安装一个软件几分钟几秒钟
系统更新半小时+几分钟

1.3 国内有哪些镜像源?

镜像源提供方特点
华为云镜像华为速度最快,Debian源优秀
阿里云镜像阿里巴巴速度快,支持树莓派专用源
清华大学镜像清华大学老牌镜像,稳定
中科大镜像中国科学技术大学学术界常用

重要:不同镜像源的速度因地区和网络而异,本教程会教你如何测试选择最快的。


二、准备工作

2.1 你需要的东西

工具用途下载地址
PuTTYSSH远程连接树莓派https://www.putty.org/
树莓派需要换源的那台已开机联网

2.2 确认你的系统版本

打开PuTTY,连接到树莓派,输入以下命令查看系统版本:

cat /etc/os-release

你会看到类似这样的输出:

PRETTY_NAME="Debian GNU/Linux 13 (trixie)"
VERSION_ID="13"
VERSION_CODENAME=trixie

记住 VERSION_CODENAME 那一行的值(如 trixiebookworm),后面要用。

什么是版本代号? Debian系统每个版本都有一个代号,比如12叫bookworm,13叫trixie。镜像源的地址里必须写对代号,否则会找不到软件包。


三、测试哪个镜像源最快(推荐做)

不要盲目跟教程换源,先测一下哪个最快!

3.1 连接树莓派

用PuTTY连接树莓派(如果不会,请参考第一章VNC教程中的SSH连接部分)。

3.2 运行速度测试

复制以下命令,粘贴到PuTTY中执行:

echo "=== 华为云 ===" && curl -o /dev/null -s -w "速度: %{speed_download} bytes/s\n时间: %{time_total}s\n" https://repo.huaweicloud.com/debian/dists/你的版本代号/Release && echo "=== 阿里云 ===" && curl -o /dev/null -s -w "速度: %{speed_download} bytes/s\n时间: %{time_total}s\n" https://mirrors.aliyun.com/debian/dists/你的版本代号/Release && echo "=== 清华 ===" && curl -o /dev/null -s -w "速度: %{speed_download} bytes/s\n时间: %{time_total}s\n" https://mirrors.tuna.tsinghua.edu.cn/debian/dists/你的版本代号/Release && echo "=== 中科大 ===" && curl -o /dev/null -s -w "速度: %{speed_download} bytes/s\n时间: %{time_total}s\n" https://mirrors.ustc.edu.cn/debian/dists/你的版本代号/Release

⚠️ 把 你的版本代号 替换成你实际看到的代号,比如 trixiebookworm

3.3 看结果选最快的

速度数字越大越好,时间越小越好。选速度最快的那个作为你的Debian主源。


四、替换镜像源(核心步骤)

⚠️ 关键知识:你的树莓派有两套源配置

这是很多人踩坑的地方!Debian 13 (trixie) 和 Debian 12 (bookworm) 使用了两种格式的源文件

文件格式说明
/etc/apt/sources.list传统格式(旧)一行一个源地址
/etc/apt/sources.list.d/*.sourcesDEB822格式(新)结构化配置,优先级更高

重要:如果 .sources 文件存在,它会覆盖 sources.list 的设置!所以你必须两个都改,否则改了等于没改。

4.1 第一步:备份!备份!备份!

改任何配置文件之前,先备份原文件。万一改错了还能恢复:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo cp /etc/apt/sources.list.d/debian.sources /etc/apt/sources.list.d/debian.sources.bak
sudo cp /etc/apt/sources.list.d/raspi.sources /etc/apt/sources.list.d/raspi.sources.bak
sudo cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak

如果恢复备份:sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list

4.2 第二步:替换 Debian 主源(华为云最快)

4.2.1 修改 debian.sources(DEB822新格式,优先级高)
sudo nano /etc/apt/sources.list.d/debian.sources

把内容全部删除,替换为(注意把 trixie 换成你的版本代号):

Types: deb
URIs: https://repo.huaweicloud.com/debian/
Suites: trixie trixie-updates
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp

Types: deb
URIs: https://repo.huaweicloud.com/debian-security/
Suites: trixie-security
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp

nano编辑器操作Ctrl+O 保存 → Enter 确认 → Ctrl+X 退出

如果你测出来阿里云更快,把 repo.huaweicloud.com 换成 mirrors.aliyun.com

4.2.2 修改 sources.list(传统格式,保持一致)
sudo nano /etc/apt/sources.list

替换为:

deb https://repo.huaweicloud.com/debian/ trixie main contrib non-free non-free-firmware
deb https://repo.huaweicloud.com/debian-security/ trixie-security main contrib non-free non-free-firmware
deb https://repo.huaweicloud.com/debian/ trixie-updates main contrib non-free non-free-firmware

4.3 第三步:替换树莓派专用源(阿里云最快)

树莓派有一些专用软件(如GPIO库、摄像头工具等),需要单独的树莓派源。

为什么不用华为云? 华为云的树莓派专用源速度很慢(实测只有16KB/s),阿里云快得多(574KB/s)。

4.3.1 修改 raspi.sources(DEB822新格式)
sudo nano /etc/apt/sources.list.d/raspi.sources

替换为:

Types: deb
URIs: https://mirrors.aliyun.com/raspberrypi/
Suites: trixie
Components: main
Signed-By: /usr/share/keyrings/raspberrypi-archive-keyring.gpg
4.3.2 修改 raspi.list(传统格式)
sudo nano /etc/apt/sources.list.d/raspi.list

替换为:

deb https://mirrors.aliyun.com/raspberrypi/ trixie main

4.4 第四步:配置 pip 镜像源(Python包管理器)

如果你以后要用Python安装包(如 pip install numpy),也需要换源:

mkdir -p ~/.config/pip
nano ~/.config/pip/pip.conf

写入以下内容:

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com

五、验证镜像源是否生效

5.1 更新软件列表

sudo apt update

观察输出,如果看到 Get:1 https://repo.huaweicloud.comGet:2 https://mirrors.aliyun.com,说明镜像源已生效!

5.2 查看当前使用的源

apt policy | head -30

输出中应该能看到 origin repo.huaweicloud.comorigin mirrors.aliyun.com

5.3 测试pip镜像

pip3 install --dry-run --break-system-packages requests

如果输出 Looking in indexes: https://mirrors.aliyun.com/pypi/simple/,说明pip镜像也生效了。


六、常见问题

Q1:apt update 报错 “Could not resolve”

原因:DNS解析失败,树莓派无法访问镜像源地址。

解决

sudo nano /etc/resolv.conf

添加:

nameserver 8.8.8.8
nameserver 114.114.114.114

Q2:apt update 报错 “The repository is not signed”

原因:镜像源的签名密钥不匹配。

解决:重新安装密钥:

sudo apt install --reinstall debian-archive-keyring raspberrypi-archive-keyring

Q3:我改了 sources.list 但没效果

原因.sources 文件优先级更高,覆盖了你的修改。

解决:必须同时修改 .sources 文件!参见本文第四章。

Q4:Debian 12 (bookworm) 和 Debian 13 (trixie) 有什么区别?

回答:bookworm是Debian 12(2023年发布),trixie是Debian 13(2025年发布)。它们的软件包版本不同,镜像源地址中的代号也不同。请用 cat /etc/os-release 确认你的版本代号。

Q5:pip install 报错 “externally-managed-environment”

原因:Debian 13 出于安全考虑,不允许直接用pip安装系统级Python包。

解决方法(三选一)

  1. 推荐:使用虚拟环境
python3 -m venv myenv
source myenv/bin/activate
pip install 你要的包
  1. 简单粗暴:加 --break-system-packages 参数
pip3 install --break-system-packages 你要的包
  1. 用apt安装:很多Python包有apt版本
sudo apt install python3-包名

七、关于"国外资源"的说明

你可能会问:国内镜像源会不会缺少某些国外的软件包?

答案:几乎不会。 国内镜像源是官方源的完整同步副本,包含所有软件包。但以下情况需要注意:

场景是否受影响解决方案
apt安装普通软件❌ 不受影响国内镜像有完整副本
pip安装Python包❌ 不受影响国内镜像有完整副本
Docker镜像拉取⚠️ 可能受影响配置Docker国内镜像加速器
GitHub下载release⚠️ 可能受影响使用GitHub镜像站
npm安装Node.js包⚠️ 可能受影响配置npm国内镜像

如果你后续需要用到Docker、GitHub等国外资源,可以在对应章节中配置加速方案。


八、命令速查表

操作命令
查看系统版本cat /etc/os-release
备份源文件sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
编辑源文件sudo nano /etc/apt/sources.list
更新软件列表sudo apt update
升级所有软件sudo apt upgrade -y
查看当前源apt policy | head -30
恢复备份sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list
测试镜像速度curl -o /dev/null -s -w "速度: %{speed_download}\n" 镜像地址
查看pip配置pip3 config list

九、镜像源地址速查

Debian 主源

镜像地址
华为云https://repo.huaweicloud.com/debian/
阿里云https://mirrors.aliyun.com/debian/
清华https://mirrors.tuna.tsinghua.edu.cn/debian/
中科大https://mirrors.ustc.edu.cn/debian/

Debian 安全更新源

镜像地址
华为云https://repo.huaweicloud.com/debian-security/
阿里云https://mirrors.aliyun.com/debian-security/
清华https://mirrors.tuna.tsinghua.edu.cn/debian-security/
中科大https://mirrors.ustc.edu.cn/debian-security/

树莓派专用源

镜像地址
阿里云https://mirrors.aliyun.com/raspberrypi/
清华https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/

pip Python包源

镜像地址
阿里云https://mirrors.aliyun.com/pypi/simple/
清华https://pypi.tuna.tsinghua.edu.cn/simple/
华为云https://repo.huaweicloud.com/repository/pypi/simple/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗨菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值