由于众所周知的原因,在国内使用Debian服务器,那么更换国内镜像源这件事几乎可以算是装机后的必备操作,虽然这个操作不常做,但却是运维的必备技能。

国内有一大批高速、稳定的镜像站(阿里云、清华、中科大……),换上之后,速度立竿见影,体验瞬间从“煎熬”变成“丝滑”。

今天我们就用最直接的方式,一步步和你一起完成配置。

第一步:查清你的Debian版本代号

登录服务器,执行下面这条命令:

1
lsb_release -c

你会看到一个代号,比如:

  • bookworm → Debian 12
  • bullseye → Debian 11
  • buster → Debian 10
    请一定记住这个代号,后面配置全靠它。

第二步:确认你的配置文件格式(关键!)

Debian 12开始引入了一种新格式,配置文件位置变了。
我们分两种情况,对号入座就行。

📁 情况A:传统格式(适用于Debian 11及更早,或部分Debian 12)

配置文件是 /etc/apt/sources.list

① 先备份(防手误)

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

② 编辑文件

将原有内容全部删除或注释掉,然后粘贴下面任意一组镜像源(记得把bookworm换成你的代号):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 中科大
deb http://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb http://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb http://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware

# 清华大学
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware

# 阿里云
deb https://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware

📁 情况B:DEB822新格式(Debian 12新装系统或容器镜像常见)

配置文件是 /etc/apt/sources.list.d/debian.sources

① 备份

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

② 编辑文件

将原有内容全部删除或注释掉,然后粘贴下面内容(以中科大为例子,记得把bookworm换成你的代号):

1
2
3
4
5
6
7
8
9
10
11
Types: deb
URIs: http://mirrors.ustc.edu.cn/debian
Suites: bookworm bookworm-updates
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

Types: deb
URIs: http://mirrors.ustc.edu.cn/debian-security
Suites: bookworm-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

第三步:更新软件包列表,使配置生效

无论你用哪种格式,最后必须执行

1
sudo apt update

如果屏幕没有报错,恭喜你,配置成功啦!
现在你再装任何软件,都能享受飞一般的速度。

🎁 附赠:常用国内镜像源速查表

你还可以选这些国内镜像源:

镜像站 地址(以bookworm为例)
清华大学 https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm ...
阿里云 https://mirrors.aliyun.com/debian/ bookworm ...
华为云 https://mirrors.huaweicloud.com/debian/ bookworm ...
网易 http://mirrors.163.com/debian/ bookworm ...
腾讯云 https://mirrors.cloud.tencent.com/debian/ bookworm ...

⚠️ 注意事项

  • 务必先备份原文件,万一写错还能恢复。
  • 版本代号(如bookworm)一定要正确,否则会报错。
  • 如果apt update后出现“签名”相关错误,检查Signed-By路径是否正确(通常默认没问题)。
  • 若使用非官方镜像源,请确保信任该源,国内主流大厂均可放心。

结语

换源是Debian服务器优化中最简单、最见效的一步。
整个过程不超过3分钟,却能节省你日后无数等待时间。

如果你觉得有用,欢迎点赞、在看、转发,让更多小伙伴告别慢速源,畅快用Debian!