Operations 8 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Build a Local YUM Repository on CentOS Using Scripts, ISO, Aliyun Mirror, and LAN Sharing

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仓库已经创建完成!"
fi

Method 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.5

Backup existing repo files:

mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak

Create 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-7

Refresh the cache:

yum clean all
yum repolist

Method 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 bak

Download 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.repo

Verify the files were downloaded:

# ls
a  bak  CentOS-Base.repo  epel.repo

Optionally install a test program (e.g., cowsay) to confirm the repo works:

yum -y install cowsay

Method 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 firewalld

Mount 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=0

Refresh the client cache and list available repos:

yum clean all
yum makecache
yum repolist

References

Aliyun official YUM source: https://developer.aliyun.com/article/675241

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

linuxpackage managementCentOSshell script
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.