How to Build a Fast Local Yum Repository on CentOS and Switch to Domestic Mirrors
This guide shows how to create a local Yum repository on CentOS using the installation ISO, back up and replace the default repo files, mount the ISO, configure a repo file, clean the cache, then switch to domestic Aliyun or 163 mirrors, set repository priorities, and verify the changes with package installation tests.
Setting Up a Local Yum Repository on CentOS
Because the default Yum source in China is slow or inaccessible, a local repository built from the CentOS ISO can greatly improve package installation speed.
1. View the default Yum repository files
[root@kangvcar ~]# ll /etc/yum.repos.d/
total 32
-rw-r--r--. 1 root root 1664 Dec 9 2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Dec 9 2015 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Dec 9 2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 290 Dec 9 2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Dec 9 2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Dec 9 2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 Dec 9 2015 CentOS-Vault.repo2. (Optional) Backup the existing Yum repository files
[root@kangvcar ~]# mkdir /opt/centos-yum.bak
[root@kangvcar ~]# mv /etc/yum.repos.d/* /opt/centos-yum.bak/3. Mount the CentOS ISO in the virtual machine
[root@kangvcar ~]# mount -t iso9660 /dev/sr0 /opt/centos4. Create a repo file that points to the mounted directory
[root@kangvcar ~]# vi /etc/yum.repos.d/local.repo
[local]
name=local
baseurl=file:///opt/centos
enabled=1
gpgcheck=05. Clean the Yum cache and rebuild it
[root@kangvcar ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: local
Cleaning up everything
Cleaning fastestmirror list
[root@kangvcar ~]# yum makecache # cache the repository locally
[root@kangvcar ~]# yum list # shows the number of packages (e.g., 3780)Switching to Domestic Aliyun Yum Mirrors
Aliyun official tutorial: http://mirrors.aliyun.com/help/centos
1. View the default repository files (same as above)
[root@kangvcar ~]# ll /etc/yum.repos.d/ ...2. (Optional) Backup the repository files
[root@kangvcar ~]# mkdir /opt/centos-yum.bak
[root@kangvcar ~]# mv /etc/yum.repos.d/* /opt/centos-yum.bak/3. Download the appropriate Aliyun repo file
# For CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
# For CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
# For CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # Verify the system version
cat /etc/redhat-release # e.g., CentOS Linux release 7.2.1511 (Core)
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo4. Clean the cache again
[root@kangvcar ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up everything
Cleaning fastestmirror list
[root@kangvcar ~]# yum makecache # cache the Aliyun repo
[root@kangvcar ~]# yum list # total packages (e.g., 9954)Switching to Domestic 163 Yum Mirrors
163 official tutorial: http://mirrors.163.com/.help/centos.html
1. View the default repository files
[root@kangvcar ~]# ll /etc/yum.repos.d/ ...2. (Optional) Backup the repository files
[root@kangvcar ~]# mkdir /opt/centos-yum.bak
[root@kangvcar ~]# mv /etc/yum.repos.d/* /opt/centos-yum.bak/3. Download the 163 repo file
# For CentOS 5
wget -O /etc/yum.repos.d/CentOS5-Base-163.repo http://mirrors.163.com/.help/CentOS5-Base-163.repo
# For CentOS 6
wget -O /etc/yum.repos.d/CentOS6-Base-163.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo
# For CentOS 7
wget -O /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo # Verify version and download
cat /etc/redhat-release # e.g., CentOS Linux release 7.2.1511 (Core)
wget -O /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo4. Clean the cache again
[root@kangvcar ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up everything
Cleaning fastestmirror list
[root@kangvcar ~]# yum makecache # cache the 163 repo
[root@kangvcar ~]# yum list # total packages (e.g., 9951)Setting Yum Repository Priority
When both a local repository and a remote mirror exist, you may want the local repository to be used first. The yum-plugin-priorities plugin can control this behavior.
1. Check whether the priorities plugin is installed
[root@kangvcar ~]# rpm -qa | grep yum-plugin-
# Example output shows yum-plugin-fastestmirror but not yum-plugin-priorities2. Install the priorities plugin
[root@kangvcar ~]# yum -y install yum-plugin-priorities.noarch3. Verify that the plugin is enabled
[root@kangvcar ~]# cat /etc/yum/pluginconf.d/priorities.conf
[main]
enabled = 1 # 1 = enabled, 0 = disabled4. Set a higher priority for the local repository
# Edit /etc/yum.repos.d/local.repo
[local]
name=local
baseurl=file:///opt/centos
enabled=1
gpgcheck=0
priority=1 # lower number = higher priority5. Test the configuration
# Before setting priority (using Aliyun repo)
[root@kangvcar ~]# yum -y install vim
Dependencies Resolved
... Repository: updates ...
# After setting priority (using local repo)
[root@kangvcar ~]# yum -y install vim
Dependencies Resolved
... Repository: local ...Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
