How to Build a Local YUM Repository on CentOS: Scripts, ISO Mount, and Network Sharing
This guide walks through four practical methods to create a local YUM repository on CentOS—using a Bash script, mounting an ISO image, configuring the official Aliyun mirror, and sharing via an HTTP server—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 installation DVD is already mounted. If it is, the DVD is mounted at /mnt; otherwise, the script prints a prompt to mount the disc and exits with code 2.
It then changes to /etc/yum.repos.d/ and checks whether a directory named a exists. If the directory exists, the script reports that the file already exists; if not, it creates the directory.
All files beginning with Cent* are moved into the a directory, and a new repository file a.repo is created with the following content:
[base]
name=a
baseurl=file:///mnt
gpgcheck=0The script runs yum clean all and yum makecache to clear and rebuild the YUM cache. If both commands succeed (exit code 0), it prints a success message.
#!/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 DVD 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 repository files:
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bakCreate a new repository 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
Replace the existing repository files with the Aliyun mirror configuration. First, back up current .repo files:
mkdir bak
mv *.repo bakDownload the Aliyun base repository and the EPEL repository:
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 exist and optionally install additional utilities such as cowsay:
yum -y install cowsayMethod 4: Share a Local YUM Repository Over a LAN
Install and start an HTTP server (e.g., httpd) on the machine that holds the mounted ISO:
yum install -y httpd
systemctl start httpd
systemctl stop firewalldMount the ISO to a directory served by the web server:
mount /opt/CentOS-7.5-1804.iso /var/www/html/CentOS7.5/On client machines, create a repository file pointing to the server’s address:
vim /etc/yum.repos.d/CentOS-Base.repo
[local]
name=net_bendiyum
baseurl=http://192.168.1.8/CentOS7.5/
enabled=1
gpgcheck=0Run yum clean all and yum makecache on the client to refresh metadata.
References
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.
