Linux换源指南

发布时间:2023-05-21

一、为什么要换源

1、提高软件下载速度 使用默认官方源下载软件速度通常较慢,而更换国内的镜像源可以大大提升下载速度。 2、解决软件更新失败问题 有些软件在更新时可能会出现因官方源不可达而更新失败的情况,更换镜像源可以解决这类问题。 3、获得更好的软件兼容性 国内镜像站通常会对软件进行一些本地化的改动,提供更好的兼容性,而在国外镜像站下载的软件可能存在一些兼容性问题。

二、如何换源

1、备份当前源配置

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

2、编辑源文件

sudo vim /etc/apt/sources.list

一般情况下修改文件中的deb和deb-src为相应的镜像站地址即可,例如:

# 默认官方源
deb http://archive.ubuntu.com/ubuntu/ bionic main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ bionic-security main restricted universe multiverse
# 国内镜像源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse

3、更新源

sudo apt-get update

三、常用的国内镜像站

1、阿里云镜像站
地址:https://developer.aliyun.com/mirror
2、清华大学镜像站
地址:https://mirrors.tuna.tsinghua.edu.cn/
3、网易163镜像站
地址:http://mirrors.163.com/
4、中科大镜像站
地址:https://mirrors.ustc.edu.cn/
5、华为镜像站
地址:https://mirrors.huaweicloud.com/

四、如何验证源速度和可用性

1、使用aptitude

sudo aptitude update

aptitude会根据不同源的速度和可用性自动选择最优的源,可以通过输出结果看到。 2、使用apt-fast apt-fast是一个基于apt-get的脚本,加入了多线程下载功能,可以快速下载软件包。安装和使用:

sudo add-apt-repository ppa:saiarcot895/myppa
sudo apt-get update
sudo apt-get install apt-fast
sudo apt-fast update
sudo apt-fast upgrade

3、手动测速 可以使用ping、curl等工具手动测试源的速度和可用性。 例如,ping清华大学镜像站的速度和延迟:

ping mirrors.tuna.tsinghua.edu.cn

五、换源的注意事项

1、更换源时注意备份当前源文件,以便出现问题时恢复原来的配置。 2、最好选择知名的镜像站,以免遇到恶意镜像。 3、不要频繁更换镜像站,应该在比较长的时间间隔内使用同一镜像站。 4、部分软件不适用于中国境内镜像站,例如Google Chrome,需要在官方网站下载安装包。 5、一些特殊的软件源需要特定的GPG签名验证,这些可以在相应镜像站或软件开发者的官方安装文档中查找。