CentOS 7.x 默认源修改的教程方法

由于 CentOS 7 官方已停止维护,默认的 YUM 源已无法使用,因此需要修改为国内镜像源。

修改源的步骤非常简单,你可以直接复制以下命令在服务器上执行。


⚙️ 通用操作流程

无论选择哪个镜像源,大致的步骤都是:① 备份 → ② 下载新源配置 → ③ 生成缓存

第一步:备份原有源配置文件

在进行任何修改前,建议先备份。你可以根据需要选择以下任一命令:

# 方案A:备份单个主配置文件(推荐)
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

# 方案B:备份整个目录
cp -r /etc/yum.repos.d /etc/yum.repos.d.bak

# 方案C:重命名备份(可直接跳过后续下载)
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

第二步:下载新的镜像源配置

执行对应镜像源的命令(见下方主流镜像源配置)。

第三步:清理并生成新的缓存

yum clean all # 清理所有缓存

yum makecache # 生成新的缓存,加速后续安装

📝 主流镜像源配置

你可以选择任一国内镜像源,二选一执行对应的 wget 或 curl 命令下载配置文件即可。

阿里云镜像 (Aliyun):推荐阿里云用户使用,内网连接更快。

# 使用 wget 下载
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

# 或者使用 curl 下载
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

腾讯云镜像 (Tencent):推荐腾讯云用户使用,内网连接更快。

# 使用 wget 下载
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo

# 或者使用 curl 下载
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo

清华大学镜像 (Tsinghua):教育网用户体验优秀。

# 使用 wget 下载
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/CentOS-Base.repo

# 或者使用 curl 下载
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/CentOS-Base.repo

中科大镜像 (USTC):教育网及科研网络用户体验不错。

# 使用 wget 下载
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.ustc.edu.cn/centos/7/os/x86_64/CentOS-Base.repo

# 或者使用 curl 下载
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.ustc.edu.cn/centos/7/os/x86_64/CentOS-Base.repo

网易镜像 (163):老牌镜像站,稳定性好。

# 使用 wget 下载
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo

# 或者使用 curl 下载
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo

⚠️ 安全提醒

务必通过正规镜像站下载配置文件,切勿随意执行来历不明的脚本。部分第三方教程可能提供个人维护的脚本,存在捆绑恶意软件的风险。本教程中所有的命令和配置均可安全执行。

执行 wget 或 curl 命令前,建议先通过浏览器访问确认链接的有效性。命令中提供的各镜像站链接均为官方公开的地址。

上一篇 WordPress自动本地化文章中的外联图片
下一篇 泰坦尼克号