前言
我们常说的给 Ubuntu 换源,实际上是指的给 Debian/Ubuntu 系的 Linux 操作系统中的软件包管理器(Advanced Packaging Tool,APT)更换仓库地址
OS | Format | Tools | Update packages |
---|---|---|---|
Debian | .deb | apt, apt-cache, apt-get, dpkg | sudo apt-get update |
Ubuntu | .deb | apt, apt-cache, apt-get, dpkg | sudo apt-get update |
CentOS | .rpm | yum | yum check-update |
Fedora | .rpm | dnf | dnf check-update |
FreeBSD | .txz, [Ports] | pkg, [make] | sudo pkg update [sudo portsnap fetch update] |
回到正题,在国内使用官方源由于网络的问题下载太慢,因此我们一般需要更换到国内的一些镜像站提供的数据源
更换方案
Ubuntu 的源存放在 /etc/apt/sources.list
下,以 18.04 LTS 版本为例,内容如下
$ cat /etc/apt/sources.list
#deb cdrom:[Ubuntu 18.04.5 LTS _Bionic Beaver_ - Release amd64 (20200806.1)]/ bionic main restricted
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://us.archive.ubuntu.com/ubuntu/ bionic main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ bionic universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic universe
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://us.archive.ubuntu.com/ubuntu/ bionic multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic multiverse
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu bionic partner
# deb-src http://archive.canonical.com/ubuntu bionic partner
deb http://security.ubuntu.com/ubuntu bionic-security main restricted
# deb-src http://security.ubuntu.com/ubuntu bionic-security main restricted
deb http://security.ubuntu.com/ubuntu bionic-security universe
# deb-src http://security.ubuntu.com/ubuntu bionic-security universe
deb http://security.ubuntu.com/ubuntu bionic-security multiverse
# deb-src http://security.ubuntu.com/ubuntu bionic-security multiverse
方案一:手动更换源列表
大致步骤如下
- 备份官方源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
- 按照镜像站提供信息覆盖文件
sudo vim /etc/apt/sources.list
收集到的一些国内镜像源地址
镜像源 | Help link |
---|---|
中科大 USTC | https://mirrors.ustc.edu.cn/help/ubuntu.html |
阿里云 aliyun | https://developer.aliyun.com/mirror/ubuntu |
清华 tsinghua | https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ |
网易 163 | https://mirrors.163.com/.help/ubuntu.html |
清华源比较直接,选好 Ubuntu 发行版本复制内容到 sources.list 即可
方案二:通过图形化界面更新
打开文件夹 /etc/apt
,双击 sources.list,会自动展开相关设置
选择 other
选择 China 中的某个镜像源,如阿里云,完成
方案三:脚本自动化更新
待实现 TODO
参考
[1] Ubuntu 换源看这一篇就够了:https://cloud.tencent.com/developer/article/1880981
[2] Linux 包管理基础:apt、yum、dnf 和 pkg :https://linux.cn/article-8782-1.html