apt-get 软件源文件格式以及ubuntu 18.04更换阿里源

#前言
由于种种原因,例如下载速度慢、找不到所需应用等,我们经常需要修改debian系统的软件源文件/etc/apt/sources.list,之前一直是直接在网上搜索别人现有的源内容,然后复制粘贴过去···今天,我们来看看这个sources.list究竟是啥子一个格式来的。先通过一个示例文件,分析sources.list各个行的格式,然后依葫芦画瓢,我们尝试编写适合ubuntu18.04的sources.list。

sources.list的格式 探究

输入

cat /etc/apt/sources.list

可以看到类似如下的内容:

deb http://us.archive.ubuntu.com/ubuntu/ trusty main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://us.archive.ubuntu.com/ubuntu/ trusty-updates main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-updates main restricted

## 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://us.archive.ubuntu.com/ubuntu/ trusty universe
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty universe
deb http://us.archive.ubuntu.com/ubuntu/ trusty-updates universe
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-updates universe
···

除了注释##外,核心的语句其实就是两类:

deb http://us.archive.ubuntu.com/ubuntu/ trusty universe
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty unniverse

其中deb和deb-src的区别在于:

The lines starting with “deb” are binary package repositories (ie. the place where the “.deb” packages which get installed are downloaded from).

The ones starting with “deb-src” are source package repositories, which provide access to the source code of the applications and the files needed to create a “.deb” package out of them.

So, unless you are involved with packaging work, or you want this as a convenient way to access the application’s source code (if you have the appropriate “deb-src” lines you can get the code for, eg., gbrainy, by running “apt-get source gbrainy”), you can just comment out those lines.

If you don’t want to edit the sources.list file directly, you can disable them through a graphical interface by going to System -> Administration -> Software Sources, and in the first tab, under “Downloadable from the Internet”, uncheck the “Source code” box.

也就是说deb后面跟着的是二进制包仓库地址,而deb-src跟着的是源代码包仓库。通过deb-src可以下载源码版本的软件包。
deb-src 提供获取软件源码以及生成deb包之外的文件的能力。除非我们参与软件的打包中,或者我们想获取软件的源码,一般我们不适用deb-src

所以我们只看deb相关的那些行。

deb http://us.archive.ubuntu.com/ubuntu/ trusty universe
···

下面我们从已有的示例出发deb http://us.archive.ubuntu.com/ubuntu/ trusty universe,一步步分解这条完整的指令。
deb后面跟着的其实有三大部分:deb repos_path section1 section2

##1
repos_path: 源的路径,deb可支持http,ftp,以及本地文件cdrom等路径。
我们访问实例的源路径:http://us.archive.ubuntu.com/ubuntu/ 可以看到如下内容:
这里写图片描述
其中,比较重要的就是dists目录和pool目录。dists目录包含了当前源的所有软件包的索引。这些索引通过系统版本分类在不同的文件夹。

##2
进入dists目录,看到如下内容:
这里写图片描述
可以看到很多文件夹,这些文件夹都是以下格式命名的:

codename
codename-backports()
codename-proposed()
codename-security()
codename-updates ()

其中codename就是ubuntu不同发行版本的"代号",可在官网查询到不同版本对应的codename,下表显示了截止目前主要版本对应的codename。这一级的文件夹名其实就是对应了deb后面的section1。
这里写图片描述
可以看到14.04版本对应的代号是trusty。
这几个文件夹的差别在于:

Security - Important Security Updates. 

Updates - Recommended Updates. 

Proposed - Pre-released Updates. 

Backports - Unsupported Updates. 

除了backports,其它文件的源其实都可以用上的。因为这个文件夹下面的软件是不支持更新维护的。
我们可以根据所需填写不同的section1。
##3
打开这一级的任意一个文件夹,可以看到如下内容:
这里写图片描述
有main,multiverse,restricted,universe等文件夹,这些文件夹名其实对应命令的section2。这些文件夹里面包含了不同软件包索引。这几个文件夹的包的区别在于

main:完全的自由软件。
restricted:不完全的自由软件。
universe:ubuntu官方不提供支持与补丁,全靠社区支持。
muitiverse:非自由软件,完全不提供支持和补丁。

##4 稍微再深入一步
我们打开main目录下的binary-i386子目录下的Packages.gz文件,可以看到如下内容:

Package: flashplugin-downloader
Architecture: i386
Version: 29.0.0.171ubuntu0.17.10.1
Multi-Arch: foreign
Priority: optional
Section: multiverse/web
Source: flashplugin-nonfree
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Bart Martens <bartm@knars.be>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 25
Depends: flashplugin-installer (>= 11.1.102.55ubuntu3)
Filename: pool/multiverse/f/flashplugin-nonfree/flashplugin-downloader_29.0.0.171ubuntu0.17.10.1_i386.deb
Size: 1894
MD5sum: a25f9bd19b8c6b4247e7b4f556f96eb5
SHA1: 08358cfd6a59f7724ef6fab66dc0cfabe4792c17
SHA256: 0ee2d3fa22cd37e80cd9180b5795c7dc8ac5de942b23c56252fde31276b93fe1
Homepage: http://www.adobe.com/products/flashplayer.html
Description: Adobe Flash Player plugin installer (transitional package)
Description-md5: 66ea91f4e504085408ea841953dc65d0


···

我们注意这个文件其实是一个“索引”文件,里面记录了各种包的包名Package、运行平台(Architecture)、版本号(Version)、依赖关系(Depends)、deb包地址(Filename)等
注意到Filename指向的是源服务器pool目录下的某个deb。

因此我们可以猜测,apt-get install 某个软件时,其实就是基于这些Packages来计算依赖关系,根据其中的Filename地址来下载所需的deb地址,然后再执行dpkg -i xxx.deb来完成软件包的安装。

编写适合18.04版本的sources.list

根据第二部分的分析,我们只要依葫芦画瓢填写好respos_path,section1和section2即可。具体步骤如下:

#deb源格式:
deb repos_path section1 section2

##1.
获取ubuntu 18.04的codename,根据Wiki可知ubuntu 18.04的代号为bionic
##2.
确定repos_path:如果觉得国外的官网软件源比较慢,那么可以选择国内的几大软件源。
国内源的url可参照ubuntu官网对国内源的介绍:http://wiki.ubuntu.org.cn/模板:18.04source

国内常用的几大源有:

Ubuntu 官方中國(目前是阿里云)http://cn.archive.ubuntu.com/ubuntu/
網易(廣東廣州電信/聯通千兆雙線接入)http://mirrors.163.com/ubuntu/
搜狐(山東聯通千兆接入)http://mirrors.sohu.com/ubuntu/
阿里云(北京萬網/浙江杭州) http://mirrors.aliyun.com/ubuntu/
開源鏡像文件搜索 http://ubuntu.mirrors.org.cn/

教育網专线:

中科大 LUG(合肥,電信/聯通/移動/教育網自動分流,同時也是 Deepin 官方)
https://mirrors.ustc.edu.cn/ubuntu/ (v4/v6) http://mirrors4.ustc.edu.cn/ubuntu/ (v4) http://mirrors6.ustc.edu.cn/ubuntu/ (v6)
中科院 OpenCAS
http://mirrors.opencas.cn/ubuntu/
清華 TUNA(教育網核心節點百兆接入,已計畫提高到千兆)
http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ (v4/v6) http://mirrors.4.tuna.tsinghua.edu.cn/ubuntu/ (v4) http://mirrors.6.tuna.tsinghua.edu.cn/ubuntu/ (v6)

任选一下url,填入repos_path 即可。我们选择阿里的:http://cn.archive.ubuntu.com/ubuntu/

##3.
确定section1,
打开刚刚填入的 http://cn.archive.ubuntu.com/ubuntu/,如果看下如下目录结构(尤其是dists目录和pool目录),说明该源可用:
这里写图片描述

打开dists目录,检查该源是否包含可供18.04使用的软件包。
这里写图片描述
可以看到以 bionic-* 为名的文件夹,这说明阿里源现在支持18.04。
现在 我们可以将bionic-*填入section1了,值得注意的是 一条deb源只能填入一个section1,如果想添加多个源就需要另一行重新书写新的deb源。
于是我们目前确认以下sources.list的内容:

deb http://cn.archive.ubuntu.com/ubuntu bionic section2
deb http://cn.archive.ubuntu.com/ubuntu bionic-updates section2
deb http://cn.archive.ubuntu.com/ubuntu bionic-security section2
deb http://cn.archive.ubuntu.com/ubuntu bionic-proposed section2

可见 还有section2尚未确定。

4.

确定section2
依次打开bionic,bionic-updates,bionic-security,bionic-proposed目录,看包含那几大类属性的软件。
例如bionic目录,
这里写图片描述
可见有main/multiverse/restricted/universe几大属性包。
于是将main multiverse restricted universe填入section2即可。
最终,可以确定sources.list的内容为:

deb http://cn.archive.ubuntu.com/ubuntu bionic main multiverse restricted universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-updates main multiverse restricted universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-security main multiverse restricted universe
deb http://cn.archive.ubuntu.com/ubuntu bionic-proposed main multiverse restricted universe

保存,然后执行sudo apt-get update 即可完成ubuntu 18.04 源的修改。

<think>好的,我现在需要帮助用户在Ubuntu 18.04更换apt-get软件,以解决执行sudo apt-get update时出现的问题。首先,我要回忆一下相关的步骤和可能遇到的坑。 根据用户提供的引用内容,特别是引用[3]提到使用鱼香ROS的一键换脚本,这可能是一个简便的方法。但用户可能也希望手动更换,以便更了解过程或者自定义地址。另外,引用[2]和[3]都提到了更换国内,比如清华,这可能是一个常用的解决方案。 首先,手动更换的步骤应该是备份原来的sources.list文件,然后替换成国内的镜像,比如清华或者阿里云。步骤大致如下: 1. 备份现有列表:使用sudo cp命令备份/etc/apt/sources.list文件,防止出错后可以恢复。 2. 编辑sources.list文件:使用文本编辑器如nano或gedit,替换内容为国内。 3. 更新软件列表:执行sudo apt-get update测试是否成功。 但用户可能在更换时遇到权限问题,或者编辑文件时格式错误,导致不可用。比如,引用[3]中提到的404错误可能就是因为地址不正确或者版本不匹配(如bionic对应18.04)。 另外,引用[4]提到安装pip3时需要先更新,说明正确配置对于后续软件安装很重要。 可能的错误点包括:未正确选择Ubuntu版本对应的,比如把18.04bionic)的写成其他版本的代号;或者换后没有执行update命令。 现在需要将步骤详细分解,确保用户能一步步操作,并且提示可能的注意事项,比如选择正确的镜像,备份原文件等。此外,可能需要解释如何选择适合的国内,以及验证是否成功。 还要考虑用户可能不熟悉命令行操作,比如使用sudo和文本编辑器的方法,所以需要给出具体的命令示例,比如使用sed命令快速替换,或者使用图形界面工具。 另外,引用[3]提到的一键换脚本也是一个选项,但需要提醒用户谨慎使用第三方脚本,确保来可信。 总结步骤: 1. 备份原文件 2. 注释或删除原文件内容,替换为国内 3. 保存文件后执行update 4. 处理可能的错误,如404 Not Found,这时候可能需要检查地址是否正确 在回答中需要明确这些步骤,并给出具体的命令和示例内容。同时,根据引用中的信息,推荐使用清华或者中科大,并确保用户使用的是bionic版本。 最后,生成相关问题,可能涉及如何解决更新中的特定错误,或者如何安装特定软件。</think>### 如何在 Ubuntu 18.04更换 apt-get 软件 #### **步骤说明** 1. **备份原有列表** 执行以下命令备份默认源文件,防止误操作: ```bash sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak ``` 2. **编辑列表文件** 使用 `nano` 或 `gedit` 编辑源文件: ```bash sudo nano /etc/apt/sources.list # 命令行编辑器 # 或 sudo gedit /etc/apt/sources.list # 图形界面编辑器 ``` 3. **替换为国内镜像** 删除原有内容,粘贴以下镜像(以清华为例,适用于 Ubuntu 18.04 "Bionic"): ```plaintext deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse ``` 4. **保存并退出编辑器** - **nano**:按 `Ctrl+O` 保存,`Ctrl+X` 退出。 - **gedit**:直接点击保存并关闭窗口。 5. **更新软件列表** 执行以下命令使新生效: ```bash sudo apt-get update ``` --- #### **注意事项** - **镜像选择**:若清华速度慢,可替换为阿里云、中科大等镜像(需确保版本为 `bionic`)。 - **一键换脚本**(谨慎使用): 引用[3]提到的鱼香ROS脚本可快速换,命令如下: ```bash wget http://fishros.com/install -O fishros && bash fishros ``` 选择“换”后按提示操作,但需确保脚本来可信。 - **常见错误处理**: - **404 Not Found**:检查地址中是否写错版本代号(如将 `bionic` 误写为 `xenial`)[^3]。 - **依赖问题**:若 `apt-get` 缺失,可先通过引用[4]的方法安装基础工具: ```bash sudo apt-get install -y apt-transport-https ca-certificates ``` --- #### **验证是否成功** 执行 `sudo apt-get update`,若输出中显示从新镜像下载信息(如 `mirrors.tuna.tsinghua.edu.cn`),则表明换成功。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值