When using CentOS 7 in mainland China, the default Yum repositories may cause slow download speeds or even connection timeouts. This tutorial provides a simple shell script to help you switch to popular domestic mirror sources with just one command.
Script Content
#!/bin/bash
set -e
echo "Backing up the original Yum repo configuration..."
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
echo "Downloading Alibaba Cloud CentOS 7 repo configuration..."
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
echo "Cleaning up cache and generating new cache..."
yum clean all
yum makecache
echo "Yum repo has been switched to Alibaba Cloud mirror"
How to Use
- Save the script above as
switch_centos7_repo.sh
- Grant execute permission:
chmod +x switch_centos7_repo.sh
- Run it as root:
sudo ./switch_centos7_repo.sh
You can also replace the download link in the script with other sources such as Tsinghua University Mirror, Tencent Cloud Mirror, and more.