CentOS7.9 创建本地YUM源和局域网YUM源

背景

由于特殊环境只有局域网,没法使用网上的各种YUM源,来回拷贝rpm包安装麻烦,还得解决依赖问题。想着在CentOS7.9搭建个本地/局域网YUM源,方便安装软件。

环境

[root@yum ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

安装

本地YUM源
ISO源

准备rpm包
挂载CentOS-7-x86_64-Everything-2009.iso,把里面所有文件都拷贝到本地目录/yum/yum-iso

[root@yum ~]# mkdir /mnt/yum-iso
[root@yum ~]# mount /dev/cdrom /mnt/yum-iso/
[root@yum ~]# mkdir  /yum
[root@yum ~]# cp -a /mnt/yum-iso/ /yum/
[root@yum ~]# ll /yum/yum-iso/
总用量 328
-rw-r--r--. 1 root root     14 10月 30 2020 CentOS_BuildTag
drwxr-xr-x. 3 root root     35 10月 27 2020 EFI
-rw-rw-r--. 1 root root    227 8月  30 2017 EULA
-rw-rw-r--. 1 root root  18009 12月 10 2015 GPL
drwxr-xr-x. 3 root root     57 10月 27 2020 images
drwxr-xr-x. 2 root root    198 11月  3 2020 isolinux
drwxr-xr-x. 2 root root     43 10月 27 2020 LiveOS
drwxr-xr-x. 2 root root 225280 11月  4 2020 Packages
drwxr-xr-x. 2 root root   4096 11月  4 2020 repodata
-rw-rw-r--. 1 root root   1690 12月 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r--. 1 root root   1690 12月 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root   2883 11月  4 2020 TRANS.TBL
备份、移除其他repo
[root@yum ~]# cd /etc/yum.repos.d/
[root@yum yum.repos.d]# ll
总用量 40
-rw-r--r--. 1 root root 1664 10月 23 2020 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 10月 23 2020 CentOS-CR.repo
-rw-r--r--. 1 root root  649 10月 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 10月 23 2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 10月 23 2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 10月 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 10月 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root  616 10月 23 2020 CentOS-x86_64-kernel.repo

[root@yum yum.repos.d]# tar zcvf repo-bk.tar.gz CentOS-*
CentOS-Base.repo
CentOS-CR.repo
CentOS-Debuginfo.repo
CentOS-fasttrack.repo
CentOS-Media.repo
CentOS-Sources.repo
CentOS-Vault.repo
CentOS-x86_64-kernel.repo

[root@yum yum.repos.d]# rm -rf CentOS-*
[root@yum yum.repos.d]# ll
总用量 4
-rw-r--r--. 1 root root 2705 3月  17 14:18 repo-bk.tar.gz

配置本地repo
 vi CentOS-Media.repo
#填入如下内容
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///yum/yum-iso/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

注释:配置repo路径、关闭gbp校验、启用这个repo

制作cache
[root@yum yum.repos.d]# yum clean all
已加载插件:fastestmirror
正在清理软件源: c7-media
[root@yum yum.repos.d]# yum makecache
已加载插件:fastestmirror
Determining fastest mirrors
c7-media                                                                             | 3.6 kB  00:00:00     
(1/4): c7-media/group_gz                                                             | 153 kB  00:00:00     
(2/4): c7-media/primary_db                                                           | 3.3 MB  00:00:00     
(3/4): c7-media/filelists_db                                                         | 3.3 MB  00:00:00     
(4/4): c7-media/other_db                                                             | 1.3 MB  00:00:00     
元数据缓存已建立

这样就可以使用yum安装软件啦

[root@yum yum.repos.d]# yum install tree
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 tree.x86_64.0.1.6.0-10.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

============================================================================================================
 Package              架构                   版本                            源                        大小
============================================================================================================
正在安装:
 tree                 x86_64                 1.6.0-10.el7                    c7-media                  46 k

事务概要
============================================================================================================
安装  1 软件包

总下载量:46 k
安装大小:87 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : tree-1.6.0-10.el7.x86_64                                                                1/1 
  验证中      : tree-1.6.0-10.el7.x86_64                                                                1/1 

已安装:
  tree.x86_64 0:1.6.0-10.el7                                                                                

完毕!
自定义YUM源
#createrepo 命令用于创建yum源(软件仓库),即为存放于本地特定位置的众多rpm包建立索引,描述各包所需依赖信息,并形成元数据。
[root@yum yum.repos.d]# yum -y install createrepo
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 createrepo.noarch.0.0.9.9-28.el7 将被 安装
--> 正在处理依赖关系 python-deltarpm,它被软件包 createrepo-0.9.9-28.el7.noarch 需要
--> 正在处理依赖关系 libxml2-python,它被软件包 createrepo-0.9.9-28.el7.noarch 需要
--> 正在处理依赖关系 deltarpm,它被软件包 createrepo-0.9.9-28.el7.noarch 需要
--> 正在检查事务
---> 软件包 deltarpm.x86_64.0.3.6-3.el7 将被 安装
---> 软件包 libxml2-python.x86_64.0.2.9.1-6.el7.5 将被 安装
---> 软件包 python-deltarpm.x86_64.0.3.6-3.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

============================================================================================================================================================================
 Package                                      架构                                版本                                          源                                     大小
============================================================================================================================================================================
正在安装:
 createrepo                                   noarch                              0.9.9-28.el7                                  c7-media                               94 k
为依赖而安装:
 deltarpm                                     x86_64                              3.6-3.el7                                     c7-media                               82 k
 libxml2-python                               x86_64                              2.9.1-6.el7.5                                 c7-media                              247 k
 python-deltarpm                              x86_64                              3.6-3.el7                                     c7-media                               31 k

事务概要
============================================================================================================================================================================
安装  1 软件包 (+3 依赖软件包)

总下载量:454 k
安装大小:2.0 M
Downloading packages:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                         10 MB/s | 454 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : deltarpm-3.6-3.el7.x86_64                                                                                                                               1/4 
  正在安装    : python-deltarpm-3.6-3.el7.x86_64                                                                                                                        2/4 
  正在安装    : libxml2-python-2.9.1-6.el7.5.x86_64                                                                                                                     3/4 
  正在安装    : createrepo-0.9.9-28.el7.noarch                                                                                                                          4/4 
  验证中      : createrepo-0.9.9-28.el7.noarch                                                                                                                          1/4 
  验证中      : libxml2-python-2.9.1-6.el7.5.x86_64                                                                                                                     2/4 
  验证中      : deltarpm-3.6-3.el7.x86_64                                                                                                                               3/4 
  验证中      : python-deltarpm-3.6-3.el7.x86_64                                                                                                                        4/4 

已安装:
  createrepo.noarch 0:0.9.9-28.el7                                                                                                                                          

作为依赖被安装:
  deltarpm.x86_64 0:3.6-3.el7                        libxml2-python.x86_64 0:2.9.1-6.el7.5                        python-deltarpm.x86_64 0:3.6-3.el7                       

完毕!
准备rpm包

需要自己确定依赖包、目录。这里使用tree做演示。

#查找tree-1.6.0-10.el7.x86_64.rpm
[root@yum yum.repos.d]# find / -name tree*
/usr/bin/tree
/usr/share/doc/tree-1.6.0
/usr/share/man/man1/tree.1.gz
/usr/share/mime/treemagic
/usr/include/c++/4.8.2/ext/pb_ds/detail/tree_policy
/usr/include/c++/4.8.2/ext/pb_ds/detail/tree_trace_base.hpp
/usr/include/c++/4.8.2/ext/pb_ds/tree_policy.hpp
/usr/src/kernels/3.10.0-1160.el7.x86_64/include/config/audit/tree.h
/usr/src/kernels/3.10.0-1160.el7.x86_64/include/config/interval/tree.h
/usr/src/kernels/3.10.0-1160.el7.x86_64/include/config/quota/tree.h
/usr/src/kernels/3.10.0-1160.el7.x86_64/include/config/radix/tree
/usr/src/kernels/3.10.0-1160.el7.x86_64/include/config/tree
/usr/src/kernels/3.10.0-1160.el7.x86_64/scripts/dtc/treesource.c
/mnt/yum-iso/Packages/tree-1.6.0-10.el7.x86_64.rpm
/yum/yum-iso/Packages/tree-1.6.0-10.el7.x86_64.rpm

[root@yum yum.repos.d]# cp /yum/yum-iso/Packages/tree-1.6.0-10.el7.x86_64.rpm /yum/yum-custom/packages/
[root@yum yum.repos.d]# ll /yum/yum-custom/packages/
总用量 48
-rw-r--r--. 1 root root 47508 3月  17 15:48 tree-1.6.0-10.el7.x86_64.rpm

创建repo

关键步骤:-----生成repodata目录,自动创建索引信息

否则安装tree的时候会出现报错:
Error downloading packages:
tree-1.6.0-10.el7.x86_64: failed to retrieve packages/tree-1.6.0-10.el7.x86_64.rpm from c7-media
error was [Errno 2] Local file does not exist: /etc/yum.repos.d/-d/packages/tree-1.6.0-10.el7.x86_64.rpm

[root@yum yum.repos.d]# cd /yum/yum-custom/
[root@yum yum-custom]# createrepo -pdo /yum/yum-custom/ /yum/yum-custom/
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 0 pkgs
Spawning worker 2 with 0 pkgs
Spawning worker 3 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
#可以看到生成了一个repodata的目录
[root@yum yum-custom]# ll
总用量 4
drwxr-xr-x. 2 root root   42 3月  19 09:38 packages
drwxr-xr-x  2 root root 4096 3月  19 09:46 repodata

#想要查看输入信息。
#执行命令:
[root@yum yum-custom]# createrepo  -v  ./
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 0 pkgs
Spawning worker 2 with 0 pkgs
Spawning worker 3 with 0 pkgs
Worker 0: reading packages/tree-1.6.0-10.el7.x86_64.rpm
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Starting other db creation: Sat Mar 19 10:07:57 2022
Ending other db creation: Sat Mar 19 10:07:57 2022
Starting filelists db creation: Sat Mar 19 10:07:57 2022
Ending filelists db creation: Sat Mar 19 10:07:57 2022
Starting primary db creation: Sat Mar 19 10:07:57 2022
Ending primary db creation: Sat Mar 19 10:07:57 2022
Sqlite DBs complete

配置自定义repo
[root@yum yum.repos.d]# vi /etc/yum.repos.d/CentOS-Media1.repo
#填入如下内容
[c9-media]
name=CentOS-$releasever-Media
baseurl=file:///yum/yum-custom/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
制作cache
[root@yum yum.repos.d]# yum clean all
已加载插件:fastestmirror
正在清理软件源: c9-media
Cleaning up list of fastest mirrors
[root@yum yum.repos.d]# yum makecache
已加载插件:fastestmirror
Determining fastest mirrors
c9-media                                                                                                                                             | 2.9 kB  00:00:00     
(1/3): c9-media/primary_db                                                                                                                           | 1.8 kB  00:00:00     
(2/3): c9-media/filelists_db                                                                                                                         |  880 B  00:00:00     
(3/3): c9-media/other_db                                                                                                                             | 1.3 kB  00:00:00     
元数据缓存已建立

使用自定义repo
[root@yum yum.repos.d]# yum install tree
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 tree.x86_64.0.1.6.0-10.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

============================================================================================================================================================================
 Package                              架构                                   版本                                            源                                        大小
============================================================================================================================================================================
正在安装:
 tree                                 x86_64                                 1.6.0-10.el7                                    c7-media                                  46 k

事务概要
============================================================================================================================================================================
安装  1 软件包

总下载量:46 k
安装大小:87 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : tree-1.6.0-10.el7.x86_64                                                                                                                                1/1 
  验证中      : tree-1.6.0-10.el7.x86_64                                                                                                                                1/1 
已安装:
  tree.x86_64 0:1.6.0-10.el7                                                                                                                                                
完毕!

局域网YUM源

局域网YUM源可以配成:本地YUM源 + FTP服务器
这里使用自定义源yum-custom(见上面) + VSFTP演示。

下载vsftpd
[root@yum yum.repos.d]# yum -y install vsftpd
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 vsftpd.x86_64.0.3.0.2-28.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

============================================================================================================================================================================
 Package                                架构                                   版本                                          源                                        大小
============================================================================================================================================================================
正在安装:
 vsftpd                                 x86_64                                 3.0.2-28.el7                                  c7-media                                 172 k

事务概要
============================================================================================================================================================================
安装  1 软件包

总下载量:172 k
安装大小:353 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : vsftpd-3.0.2-28.el7.x86_64                                                                                                                              1/1 
  验证中      : vsftpd-3.0.2-28.el7.x86_64                                                                                                                              1/1 

已安装:
  vsftpd.x86_64 0:3.0.2-28.el7                                                                                                                                              
完毕!
关闭selinux和防火墙
查看selinux状态
[root@yum yum-custom]# getenforce
Disabled

[root@yum yum.repos.d]# vi /etc/selinux/config
修改如下:
SELINUX=disabled

[root@yum yum.repos.d]#  reboot

[root@yum yum-custom]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 
关闭防火墙,并永久关闭
[root@yum yum-custom]# systemctl stop firewalld
[root@yum yum-custom]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@yum yum-custom]# 

配置vsftpd

编辑vsftp.conf
并增加匿名用户root目录(默认已经启用匿名访问)
anon_root=/yum/yum-custom/
注意目录权限如下

[root@yum yum-custom]# ll -a /yum/yum-custom/
总用量 4
drwxr-xr-x. 4 root root   38 3月  19 10:07 .
drwxr-xr-x. 4 root root   39 3月  17 15:46 ..
drwxr-xr-x. 2 root root   42 3月  19 09:38 packages
drwxr-xr-x  2 root root 4096 3月  19 10:07 repodata

启用vsftp
[root@yum ~]# systemctl start vsftpd
[root@yum ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
[root@yum ~]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2022-03-18 15:02:44 CST; 17s ago
 Main PID: 1668 (vsftpd)
   CGroup: /system.slice/vsftpd.service
           └─1668 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

3月 18 15:02:44 yum systemd[1]: Starting Vsftpd ftp daemon...
3月 18 15:02:44 yum systemd[1]: Started Vsftpd ftp daemon.
[root@yum ~]# 

本地电脑访问ftp://192.168.100.169


image.png
局域网使用

在另外一台服务器web1上配置repo

[root@web1 ~]# cd /etc/yum.repos.d/
[root@web1 yum.repos.d]# mkdir back
[root@web1 yum.repos.d]# mv *.repo back/
[root@web1 yum.repos.d]# vim CentOS-Media.repo
[c7-media]
name=CentOS-$releasever - Media
baseurl=ftp://192.168.100.169
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

其中192.168.118.169为上面vsftp服务器地址
启用这个局域网的repo
[root@web1 yum.repos.d]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: c7-media
Cleaning up everything
Cleaning up list of fastest mirrors
[root@web1 yum.repos.d]# yum makecache
已加载插件:fastestmirror, langpacks
c7-media                                                                                                                  | 2.9 kB  00:00:00     
(1/3): c7-media/filelists_db                                                                                              |  880 B  00:00:00     
(2/3): c7-media/other_db                                                                                                  | 1.3 kB  00:00:00     
(3/3): c7-media/primary_db                                                                                                | 1.8 kB  00:00:00     
Determining fastest mirrors
元数据缓存已建立

[root@web1 yum.repos.d]# yum install -y tree
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 tree.x86_64.0.1.6.0-10.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=================================================================================================================================================
 Package                        架构                             版本                                   源                                  大小
=================================================================================================================================================
正在安装:
 tree                           x86_64                           1.6.0-10.el7                           c7-media                            46 k

事务概要
=================================================================================================================================================
安装  1 软件包

总下载量:46 k
安装大小:87 k
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm                                                                                              |  46 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : tree-1.6.0-10.el7.x86_64                                                                                                     1/1 
  验证中      : tree-1.6.0-10.el7.x86_64                                                                                                     1/1 

已安装:
  tree.x86_64 0:1.6.0-10.el7                                                                                                                     

完毕!

参考:https://www.cnblogs.com/hailun1987/p/9827329.html?ivk_sa=1024320u

你可能感兴趣的:(CentOS7.9 创建本地YUM源和局域网YUM源)