在我们安装命令或软件时想要离线安装,但是我们会发现辛辛苦苦在网上找的rpm包安装总是会报依赖不存在问题。

怎么解决呢,难道一个一个的去把依赖包下完吗?甚至你下载的依赖包还依赖了其他的rpm包,然后进行套娃操作。

重点来了!!!

我们知道yum是可以自动下载rpm并进行安装的。当然yum是需要网络支撑的。那有什么办法能够去解决问题吗?

在windows或android上你没有某个软件是否会让你的朋友把安装包发给你呢?在Linux我们也可以用这样的思路来解决问题。

所以前提是我们要有:

  • 一台能够上网的Linux系统;(云服务器,虚拟机)

现在上网的机器就是我们的朋友。我们可以通过(这里我们用vim举例)不建议使用

yum install --downloaddir=/home/vim_rpm --downloadonly vim

以上命令来只下载依赖而不安装。

--downloaddir:指定下载目录

--downloadonly:只下载不安装

执行之后我们会发现我们的/home/vim_rpm下有很多的rpm包,这些包即为vim所需的所有依赖。

有些同学会发现我这里没有任何东西呀!这种情况是会发生的,当我们的系统已经有vim命令yum是不会下载的。细心的同学会发现执行命令时提示了:vim already exists,nothing to do等,这也是不建议使用这种方式的原因

那该如何解决呢?难道要把vim删了?

如果通过删除这种方式的话我们不一定能够得到所有我们需要的rpm。如果有一个依赖没删掉那么就不会下载这个依赖。我们安装时仍然会缺少依赖

所以这里我们会使用Docker来解决这个问题。

首先为了你找到的朋友使用的系统和你一样。我们通常会先看看没网的机器系统是什么版本(centos举例):

[root@VM-12-16-centos ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)

得到我们机器的版本后我们需要在docker上也装上一个系统。

[root@VM-12-16-centos ~]# docker pull centos:7
Trying to pull repository docker.io/library/centos ... 
7: Pulling from docker.io/library/centos
Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
Status: Image is up to date for docker.io/centos:7


[root@VM-12-16-centos ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    7                   eeb6ee3f44bd        15 months ago       204 MB

运行这个镜像:

[root@VM-12-16-centos ~]# docker run -d --rm --name centos_download eeb6ee3f44bd tail -f /dev/null
a328f1c9a0c3a0723fa216eb2afe0c279b3a79506fe63c19840a1b74b6a8ae26
[root@VM-12-16-centos ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS               NAMES
a328f1c9a0c3        eeb6ee3f44bd        "tail -f /dev/null"   3 seconds ago       Up 3 seconds                            centos_download

现在我们可以通过镜像下载我们所需要的rpm了:

#
#第一步
#
#进入容器下载我们想要的依赖
#
#
#
[root@VM-12-16-centos ~]# docker exec -it centos_download yum install --downloaddir=/home/vim_rpm --downloadonly vim
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.ustc.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
base                                                                                                                                                           | 3.6 kB  00:00:00     
extras                                                                                                                                                         | 2.9 kB  00:00:00     
updates                                                                                                                                                        | 2.9 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                                                                                                                              | 249 kB  00:00:00     
(2/4): base/7/x86_64/group_gz                                                                                                                                  | 153 kB  00:00:00     
(3/4): base/7/x86_64/primary_db                                                                                                                                | 6.1 MB  00:00:00     
(4/4): updates/7/x86_64/primary_db                                                                                                                             |  19 MB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed
--> Processing Dependency: vim-common = 2:7.4.629-8.el7_9 for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: which for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: perl(:MODULE_COMPAT_5.16.3) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libperl.so()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Running transaction check
---> Package gpm-libs.x86_64 0:1.20.7-6.el7 will be installed
---> Package perl.x86_64 4:5.16.3-299.el7_9 will be installed
--> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl-macros for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(threads::shared) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(threads) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(constant) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Time::Local) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Time::HiRes) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Storable) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Socket) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Scalar::Util) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Pod::Simple::XHTML) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Pod::Simple::Search) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Getopt::Long) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Filter::Util::Call) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Temp) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec::Unix) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec::Functions) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Path) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Exporter) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Cwd) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Carp) for package: 4:perl-5.16.3-299.el7_9.x86_64
---> Package perl-libs.x86_64 4:5.16.3-299.el7_9 will be installed
---> Package vim-common.x86_64 2:7.4.629-8.el7_9 will be installed
--> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.629-8.el7_9.x86_64
---> Package which.x86_64 0:2.20-7.el7 will be installed
--> Running transaction check
---> Package perl-Carp.noarch 0:1.26-244.el7 will be installed
---> Package perl-Exporter.noarch 0:5.68-3.el7 will be installed
---> Package perl-File-Path.noarch 0:2.09-2.el7 will be installed
---> Package perl-File-Temp.noarch 0:0.23.01-3.el7 will be installed
---> Package perl-Filter.x86_64 0:1.49-3.el7 will be installed
---> Package perl-Getopt-Long.noarch 0:2.40-3.el7 will be installed
--> Processing Dependency: perl(Pod::Usage) >= 1.14 for package: perl-Getopt-Long-2.40-3.el7.noarch
--> Processing Dependency: perl(Text::ParseWords) for package: perl-Getopt-Long-2.40-3.el7.noarch
---> Package perl-PathTools.x86_64 0:3.40-5.el7 will be installed
---> Package perl-Pod-Simple.noarch 1:3.28-4.el7 will be installed
--> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
--> Processing Dependency: perl(Encode) for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
---> Package perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 will be installed
---> Package perl-Socket.x86_64 0:2.010-5.el7 will be installed
---> Package perl-Storable.x86_64 0:2.45-3.el7 will be installed
---> Package perl-Time-HiRes.x86_64 4:1.9725-3.el7 will be installed
---> Package perl-Time-Local.noarch 0:1.2300-2.el7 will be installed
---> Package perl-constant.noarch 0:1.27-2.el7 will be installed
---> Package perl-macros.x86_64 4:5.16.3-299.el7_9 will be installed
---> Package perl-threads.x86_64 0:1.87-4.el7 will be installed
---> Package perl-threads-shared.x86_64 0:1.43-6.el7 will be installed
---> Package vim-filesystem.x86_64 2:7.4.629-8.el7_9 will be installed
--> Running transaction check
---> Package perl-Encode.x86_64 0:2.51-7.el7 will be installed
---> Package perl-Pod-Escapes.noarch 1:1.04-299.el7_9 will be installed
---> Package perl-Pod-Usage.noarch 0:1.63-3.el7 will be installed
--> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-3.el7.noarch
--> Processing Dependency: perl-Pod-Perldoc for package: perl-Pod-Usage-1.63-3.el7.noarch
---> Package perl-Text-ParseWords.noarch 0:3.29-4.el7 will be installed
--> Running transaction check
---> Package perl-Pod-Perldoc.noarch 0:3.20-4.el7 will be installed
--> Processing Dependency: perl(parent) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: perl(HTTP::Tiny) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: groff-base for package: perl-Pod-Perldoc-3.20-4.el7.noarch
---> Package perl-podlators.noarch 0:2.5.1-3.el7 will be installed
--> Running transaction check
---> Package groff-base.x86_64 0:1.22.2-8.el7 will be installed
---> Package perl-HTTP-Tiny.noarch 0:0.033-3.el7 will be installed
---> Package perl-parent.noarch 1:0.225-244.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================================================================
 Package                                             Arch                                Version                                           Repository                            Size
======================================================================================================================================================================================
Installing:
 vim-enhanced                                        x86_64                              2:7.4.629-8.el7_9                                 updates                              1.1 M
Installing for dependencies:
 gpm-libs                                            x86_64                              1.20.7-6.el7                                      base                                  32 k
 groff-base                                          x86_64                              1.22.2-8.el7                                      base                                 942 k
 perl                                                x86_64                              4:5.16.3-299.el7_9                                updates                              8.0 M
 perl-Carp                                           noarch                              1.26-244.el7                                      base                                  19 k
 perl-Encode                                         x86_64                              2.51-7.el7                                        base                                 1.5 M
 perl-Exporter                                       noarch                              5.68-3.el7                                        base                                  28 k
 perl-File-Path                                      noarch                              2.09-2.el7                                        base                                  26 k
 perl-File-Temp                                      noarch                              0.23.01-3.el7                                     base                                  56 k
 perl-Filter                                         x86_64                              1.49-3.el7                                        base                                  76 k
 perl-Getopt-Long                                    noarch                              2.40-3.el7                                        base                                  56 k
 perl-HTTP-Tiny                                      noarch                              0.033-3.el7                                       base                                  38 k
 perl-PathTools                                      x86_64                              3.40-5.el7                                        base                                  82 k
 perl-Pod-Escapes                                    noarch                              1:1.04-299.el7_9                                  updates                               52 k
 perl-Pod-Perldoc                                    noarch                              3.20-4.el7                                        base                                  87 k
 perl-Pod-Simple                                     noarch                              1:3.28-4.el7                                      base                                 216 k
 perl-Pod-Usage                                      noarch                              1.63-3.el7                                        base                                  27 k
 perl-Scalar-List-Utils                              x86_64                              1.27-248.el7                                      base                                  36 k
 perl-Socket                                         x86_64                              2.010-5.el7                                       base                                  49 k
 perl-Storable                                       x86_64                              2.45-3.el7                                        base                                  77 k
 perl-Text-ParseWords                                noarch                              3.29-4.el7                                        base                                  14 k
 perl-Time-HiRes                                     x86_64                              4:1.9725-3.el7                                    base                                  45 k
 perl-Time-Local                                     noarch                              1.2300-2.el7                                      base                                  24 k
 perl-constant                                       noarch                              1.27-2.el7                                        base                                  19 k
 perl-libs                                           x86_64                              4:5.16.3-299.el7_9                                updates                              690 k
 perl-macros                                         x86_64                              4:5.16.3-299.el7_9                                updates                               44 k
 perl-parent                                         noarch                              1:0.225-244.el7                                   base                                  12 k
 perl-podlators                                      noarch                              2.5.1-3.el7                                       base                                 112 k
 perl-threads                                        x86_64                              1.87-4.el7                                        base                                  49 k
 perl-threads-shared                                 x86_64                              1.43-6.el7                                        base                                  39 k
 vim-common                                          x86_64                              2:7.4.629-8.el7_9                                 updates                              5.9 M
 vim-filesystem                                      x86_64                              2:7.4.629-8.el7_9                                 updates                               11 k
 which                                               x86_64                              2.20-7.el7                                        base                                  41 k

Transaction Summary
======================================================================================================================================================================================
Install  1 Package (+32 Dependent packages)

Total download size: 19 M
Installed size: 63 M
Background downloading packages, then exiting:
warning: /home/vim_rpm/gpm-libs-1.20.7-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for gpm-libs-1.20.7-6.el7.x86_64.rpm is not installed
(1/33): gpm-libs-1.20.7-6.el7.x86_64.rpm                                                                                                                       |  32 kB  00:00:00     
(2/33): perl-Carp-1.26-244.el7.noarch.rpm                                                                                                                      |  19 kB  00:00:00     
(3/33): groff-base-1.22.2-8.el7.x86_64.rpm                                                                                                                     | 942 kB  00:00:00     
(4/33): perl-Exporter-5.68-3.el7.noarch.rpm                                                                                                                    |  28 kB  00:00:00     
(5/33): perl-File-Path-2.09-2.el7.noarch.rpm                                                                                                                   |  26 kB  00:00:00     
Public key for perl-5.16.3-299.el7_9.x86_64.rpm is not installed
(6/33): perl-5.16.3-299.el7_9.x86_64.rpm                                                                                                                       | 8.0 MB  00:00:00     
(7/33): perl-Encode-2.51-7.el7.x86_64.rpm                                                                                                                      | 1.5 MB  00:00:00     
(8/33): perl-File-Temp-0.23.01-3.el7.noarch.rpm                                                                                                                |  56 kB  00:00:00     
(9/33): perl-Getopt-Long-2.40-3.el7.noarch.rpm                                                                                                                 |  56 kB  00:00:00     
(10/33): perl-Filter-1.49-3.el7.x86_64.rpm                                                                                                                     |  76 kB  00:00:00     
(11/33): perl-HTTP-Tiny-0.033-3.el7.noarch.rpm                                                                                                                 |  38 kB  00:00:00     
(12/33): perl-Pod-Perldoc-3.20-4.el7.noarch.rpm                                                                                                                |  87 kB  00:00:00     
(13/33): perl-Pod-Simple-3.28-4.el7.noarch.rpm                                                                                                                 | 216 kB  00:00:00     
(14/33): perl-Pod-Usage-1.63-3.el7.noarch.rpm                                                                                                                  |  27 kB  00:00:00     
(15/33): perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm                                                                                                        |  36 kB  00:00:00     
(16/33): perl-Socket-2.010-5.el7.x86_64.rpm                                                                                                                    |  49 kB  00:00:00     
(17/33): perl-Storable-2.45-3.el7.x86_64.rpm                                                                                                                   |  77 kB  00:00:00     
(18/33): perl-Text-ParseWords-3.29-4.el7.noarch.rpm                                                                                                            |  14 kB  00:00:00     
(19/33): perl-Time-HiRes-1.9725-3.el7.x86_64.rpm                                                                                                               |  45 kB  00:00:00     
(20/33): perl-Time-Local-1.2300-2.el7.noarch.rpm                                                                                                               |  24 kB  00:00:00     
(21/33): perl-constant-1.27-2.el7.noarch.rpm                                                                                                                   |  19 kB  00:00:00     
(22/33): perl-parent-0.225-244.el7.noarch.rpm                                                                                                                  |  12 kB  00:00:00     
(23/33): perl-podlators-2.5.1-3.el7.noarch.rpm                                                                                                                 | 112 kB  00:00:00     
(24/33): perl-threads-1.87-4.el7.x86_64.rpm                                                                                                                    |  49 kB  00:00:00     
(25/33): perl-threads-shared-1.43-6.el7.x86_64.rpm                                                                                                             |  39 kB  00:00:00     
(26/33): perl-macros-5.16.3-299.el7_9.x86_64.rpm                                                                                                               |  44 kB  00:00:01     
(27/33): perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm                                                                                                            |  52 kB  00:00:01     
(28/33): perl-PathTools-3.40-5.el7.x86_64.rpm                                                                                                                  |  82 kB  00:00:01     
(29/33): perl-libs-5.16.3-299.el7_9.x86_64.rpm                                                                                                                 | 690 kB  00:00:01     
(30/33): vim-enhanced-7.4.629-8.el7_9.x86_64.rpm                                                                                                               | 1.1 MB  00:00:00     
(31/33): vim-filesystem-7.4.629-8.el7_9.x86_64.rpm                                                                                                             |  11 kB  00:00:00     
(32/33): vim-common-7.4.629-8.el7_9.x86_64.rpm                                                                                                                 | 5.9 MB  00:00:00     
(33/33): which-2.20-7.el7.x86_64.rpm                                                                                                                           |  41 kB  00:00:00     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                 9.7 MB/s |  19 MB  00:00:01     
exiting because "Download Only" specified
#
#第二步
#
#将docker容器的资源复制到服务器
#
#
#
#
docker cp centos_download:/home/vim_rpm 服务器路径

[root@VM-12-16-centos home]# docker cp centos_download:/home/vim_rpm /home/vim_rpm
[root@VM-12-16-centos home]# ls
vim_rpm
[root@VM-12-16-centos home]# cd vim_rpm
[root@VM-12-16-centos vim_rpm]# ls
gpm-libs-1.20.7-6.el7.x86_64.rpm         perl-Filter-1.49-3.el7.x86_64.rpm           perl-Pod-Perldoc-3.20-4.el7.noarch.rpm          perl-Time-HiRes-1.9725-3.el7.x86_64.rpm
groff-base-1.22.2-8.el7.x86_64.rpm       perl-Getopt-Long-2.40-3.el7.noarch.rpm      perl-Pod-Simple-3.28-4.el7.noarch.rpm           perl-Time-Local-1.2300-2.el7.noarch.rpm
perl-5.16.3-299.el7_9.x86_64.rpm         perl-HTTP-Tiny-0.033-3.el7.noarch.rpm       perl-Pod-Usage-1.63-3.el7.noarch.rpm            vim-common-7.4.629-8.el7_9.x86_64.rpm
perl-Carp-1.26-244.el7.noarch.rpm        perl-libs-5.16.3-299.el7_9.x86_64.rpm       perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm  vim-enhanced-7.4.629-8.el7_9.x86_64.rpm
perl-constant-1.27-2.el7.noarch.rpm      perl-macros-5.16.3-299.el7_9.x86_64.rpm     perl-Socket-2.010-5.el7.x86_64.rpm              vim-filesystem-7.4.629-8.el7_9.x86_64.rpm
perl-Encode-2.51-7.el7.x86_64.rpm        perl-parent-0.225-244.el7.noarch.rpm        perl-Storable-2.45-3.el7.x86_64.rpm             which-2.20-7.el7.x86_64.rpm
perl-Exporter-5.68-3.el7.noarch.rpm      perl-PathTools-3.40-5.el7.x86_64.rpm        perl-Text-ParseWords-3.29-4.el7.noarch.rpm
perl-File-Path-2.09-2.el7.noarch.rpm     perl-Pod-Escapes-1.04-299.el7_9.noarch.rpm  perl-threads-1.87-4.el7.x86_64.rpm
perl-File-Temp-0.23.01-3.el7.noarch.rpm  perl-podlators-2.5.1-3.el7.noarch.rpm       perl-threads-shared-1.43-6.el7.x86_64.rpm

做完上面的事情我们便可以将vim_rpm文件夹上传到我们不能上网的服务器上。

然后进入vim_rpm文件夹下安装即可:

yum install -y *.rpm

本方式适用于所有能通过yum源安装的命令与软件