Operations 8 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Build a Local YUM Repository on CentOS: Scripts, ISO Mount, and Network Sharing

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

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

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

Backup existing repository files:

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

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

Refresh the cache:

yum clean all
yum repolist

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

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

Verify the files exist and optionally install additional utilities such as cowsay:

yum -y install cowsay

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

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

Run yum clean all and yum makecache on the client to refresh metadata.

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.

LinuxCentOSscriptISOlocal repositorynetwork sharing
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.