配置内部centos9的yum源服务器提供给没有外网的机器使用。
假设该服务器 IP 是:192.168.0.100
一、准备工作(在内部源服务器上操作)
1. 安装必需工具
dnf install -y dnf-utils createrepo httpd screen vim mtr iftop htop curl git wget -y
关闭setenforce
setenforce 0
或手动编辑配置文件关闭。
二、更换 YUM 源为中科大
1. 备份原有源配置
mkdir -p /etc/yum.repos.d/backup
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
2. 下载中科大 CentOS Stream 9 的源
curl -o /etc/yum.repos.d/centos-ustc.repo https://mirrors.ustc.edu.cn/repoconfig/centos-stream.repo
如果你用的是非 Stream 版本,可以使用中科大 vault 源
3. 更新缓存测试源是否可用
dnf clean all
dnf makecache
三、同步本地 YUM 仓库
1. 创建本地仓库目录
mkdir -p /data/yum/BaseOS
mkdir -p /data/yum/AppStream
2. 编写同步脚本 /usr/local/bin/sync-centos9-yum.sh
cat > /usr/local/bin/sync-centos9-yum.sh << 'EOF'
#!/bin/bash
LOG_FILE="/var/log/yum_sync_$(date +%F).log"
exec >> "$LOG_FILE" 2>&1
echo "=== $(date '+%F %T') Start CentOS 9 reposync ==="
# 同步 BaseOS
reposync --repo=baseos --download-path=/data/yum/BaseOS --download-metadata --newest-only
# 同步 AppStream
reposync --repo=appstream --download-path=/data/yum/AppStream --download-metadata --newest-only
# 生成 metadata
createrepo /data/yum/BaseOS
createrepo /data/yum/AppStream
echo "=== $(date '+%F %T') Finished ==="
EOF
chmod +x /usr/local/bin/sync-centos9-yum.sh
⚠️ 注意:--repo=baseos 和 --repo=appstream 的名字应与你中科大 .repo 文件中对应的 id 一致。
四、配置 Apache 提供 YUM 源
1. 启动 HTTP 服务并设置开机自启
systemctl enable --now httpd
2. 将本地仓库目录链接到 HTTP 目录
ln -s /data/yum /var/www/html/yum
3. 开放防火墙端口
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
五、设置定时任务每日同步
crontab -e
添加:
0 2 * * * /usr/local/bin/sync-centos9-yum.sh
六、客户端配置为使用你的内部源(其他 CentOS 9 机器)
1. 备份旧的 repo 文件
mkdir -p /etc/yum.repos.d/backup/
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
2. 创建新 repo 文件 /etc/yum.repos.d/internal.repo
[BaseOS]
name=Internal CentOS 9 - BaseOS
baseurl=http://10.2.40.155/yum/BaseOS/
enabled=1
gpgcheck=0
[AppStream]
name=Internal CentOS 9 - AppStream
baseurl=http://10.2.40.155/yum/AppStream/
enabled=1
gpgcheck=0
3. 使用测试
dnf clean all
dnf makecache
dnf update
小提示:
**永久关闭 Enforcing(重启后仍然关闭)**
#### 1. 修改配置文件
编辑配置文件 `/etc/selinux/config`,找到
SELINUX=enforcing
修改为
SELINUX=disabled
或
SELINUX=permissive
– `disabled`:重启后完全关闭 SELinux
– `permissive`:仍加载但只警告不拦截