为Ubuntu的apt配置大陆镜像源

本文时间久远,部分内容可能过时,仅供参考。

基于DebianLinux发行版可以使用apt(Advanced Packaging Tool)高级包管理器安装软件。

# 比如安装jekyll,一个静态站点生成工具
sudo apt install jekyll

apt会从指定的服务器上查找jekyll软件包,下载并安装,在Software & Update中可以看到系统当前使用的源是Main server

Ubuntu

众所周知的网络原因,Main server在中国大陆的访问速度非常慢,有必要修改为国内的镜像源。Ubuntu 18.04.3 LTS点击Download from下拉列表,会发现系统提供很多大陆阿里云是一个不错的选择。

Ubuntu

设置好后,在Terminal里更新一下包索引,可以看到apt源已经变成阿里云镜像

Ubuntu Terminal

手动配置

对于一些老版本Ubuntu,需要手动修改/etc/apt/sources.list文件来配置apt源,其实Software & Update本质也是修改这个文件,只是提供更直观的图形界面。

默认的/etc/apt/sources.list文件内容类似于如下格式,可能服务器地址会有不同,与安装Ubuntu时选择的国家和地区有关。

main multiverse #Added by software-properties

## 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://cn.archive.ubuntu.com/ubuntu/ bionic universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic universe
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-updates universe
# deb-src http://cn.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://cn.archive.ubuntu.com/ubuntu/ bionic multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ bionic-updates multiverse
# deb-src http://cn.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://cn.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse #Added by software-properties

## 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 universe restricted main multiverse #Added by software-properties
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

把注释去掉,调整一下格式,能看得更清楚一点:

deb http://cn.archive.ubuntu.com/ubuntu/ bionic universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic universe multiverse

deb http://security.ubuntu.com/ubuntu bionic-security main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu bionic-security main restricted universe multiverse 

deb http://cn.archive.ubuntu.com/ubuntu/ bionic-updates universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic-updates universe multiverse

deb http://cn.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse

每一行都由3部分组成,定义一个镜像服务器的多个目录地址。

  • 第1部分是debdeb-src,分别表示直接通过.deb包安装和通过源代码安装。
  • 第2部分是服务器的URL根地址。
  • 第3部分是服务器的URL根地址之下的具体目录结构。

浏览器访问http://cn.archive.ubuntu.com/ubuntu/

apt

进入dists/目录:

apt

可以看到,每一个Ubuntu系统代号都有5个目录,比如18.04代号是bionic,对应bionic/bionic-backports/bionic-proposed/bionic-sercurity/bionic-updates/

随便进入一个目录,/bionic/

apt

可以看到main/universe/multiverse/retricted/都是具体目录。

根据官方文档,阿里云对应Ubuntu 18.04镜像地址如下:

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

自定义apt源可以直接修改/etc/apt/sources.list文件,替换成以上地址。

# 修改系统文件前备份是个好习惯
sudo cp /etc/apt/sources.list /etc/apt/sources.list.back
sudo vim /etc/apt/sources.list
# 修改完成后,更新包索引
sudo apt update

Ubuntu也提供/etc/apt/sources.list.d/目录来存放用户自定义的地址,可以不修改/etc/apt/sources.list而在/etc/apt/sources.list.d/目录下新建一个aliyun.list文件,把阿里云镜像地址复制进去即可。

# 创建aliyun.list文件,把阿里云的镜像地址复制进去
sudo vim /etc/apt/sources.list.d/aliyun.list
# 修改完成后,更新包索引
sudo apt update
arrow_upward