Ubuntu 22.04前的系统,默认源都写在/etc/apt/sources.list文件里。从Ubuntu 24.04起,被迁移到了/etc/apt/sources.list.d/目录下,专门为系统主源创建了一个名为ubuntu.sources的文件。并且,从每行一个软件源,用空格分隔各项参数的One-Line-Style(单行格式),变为了DEB822格式。
默认的apt源,服务器在国外,连接速度较慢,可以将其更改为国内镜像源,提高连接、下载的速度。
matthewfjnd@matthewfjndubuntu:~$ cat /etc/apt/sources.list.d/ubuntu.sourcesTypes: debURIs: http://archive.ubuntu.com/ubuntu/Suites: resolute resolute-updates resolute-backportsComponents: main restricted universe multiverseSigned-By: /usr/share/keyrings/ubuntu-archive-keyring.gpgTypes: debURIs: http://security.ubuntu.com/ubuntu/Suites: resolute-securityComponents: main restricted universe multiverseSigned-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
为了方便编辑,我们先安装vim文本编辑器。
matthewfjnd@matthewfjndubuntu:~$ sudo apt install vimThe following package was automatically installed and is no longer required: grub-pc-binUse 'sudo apt autoremove' to remove it.Installing: vimInstalling dependencies: libgpm2 libsodium23 vim-common vim-runtimeSuggested packages: gpm ctags vim-doc vim-scriptsSummary: Upgrading: 0, Installing: 5, Removing: 0, Not Upgrading: 0 Download size: 10.3 MB Space needed: 46.5 MB / 88.8 GB availableContinue? [Y/n] y
等待完成安装即可。
安装完毕后,我们发现弹出了一条提示:
The following package was automatically installed and is no longer required: grub-pc-binUse 'sudo apt autoremove' to remove it.
系统提示,grub-pc-bin已经不需要了,可以sudo apt autoremove这个命令删除。我们后面再来删除他。
4.使用vim编辑apt源文件
首先,先备份原来的apt源文件,这是一个好习惯。
sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak
apt只会搜索/etc/apt/sources.list.d/目录下的*.sources或*.list文件,读取其中内容。所以,ubuntu.sources.bak文件中的内容不会被读取。
参考资料:https://manpages.debian.org/testing/apt/sources.list.5.en.html
Filenames need to have either the extension .list or .sources depending on the contained format.
接下来使用刚刚下载的vim编辑apt源文件
sudo vim /etc/apt/sources.list.d/ubuntu.sources
将文件内容清空。在vim中,可以使用快捷键
清空文件内容,前面的数字是选取文件光标(含)之下几行,dd是删除的意思。结合起来,就是删除文件光标(含)之下999行。如数字是1,则可以省略,直接dd,删除光标当前行即可。类似的,还有复制快捷键yy,如:
这条命令的意思是复制文件光标(含)之下12含。复制完后需要粘贴,粘贴的快捷键是p,在光标所在行之下粘贴。
vim的基础命令,和其他Linux基础命令,我会再开几篇来讲授,这里就不多做解释了。
接下来,在vim中,按i(可以用insert来记忆)进入编辑模式。将下面的源输入文本中。
Types: debURIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntuSuites: resolute resolute-updates resolute-backportsComponents: main restricted universe multiverseSigned-By: /usr/share/keyrings/ubuntu-archive-keyring.gpgTypes: debURIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntuSuites: resolute-securityComponents: main restricted universe multiverseSigned-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
输入完毕后,按esc键退出编辑模式,然后再按“:”键(注意,需要英文半角的“:”),进入vim的末行模式,输入wq(可以用wirte、quit来记忆),退出vim。
最后,我们使用
来更新apt软件包列表。更新完毕后,我们可能会看到类似如下的提示:
46 packages can be upgraded. Run 'apt list --upgradable' to see them.
这个意思是,在这个源中,有46个软件(包)可以更新,请运行apt list --upgradable来查看他们。这里我们先忽略,赶紧把环境配置好才是正事。