Linux學習筆記之在 CentOS 裏下載 RPM 包及其全部依賴包

 

0x00 概述

前幾天我嘗試去建立一個僅包含咱們常常在 CentOS 7 下使用的軟件的本地倉庫。固然,咱們可使用 curl 或者 wget 下載任何軟件包,然而這些命令並不能下載要求的依賴軟件包。你必須去花一些時間並且手動的去尋找和下載被安裝的軟件所依賴的軟件包。然而,咱們並非必須這樣。在這個簡短的教程中,我將會帶領你以兩種方式下載軟件包及其全部依賴包。我已經在 CentOS 7 下進行了測試,不過這些相同的步驟或許在其餘基於 RPM 管理系統的發行版上也能夠工做,例如 RHEL,Fedora 和 Scientific Linux。html

 

0x01 利用 Downloadonly 插件下載 RPM 軟件包及其全部依賴包

咱們能夠經過 yum 命令的 Downloadonly 插件下載 RPM 軟件包及其全部依賴包。
爲了安裝 Downloadonly 插件,以 root 身份運行如下命令。linux

yum install yum-plugin-downloadonly

如今,運行如下命令去下載一個 RPM 軟件包。centos

yum install --downloadonly <package-name>
# 默認狀況下,這個命令將會下載並把軟件包保存到 /var/cache/yum/ 的 rhel-{arch}-channel/packageslocation 目錄,不過,你也能夠下載和保存軟件包到任何位置,你能夠經過 –downloaddir 選項來指定。
yum install --downloadonly --downloaddir=<directory> <package-name>

例子:curl

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

終端輸出:工具

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * epel: epel.mirror.angkasa.id
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package Arch Version Repository Size
=======================================================================================================================================
Installing:
 httpd x86_64 2.4.6-40.el7.centos.4 updates 2.7 M
Installing for dependencies:
 apr x86_64 1.4.8-3.el7 base 103 k
 apr-util x86_64 1.5.2-6.el7 base 92 k
 httpd-tools x86_64 2.4.6-40.el7.centos.4 updates 83 k
 mailcap noarch 2.1.41-2.el7 base 31 k

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

Total download size: 3.0 M
Installed size: 10 M
Background downloading packages, then exiting:
(1/5): apr-1.4.8-3.el7.x86_64.rpm | 103 kB 00:00:01 
(2/5): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:01 
(3/5): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:01 
(4/5): httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm | 83 kB 00:00:01 
(5/5): httpd-2.4.6-40.el7.centos.4.x86_64.rpm | 2.7 MB 00:00:09 
---------------------------------------------------------------------------------------------------------------------------------------
Total 331 kB/s | 3.0 MB 00:00:09 
exiting because "Download Only" specified

 如今去你指定的目錄位置下,你將會看到那裏有下載好的軟件包和依賴的軟件。在我這種狀況下,我已經把軟件包下載到 /root/mypackages/ 目錄下。讓咱們來查看一下內容:測試

ls /root/mypackages/

樣本輸出:url

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

正如你在上面輸出所看到的, httpd軟件包已經被依據全部依賴性下載完成了 。
請注意,這個插件適用於 yum install/yum update, 可是並不適用於 yum groupinstall 。默認狀況下,這個插件將會下載倉庫中最新可用的軟件包。然而你能夠經過指定版本號來下載某個特定的軟件版本。spa

 例子:.net

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

此外,你也能夠以下一次性下載多個包:插件

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

 

0x02 使用 Yumdownloader 工具來下載 RPM 軟件包及其全部依賴包

Yumdownloader是一款簡單,可是卻十分有用的命令行工具,它能夠一次性下載任何 RPM 軟件包及其全部依賴包。
以 root 身份運行以下命令安裝 Yumdownloader 工具。

yum install yum-utils

一旦安裝完成,運行以下命令去下載一個軟件包,例如 httpd。

yumdownloader httpd

爲了根據全部依賴性下載軟件包,咱們使用 --resolve參數:

yumdownloader --resolve httpd

默認狀況下,Yumdownloader 將會下載軟件包到當前工做目錄下。
爲了將軟件下載到一個特定的目錄下,咱們使用 --destdir 參數:

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

或者

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

終端輸出:

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

讓咱們確認一下軟件包是否被下載到咱們指定的目錄下。

ls /root/mypackages/

終端輸出:

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

不像 Downloadonly 插件,Yumdownload 能夠下載一組相關的軟件包。

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

在我看來,我喜歡 Yumdownloader 更勝於 Yum 的 Downloadonly 插件。可是,二者都是十分簡單易懂並且能夠完成相同的工做。

 

參考

相關文章
相關標籤/搜索