Configuring Alternative YUM Repositories on CentOS
In many productoin environments, the default CentOS repositories may be slow or inaccessible due to geographical locations. Switching to local mirrors such as Alibaba Cloud or NetEase (163) can significantly improve package download speeds and update reliability. This guide outlines the process of backing up, replacing, and refreshing your YUM source configurations.
1. Backing Up Existing Repository Files
Before making any changes, it is critical to back up the original configuration file to prevent system update failures if the new repository configuration is incorrect.
# Create a backup of the current base repository configuration
sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.original
2. Downloadign New Repository Configurations
Depending on your distribution version and preferred mirror provider, use either wget or curl to fetch the .repo file and place it in the configuration directory.
Option A: Using Alibaba Cloud Mirrors
Alibaba Cloud provides high-performance mirrors for various CentOS versions. Choose the command corresponding to your OS version:
# CentOS 7
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# OR
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# CentOS 8 (Stream)
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
If you are using an Aliyun ECS instance, you might want to remove internal mirror references to avoid resolution issues in non-VPC environments:
sudo sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
Option B: Using NetEase (163) Mirrors
NetEase is another popular and reliable choice for developers in Asia.
# CentOS 7
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
# CentOS 6
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo
3. Refreshing the Repository Cache
After replacing the configuration file, you must clear the old metadata and regenerate the cache to apply the changes.
# Clear cached package information and headers
yum clean all
# Rebuild the metadata cache
yum makecache
4. Essential YUM Management Commands
Once the new source is configured, you can manage your packages using the standard yum utility. The general syntax is: yum [options] [command] [package].
- yum check-update: Check for available updates without installing them.
- yum update: Upgrade all installed packages to the latest version.
- yum install <package_name>: Download and install a specific software package.
- yum remove <package_name>: Uninstall a specific package and its dependencies.
- yum list: Display a list of all packages in the repository.
- yum search <keyword>: Find packages based on a description or name.
To specifically manage cache cleanup, you can use more granular commands:
yum clean packages: Remove cached RPM files from the system.yum clean headers: Remove cached header files used for dependency resolution.yum clean all: Performs a complete cleanup of all cached data.