Linux系统下,CentOS和Ubuntu的镜像源修改教程因版本不同而有所差异。以下是针对CentOS 7至9以及Ubuntu 16.04至22.04的国内外镜像源修改教程:
CentOS
CentOS 7
备份原有的YUM源文件:
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
选择并下载国内镜像源:
阿里云源:
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
网易源:
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
清理原有缓存并生成新缓存:
sudo yum clean all sudo yum makecache
更新系统:
sudo yum update -y
CentOS 8
备份原有的YUM源文件:
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
选择并下载国内镜像源:
阿里云源:
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
清理原有缓存并生成新缓存:
sudo yum clean all sudo yum makecache
更新系统:
sudo yum update -y
CentOS 9
备份原有的YUM配置文件:
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base-backup.repo
选择并添加国内镜像源:
阿里云源:
打开编辑器(如vi或nano):
sudo vi /etc/yum.repos.d/CentOS-Base.repo
在文件末尾追加阿里云的CentOS镜像源配置:
[aliyun] name=阿里云CentOS mirror baseurl=https://mirrors.aliyun.com/repo/CentOS-$releasever/ gpgcheck=0 enabled=1
注意:$releasever需要替换为你的CentOS 9版本(例如"stream")。
清理原有缓存并生成新缓存:
sudo yum clean all sudo yum makecache fast
更新系统:
sudo yum update -y
Ubuntu
Ubuntu 16.04
备份源文件:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
修改源文件:
使用文本编辑器(如gedit、vi、nano等)打开
/etc/apt/sources.list
文件,将原有的所有源注释掉,并添加国内源。例如,添加阿里云源:deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
更新源文件:
sudo apt update
Ubuntu 18.04
备份源文件:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
修改源文件:
使用文本编辑器打开
/etc/apt/sources.list
文件,将原有的所有源注释掉,并添加国内源。例如,添加阿里云源:deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
更新源文件:
sudo apt update
Ubuntu 20.04 和 22.04
对于Ubuntu 20.04和22.04,修改源文件的步骤与18.04类似。只需将源文件中的版本号(如bionic
)替换为对应版本的代号(如focal
对应20.04,jammy
对应22.04),并添加相应的国内源地址即可。
以下是Ubuntu 20.04和22.04国内镜像源修改的详细教程:
Ubuntu 20.04 国内镜像源修改教程
备份原有的镜像源文件
打开终端(Ctrl+Alt+T),输入以下命令备份原有的Ubuntu 20.04镜像源文件:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
编辑镜像源文件
使用文本编辑器(如nano或vim)打开
/etc/apt/sources.list
文件,并将原有的镜像源地址替换为国内镜像源地址。例如,使用nano编辑器:sudo nano /etc/apt/sources.list
将文件中的内容替换为以下格式(以清华大学镜像源为例):
# 默认注释掉的源,如果需要启用,请取消注释并将下面的清华大学源地址替换为官方源地址 # deb http://archive.ubuntu.com/ubuntu/ focal main restricted # deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted # deb http://archive.ubuntu.com/ubuntu/ focal universe # deb http://archive.ubuntu.com/ubuntu/ focal-updates universe # deb http://archive.ubuntu.com/ubuntu/ focal multiverse # deb http://archive.ubuntu.com/ubuntu/ focal-updates multiverse # deb http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse # deb http://security.ubuntu.com/ubuntu/ focal-security main restricted # deb http://security.ubuntu.com/ubuntu/ focal-security universe # deb http://security.ubuntu.com/ubuntu/ focal-security multiverse # 替换为清华大学镜像源 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
保存并退出编辑器。
更新软件包索引
输入以下命令更新软件包索引,以确保系统能够识别新的镜像源:
sudo apt update
验证新配置的镜像源
可以通过尝试安装一个软件包来验证新配置的镜像源是否生效。例如,安装curl:
sudo apt install curl
如果安装过程顺利且速度较快,说明新配置的镜像源已经生效。
Ubuntu 22.04 国内镜像源修改教程
安装software-properties-common
打开终端,输入以下命令安装
software-properties-common
:sudo apt update && sudo apt install software-properties-common
添加国内镜像源
以添加清华大学源为例,输入以下命令:
sudo add-apt-repository "deb [arch=amd64] http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" sudo add-apt-repository "deb-src [arch=amd64] http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse"
如果需要添加阿里云源,可以使用以下命令(注意替换为正确的仓库地址):
sudo add-apt-repository ppa:chenyunkun/aliyun-ubuntu-repo sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E56151AF # 然后编辑 /etc/apt/sources.list 文件,将原有的源替换为阿里云源
或者直接编辑
/etc/apt/sources.list
文件,将原有的源替换为阿里云源,例如:deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
更新软件包索引
输入以下命令更新软件包索引:
sudo apt update
验证新配置的镜像源
同样可以通过尝试安装一个软件包来验证新配置的镜像源是否生效。
通过以上步骤,您可以成功在Ubuntu 20.04和22.04中配置国内镜像源,从而提升软件包下载速度。
注意事项
- 在修改镜像源之前,请确保网络连接正常,并能够访问所选的国内镜像源。
- 修改镜像源可能会影响软件包的可用性和安全性,因此请确保选择可靠的国内源。
- 定期检查和更新源列表文件,以确保系统能够获取到最新的软件包和更新。
通过以上步骤,您可以成功修改CentOS 7至9以及Ubuntu 16.04至22.04的国内外镜像源。