Redhat Linux 9.6 配置本地 yum 源

发布于:2025-08-10 ⋅ 阅读:(22) ⋅ 点赞:(0)

在这里插入图片描述

1. 安装并配置HTTP服务器

bash

# 安装httpd服务
yum install -y httpd

# 启动并设置开机自启
systemctl enable httpd
systemctl start httpd
systemctl status httpd

2. 创建本地仓库目录结构

bash

# 创建Web目录下的仓库目录
mkdir -p /var/www/html/repo/{baseos,appstream}

# 设置正确的权限
chown -R apache:apache /var/www/html/repo
chmod -R 755 /var/www/html/repo

3. 同步仓库数据到Web目录

bash

# 同步BaseOS仓库到Web目录
dnf reposync --download-metadata --newest-only --repo=rhel-9-for-x86_64-baseos-rpms -p /var/www/html/repo/

# 同步AppStream仓库到Web目录
dnf reposync --download-metadata --newest-only --repo=rhel-9-for-x86_64-appstream-rpms -p /var/www/html/repo/

# 创建仓库元数据
createrepo /var/www/html/repo/rhel-9-for-x86_64-baseos-rpms/
createrepo /var/www/html/repo/rhel-9-for-x86_64-appstream-rpms/

输出:

$ dnf reposync --download-metadata --newest-only --repo=rhel-9-for-x86_64-baseos-rpms -p /var/www/html/repo/
...
(11098/11100): python3-libxml2-2.9.13-12.el9_6.x86_64.rpm                                                                                                      53 kB/s | 225 kB     00:04
(11099/11100): libxml2-2.9.13-12.el9_6.x86_64.rpm                                                                                                             138 kB/s | 747 kB     00:05
(11100/11100): libxml2-2.9.13-12.el9_6.i686.rpm                                                                                                               112 kB/s | 785 kB     00:07
[root@localhost ~]#

4. 配置防火墙

bash

# 开放HTTP端口
firewall-cmd --permanent --add-service=http
firewall-cmd --reload

# 检查防火墙规则
firewall-cmd --list-all

5. 配置SELinux(如果启用)

bash

# 设置SELinux上下文
setsebool -P httpd_enable_homedirs on
restorecon -R /var/www/html/repo

6. 创建本地仓库配置文件

在本机创建本地HTTP源配置:

bash

cat > /etc/yum.repos.d/local-http.repo << 'EOF'
[local-baseos-http]
name=Local Red Hat Enterprise Linux 9 - BaseOS (HTTP)
baseurl=http://192.168.0.10/repo/rhel-9-for-x86_64-baseos-rpms/
enabled=1
gpgcheck=0
priority=1

[local-appstream-http]
name=Local Red Hat Enterprise Linux 9 - AppStream (HTTP)
baseurl=http://192.168.0.10/repo/rhel-9-for-x86_64-appstream-rpms/
enabled=1
gpgcheck=0
priority=1
EOF

7. 创建首页和目录浏览

bash

# 创建仓库首页
cat > /var/www/html/index.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
    <title>Local RHEL 9 Repository Server</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        h1 { color: #cc0000; }
        .repo-link { display: block; margin: 10px 0; padding: 10px; background: #f0f0f0; text-decoration: none; }
        .repo-link:hover { background: #e0e0e0; }
    </style>
</head>
<body>
    <h1>Red Hat Enterprise Linux 9 本地仓库服务器</h1>
    <p>服务器地址: 192.168.0.10</p>
    <h2>可用仓库:</h2>
    <a href="/repo/rhel-9-for-x86_64-baseos-rpms/" class="repo-link">BaseOS Repository</a>
    <a href="/repo/rhel-9-for-x86_64-appstream-rpms/" class="repo-link">AppStream Repository</a>
    
    <h2>客户端配置:</h2>
    <pre>
[local-baseos-http]
name=Local Red Hat Enterprise Linux 9 - BaseOS (HTTP)
baseurl=http://192.168.0.10/repo/rhel-9-for-x86_64-baseos-rpms/
enabled=1
gpgcheck=0

[local-appstream-http]
name=Local Red Hat Enterprise Linux 9 - AppStream (HTTP)
baseurl=http://192.168.0.10/repo/rhel-9-for-x86_64-appstream-rpms/
enabled=1
gpgcheck=0
    </pre>
</body>
</html>
EOF

8. 配置Apache目录浏览

bash

# 在Apache配置中启用目录浏览
cat > /etc/httpd/conf.d/repo.conf << 'EOF'
<Directory "/var/www/html/repo">
    Options +Indexes +FollowSymLinks
    AllowOverride None
    Require all granted
    IndexOptions FancyIndexing HTMLTable SuppressDescription
</Directory>
EOF

# 重启Apache服务
systemctl restart httpd

9. 测试HTTP仓库服务

bash

# 测试本地访问
curl -I http://192.168.0.10/
curl -I http://192.168.0.10/repo/

# 清理并测试yum缓存
yum clean all
yum repolist

# 测试安装软件包
yum install -y tree

10. 为其他客户端提供配置文件

其他RHEL 9机器可以使用以下配置:

bash

# 在客户端机器上执行
cat > /etc/yum.repos.d/local-http.repo << 'EOF'
[local-baseos-http]
name=Local Red Hat Enterprise Linux 9 - BaseOS (HTTP)
baseurl=http://192.168.0.10/repo/rhel-9-for-x86_64-baseos-rpms/
enabled=1
gpgcheck=0
priority=1

[local-appstream-http]
name=Local Red Hat Enterprise Linux 9 - AppStream (HTTP)
baseurl=http://192.168.0.10/repo/rhel-9-for-x86_64-appstream-rpms/
enabled=1
gpgcheck=0
priority=1
EOF

# 禁用原始仓库(可选)
# sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/redhat.repo

11. 创建自动更新脚本

bash

cat > /usr/local/bin/update-http-repo.sh << 'EOF'
#!/bin/bash
echo "$(date): 开始更新本地HTTP仓库"

# 同步仓库
dnf reposync --download-metadata --newest-only --delete --repo=rhel-9-for-x86_64-baseos-rpms -p /var/www/html/repo/
dnf reposync --download-metadata --newest-only --delete --repo=rhel-9-for-x86_64-appstream-rpms -p /var/www/html/repo/

# 重新创建元数据
createrepo --update /var/www/html/repo/rhel-9-for-x86_64-baseos-rpms/
createrepo --update /var/www/html/repo/rhel-9-for-x86_64-appstream-rpms/

# 设置权限
chown -R apache:apache /var/www/html/repo
chmod -R 755 /var/www/html/repo

echo "$(date): 仓库更新完成"
EOF

chmod +x /usr/local/bin/update-http-repo.sh

现在您可以通过浏览器访问 http://192.168.0.10 来查看仓库状态,其他机器也可以使用这个HTTP源来安装软件包。

12. 禁用官方源

方法 1:用 subscription-manager 禁用(推荐)

# 禁用 BaseOS
subscription-manager repos --disable=rhel-9-for-x86_64-baseos-rpms

# 禁用 AppStream
subscription-manager repos --disable=rhel-9-for-x86_64-appstream-rpms

这样会在 /etc/yum.repos.d/redhat.repo 中把对应仓库的 enabled=1 改成 enabled=0,并且保留本地源可用。

方法 2:直接编辑 /etc/yum.repos.d/redhat.repo

vi /etc/yum.repos.d/redhat.repo
找到这两个段落:
[rhel-9-for-x86_64-baseos-rpms]
enabled=1

[rhel-9-for-x86_64-appstream-rpms]
enabled=1enabled=1 改成:
enabled=0
保存退出即可。

方法 3:全局禁用 redhat.repo 文件(不建议,太绝对)
如果你根本不想让 RHEL 系统自动生成 redhat.repo:

subscription-manager config --rhsm.manage_repos=0

执行后 subscription-manager 将不再管理或更新 redhat.repo,只会用你手动添加的 .repo 文件。

✅ 建议:
如果你是离线环境用本地 HTTP 仓库,就用 方法 1 禁用官方源,避免 yum/dnf 在访问网络时超时;这样还能保留 subscription-manager 的正常工作。


网站公告

今日签到

点亮在社区的每一天
去签到