RHEL - 为网络隔离主机构建本地软件 Repo

OpenShift / RHEL / DevSecOps 汇总目录

构建本地共享软件 Repo 的方法说明

本文将说明如何构建本地的软件 Repo,以供那些运行在本地且无法直接联网的 RHEL 主机使用。实现方法是在一个可以联网的 RHEL 主机(例如 rhel-repo)中构建本地软件 Repo,然后将其共享给所有无法联网的 RHEL主机使用。

准备可联网主机

  1. 提供本地共享软件 Repo 的主机需要能够访问互联网。在该节点安装完 RHEL 后先根据《RHEL - 订阅、注册系统和 Yum Repository》一文的 “注册和注销 RHEL 系统” 章节完成主机注册,以让该主机能够访问到 RedHat 相关的订阅。
  2. 安装 http 服务。
$ yum install httpd -y
  1. 向防火墙增加 http 服务。
$ firewall-cmd --list-services
ssh dhcpv6-client
$ firewall-cmd --add-service=http --permanent
success
$ firewall-cmd --reload
$ firewall-cmd --list-services
ssh dhcpv6-client http

配置 CDN,提升软件包下载速度

可以为 subscription-manager 配置中国区的 CDN 以便可以加速软件包的下载速度。

$ subscription-manager config --rhsm.baseurl=https://china.cdn.redhat.com

方法1:使用 ISO 制作本地共享 Repo

本方法使用下载到本地的 RHEL 安装介质 ISO 文件制作本地共享 Repo。它的特点如下:

优势 :

  • 可以在单一主机上制作并提供多个主次版本的 RHEL Repo。

劣势:

  • RHEL 约每半年发布一个小版本及其对应的 ISO,每个版本会包含这期间所有的安全、功能更新。不过在每半年更新的 ISO 中不能及时包含针对高危漏洞红帽随时发布的安全补丁。
  • 订阅中有很多附加软件并没有包含在 RHEL 的 ISO 中。
  1. 从 RedHat 下载 RHEL 的 ISO 文件到 rhel-repo 主机,例如 /data/rhel-server-7.9-x86_64-dvd.iso。
  2. 打开 https://access.redhat.com/labs/yumrepoconfighelper/remote/ 网址。
  3. 在 “Yum Server Information” 中确定 ISO 文件的 RHEL 主版本,提供共享软件 Repo 的主机 IP。
    在这里插入图片描述
  4. 在 “Yum Server Connection Configuration” 中选择共享访问的协议端口。
    在这里插入图片描述
  5. 在 “Yum Repository Configuration Options” 中选择 RHEL 软件版本、架构和 ISO 文件的路径。这里可以加多个 RHEL 版本的 配置。
    在这里插入图片描述
  6. 在 “Ansible Information” 和 “Review” 步骤中接受默认配置,最后点击 Generate 即可。
    在这里插入图片描述
  7. 在以下生成结果的页面中将 Bash Script 对应的文件 - yumrepoconfig.sh 和 Repo File Package 对应的文件 - allRepos.zip 下载到 rhel-repo 主机上。
    在这里插入图片描述
  8. 在 rhel-repo 主机上执行 yumrepoconfig.sh 脚本。
$ chmod +x yumrepoconfig.sh
$ ./yumrepoconfig.sh
  1. 解压 allRepos.zip 文件。
$ yum install unzip -y
$ unzip  allRepos.zip
  1. 将 repo 文件传到本地无法访问互联网的 RHEL 主机节点(例如 rhel-1)上。
$ scp RHEL7-7.9-x86_64-RedHatEnterpriseLinuxServer-0.repo root@rhel-1:/etc/yum.repos.d/
  1. 在 rhel-1 主机上执行命令,确认已经有可用的 Repo 了。
$ yum repolist
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is registered with an entitlement server, but is not receiving updates. You can use subscription-manager to assign subscriptions.
repo id                                                repo name                                                      status
RHEL7-7.9-RedHatEnterpriseLinuxServer-DVD0             Red Hat Enterprise Linux 7Server - x86_64 (DVD)                 5,230

方法2:使用 reposync 制作本地共享 Repo

本方法是在 Repo 主机上使用 reposync 工具直接从 RedHat 官方将订阅中指定 Reop 中包含的软件同步下载到本地,然后再共享给其他 RHEL 主机。

优势 :

  • 可以随时获得最新的软件更新包到本地,及时性强。
  • reposync 将根据 $releasever 参数(如不设置该参数,则使用主版本对应的最新一个次要版本)下载对应版本的 Repo 中包含的软件包。如果使用 --newest-only 参数,则每种文件只会下载其最新版,如果不加 --newest-only 参数,则每种文件会下载其历史版本。
  • 可以同步所有订阅中包含的软件 Repo,而不仅限于 ISO 包含的软件。

劣势:

  • 只能同步和 Repo 节点具有相同 RHEL 主版本的软件 Repo。例如,如果同步节点是 RHEL 7,则该节点只能同步 RHEL 7 相关的 Repo,而不能在同一个节点上同时同步 RHEL 7/8/9 的 Repo。因此上述需求只能分别在运行 RHEL 7/8/9 的三个主机上实现。
  1. 安装必要的工具。
$ yum install -y yum-utils createrepo
  1. 允许使用需要同步的 Repo,以 rhel-ha-for-rhel-7-server-rpms 为例,并查看其占用空间以确认本地存储够用。注意:由于没有设置$releasever 参数,因此下载的是 RHEL 7 最新版 7.9 对应的 rhel-ha-for-rhel-7-server-rpms。
$ subscription-manager repos --enable=rhel-ha-for-rhel-7-server-rpms
$ yum repolist -v
Not loading "rhnplugin" plugin, as it is disabled
Loading "product-id" plugin
Loading "search-disabled-repos" plugin
Loading "subscription-manager" plugin
Updating Subscription Management repositories.
This system is registered with an entitlement server, but is not receiving updates. You can use subscription-manager to assign subscriptions.
Config time: 6.378
Yum version: 3.4.3
Setting up Package Sacks
pkgsack time: 0.013
Repo-id      : rhel-7-server-rpms/7Server/x86_64
Repo-name    : Red Hat Enterprise Linux 7 Server (RPMs)
Repo-revision: 1704892603
Repo-updated : Wed Jan 10 08:16:43 2024
Repo-pkgs    : 34,148
Repo-size    : 64 G
Repo-baseurl : https://cdn.redhat.com/content/dist/rhel/server/7/7Server/x86_64/os
Repo-expire  : 86,400 second(s) (last: Mon Jan 15 11:54:40 2024)
  Filter     : read-only:present
Repo-filename: /etc/yum.repos.d/redhat.repo
 
Repo-id      : rhel-ha-for-rhel-7-server-rpms/7Server/x86_64
Repo-name    : Red Hat Enterprise Linux High Availability (for RHEL 7 Server) (RPMs)
Repo-revision: 1704710878
Repo-updated : Mon Jan  8 05:47:58 2024
Repo-pkgs    : 865
Repo-size    : 1.5 G
Repo-baseurl : https://cdn.redhat.com/content/dist/rhel/server/7/7Server/x86_64/highavailability/os
Repo-expire  : 86,400 second(s) (last: Mon Jan 15 11:54:41 2024)
  Filter     : read-only:present
Repo-filename: /etc/yum.repos.d/redhat.repo
 
repolist: 35,013
  1. 执行命令,将指定 repo 包含的所有文件下载到 http 缺省的文件根目录(/var/www/html/repos)下的 repos/rhel7 子目录中。注意:不要修改 HTTP_DOC_ROOT 参数,因为如果要更改缺省的文件根目录,需要配合其他设置才可。
$ HTTP_DOC_ROOT=/var/www/html
$ mkdir -p $HTTP_DOC_ROOT/repos/rhel7
$ reposync -m --download-metadata --gpgcheck -p $HTTP_DOC_ROOT/repos/rhel7 --repoid=rhel-ha-for-rhel-7-server-rpms
  1. 执行命令,创建索引。
$ createrepo $HTTP_DOC_ROOT/repos/rhel7/rhel-ha-for-rhel-7-server-rpms

如果索引已有,则可更新索引。

$ createrepo --update $HTTP_DOC_ROOT/repos/rhel7/rhel-ha-for-rhel-7-server-rpms
  1. 配置 SELinux 和防火墙。
$ chcon -R -t httpd_sys_content_t /var/www/html
$ firewall-cmd --add-service=http --permanent
$ firewall-cmd --reload
  1. 确认可以远程访问到 Repo 了。
    在这里插入图片描述
  2. 在无法访问互联网的 RHEL 主机节点(例如 rhel-1)上执行命令,创建 repo 文件。
$ cat << EOF > /etc/yum.repos.d/rhel-ha-for-rhel-7-server.repo
[rhel-ha-for-rhel-7-server]
name=rhel-ha-for-rhel-7-server
baseurl=http://192.168.1.100/repo/rhel7/rhel-ha-for-rhel-7-server-rpms
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
EOF
  1. 确认可以从主机节点访问到远程的 Repo。
$ yum repolist
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is registered with an entitlement server, but is not receiving updates. You can use subscription-manager to assign subscriptions.
repo id                                                     repo name                                                   status
rhel-ha-for-rhel-7-server                                   rhel-ha-for-rhel-7-server                                   865
repolist: 865

方法3:制作包含特定软件的本地独享 Repo

此方法类似“方法2”,只能制作和 Repo 节点具有相同 RHEL 主版本的 Repo。和“方法2” 的区别是本方法可针对特定次要版本的 RHEL 中包含所的特定软件包、CVE、RHSA/RHBA/RHEA 制作对应的 Repo。

  1. 安装必要软件
$ yum install -y yum-utils createrepo
  1. 使用 downloadonly 参数将 httpd 软件包下载到指定目录下,然后创建 Repo 索引,最后打包。
$ mkdir /var/tmp/httpd
$ mkdir /var/tmp/httpd-installroot
$ yum install --downloadonly --installroot=/var/tmp/httpd-installroot --releasever=7.9 --downloaddir=/var/tmp/httpd httpd
$ createrepo --database /var/tmp/httpd
$ tar zcvf httpd.tar.gz /var/tmp/httpd
  1. 将打包文件传到需要的主机节点,然后在这些主机节点解压文件。
$ tar zxvf httpd.tar.gz
  1. 在这些节点创建 repo 文件后就可以安装使用 Repo 包含的软件了。
$ cat << EOF | tee /etc/yum.repos.d/offline-httpd.repo
[offline-httpd]
name=rhel-$releasever - httpd
baseurl=file:///var/tmp/httpd
enabled=1
gpgcheck=0
EOF

$ yum install --disablerepo=\* --enablerepo=offline-httpd httpd

方法4:使用 RedHat Satellite

RedHat Satellite 具备以上方法1/2/3 所包含的全部优势,即可在单节点(考虑到可靠性,可以是双机部署)同时提供个多个主次版本的 RHEL 和其相关附加软件的 Repo,因此优势最为明显。使用方法参见:

Red Hat Satellite - 核心概念篇

Red Hat Satellite - 安装篇

Red Hat Satellite - 导入订阅清单

Red Hat Satellite - 为主机配置可使用的内容(存储库),注册主机并安装软件包

参考

https://access.redhat.com/solutions/29269
https://access.redhat.com/solutions/7227
https://access.redhat.com/solutions/23016
https://access.redhat.com/solutions/7019225
https://access.redhat.com/zh_CN/solutions/7045415
https://access.redhat.com/solutions/3459631
https://access.redhat.com/discussions/4358851
https://access.redhat.com/documentation/zh-cn/red_hat_ansible_automation_platform/2.3/html-single/red_hat_ansible_automation_platform_installation_guide/index#proc-synchronizing-rpm-repositories-by-using-reposync_disconnected-installation
https://www.cnblogs.com/vilenx/p/12533361.html

  • 15
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值