1. 为什么内网需要搭建本地YUM源
在企业级Linux运维环境中,内网服务器通常无法直接访问互联网上的公共YUM仓库。我曾经在某金融机构的数据中心遇到过这样的场景:200多台RHEL服务器需要同时安装相同的安全补丁,但每台服务器都通过公网下载不仅耗时,还会占用大量出口带宽。这时候,本地YUM源的价值就凸显出来了。
本地YUM源的核心优势体现在三个方面:
- 安装速度提升:内网传输速度通常是外网的10倍以上,一个500MB的软件包从本地源安装只需几秒
- 版本统一控制:避免不同服务器从不同镜像站获取到版本差异的软件包
- 安全合规:完全隔绝外网依赖,符合金融、政务等行业的监管要求
提示:即使在内网环境,也要定期同步安全更新。我建议至少每周同步一次关键安全仓库。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 搭建前的准备工作
2.1 硬件资源规划
根据我的经验,一个中等规模的内网环境(100-500节点)建议配置:
- 存储空间:基础仓库需要20-50GB(仅包含常用包),完整镜像需要200GB+
- 内存:4GB以上(createrepo操作较耗内存)
- 网络:千兆内网(百兆网络同步大型仓库会非常慢)
2.2 软件选型对比
| 工具 | 适用场景 | 优缺点对比 |
|---|---|---|
| reposync | 官方仓库同步 | 支持增量同步,但配置较复杂 |
| createrepo | 生成元数据 | 必选工具,新版支持多线程 |
| Nginx | 仓库分发 | 性能最好,支持HTTP/HTTPS |
| Apache | 老系统兼容 | 配置简单但并发性能一般 |
| Docker | 容器化部署 | 方便迁移但存储管理需要技巧 |
我强烈推荐使用Nginx+reposync的组合方案,这是经过多个生产环境验证的最稳定配置。
3. 详细搭建步骤
3.1 基础环境配置
bash复制# 在作为YUM源的服务器上操作
yum install -y epel-release
yum install -y nginx createrepo reposync yum-utils
systemctl enable --now nginx
创建仓库存储目录:
bash复制mkdir -p /var/www/html/repos/{base,epel,updates}
chmod -R 755 /var/www/html/repos
3.2 仓库同步实战
以同步CentOS 7基础仓库为例:
bash复制reposync -n -g -l -d -m --repoid=base --newest-only \
--download-metadata --download-path=/var/www/html/repos/base
createrepo -v /var/www/html/repos/base
关键参数解析:
-n:仅下载最新版本-g:保留软件组信息--newest-only:避免下载旧版本占用空间-m:下载模块化仓库数据(RHEL8+需要)
注意:首次同步完整仓库可能需要数小时,建议在业务低峰期操作。我曾遇到一个完整的EPEL仓库同步耗时8小时的情况。
3.3 Nginx优化配置
在/etc/nginx/conf.d/yum.conf中添加:
nginx复制server {
listen 80;
server_name yum.internal.company.com;
root /var/www/html/repos;
autoindex on;
location / {
try_files $uri $uri/ =404;
expires 30d;
add_header Cache-Control "public";
}
# 限制单个IP连接数防止滥用
limit_conn conn_limit_per_ip 10;
limit_req zone=req_limit_per_ip burst=20 nodelay;
}
执行配置检查:
bash复制nginx -t && systemctl reload nginx
4. 客户端配置指南
4.1 基础配置
创建/etc/yum.repos.d/local.repo:
ini复制[local-base]
name=Local Base Repo
baseurl=http://yum-server-ip/base
enabled=1
gpgcheck=0
priority=1
[local-updates]
name=Local Updates
baseurl=http://yum-server-ip/updates
enabled=1
gpgcheck=0
priority=1
4.2 优先级管理
当同时存在多个源时,必须设置优先级:
bash复制yum install -y yum-plugin-priorities
然后在.repo文件中添加:
ini复制priority=1 # 数字越小优先级越高
5. 高级维护技巧
5.1 自动化同步脚本
创建/usr/local/bin/sync_repo.sh:
bash复制#!/bin/bash
REPOS=(base epel updates)
LOG_FILE="/var/log/repo_sync.log"
for repo in ${REPOS[@]}; do
echo "[$(date)] Syncing $repo" >> $LOG_FILE
reposync -n -g -l -d -m --repoid=$repo \
--download-path=/var/www/html/repos/$repo >> $LOG_FILE 2>&1
if [ $? -eq 0 ]; then
createrepo --update /var/www/html/repos/$repo >> $LOG_FILE 2>&1
echo "[$(date)] $repo updated successfully" >> $LOG_FILE
else
echo "[$(date)] $repo sync failed" >> $LOG_FILE
fi
done
添加到crontab:
bash复制0 3 * * * /usr/local/bin/sync_repo.sh
5.2 存储空间优化
使用硬链接节省空间:
bash复制reposync --downloadcomps --download-metadata \
--delete --norepopath --gpgcheck \
--repoid=epel --download_path=/var/www/html/repos/epel \
--plugins --tempcache --source \
--newest-only --arch=x86_64 --delete \
--downloadcomps --download-metadata
6. 常见问题排查
6.1 客户端报错404 Not Found
典型症状:
code复制http://yum-server/base/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
排查步骤:
- 在服务器检查文件是否存在:
bash复制ls -l /var/www/html/repos/base/repodata/repomd.xml - 验证Nginx权限:
bash复制
namei -l /var/www/html/repos/base/repodata/repomd.xml - 检查SELinux状态:
bash复制getenforce # 如果是Enforcing模式需要设置 chcon -R -t httpd_sys_content_t /var/www/html/repos/
6.2 元数据生成失败
当createrepo报错时,尝试:
bash复制# 清理旧元数据
rm -rf /var/www/html/repos/base/.repodata
# 重新生成并显示详细日志
createrepo -v --update /var/www/html/repos/base
7. 性能优化实践
7.1 Nginx调优参数
在/etc/nginx/nginx.conf的http块中添加:
nginx复制open_file_cache max=10000 inactive=30s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
gzip on;
gzip_types application/x-rpm;
7.2 仓库结构优化
对于大型环境,建议按功能拆分仓库:
code复制/repos
/os # 基础系统包
/security # 仅安全更新
/apps # 业务应用
/thirdparty # 第三方依赖
这样不同部门可以只启用需要的仓库,减少客户端元数据加载时间。
