How to Build a Local YUM Repository on CentOS Using Scripts, ISO, Aliyun Mirror, and LAN Sharing
This guide explains four ways to create a local YUM repository on CentOS 7—using a Bash script, mounting an ISO image, configuring the official Aliyun mirror, and sharing the repository over a LAN with httpd—complete with commands, configuration files, and troubleshooting tips.
Method 1: Create a Local YUM Repository with a Bash Script
The script first checks whether the CD-ROM is already mounted. If it is, it mounts the disc to /mnt; otherwise it prompts the user to mount the disc and exits with code 2. It then switches to /etc/yum.repos.d/, creates a directory a if it does not exist, moves all Cent* files into it, and generates an a.repo file that points the baseurl to file:///mnt. Finally it runs yum clean all and yum makecache to build the cache, reporting success when the commands return exit code 0.
#!/bin/bash
mount |grep sr0 &> /dev/null
if [ $? -eq 0 ]; then
mount /dev/cdrom /mnt &> /dev/null
else
echo "请挂载光盘!"
exit 2
fi
cd /etc/yum.repos.d/
if [ -e a ]; then
echo "文件已经存在"
else
mkdir a
fi
mv Cent* a &> /dev/null
cat << EOR >> a.repo
[base]
name=a
baseurl=file:///mnt
gpgcheck=0
EOR
yum clean all
yum makecache
if [ $? -eq 0 ]; then
echo "本地yum仓库已经创建完成!"
fiMethod 2: Build a Local YUM Repository by Mounting an ISO Image
Upload the CentOS‑7 ISO (e.g., CentOS-7-x86_64-DVD-1611.iso) to /opt. Create a mount point and mount the ISO:
mkdir /mnt/centos7.5
mount /opt/CentOS-7-x86_64-DVD-1611.iso /mnt/centos7.5Backup existing repo files:
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bakCreate a new repo configuration file CentOS.Base.repo with the following content:
[centos7.5]
name=centos7.5
baseurl=file:///mnt/centos7.5
enabled=1
gpgcheck=0
gpgkey=file:///mnt/centos7.5/RPM-GPG-KEY-CentOS-7Refresh the cache:
yum clean all
yum repolistMethod 3: Use the Official Aliyun YUM Mirror
Navigate to the repo directory and back up existing .repo files:
cd /etc/yum.repos.d/
mkdir bak
mv *.repo bakDownload the Aliyun base repo and EPEL repo:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repoVerify the files were downloaded:
# ls
a bak CentOS-Base.repo epel.repoOptionally install a test program (e.g., cowsay) to confirm the repo works:
yum -y install cowsayMethod 4: Share a Local YUM Repository Over a LAN
Install and start the Apache HTTP server on the host that holds the repository:
yum install -y httpd
systemctl start httpd
systemctl stop firewalldMount the ISO to a directory served by Apache:
mount /opt/CentOS-7.5-1804.iso /var/www/html/CentOS7.5/On each client, create a repo file pointing to the host’s IP address:
vim /etc/yum.repos.d/CentOS-Base.repo
[local]
name=net_bendiyum
baseurl=http://192.168.1.8/CentOS7.5/
enabled=1
gpgcheck=0Refresh the client cache and list available repos:
yum clean all
yum makecache
yum repolistReferences
Aliyun official YUM source: https://developer.aliyun.com/article/675241
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
