Ubuntu 18.04更改apt为阿里云软件源

幻昼 2021年05月20日 328次浏览

Ubuntu更改apt源为国内更新源(或者软件源)方法早就有了,内容大同小异,我们应当掌握其规律了,其实每一版内容不同的地方就是版本号(或者官方一点的说:系统代号),所以我们先了解下新版本的系统代号:

使用命令:lsb_release -c

Ubuntu 18.04更改apt为阿里云软件源

得到本系统的系统代号,如下图所示:

我们可以看到新版本的Ubuntu系统代号为bionic

同样的我们也可以得到之前任意版本的系统代号:

Ubuntu 12.04 (LTS)代号为precise。
Ubuntu 14.04 (LTS)代号为trusty。
Ubuntu 15.04 代号为vivid。
Ubuntu 15.10 代号为wily。
Ubuntu 16.04 (LTS)代号为xenial。

所以这也就解释了为什么利用搜索引擎搜出来的那么多方案里面内容不尽相同的原因,因为大家更改apt安装源时用的系统不一样。

一、首先备份Ubuntu系统的官方源文件

1、打开Ubuntu的命令终端,进入源文件 sources.list 所在的目录:

Ubuntu 18.04更改apt为阿里云软件源

2、然后执行备份命令,执行 sudo cp sources.list sources.list.backup 对源文件内容进行备份,以防万一。

cd /etc/apt
sudo cp sources.list sources.list.backup

二、修改源文件内容

1、阿里源文件内容如下:

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-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports 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

值得注意的是 sources.list 文件的条目都是有格式的(通过上面的内容大家也看的出来),一般有如下形式:

deb http://site.example.com/debian distribution component1 component2 component3
deb-src http://site.example.com/debian distribution component1 component2 component3

所以后面几个参数是对软件包的分类(Ubuntu下是main, restricted,universe ,multiverse这四个)
所以你把内容写成:

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

上面之类的也是可以的,之前我有这个疑惑,所以在这里一并告知和我有一样疑惑的朋友。

2、将上述阿里源的文件内容全部复制,使用 sudo vim sources.list 打开文件,输入 ggdG(vim操作指令)删除所有内容(这句指令可以理解为删除第一行到最后一行的的全部内容)

sudo vim sources.list # 在 /etc/apt 目录下
ggdG # 使用vim打开 sources.list 文件后执行此命令

Ubuntu 18.04更改apt为阿里云软件源

3、将复制的阿里源文件内容全部粘贴到文件中后,输入 : wq! 保存退出。

更新源

使用 sudo apt-get update 即可更新获取 阿里软件源 提供的软件列表

sudo apt-get update

Ubuntu 18.04更改apt为阿里云软件源

更新软件(慎用)

使用 sudo apt-get upgrade 即可跟新软件
sudo apt-get upgrade