为Ubuntu的apt配置大陆镜像源
立泉本文时间久远,部分内容可能过时,仅供参考。
基于Debian
的Linux
发行版可以使用apt(Advanced Packaging Tool)
高级包管理器安装软件。
# 比如安装jekyll,一个静态站点生成工具
sudo apt install jekyll
apt
会从指定的源
服务器上查找jekyll
软件包,下载并安装,在Software & Update
中可以看到系统当前使用的源是Main server
。
众所周知的网络原因,Main server
在中国大陆的访问速度非常慢,有必要修改为国内的镜像
源。Ubuntu 18.04.3 LTS
点击Download from
下拉列表,会发现系统提供很多大陆源
,阿里云
是一个不错的选择。
设置好后,在Terminal
里更新一下包索引,可以看到apt源
已经变成阿里云镜像
。
手动配置
对于一些老版本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部分是
deb
或deb-src
,分别表示直接通过.deb包
安装和通过源代码
安装。 - 第2部分是
源
服务器的URL
根地址。 - 第3部分是
源
服务器的URL
根地址之下的具体目录结构。
浏览器访问http://cn.archive.ubuntu.com/ubuntu/
:
进入dists/
目录:
可以看到,每一个Ubuntu
系统代号都有5个目录,比如18.04
代号是bionic
,对应bionic/
、bionic-backports/
、bionic-proposed/
、bionic-sercurity/
和bionic-updates/
。
随便进入一个目录,/bionic/
:
可以看到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