Linux 搭建本地镜像源(CentOS 离线 yum)

使用 RPM 包搭建本地镜像源

1. 下载所有源中的 RPM 包

yum -y install httpd yum-utils createrepo 

创建 RPM 包的下载目录

mkdir /var/www/html/centos

启动 httpd 服务

[root@localhost ~]# systemctl start httpd
[root@localhost ~]# ss -antulp | grep :80
tcp   LISTEN 0      511                *:80               *:*    users:(("httpd",pid=10418,fd=4),("httpd",pid=10417,fd=4),("httpd",pid=10416,fd=4),("httpd",pid=10414,fd=4))

在这里插入图片描述
查看当前 yum

[root@localhost ~]# yum repolist
repo id                                           repo name
appstream                                         CentOS Stream 9 - AppStream
baseos                                            CentOS Stream 9 - BaseOS
extras-common                                     CentOS Stream 9 - Extras packages

可以看到当前一共有三个源仓库,分别为 appstreambaseosextras-common。下面分别将这些仓库中的 RPM 包下载下来。

将当前源中所有的软件包下载到创建好的目录

[root@localhost ~]# reposync --repo appstream -p /var/www/html/centos/

之后用同样的命令,将所有的软件源的 RPM 包都下载好。
在这里插入图片描述
[注]:下载数量很多需内心等待。

[root@localhost ~]# reposync --repo baseos -p /var/www/html/centos/
[root@localhost ~]# reposync --repo extras-common -p /var/www/html/centos/

2. 为 yum 源生成 repodata 索引依赖

查看下载好的所有源的 RPM 包。

[root@localhost ~]# ll /var/www/html/centos/
total 0
drwxr-xr-x. 3 root root 22 Nov 23 16:02 appstream
drwxr-xr-x. 3 root root 22 Nov 24 09:21 baseos
drwxr-xr-x. 3 root root 22 Nov 24 09:49 extras-common

进入下载的目录

[root@localhost ~]# cd /var/www/html/centos/

分别为所有仓库生成 repodata 索引。
生成 appstream 仓库的 repodata 索引依赖。

[root@localhost centos]# createrepo appstream/
Directory walk started
Directory walk done - 13373 packages
Temporary output repo path: appstream/.repodata/
Preparing sqlite DBs
Pool started (with 5 workers)
Pool finished

生成 baseos 仓库的 repodata 索引依赖。

[root@localhost centos]# createrepo baseos/
Directory walk started
Directory walk done - 3848 packages
Temporary output repo path: baseos/.repodata/
Preparing sqlite DBs
Pool started (with 5 workers)
Pool finished

生成 extras-common 仓库的 repodata 索引依赖。

[root@localhost centos]# createrepo extras-common/
Directory walk started
Directory walk done - 38 packages
Temporary output repo path: extras-common/.repodata/
Preparing sqlite DBs
Pool started (with 5 workers)
Pool finished

成功后即可在仓库目录中看到生成好的 repodata:

[root@localhost centos]# ls appstream/
Packages  repodata
[root@localhost centos]# ls baseos/
Packages  repodata
[root@localhost centos]# ls extras-common/
Packages  repodata
[root@localhost centos]# ll appstream/repodata/
total 41540
-rw-r--r--. 1 root root 12051562 Nov 24 09:59 19be5e75d0fe8a9d2f1a32b2994cf8e1c2437860f66cb1f64519ac3dfe68ca19-filelists.sqlite.bz2
-rw-r--r--. 1 root root  7073863 Nov 24 09:59 32dc077706f3317759e93ab06c6c8c59a4fb48d62f5277ba746436f02446c902-primary.sqlite.bz2
-rw-r--r--. 1 root root  4038253 Nov 24 09:59 3a15770cdd1ff2a49c78568050569ca8b55c93d1b46917c89694d5e575fc80cb-other.sqlite.bz2
-rw-r--r--. 1 root root  3245284 Nov 24 09:59 b339d402b66a76c904679c448fa600cbbec0b316bbe1ee7405610000555c598a-other.xml.gz
-rw-r--r--. 1 root root  2758017 Nov 24 09:59 c2f8de3be4e63df01b0d4feda3976eb3a976c90b6315b4e71161f0529f62d720-primary.xml.gz
-rw-r--r--. 1 root root 13349045 Nov 24 09:59 d97fb51cba16b5a2029174c1b83bed4f3c08758bef1a3435d1925739dc623c76-filelists.xml.gz
-rw-r--r--. 1 root root     3117 Nov 24 09:59 repomd.xml

3. 备份 yum 配置文件,并配置新 yum 源

进入 yum 源配置文件目录,并备份之前的配置文件:

[root@localhost ~]# cd /etc/yum.repos.d
[root@localhost yum.repos.d]# ls
centos-addons.repo  centos.repo
[root@localhost yum.repos.d]# mkdir bk
[root@localhost yum.repos.d]# mv centos* bk
[root@localhost yum.repos.d]# ls bk
centos-addons.repo  centos.repo

创建新的配置文件并添加源:

[root@localhost yum.repos.d]# vim im.repo

添加如下内容:

[appstream]		
name=appstream			# 根据自己的仓库名命名
baseurl=http://10.1.1.99/centos/appstream
gpgcheck=0
enabled=1
[baseos]
name=baseos
baseurl=http://10.1.1.99/centos/baseos
gpgcheck=0
enabled=1
[extras-common]
name=extras-common
baseurl=http://10.1.1.99/centos/extras-common
gpgcheck=0
enabled=1

4. 验证

清理当前 yum

[root@localhost ~]# yum clean all
15 files removed

建立 yum 缓存

[root@localhost ~]# yum makecache
appstream                                                                               196 MB/s |  15 MB     00:00
baseos                                                                                  246 MB/s | 5.9 MB     00:00
extras-common                                                                           3.8 MB/s | 8.7 kB     00:00
Metadata cache created.

查看当前 yum 仓库

[root@localhost ~]# yum repolist
repo id                                                     repo name
appstream                                                   appstream
baseos                                                      baseos
extras-common                                               extras-common

更新 yum

[root@localhost ~]# yum update

使用本地源下载软件

[root@localhost ~]# yum install -y tree
Last metadata expiration check: 0:03:44 ago on Thu 24 Nov 2022 10:47:10 AM CST.
Dependencies resolved.
========================================================================================================================
 Package                  Architecture               Version                           Repository                  Size
========================================================================================================================
Installing:
 tree                     x86_64                     1.8.0-10.el9                      baseos                      56 k

Transaction Summary
========================================================================================================================
Install  1 Package

Total download size: 56 k
Installed size: 113 k
Downloading Packages:
tree-1.8.0-10.el9.x86_64.rpm                                                             24 MB/s |  56 kB     00:00
------------------------------------------------------------------------------------------------------------------------
Total                                                                                   5.4 MB/s |  56 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1
  Installing       : tree-1.8.0-10.el9.x86_64                                                                       1/1
  Running scriptlet: tree-1.8.0-10.el9.x86_64                                                                       1/1
  Verifying        : tree-1.8.0-10.el9.x86_64                                                                       1/1

Installed:
  tree-1.8.0-10.el9.x86_64

Complete!

下载成功!

完成

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Imagine Miracle

爱你哟 =^ v ^=

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值