Centos7.9更换yum源

centos系统的官方yum源太慢了,我们需要更换成国内的源,建议先备份

手动更换

查看当前已有的 yum 源

ls  /etc/yum.repos.d

备份yum源

cd  /etc/yum.repos.d
cp CentOS-Base.repo CentOS-Base.repo.bak

Centos7.9更换yum源_第1张图片

更换新源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

清理原先的源

yum clean all

生成yum源缓存

yum makecache

如果还是有问题,可以把 yum.repos.d 文件下,所有的文件全部删除,再执行上述操作。(这个慎用!尤其是生产环境下,仅在实在不行的情况下试试)

自动化脚本

是否需要root权限,您可以自行选择。

#!/bin/bash

# 检查用户是否为root
if [[ $EUID -ne 0 ]]; then
   echo "此脚本必须以root权限运行"
   exit 1
fi

# 备份当前的yum源配置文件
echo "备份当前的yum源配置文件..."
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

# 下载阿里云源配置文件
echo "下载阿里云源配置文件..."
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 清除缓存
echo "清除yum缓存..."
yum clean all
yum makecache

# 输出完成信息
echo "Yum源已更换为阿里云源。"

到此就可以结束了。

禁用fastestmirror

sudo sed -i 's/enabled=1/enabled=0/' /etc/yum/pluginconf.d/fastestmirror.conf

开启fastestmirror

sudo sed -i 's/enabled=0/enabled=1/' /etc/yum/pluginconf.d/fastestmirror.conf

你可能感兴趣的:(linux)