1. 首页 > Linux教程 > 正文

Linux教程FG087-本地yum仓库搭建与使用案例

更多视频教程www.fgedu.net.cn

Part01-基础概念与理论知识

1.1 YUM仓库基础

YUM(Yellowdog Updater Modified)是RHEL系统的包管理工具,本地YUM仓库具有以下优势:

  • 快速安装:无需从网络下载,安装速度快
  • 节省带宽:减少网络流量,降低网络成本
  • 离线安装:支持无网络环境下的软件安装
  • 版本控制:可以控制软件包的版本
  • 安全可靠:避免网络中断导致的安装失败

1.2 YUM仓库类型

仓库类型 特点 适用场景
本地仓库 使用本地ISO镜像或目录 离线环境、内网环境
网络仓库 通过HTTP/FTP/NFS访问 局域网共享
镜像仓库 镜像官方仓库 加速访问、节省带宽
第三方仓库 EPEL、Remi等 获取额外软件包

1.3 YUM配置文件

  • /etc/yum.conf:YUM主配置文件
  • /etc/yum.repos.d/:仓库配置文件目录
  • /var/cache/yum/:YUM缓存目录
  • /var/log/yum.log:YUM操作日志

Part02-实战环境准备与配置

2.1 准备ISO镜像文件

# 1. 创建ISO镜像目录
# mkdir -p /mnt/iso
# mkdir -p /var/www/html/repo

# 2. 查看可用的ISO镜像文件
# ls -lh /tmp/*.iso 2>/dev/null || echo “未找到ISO镜像文件”

# 3. 挂载ISO镜像(假设有RHEL 10 ISO镜像)
# mount -o loop /tmp/rhel-10-x86_64-dvd.iso /mnt/iso
mount: /tmp/rhel-10-x86_64-dvd.iso: No such file or directory

# 4. 如果没有ISO镜像,创建测试仓库结构
# mkdir -p /var/www/html/repo/Packages
# mkdir -p /var/www/html/repo/repodata

# 5. 查看目录结构
# tree /var/www/html/repo
/var/www/html/repo
├── Packages
└── repodata

2 directories, 0 files

2.2 安装必要工具

# 1. 安装createrepo工具
# dnf install -y createrepo
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Apr 2 12:00:00 2026.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
createrepo x86_64 1.0.1-5.el10 baseos 85 k
Installing dependencies:
deltarpm x86_64 3.6-3.el10 baseos 82 k
python3-deltarpm x86_64 3.6-3.el10 baseos 78 k

Transaction Summary
================================================================================
Install 3 Packages

Total download size: 245 k
Installed size: 890 k
Downloading Packages:
(1/3): deltarpm-3.6-3.el10.x86_64.rpm 82 kB/s | 82 kB 00:01
(2/3): python3-deltarpm-3.6-3.el10.x86_64.rpm 78 kB/s | 78 kB 00:01
(3/3): createrepo-1.0.1-5.el10.x86_64.rpm 85 kB/s | 85 kB 00:01
——————————————————————————–
Total 245 kB/s | 245 kB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : deltarpm-3.6-3.el10.x86_64 1/3
Installing : python3-deltarpm-3.6-3.el10.x86_64 2/3
Installing : createrepo-1.0.1-5.el10.x86_64 3/3
Verifying : deltarpm-3.6-3.el10.x86_64 1/3
Verifying : python3-deltarpm-3.6-3.el10.x86_64 2/3
Verifying : createrepo-1.0.1-5.el10.x86_64 3/3

Installed:
createrepo-1.0.1-5.el10.x86_64
deltarpm-3.6-3.el10.x86_64
python3-deltarpm-3.6-3.el10.x86_64

Complete!

# 2. 安装HTTP服务器(用于网络仓库)
# dnf install -y httpd
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Apr 2 12:00:00 2026.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.57-5.el10 baseos 1.2 M
Installing dependencies:
httpd-filesystem noarch 2.4.57-5.el10 baseos 23 k
mod_http2 x86_64 2.0.10-3.el10 baseos 85 k
mod_ldap x86_64 2.4.57-5.el10 baseos 45 k

Transaction Summary
================================================================================
Install 4 Packages

Total download size: 1.3 M
Installed size: 4.5 M
Downloading Packages:
(1/4): httpd-filesystem-2.4.57-5.el10.noarch.rpm 23 kB/s | 23 kB 00:01
(2/4): mod_ldap-2.4.57-5.el10.x86_64.rpm 45 kB/s | 45 kB 00:01
(3/4): mod_http2-2.0.10-3.el10.x86_64.rpm 85 kB/s | 85 kB 00:01
(4/4): httpd-2.4.57-5.el10.x86_64.rpm 1.2 MB/s | 1.2 MB 00:01
——————————————————————————–
Total 1.3 MB/s | 1.3 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : httpd-filesystem-2.4.57-5.el10.noarch 1/4
Installing : mod_ldap-2.4.57-5.el10.x86_64 2/4
Installing : mod_http2-2.0.10-3.el10.x86_64 3/4
Installing : httpd-2.4.57-5.el10.x86_64 4/4
Verifying : httpd-filesystem-2.4.57-5.el10.noarch 1/4
Verifying : mod_ldap-2.4.57-5.el10.x86_64 2/4
Verifying : mod_http2-2.0.10-3.el10.x86_64 3/4
Verifying : httpd-2.4.57-5.el10.x86_64 4/4

Installed:
httpd-2.4.57-5.el10.x86_64
httpd-filesystem-2.4.57-5.el10.noarch
mod_http2-2.0.10-3.el10.x86_64
mod_ldap-2.4.57-5.el10.x86_64

Complete!

# 3. 启动HTTP服务
# systemctl start httpd
# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

# 4. 查看HTTP服务状态
# systemctl status httpd
● httpd.service – The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2026-04-02 12:00:00 CST; 5s ago
Docs: man:httpd.service(8)
Main PID: 5678 (httpd)
Tasks: 213 (limit: 4915)
Memory: 15.2M
CGroup: /system.slice/httpd.service
├─5678 /usr/sbin/httpd -DFOREGROUND
├─5679 /usr/sbin/httpd -DFOREGROUND
├─5680 /usr/sbin/httpd -DFOREGROUND
├─5681 /usr/sbin/httpd -DFOREGROUND
└─5682 /usr/sbin/httpd -DFOREGROUND

# 5. 配置防火墙
# firewall-cmd –permanent –add-service=http
success
# firewall-cmd –reload
success

Part03-核心命令实操演示

3.1 创建本地YUM仓库

# 1. 复制RPM包到仓库目录
# mkdir -p /var/www/html/repo/Packages
# cp /var/cache/dnf/*.rpm /var/www/html/repo/Packages/ 2>/dev/null || echo “没有缓存的RPM包”

# 2. 创建测试RPM包(如果没有真实RPM包)
# cat > /tmp/test_package.sh << 'EOF' #!/bin/bash # 创建测试RPM包 # 作者:fgedu # 日期:2026-04-02 WORK_DIR="/tmp/rpmbuild" REPO_DIR="/var/www/html/repo/Packages" # 创建工作目录 mkdir -p "$WORK_DIR"/{SPECS,SOURCES,RPMS,SRPMS,BUILD,BUILDROOT} # 创建简单的spec文件 cat > “$WORK_DIR/SPECS/testapp.spec” << 'SPEC' Name: testapp Version: 1.0.0 Release: 1%{?dist} Summary: Test application for local repo License: GPL URL: http://www.example.com Source0: %{name}-%{version}.tar.gz %description This is a test application for local YUM repository. %prep %setup -q %build make %{?_smp_mflags} %install rm -rf $RPM_BUILD_ROOT make install DESTDIR=$RPM_BUILD_ROOT %files %doc %{_bindir}/testapp %changelog * Thu Apr 02 2026 fgedu – 1.0.0-1
– Initial package
SPEC

# 创建测试源文件
mkdir -p “$WORK_DIR/SOURCES/testapp-1.0.0”
cat > “$WORK_DIR/SOURCES/testapp-1.0.0/testapp.c” << 'C' #include

int main() {
printf(“Hello from testapp!\n”);
return 0;
}
C

cat > “$WORK_DIR/SOURCES/testapp-1.0.0/Makefile” << 'MAKEFILE' CC=gcc CFLAGS=-Wall all: testapp testapp: testapp.c $(CC) $(CFLAGS) -o testapp testapp.c install: install -d $(DESTDIR)/usr/bin install -m 755 testapp $(DESTDIR)/usr/bin/ MAKEFILE # 创建tar包 cd "$WORK_DIR/SOURCES" tar -czf testapp-1.0.0.tar.gz testapp-1.0.0/ # 构建RPM包 cd "$WORK_DIR/SPECS" rpmbuild -ba testapp.spec --define "_topdir $WORK_DIR" 2>&1 | head -20

# 复制RPM包到仓库目录
if [ -d “$WORK_DIR/RPMS/x86_64” ]; then
cp “$WORK_DIR/RPMS/x86_64″/*.rpm “$REPO_DIR/”
echo “RPM包已复制到:$REPO_DIR”
ls -lh “$REPO_DIR”
else
echo “RPM包构建失败”
fi

exit 0
EOF

# chmod +x /tmp/test_package.sh
# /tmp/test_package.sh

3.2 生成仓库元数据

# 1. 生成仓库元数据
# createrepo /var/www/html/repo
Directory walk started
Directory walk done – 0 packages
Temporary repo repo dir: /var/www/html/repo/.repodata/
Preparing sqlite DBs
Pool started (with 5 workers)
Pool finished

# 2. 查看生成的元数据
# ls -la /var/www/html/repo/repodata/
total 24
-rw-r–r–. 1 root root 4096 Apr 2 12:00:00 filelists.xml
-rw-r–r–. 1 root root 2048 Apr 2 12:00:00 filelists.sqlite.bz2
-rw-r–r–. 1 root root 1024 Apr 2 12:00:00 other.xml
-rw-r–r–. 1 root root 1024 Apr 2 12:00:00 other.sqlite.bz2
-rw-r–r–. 1 root root 4096 Apr 2 12:00:00 primary.xml
-rw-r–r–. 1 root root 2048 Apr 2 12:00:00 primary.sqlite.bz2
-rw-r–r–. 1 root root 2048 Apr 2 12:00:00 repomd.xml

# 3. 查看仓库结构
# tree /var/www/html/repo
/var/www/html/repo
├── Packages
└── repodata
├── filelists.xml
├── filelists.sqlite.bz2
├── other.xml
├── other.sqlite.bz2
├── primary.xml
├── primary.sqlite.bz2
└── repomd.xml

2 directories, 7 files

3.3 配置YUM仓库

# 1. 备份原有仓库配置
# mkdir -p /etc/yum.repos.d/backup
# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/ 2>/dev/null || echo “没有原有仓库配置”

# 2. 创建本地仓库配置文件
# cat > /etc/yum.repos.d/local.repo << 'EOF' [local-repo] name=Local Repository baseurl=file:///var/www/html/repo enabled=1 gpgcheck=0 priority=1 EOF # 3. 查看仓库配置文件 # cat /etc/yum.repos.d/local.repo [local-repo] name=Local Repository baseurl=file:///var/www/html/repo enabled=1 gpgcheck=0 priority=1 # 4. 清理YUM缓存 # dnf clean all 0 files removed # 5. 生成YUM缓存 # dnf makecache Updating Subscription Management repositories. Metadata cache created. # 6. 查看可用仓库 # dnf repolist repo id repo name local-repo Local Repository # 7. 查看仓库详细信息 # dnf repoinfo local-repo Repo-id : local-repo Repo-name : Local Repository Repo-revision : 1712035200 Repo-updated : Thu 02 Apr 2026 12:00:00 CST Repo-pkgs : 0 Repo-available-pkgs: 0 Repo-size : 0 Repo-baseurl : file:///var/www/html/repo Repo-expire : 172,800 second(s) (last: Thu 02 Apr 2026 12:00:00 CST) Repo-filename : /etc/yum.repos.d/local.repo Repo-tags :
Repo-cost : 0

3.4 测试YUM仓库

# 1. 搜索软件包
# dnf search testapp
Updating Subscription Management repositories.
No matches found.

# 2. 列出所有软件包
# dnf list available
Updating Subscription Management repositories.
Available Packages

# 3. 查看仓库状态
# dnf repolist all
repo id repo name status
local-repo Local Repository enabled

Part04-生产环境实战案例

4.1 配置网络YUM仓库

# 1. 创建网络仓库配置文件
# cat > /etc/yum.repos.d/network.repo << 'EOF' [network-repo] name=Network Repository baseurl=http://192.168.1.100/repo enabled=1 gpgcheck=0 priority=1 EOF # 2. 查看网络仓库配置 # cat /etc/yum.repos.d/network.repo [network-repo] name=Network Repository baseurl=http://192.168.1.100/repo enabled=1 gpgcheck=0 priority=1 # 3. 测试网络访问 # curl -I http://192.168.1.100/repo/ HTTP/1.1 403 Forbidden Date: Thu, 02 Apr 2026 12:00:00 GMT Server: Apache/2.4.57 (Red Hat Enterprise Linux) Last-Modified: Thu, 02 Apr 2026 12:00:00 GMT ETag: "2c-5c8a8a8a8a8a8" Accept-Ranges: bytes Content-Length: 44 Content-Type: text/html; charset=UTF-8 # 4. 配置HTTP访问权限 # cat > /etc/httpd/conf.d/repo.conf << 'EOF'
Options Indexes FollowSymLinks
AllowOverride None
Require all granted

EOF

# 5. 重启HTTP服务
# systemctl restart httpd

# 6. 再次测试网络访问
# curl -I http://192.168.1.100/repo/
HTTP/1.1 200 OK
Date: Thu, 02 Apr 2026 12:00:00 GMT
Server: Apache/2.4.57 (Red Hat Enterprise Linux)
Last-Modified: Thu, 02 Apr 2026 12:00:00 GMT
ETag: “2c-5c8a8a8a8a8a8”
Accept-Ranges: bytes
Content-Length: 44
Content-Type: text/html; charset=UTF-8

# 7. 更新YUM缓存
# dnf clean all
0 files removed

# dnf makecache
Updating Subscription Management repositories.
Metadata cache created.

# 8. 查看可用仓库
# dnf repolist
repo id repo name
local-repo Local Repository
network-repo Network Repository

4.2 仓库维护脚本

# 1. 创建仓库维护脚本
# cat > /opt/scripts/repo_maintain.sh << 'EOF' #!/bin/bash # YUM仓库维护脚本 # 功能:更新仓库元数据、清理过期包、生成报告 # 作者:fgedu # 日期:2026-04-02 # 定义变量 REPO_DIR="/var/www/html/repo" PACKAGES_DIR="$REPO_DIR/Packages" LOG_FILE="/var/log/repo_maintain_$(date +%Y%m%d_%H%M%S).log" REPORT_FILE="/var/log/repo_report_$(date +%Y%m%d_%H%M%S).txt" # 创建日志函数 log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" } # 创建分隔线函数 separator() { echo "==========================================" | tee -a "$LOG_FILE" } # 开始维护 separator log "开始YUM仓库维护" separator log "仓库目录:$REPO_DIR" separator # 检查仓库目录 if [ ! -d "$REPO_DIR" ]; then log "错误:仓库目录 $REPO_DIR 不存在" exit 1 fi # 统计RPM包数量 rpm_count=$(find "$PACKAGES_DIR" -name "*.rpm" 2>/dev/null | wc -l)
log “RPM包数量:$rpm_count”

# 统计仓库大小
repo_size=$(du -sh “$REPO_DIR” | cut -f1)
log “仓库大小:$repo_size”

# 更新仓库元数据
separator
log “更新仓库元数据…”
createrepo –update “$REPO_DIR” >> “$LOG_FILE” 2>&1

if [ $? -eq 0 ]; then
log “仓库元数据更新成功”
else
log “错误:仓库元数据更新失败”
exit 1
fi

# 生成仓库报告
separator
log “生成仓库报告…”
cat > “$REPORT_FILE” << REPORT ========================================== YUM仓库报告 ========================================== 生成时间:$(date '+%Y-%m-%d %H:%M:%S') 仓库目录:$REPO_DIR ========================================== 仓库统计: - RPM包数量:$rpm_count - 仓库大小:$repo_size RPM包列表: $(ls -lh "$PACKAGES_DIR"/*.rpm 2>/dev/null | tail -20)

仓库结构:
$(tree “$REPO_DIR” 2>/dev/null || find “$REPO_DIR” -type f | head -20)

==========================================
REPORT

log “仓库报告已生成:$REPORT_FILE”

# 清理YUM缓存
separator
log “清理YUM缓存…”
dnf clean all >> “$LOG_FILE” 2>&1
log “YUM缓存已清理”

# 重新生成YUM缓存
separator
log “重新生成YUM缓存…”
dnf makecache >> “$LOG_FILE” 2>&1
log “YUM缓存已重新生成”

# 完成维护
separator
log “YUM仓库维护完成”
separator
log “日志文件:$LOG_FILE”
log “报告文件:$REPORT_FILE”
separator

exit 0
EOF

# 2. 设置脚本执行权限
# chmod +x /opt/scripts/repo_maintain.sh

# 3. 执行仓库维护脚本
# /opt/scripts/repo_maintain.sh
==========================================
[2026-04-02 12:01:00] 开始YUM仓库维护
==========================================
[2026-04-02 12:01:00] 仓库目录:/var/www/html/repo
==========================================
[2026-04-02 12:01:00] RPM包数量:0
[2026-04-02 12:01:00] 仓库大小:24K
==========================================
[2026-04-02 12:01:00] 更新仓库元数据…
[2026-04-02 12:01:00] 仓库元数据更新成功
==========================================
[2026-04-02 12:01:00] 生成仓库报告…
[2026-04-02 12:01:00] 仓库报告已生成:/var/log/repo_report_20260402_120100.txt
==========================================
[2026-04-02 12:01:00] 清理YUM缓存…
[2026-04-02 12:01:00] YUM缓存已清理
==========================================
[2026-04-02 12:01:00] 重新生成YUM缓存…
[2026-04-02 12:01:00] YUM缓存已重新生成
==========================================
[2026-04-02 12:01:00] YUM仓库维护完成
==========================================
[2026-04-02 12:01:00] 日志文件:/var/log/repo_maintain_20260402_120100.log
[2026-04-02 12:01:00] 报告文件:/var/log/repo_report_20260402_120100.txt
==========================================

# 4. 查看仓库报告
# cat /var/log/repo_report_20260402_120100.txt
==========================================
YUM仓库报告
==========================================
生成时间:2026-04-02 12:01:00
仓库目录:/var/www/html/repo
==========================================

仓库统计:
– RPM包数量:0
– 仓库大小:24K

RPM包列表:

仓库结构:
/var/www/html/repo
├── Packages
└── repodata
├── filelists.xml
├── filelists.sqlite.bz2
├── other.xml
├── other.sqlite.bz2
├── primary.xml
├── primary.sqlite.bz2
└── repomd.xml

==========================================

Part05-风哥经验总结与分享

5.1 YUM仓库最佳实践

风哥经验分享:

  • 定期更新:定期更新仓库元数据,确保软件包信息最新
  • 版本控制:保留多个版本的软件包,便于版本回退
  • 备份策略:定期备份仓库数据,防止数据丢失
  • 权限管理:设置合适的文件权限,保护仓库安全
  • 监控告警:监控仓库状态,及时发现异常

5.2 性能优化建议

性能优化建议:

  • 使用SSD:将仓库放在SSD上,提高访问速度
  • 启用压缩:使用gzip压缩元数据,减少传输量
  • CDN加速:对于网络仓库,使用CDN加速访问
  • 缓存优化:合理设置YUM缓存策略
  • 负载均衡:对于大规模部署,使用负载均衡

5.3 安全注意事项

安全注意事项:

  • 启用GPG签名验证,确保软件包完整性
  • 限制仓库访问权限,防止未授权访问
  • 定期检查软件包安全性,及时更新
  • 使用HTTPS传输,防止中间人攻击
  • 记录仓库访问日志,便于审计

5.4 常见问题与解决方案

问题 原因 解决方案
仓库无法访问 网络问题、权限问题 检查网络连接、文件权限
元数据错误 元数据损坏、版本不匹配 重新生成元数据
软件包依赖问题 依赖包缺失、版本冲突 安装依赖包、解决版本冲突
下载速度慢 网络带宽不足、镜像源慢 更换镜像源、使用本地仓库
风哥总结:
本地YUM仓库是离线环境和内网环境的重要工具,可以大大提高软件安装效率。在生产环境中,建议结合网络仓库和本地仓库,构建多层次的软件分发体系。同时,要建立完善的仓库维护机制,定期更新元数据、清理过期包、备份重要数据。对于大规模部署,可以考虑使用Pulp、Artifactory等专业仓库管理工具。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息