前言:
鉴于Ubuntu 18.04版本发布后体验良好,许多用户选择安装尝试。尽管网络上已有大量参考教程,但本文提供一种侧重原理解释的详细方案,力求内容详尽且易于理解。
在Linux系统中,为了获得更快的下载速度和更便捷的访问体验,通常建议将apt源修改为国内镜像站,如阿里云或豆瓣源。以下简要说明如何将源切换至阿里云。
Ubuntu18.04更改apt源为阿里云源
第一步:复制源文件进行备份,以防操作失误导致系统问题。
需要修改的核心配置文件是位于/etc/apt/目录下的sources.list。该文件记录了包管理工具apt所需的软件仓库位置。此外,同目录下的sources.list.d文件夹中还包含其他以.list结尾的配置文件,也属于同类配置。
具体操作命令如下:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
第二步:编辑源列表文件。
执行编辑命令如下:
sudo vim /etc/apt/sources.list
若终端提示“sudo:vim:command not found”,表明当前系统未安装vim编辑器。
此时可通过以下命令安装vim:
sudo apt-get install vim 安装即可
第三步:查看新版本信息。
其实Ubuntu18.04版之前的任一版更改apt源为国内源方法早就有了,内容大同小异,我们应当掌握其规律了,其实每一版内容不同的地方就是版本号(或者官方一点的说:系统代号),所以我们先了解下新版本的系统代号:
使用以下命令获取:
lsb_release -c
运行结果如下图所示:

从输出可知,新版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。
这也解释了为何网络搜索到的配置方案各不相同,主要是因为它们针对的是不同系统代号的Ubuntu版本。
第四步:注释原有内容并添加新配置(或直接替换为以下内容,确保文件中仅保留有效的新配置行)。
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
第五步:更新软件列表。
运行以下命令:
sudo apt-get update
第六步:更新已安装的软件包。
运行以下命令:
sudo apt-get upgrade
第七步:补充说明。
ubuntu sudo update与upgrade的作用及区别
每个Linux发行版(如Ubuntu)都维护着专属的软件仓库,日常使用的绝大多数软件均收录其中。这些仓库内的软件经过严格测试,安全性高且能正常安装。
在Ubuntu中,源列表由一系列网址组成,每个网址代表一个软件源。这些地址指向的数据标识了该源服务器上可供安装和使用的软件清单。
编辑源文件的命令示例:
sudo gedit /etc/apt/sources.list
在该文件中添加新源或注释掉(加#号)旧源并保存后,源列表中指向的软件资源就会相应增减。
获取最新软件包列表的命令:
sudo apt-get update
此命令会访问源列表中的每个网址,读取最新的软件列表并保存在本地电脑中。软件包管理器中显示的可用软件列表,均是通过update命令同步更新的。
执行update后,通常建议接着执行upgrade。
sudo apt-get upgrade
该命令会将本地已安装的软件与刚下载的列表进行比对。如果发现已安装软件版本较低,会提示进行更新。若所有软件均为最新版本,则提示无更新需求。
系统反馈通常为:“升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。”
简而言之,update用于更新软件列表索引,而upgrade用于实际更新软件包。
注:在执行sudo apt-get upgrade之前,通常需先执行sudo apt-get update。这与Windows下的软件检测更新类似,系统会自动处理需要更新并安装的项目。
在线直接安装软件的命令:
sudo apt-get install 软件名称
apt-get update指令会同步客户端与APT服务器的RPM索引清单(package list)。APT服务器的RPM索引清单位于base文件夹内,客户端获取该文件夹内的bz2 RPM索引清单压缩档后,会将其解压放置于/var/state/apt/lists/。当用户使用apt-get install或apt-get dist-upgrade指令时,系统会将该文件夹内的资料与客户端本地的RPM资料库比对,从而判断哪些RPM包已安装、未安装或可升级。
同时,本文借鉴了博友gong_xucheng的部分博文内容,原文链接如下:
https://blog.csdn.net/gong_xucheng/article/details/53886271
在此对原作者表示感谢。
总结
至此,关于Ubuntu 18.04更改apt源为阿里云源的详细介绍已结束。如需了解更多相关Ubuntu 18.04配置内容,请搜索本站往期文章或继续浏览后续文章,感谢大家的支持!