本地和局域网 YUM 源制作详解

本地和局域网 YUM 源制作详解

  • 环境介绍
  • 一:制作本地 YUM 源
    • 1、ISO YUM 源(光盘镜像源)
      • 1.1 添加光盘
      • 1.2 挂载光盘
      • 1.3 复制光盘文件
    • 2、备份归档本地repo
      • 2.1 备份本地repo
      • 2.2 删除无用文件
    • 3、配置本地 repo
    • 4、制作 cache
    • 5、本地 YUM 源制作完成,可以进行程序安装
      • 5.1 查看本地的 YUM 源
      • 5.2 安装 wget
      • 5.3 安装 ftp
      • 5.4 安装 tcpdump
  • 二:自定义 YUM 源(由本地 rpm 包组成 YUM 源)
    • 1、安装程序 createrepo(用于生成 repodata 目录)
    • 2、准备 rpm 包
    • 3、备份归档本地 repo
    • 4、制作 repodata
      • 4.1 生成 repodata 目录示例
    • 5、自定义 repo
    • 6、清除缓存
    • 7、构建缓存
    • 8、查看 yum repolist
    • 9、安装程序
  • 三:制作局域网 YUM 源
    • 1、YUM 源主机部署 vsftpd
      • 1.1 安装程序 vsftpd
      • 1.2 配置 vsftpd
      • 1.3 关闭 selinux
      • 1.4 启动 vsftpd
    • 2、局域网其它主机配置 repo
      • 2.1 修改 repo 文件
      • 2.2 清除缓存和构建缓存
    • 3、进行程序安装

环境介绍

[root@LeeMumu ~]# uname -r
3.10.0-957.el7.x86_64
[root@LeeMumu ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

一:制作本地 YUM 源

1、ISO YUM 源(光盘镜像源)

1.1 添加光盘

在 VMware 添加光盘。光盘名称为:CentOS-7-x86_64-Everything-1810.iso
本地和局域网 YUM 源制作详解_第1张图片

1.2 挂载光盘

光盘在Linux上的标识一般为:

# /dev/sr0
# /dev/cdrom
[root@LeeMumu ~]# mkdir /media/cdrom
[root@LeeMumu ~]# mount /dev/cdrom /media/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
[root@LeeMumu ~]# ls /media/cdrom/
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

1.3 复制光盘文件

把光盘内容复制在 /yum/cdrom/目录内。通过查看以 wire 开头的 rpm 包,确认是否复制成功。

[root@LeeMumu ~]# mkdir /yum
[root@LeeMumu ~]# cp -a /media/cdrom/ /yum
[root@LeeMumu ~]# ls /yum/cdrom/
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
[root@LeeMumu ~]# ls /yum/cdrom/Packages/wire*
/yum/cdrom/Packages/wireshark-1.10.14-16.el7.i686.rpm
/yum/cdrom/Packages/wireshark-1.10.14-16.el7.x86_64.rpm
/yum/cdrom/Packages/wireshark-devel-1.10.14-16.el7.i686.rpm
/yum/cdrom/Packages/wireshark-devel-1.10.14-16.el7.x86_64.rpm
/yum/cdrom/Packages/wireshark-gnome-1.10.14-16.el7.x86_64.rpm

2、备份归档本地repo

2.1 备份本地repo

[root@LeeMumu ~]# cd /etc/yum.repos.d/
[root@LeeMumu yum.repos.d]# ll
total 40
-rw-r--r--. 1 root root 1664 Nov 23  2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23  2018 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Nov 23  2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Nov 23  2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Nov 23  2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23  2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 5701 Nov 23  2018 CentOS-Vault.repo
-rw-r--r--. 1 root root  951 Oct  2  2017 epel.repo
-rw-r--r--. 1 root root 1050 Oct  2  2017 epel-testing.repo
[root@LeeMumu yum.repos.d]# tar -Jcf repo.tar.xz CentOS*
[root@LeeMumu yum.repos.d]# ll
total 44
-rw-r--r--. 1 root root 1664 Nov 23  2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23  2018 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Nov 23  2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Nov 23  2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Nov 23  2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23  2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 5701 Nov 23  2018 CentOS-Vault.repo
-rw-r--r--. 1 root root  951 Oct  2  2017 epel.repo
-rw-r--r--. 1 root root 1050 Oct  2  2017 epel-testing.repo
-rw-r--r--. 1 root root 2208 Jul 27 23:56 repo.tar.xz

2.2 删除无用文件

[root@LeeMumu yum.repos.d]# rm -f CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo 
[root@LeeMumu yum.repos.d]# rm -rf epel.repo CentOS-Sources.repo CentOS-Vault.repo
[root@LeeMumu yum.repos.d]# rm -rf epel-testing.repo CentOS-fasttrack.repo 
[root@LeeMumu yum.repos.d]# ll
total 16
-rw-r--r--. 1 root root  630 Nov 23  2018 CentOS-Media.repo
-rw-r--r--. 1 root root 2208 Jul 27 23:56 repo.tar.xz

3、配置本地 repo

[root@LeeMumu yum.repos.d]# vi CentOS-Media.repo      # 编辑 Media.repo 文件
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///yum/cdrom/                            # 配置 repo 路径
gpgcheck=0                                            # 关闭 gpg 校验 
enabled=1                                             # 启用此 repo
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

4、制作 cache

清理本地缓存:

[root@LeeMumu yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: c7-media epel
Cleaning up list of fastest mirrors
Other repos take up 78 M of disk space (use --verbose for details)

构建缓存:

[root@LeeMumu yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
c7-media                                            | 3.6 kB  00:00:00     
(1/4): c7-media/group_gz                            | 166 kB  00:00:00     
(2/4): c7-media/primary_db                          | 6.0 MB  00:00:00     
(3/4): c7-media/filelists_db                        | 7.1 MB  00:00:00     
(4/4): c7-media/other_db                            | 2.6 MB  00:00:00     
Metadata Cache Created

5、本地 YUM 源制作完成,可以进行程序安装

至此,本地 YUM 源制作完成,可是安装相应程序。

5.1 查看本地的 YUM 源

[root@LeeMumu yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                 repo name                   status
c7-media                CentOS-7 - Media            10,019
repolist: 10,019

5.2 安装 wget

[root@LeeMumu yum.repos.d]# rpm -ql wget         # 确保 wget 没有安装
package wget is not installed
[root@LeeMumu yum.repos.d]# yum install wget     # 安装 wget
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
Install  1 Package
Total download size: 547 k
Installed size: 2.0 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installed:
  wget.x86_64 0:1.14-18.el7                

Complete!
[root@LeeMumu yum.repos.d]# rpm -ql wget       # 查看安装 wget 生成的文件
/etc/wgetrc
/usr/bin/wget
/usr/share/doc/wget-1.14
/usr/share/doc/wget-1.14/AUTHORS
... ...
/usr/share/locale/zh_CN/LC_MESSAGES/wget.mo
/usr/share/locale/zh_TW/LC_MESSAGES/wget.mo
/usr/share/man/man1/wget.1.gz

5.3 安装 ftp

[root@LeeMumu yum.repos.d]# rpm -ql ftp
package ftp is not installed
[root@LeeMumu yum.repos.d]# yum install ftp -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
Total download size: 61 k
Installed size: 96 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction

Installed:
  ftp.x86_64 0:0.17-67.el7                                                            

Complete!

5.4 安装 tcpdump

[root@LeeMumu yum.repos.d]# rpm -ql tcpdump
package tcpdump is not installed
[root@LeeMumu yum.repos.d]# yum install tcpdump -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tcpdump.x86_64 14:4.9.2-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved
Install  1 Package
Total download size: 421 k
Installed size: 1.0 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction

Installed:
  tcpdump.x86_64 14:4.9.2-3.el7                              

Complete!

二:自定义 YUM 源(由本地 rpm 包组成 YUM 源)

1、安装程序 createrepo(用于生成 repodata 目录)

[root@LeeMumu ~]# yum install createrepo -y

2、准备 rpm 包

[root@LeeMumu ~]# mkdir -p /yum/myself/packages
[root@LeeMumu ~]# cp /yum/myself/tree-1.6.0-10.el7.x86_64.rpm /yum/myself/packages/
[root@LeeMumu ~]# ls /yum/myself/packages/
tree-1.6.0-10.el7.x86_64.rpm

3、备份归档本地 repo

[root@LeeMumu ~]# cd /etc/yum.repos.d/
[root@LeeMumu yum.repos.d]# ll
total 40
-rw-r--r--. 1 root root 1664 Nov 23  2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23  2018 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Nov 23  2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Nov 23  2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Nov 23  2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23  2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 5701 Nov 23  2018 CentOS-Vault.repo
-rw-r--r--. 1 root root 1395 Jun 22 13:42 history.txt
-rw-r--r--. 1 root root   80 Jun 22 13:38 local.repo
[root@LeeMumu yum.repos.d]# tar -Jcf repo.tar.xz CentOS*
[root@LeeMumu yum.repos.d]# 
[root@LeeMumu yum.repos.d]# rm CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Sources.repo CentOS-Vault.repo 
rm: remove regular file ‘CentOS-Base.repo’? Y
rm: remove regular file ‘CentOS-CR.repo’? Y
rm: remove regular file ‘CentOS-Debuginfo.repo’? Y
rm: remove regular file ‘CentOS-fasttrack.repo’? Y
rm: remove regular file ‘CentOS-Sources.repo’? Y
rm: remove regular file ‘CentOS-Vault.repo’? Y
[root@LeeMumu yum.repos.d]# ll
total 16
-rw-r--r--. 1 root root  630 Nov 23  2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1395 Jun 22 13:42 history.txt
-rw-r--r--. 1 root root   80 Jun 22 13:38 local.repo
-rw-r--r--. 1 root root 2208 Jul 28 01:26 repo.tar.xz

4、制作 repodata

[root@LeeMumu ~]# createrepo -u -d /yum/myself/
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 1 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
[root@LeeMumu ~]# ll /yum/myself/
total 4
drwxr-xr-x. 2 root root   42 Jul 28 01:31 packages
drwxr-xr-x. 2 root root 4096 Jul 28 01:33 repodata

4.1 生成 repodata 目录示例

如果没有 repodata 目录就会提示“无可用的 repodata/repomd.xml 文件”。

[root@Tang ~]# cd /mnt/
[root@Tang mnt]# ll
total 920
-rw-r--r--. 1 root root 307076 Oct 22 12:29 OpenIPMI-2.0.23-2.el7.x86_64.rpm
-rw-r--r--. 1 root root 533624 Oct 22 12:29 OpenIPMI-libs-2.0.23-2.el7.x86_64.rpm
-rw-r--r--. 1 root root  15900 Oct 22 12:29 OpenIPMI-modalias-2.0.23-2.el7.x86_64.rpm
-rw-r--r--. 1 root root  81104 Oct 22 12:29 watchdog-5.13-12.el7.x86_64(1).rpm
[root@Tang mnt]# createrepo .
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 1 pkgs
Spawning worker 2 with 1 pkgs
Spawning worker 3 with 1 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@Tang mnt]# ll
total 924
-rw-r--r--. 1 root root 307076 Oct 22 12:29 OpenIPMI-2.0.23-2.el7.x86_64.rpm
-rw-r--r--. 1 root root 533624 Oct 22 12:29 OpenIPMI-libs-2.0.23-2.el7.x86_64.rpm
-rw-r--r--. 1 root root  15900 Oct 22 12:29 OpenIPMI-modalias-2.0.23-2.el7.x86_64.rpm
drwxr-xr-x. 2 root root   4096 Oct 22 12:30 repodata
-rw-r--r--. 1 root root  81104 Oct 22 12:29 watchdog-5.13-12.el7.x86_64(1).rpm
[root@Tang mnt]# cd repodata/
[root@Tang repodata]# ll
total 32
-rw-r--r--. 1 root root 3135 Oct 22 12:30 20df65df3d1107d3dfa028e683631c99a7bdcc231387d9a2f516e6d7d12f7c79-other.sqlite.bz2
-rw-r--r--. 1 root root 2255 Oct 22 12:30 73f602c6329def38e99ba105796ce22fb4ac7dd96cd3f6fc1e08c5a172b66c23-filelists.sqlite.bz2
-rw-r--r--. 1 root root 2283 Oct 22 12:30 99db7fd288ed46bb8aaacbc39777e206f4c1b99ef7d8a5b59b9a20a7893478df-primary.xml.gz
-rw-r--r--. 1 root root 5326 Oct 22 12:30 f1fc55415663dfc2e0003824ad65e209076d7d87520ab60b97dc01cd3da36cbb-primary.sqlite.bz2
-rw-r--r--. 1 root root 1093 Oct 22 12:30 f3af4905051ad4d92b8be69f4f368b91ad59016ec8e2ade8f541e6176964a634-filelists.xml.gz
-rw-r--r--. 1 root root 1724 Oct 22 12:30 fabb868e2fbc0b3a03d21b26f17d5033b38bfd641a1e0d02262cf295f1213c01-other.xml.gz
-rw-r--r--. 1 root root 2973 Oct 22 12:30 repomd.xml

5、自定义 repo

[root@LeeMumu ~]# vi /etc/yum.repos.d/CentOS-Media.repo 
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///yum/myself/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

6、清除缓存

[root@LeeMumu ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: c7-media
Cleaning up list of fastest mirrors
Other repos take up 80 M of disk space (use --verbose for details)

7、构建缓存

[root@LeeMumu ~]# yum makecache
Loaded plugins: fastestmirror
Determining fastest mirrors
c7-media                                            | 3.0 kB  00:00:00     
(1/3): c7-media/filelists_db                        | 1.0 kB  00:00:00     
(2/3): c7-media/other_db                            | 1.6 kB  00:00:00     
(3/3): c7-media/primary_db                          | 2.1 kB  00:00:00     
Metadata Cache Created

8、查看 yum repolist

[root@LeeMumu yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                  repo name                     status
c7-media                 CentOS-7 - Media              2
repolist: 2

9、安装程序

root@LeeMumu ~]# yum install tree -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution

三:制作局域网 YUM 源

在 YUM 源主机上部署 YUM 源,并下载安装 vsftpd 程序,配合使用局域网 YUM 源。

1、YUM 源主机部署 vsftpd

1.1 安装程序 vsftpd

[root@LeeMumu Packages]# yum install vsftptd -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
No package vsftptd available.
Error: Nothing to do
[root@LeeMumu Packages]# yum install vsftpd -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-25.el7 will be installed
--> Finished Dependency Resolution
Installed:
  vsftpd.x86_64 0:3.0.2-25.el7                                                      
Complete!

1.2 配置 vsftpd

[root@LeeMumu Packages]# vi /etc/vsftpd/vsftpd.conf
anon_root=/yum/cdrom/

1.3 关闭 selinux

临时修改:
# setenforce 0

永久修改(此方法需要重启):
# vi /etc/selinux/config
SELINUX=disabled      

1.4 启动 vsftpd

[root@LeeMumu yum]# systemctl start vsftpd
[root@LeeMumu yum]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
[root@LeeMumu 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 Sun 2019-07-28 02:30:20 EDT; 27s ago
 Main PID: 7827 (vsftpd)
   CGroup: /system.slice/vsftpd.service
           └─7827 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

Jul 28 02:30:20 LeeMumu systemd[1]: Starting Vsftpd ftp daemon...
Jul 28 02:30:20 LeeMumu systemd[1]: Started Vsftpd ftp daemon.

2、局域网其它主机配置 repo

2.1 修改 repo 文件

[root@LeeMumu ~]# vi /etc/yum.repos.d/CentOS-Media.repo
[c7-media]
name=CentOS-$releasever - Media
baseurl=ftp://192.168.1.10/yum/cdrom                       # yum 源主机地址
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

2.2 清除缓存和构建缓存

# yum clean all
# yum makecache

3、进行程序安装

# yum install wget -y 

你可能感兴趣的:(Linux学习笔记)