方法1 利用 Downloadonly 插件下载 RPM 软件包及其所有依赖包
    我们可以通过 yum 命令的 Downloadonly 插件下载 RPM 软件包及其所有依赖包。
    为了安装 Downloadonly 插件,以 root 身份运行以下命令。

    1. yum install yum-plugin-downloadonly

    现在,运行以下命令去下载一个 RPM 软件包。

    1. yum install --downloadonly <package-name>

    默认情况下,这个命令将会下载并把软件包保存到 /var/cache/yum/ 的 rhel-{arch}-channel/packageslocation 目录,不过,你也可以下载和保存软件包到任何位置,你可以通过 –downloaddir 选项来指定。

    1. yum install --downloadonly --downloaddir=<directory> <package-name>

    例子:

    1. yum install --downloadonly --downloaddir=/root/mypackages/ httpd

    终端输出:

    1. Loaded plugins: fastestmirror
    2. Loading mirror speeds from cached hostfile
    3. * base: centos.excellmedia.net
    4. * epel: epel.mirror.angkasa.id
    5. * extras: centos.excellmedia.net
    6. * updates: centos.excellmedia.net
    7. Resolving Dependencies
    8. --> Running transaction check
    9. ---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
    10. --> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64
    11. --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64
    12. --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
    13. --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
    14. --> Running transaction check
    15. ---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
    16. ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
    17. ---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed
    18. ---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
    19. --> Finished Dependency Resolution
    20. Dependencies Resolved
    21. =======================================================================================================================================
    22. Package Arch Version Repository Size
    23. =======================================================================================================================================
    24. Installing:
    25. httpd x86_64 2.4.6-40.el7.centos.4 updates 2.7 M
    26. Installing for dependencies:
    27. apr x86_64 1.4.8-3.el7 base 103 k
    28. apr-util x86_64 1.5.2-6.el7 base 92 k
    29. httpd-tools x86_64 2.4.6-40.el7.centos.4 updates 83 k
    30. mailcap noarch 2.1.41-2.el7 base 31 k
    31. Transaction Summary
    32. =======================================================================================================================================
    33. Install 1 Package (+4 Dependent packages)
    34. Total download size: 3.0 M
    35. Installed size: 10 M
    36. Background downloading packages, then exiting:
    37. (1/5): apr-1.4.8-3.el7.x86_64.rpm | 103 kB 00:00:01
    38. (2/5): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:01
    39. (3/5): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:01
    40. (4/5): httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm | 83 kB 00:00:01
    41. (5/5): httpd-2.4.6-40.el7.centos.4.x86_64.rpm | 2.7 MB 00:00:09
    42. ---------------------------------------------------------------------------------------------------------------------------------------
    43. Total 331 kB/s | 3.0 MB 00:00:09
    44. exiting because "Download Only" specified

    image.png
    现在去你指定的目录位置下,你将会看到那里有下载好的软件包和依赖的软件。在我这种情况下,我已经把软件包下载到 /root/mypackages/ 目录下。让我们来查看一下内容:

    1. ls /root/mypackages/

    样本输出:

    1. apr-1.4.8-3.el7.x86_64.rpm
    2. apr-util-1.5.2-6.el7.x86_64.rpm
    3. httpd-2.4.6-40.el7.centos.4.x86_64.rpm
    4. httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm
    5. mailcap-2.1.41-2.el7.noarch.rpm

    image.png
    正如你在上面输出所看到的, httpd软件包已经被依据所有依赖性下载完成了 。
    请注意,这个插件适用于 yum install/yum update, 但是并不适用于 yum groupinstall 。默认情况下,这个插件将会下载仓库中最新可用的软件包。然而你可以通过指定版本号来下载某个特定的软件版本。
    例子:

    1. yum install --downloadonly --downloaddir=/root/mypackages/ httpd-2.2.6-40.el7

    此外,你也可以如下一次性下载多个包:

    1. yum install --downloadonly --downloaddir=/root/mypackages/ httpd vsftpd

    方法 2使用 Yumdownloader 工具来下载 RPM 软件包及其所有依赖包
    Yumdownloader是一款简单,但是却十分有用的命令行工具,它可以一次性下载任何 RPM 软件包及其所有依赖包。
    以 root 身份运行如下命令安装 Yumdownloader 工具。

    1. yum install yum-utils

    一旦安装完成,运行如下命令去下载一个软件包,例如 httpd。

    1. yumdownloader httpd

    为了根据所有依赖性下载软件包,我们使用 —resolve参数:

    1. yumdownloader --resolve httpd

    默认情况下,Yumdownloader 将会下载软件包到当前工作目录下。
    为了将软件下载到一个特定的目录下,我们使用 —destdir 参数:

    1. yumdownloader --resolve --destdir=/root/mypackages/ httpd

    或者,

    1. yumdownloader --resolve --destdir /root/mypackages/ httpd


    终端输出:

    1. Loaded plugins: fastestmirror
    2. Loading mirror speeds from cached hostfile
    3. * base: centos.excellmedia.net
    4. * epel: epel.mirror.angkasa.id
    5. * extras: centos.excellmedia.net
    6. * updates: centos.excellmedia.net
    7. --> Running transaction check
    8. ---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
    9. --> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64
    10. --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64
    11. --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
    12. --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
    13. --> Running transaction check
    14. ---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
    15. ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
    16. ---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed
    17. ---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
    18. --> Finished Dependency Resolution
    19. (1/5): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:01
    20. (2/5): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:02
    21. (3/5): apr-1.4.8-3.el7.x86_64.rpm | 103 kB 00:00:02
    22. (4/5): httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm | 83 kB 00:00:03
    23. (5/5): httpd-2.4.6-40.el7.centos.4.x86_64.rpm | 2.7 MB 00:00:19

    image.png
    让我们确认一下软件包是否被下载到我们指定的目录下。

    1. ls /root/mypackages/

    终端输出:

    1. apr-1.4.8-3.el7.x86_64.rpm
    2. apr-util-1.5.2-6.el7.x86_64.rpm
    3. httpd-2.4.6-40.el7.centos.4.x86_64.rpm
    4. httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm
    5. mailcap-2.1.41-2.el7.noarch.rpm

    image.png
    不像 Downloadonly 插件,Yumdownload 可以下载一组相关的软件包。

    1. yumdownloader "@Development Tools" --resolve --destdir /root/mypackages/

    在我看来,我喜欢 Yumdownloader 更胜于 Yum 的 Downloadonly 插件。但是,两者都是十分简单易懂而且可以完成相同的工作。