【原创】Ubuntu apt 更改国内阿里源

概要

1、简介

Ubuntu 使用 apt 管理系统级的包/软件非常方便,但由于这些托管包/软件的中央仓库基本都位于美国,所以对于国内用户来说使用体验相对较差,因为洲际网络相对较差。

常见的做法就是设置国内的镜像源仓库。这个原理就是某几个镜像站定时从美国的中央仓库对同步到国内的服务器上,这样国内用户就访问的是国内的服务器了,效率也就提升了,而且也减少了洲际网络的使用~

所以本文分享了 Ubuntu 18.04 设置国内的阿里云的 apt 镜像源的方法。

2、相关环境

Ubuntu 18.04.5
apt 1.6.12

设置方法

方法一:直接修改 /etc/apt/sources.list

1、原文件备份

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

2、清除原文件内容

# > /etc/apt/sources.list

注:这里需要使用 root 用户。

3、添加配置内容到 sources.list

 编辑 /etc/apt/sources.list

$ sudo vim /etc/apt/sources.list

添加如下内容(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

保存退出

4、更新 apt

$ sudo apt update                            
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease                 
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease                  
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease                  
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease                 
Reading package lists... Done                      
Building dependency tree       
Reading state information... Done
52 packages can be upgraded. Run 'apt list --upgradable' to see them.

从输出结果可看到已经成功更新为 aliyun 的镜像源了。

方法二:在 /etc/apt/sources.list.d 目录添加镜像源文件

1、进入 /etc/apt/sources.list.d 目录

$ cd /etc/apt/sources.list.d

2、新建一个 list 文件并添加配置

$ sudo vim aliyun.list

添加如下配置

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

保存退出

3、更新 apt

$ sudo apt update
Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease                                                                                                                        
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease                                                                                                                         
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease                                                                                                                        
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease                                                                                                                       
Hit:6 http://us.archive.ubuntu.com/ubuntu bionic InRelease                                                                                                 
Hit:7 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease                                   
Hit:8 http://security.ubuntu.com/ubuntu bionic-security InRelease        
Hit:9 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease     
Reading package lists... Done
Building dependency tree       
Reading state information... Done
52 packages can be upgraded. Run 'apt list --upgradable' to see them.

从输出结果可看到已经成功更新为 aliyun 的镜像源了。

注:方法二相当于只是给 apt 增加了一个 apt 源,原有的 /etc/apt/sources.list 中配置的 apt 中央仓库有效,所以如果只想使用 aliyun 的镜像源的话,推荐使用第一种方法。

总结

1、更改 apt 国内镜像源相对来说还是非常简单的,虽然不是一键式操作,但也只需要两三步即可,非常建议花两分钟配置一下 Ubuntu 的 apt 国内源,正所谓磨刀不误砍柴工。

2、上面提到的方法二在较老的 Ubuntu 版本中可能 /etc/apt/sources.list.d 目录,另外不同的 Ubuntu 版本的配置内容也不相同,本文只列举了 Ubuntu 18.04 的配置内容,如果是其他版本的 Ubuntu,请参考附录中的链接。

附录

参考阿里云镜像配置官网

https://developer.aliyun.com/mirror/ubuntu

You may also like...

1 Response

  1. 2020-10-19

    […] 在国内使用 apt-get,最重要的就是设置国内 apt 镜像源,可以请参考另一篇文章:【原创】Ubuntu apt 更改国内阿里源 […]

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注