如何在Linux中使用yum命令
yum(YellowDog Updater修改)是一种在基于RHEL的系统中提供的实用程序,用于安装,删除和搜索套餐。
它可以做到很多,而不是安装和删除,这就是我将在本教程中展示的。
yum自动安装包依赖性,例如,"yum安装httpd"将自动安装Apache WebServer服务器,并自动安装依赖关系。
通过rpm命令安装时不容易的东西。
使用RPM,我们必须下载所有必需的依赖项,然后相应安装。
yum utility从托管的存储库中获取包信息(通常由OS供应商)。
存储库基本上是RPM的集合,应该在特定的架构上工作。
例如,对于32和64位系统将有一个单独的存储库,并且使用RHEL 5,6或者7或者8.我们可以托管本地存储库并配置yum以搜索,从本地存储库安装包。
在以下示例中,将介绍一些最常用的yum命令。
1)从存储库中搜索包
以下命令搜索特定包的整个存储库:
# yum search httpd Loaded plugins: amazon-id, rhui-lb, security =============================================== N/S Matched: httpd ========== httpd.x86_64 : Apache HTTP Server httpd-devel.i686 : Development interfaces for the Apache HTTP server httpd-devel.x86_64 : Development interfaces for the Apache HTTP server httpd-manual.noarch : Documentation for the Apache HTTP server httpd-tools.x86_64 : Tools for use with the Apache HTTP Server mod_dav_svn.x86_64 : Apache httpd module for Subversion server mod_dnssd.x86_64 : An Apache HTTPD module which adds Zeroconf support
有关更详细的输出,请使用以下命令:
# yum provides httpd Loaded plugins: amazon-id, rhui-lb, security httpd-2.2.15-26.el6.x86_64 : Apache HTTP Server Repo : rhui-REGION-rhel-server-releases Matched from:
yum提供yum包中的*/httpd
查找包含'httpd'的包。
也可以使用以下命令查找有关包的详细信息:
# yum info httpd Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile ... Installed Packages Name : httpd Arch : x86_64 Version : 2.2.15 Release : 69.el6.centos Size : 3.0 M Repo : installed From repo : base Summary : Apache HTTP Server URL : http://httpd.apache.org/ License : ASL 2.0 Description : The Apache HTTP Server is a powerful, efficient, and extensible
2)在包组上运行
我们可以列出计算机上安装的所有包组。
以下命令将显示已安装和可用的包组。
# yum grouplist ... Installed Groups: E-mail server Perl Support Scalable Filesystems Security Tools ... Available Groups: Additional Development Backup Client Backup Server Base CIFS file server .....
如果我们想简要了解组中包含的软件包,则可以使用该命令参数GroupInfo
。
# yum groupinfo "PHP Support" Loaded plugins: fastestmirror Setting up Group Process ... Group: PHP Support Description: PHP web application framework. Mandatory Packages: php Default Packages: php-gd php-pdo php-pear php-xml Optional Packages: php-ldap php-mysql ...
我们可以使用组安装选项安装单个包组。
例如,我们将安装包组PHP支持。
此包组包含所需的PHP包。
# yum groupinstall "PHP Support" Loaded plugins: amazon-id, downloadonly, rhui-lb, security Setting up Group Process Warning: Group Support does not exist. Resolving Dependencies --> Running transaction check ---> Package php.x86_64 0:5.3.3-23.el6_4 will be installed --> Processing Dependency: php-common(x86-64) = 5.3.3-23.el6_4 for package: php-5.3.3-23.el6_4.x86_64 --> Processing Dependency: php-cli(x86-64) = 5.3.3-23.el6_4 for package: php-5.3.3-23.el6_4.x86_64 ---> Package php-gd.x86_64 0:5.3.3-23.el6_4 will be installed --> Processing Dependency: libXpm.so.4()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64 Dependencies Resolved ===================================================================== Package Arch Version Repository Size ===================================================================== Installing: php x86_64 5.3.3-23.el6_4 rhui-REGION-rhel-server-releases 1.1 M ...
我们可以使用以下命令更新组软件包:
]# yum groupupdate "PHP Support" Loaded plugins: fastestmirror Setting up Group Process Loading mirror speeds from cached hostfile ... Resolving Dependencies --> Running transaction check ---> Package php.x86_64 0:5.3.3-49.el6 will be installed --> Processing Dependency: php-common(x86-64) = 5.3.3-49.el6 for package: php-5.3.3-49.el6.x86_64 --> Processing Dependency: php-cli(x86-64) = 5.3.3-49.el6 for package: php-5.3.3-49.el6.x86_64 ...
要删除组包,请使用命令以下:
# yum groupremove "PHP Support" Loaded plugins: fastestmirror Setting up Group Process Loading mirror speeds from cached hostfile ... Resolving Dependencies --> Running transaction check ---> Package php.x86_64 0:5.3.3-49.el6 will be erased ---> Package php-gd.x86_64 0:5.3.3-49.el6 will be erased ...
3)使用yum安装包装
可以使用"yum install"命令"安装包,如下所示:
# yum install httpd Loaded plugins: amazon-id, rhui-lb, security Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.2.15-29.el6_4 will be installed --> Processing Dependency: httpd-tools = 2.2.15-29.el6_4 for package: httpd-2.2.15-29.el6_4.x86_64 --> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-29.el6_4.x86_64 Dependencies Resolved ============================================================================= Package Arch Version Repository Size ============================================================================= Installing: httpd x86_64 2.2.15-29.el6_4 rhui-REGION-rhel-server-releases 821
如我们所能看到yum添加了"httpd"安装的其他包。
这被称为依赖依赖性解析。
如果我们希望yum不提示[y/n]选项。
使用"yum安装-y httpd`
4)重新填写一揽子计划
有时候,我们可以使用系统中已有的软件包面临一些问题。
一种可能性是卸载并重新安装,但它太长了。
你可以询问yum为我们重新安装,它将自动完成
# yum reinstall httpd -y
此过程只需为我们重新安装软件包
5)更新包
当我们想进行更新时,我们可能需要先检查所有可用更新。
这是"校验更新"参数可能的。
如果有一些更新可用性,我们将收到输出,否则。
# yum check-update
我们可以使用"update"命令直接更新服务器上的所有包。
这将更新内核包以及最新版本,这意味着操作系统将被更新到Rhel提供的最新版本。
# yum update
我们可以更新特定包,如下所示:
# yum update httpd Loaded plugins: amazon-id, rhui-lb, security Setting up Update Process No Packages marked for Update
我们可以在使用-x
参数的更新过程中排除一个包
# yum update -x httpd
6)下载包RPM文件而不安装
使用yum从RHN或者CentOS存储库下载RPM包,而无需安装它。
我们必须首先安装yum的插件,只有yum只下载rpm。
公用事业名称是"yum-downloardonly"",可以通过yum安装如下:
# yum install yum-downloadonly Loaded plugins: amazon-id, rhui-lb, security Setting up Install Process Resolving Dependencies --> Running transaction check Dependencies Resolved =========================================================================== Package Arch Version Repository Size =========================================================================== Installing: yum-plugin-downloadonly noarch 1.1.30-14.el6 rhui-REGION-rhel-server-releases 20 k Transaction Summary =========================================================================== Install 1 Package(s)
现在,你可以下载从资源库中包不使用此命令安装它:
# yum install httpd-devel --downloadonly
默认情况下,软件包将被下载到/var/cache/yum/<arch>
目录,但我们可以通过添加另一个选项来将其下载到yum命令
# yum install httpd-devel --downloadonly --downloaddir=/opt
7)执行RPM文件的本地安装
如果我们有一个包的RPM,但我们没有依赖关系,并且我们不知道其中得到它。
我们仍然可以使用yum安装该RPM并从存储库获取所需的依赖项。
让我们安装httpd-devel-2.2.15-29.el6_4.x86_64 rpm,我们刚下载。
# yum localinstall /opt/httpd-devel-2.2.15-29.el6_4.x86_64.rpm Loaded plugins: amazon-id, downloadonly, rhui-lb, security Setting up Local Package Process Examining /opt/httpd-devel-2.2.15-29.el6_4.x86_64.rpm: httpd-devel-2.2.15-29.el6_4.x86_64 Marking /opt/httpd-devel-2.2.15-29.el6_4.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package httpd-devel.x86_64 0:2.2.15-29.el6_4 will be installed --> Processing Dependency: apr-devel for package: httpd-devel-2.2.15-29.el6_4.x86_64 --> Processing Dependency: apr-util-devel for package: httpd-devel-2.2.15-29.el6_4.x86_64 --> Running transaction check ... --> Finished Dependency Resolution Dependencies Resolved ============================================================================= Package Arch Version Repository Size ============================================================================= Installing: httpd-devel x86_64 2.2.15-29.el6_4 /httpd-devel-2.2.15-29.el6_4.x86_64 526 k Installing for dependencies: apr-devel x86_64 1.3.9-5.el6_2 rhui-REGION-rhel-server-releases 176 k ... Transaction Summary ======================================================================= Install 8 Package(s) Upgrade 2 Package(s) Total size: 10 M Is this ok [y/N]: y
8)使用yum去除包
yum删除删除包装。
# yum remove httpd Failed to set locale, defaulting to C Loaded plugins: amazon-id, downloadonly, rhui-lb, security Setting up Remove Process Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.2.15-29.el6_4 will be erased --> Processing Dependency: httpd-mmn = 20051115 for package: php-5.3.3-23.el6_4.x86_64 ... --> Finished Dependency Resolution ... Removing: httpd x86_64 2.2.15-29.el6_4 @rhui-REGION-rhel-server-releases 2.9 M Removing for dependencies: httpd-devel x86_64 2.2.15-29.el6_4 @/httpd-devel-2.2.15-29.el6_4.x86_64 526 k php x86_64 5.3.3-23.el6_4 @rhui-REGION-rhel-server-releases 3.5 M
9)列出所有已安装的软件包
如果要列出所有已安装的包,则可以使用yum列表已安装命令。
这与Grep结合使用或者检查是否已安装特定包。
这类似于查询已安装的软件包,具有"rpm -qa"命令"。
# yum list installed Loaded plugins: amazon-id, downloadonly, rhui-lb, security Installed Packages ConsoleKit.x86_64 0.4.1-3.el6 @koji-override-0/$releasever ConsoleKit-libs.x86_64 0.4.1-3.el6 @koji-override-0/$releasever MAKEDEV.x86_64 3.24-6.el6 @koji-override-0/$releasever PyYAML.x86_64 3.10-3.1.el6 @koji-override-0/$releasever Red_Hat_Enterprise_Linux-Release_Notes-6-en-US.noarch 4-2.el6 @koji-override-0/$releasever ....
10)列出可用的存储库
列出正在查询,安装和更新的可用存储库中的可用存储库。
# yum repolist Loaded plugins: amazon-id, downloadonly, rhui-lb, security repo id repo name status rhui-REGION-client-config-server-6 Red Hat Update Infrastructure 2.0 Client Configuration Server 6 4 rhui-REGION-rhel-server-releases Red Hat Enterprise Linux Server 6 (RPMs) 10994 rhui-REGION-rhel-server-releases-optional Red Hat Enterprise Linux Server 6 Optional (RPMs) 6250 repolist: 17248
我们可以列出已启用和禁用的存储库
# yum repolist all
我们还可以选择从特定存储库安装包
# yum --enablerepo=epel install phpmyadmin Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile epel/metalink | 14 kB ... epel | 4.7 kB 00:00 epel/primary_db | 6.0 MB 00:00 Resolving Dependencies --> Running transaction check ---> Package phpMyAdmin.noarch 0:4.0.10.20-1.el6 will be installed --> Processing Dependency: php-mbstring for package: phpMyAdmin-4.0.10.20-1.el6.noarch --> Processing Dependency: php-mcrypt for package: phpMyAdmin-4.0.10.20-1.el6.noarch --> Processing Dependency: php-mysqli for package: phpMyAdmin-4.0.10.20-1.el6.noarch ...
11)Yum历史
可以看到服务器上的YUM命令所做的所有操作的历史。
# yum history Loaded plugins: fastestmirror ID | Login user | Date and time | Action(s) | Altered ------------------------------------------------------------------------------ 7 | root <root> | 2019-02-20 09:57 | Reinstall | 1 6 | root <root> | 2019-02-20 09:48 | Install | 1 5 | root <root> | 2019-02-20 09:14 | Install | 22 4 | root <root> | 2019-02-20 07:56 | I, U | 103 EE 3 | root <root> | 2016-03-01 20:32 | Install | 15 2 | root <root> | 2016-03-01 20:30 | I, U | 48 1 | System <unset> | 2016-03-01 20:16 | Install | 205 history list
在列表末尾的最新动作中,从最新动作中筛选了结果。
也可以显示一个动作的详细信息。
例如
# yum history info 5 Loaded plugins: fastestmirror Transaction ID : 5 Begin time : Wed Nov 20 09:14:37 2019 Begin rpmdb : 222:02bf9bb9fcdb46cf7494b5b4b01b7df711c6d44d End time : 09:14:39 2019 (2 seconds) End rpmdb : 244:874a3a793190f9951bca4b6de4c2326da6ec5de7 User : root <root> Return-Code : Success Command Line : groupinstall PHP Support .... Packages Altered: Dep-Install apr-1.3.9-5.el6_9.1.x86_64 @base Dep-Install apr-util-1.3.9-3.el6_0.1.x86_64 @base Dep-Install apr-util-ldap-1.3.9-3.el6_0.1.x86_64 @base ...
有可能撤消像恢复它的动作。
例如,让我们撤消上一个操作5,它是程序包组"PHP支持"的安装。
# yum history undo 5 Loaded plugins: fastestmirror Undoing transaction 5, from Wed Nov 20 09:14:37 2019 Dep-Install apr-1.3.9-5.el6_9.1.x86_64 @base Dep-Install apr-util-1.3.9-3.el6_0.1.x86_64 @base Dep-Install apr-util-ldap-1.3.9-3.el6_0.1.x86_64 @base ... ... php-cli x86_64 5.3.3-49.el6 @base 6.2 M php-common x86_64 5.3.3-49.el6 @base 2.9 M php-gd x86_64 5.3.3-49.el6 @base 324 k php-pdo x86_64 5.3.3-49.el6 @base 168 k php-pear noarch 1:1.9.4-5.el6 @base 2.2 M php-xml x86_64 5.3.3-49.el6 @base 307 k Transaction Summary ============================================================================================================ Remove 22 Package(s) Installed size: 27 M Is this ok [y/N]:
并且可以将动作5与命令重做。
# yum history redo 5
或者撤消我们之前所做的行动。
但首先,我们需要再次检查历史
# yum history Loaded plugins: fastestmirror ID | Login user | Date and time | Action(s) | Altered ------------------------------------------------------------------------------ 8 | root <root> | 2019-02-20 14:15 | Erase | 22 7 | root <root> | 2019-02-20 09:57 | Reinstall | 1 6 | root <root> | 2019-02-20 09:48 | Install | 1 5 | root <root> | 2019-02-20 09:14 | Install | 22
我们可以看到undoing的操作是操作8 erase,因此我们可以撤消它
# yum history undo 8
但是,如果我们考虑一下,如果我们想恢复我们所做的事情,它很容易重做5.
12)已安装和可用包装
可以使用以下命令列出计算机中安装的所有包:
# yum list installed
但是列表可能很长,所以我们可以决定每页页面
# yum list installed | more
或者仅列出几行
# yum list installed | head -n 20
因为我们只能决定对包含我们正在搜索的包的模式名称的行进行排序
# yum list installed | grep php php.x86_64 5.3.3-49.el6 @base php-cli.x86_64 5.3.3-49.el6 @base php-common.x86_64 5.3.3-49.el6 @base php-gd.x86_64 5.3.3-49.el6 @base php-pdo.x86_64 5.3.3-49.el6 @base php-pear.noarch 1:1.9.4-5.el6 @base php-xml.x86_64 5.3.3-49.el6 @base
除了安装的包之外,还有可能列出可以安装的包,以便单词单词为系统提供可用的包,但此命令将很长
# yum list available
因此,我们可以使用Grep过滤以具有我们正在寻找的软件包的软件包行
# yum list available | grep mysql
13)安装安全更新
在系统的更新过程中,我们可以仅选择安全更新,而不是更新所有软件包,如果我们担心可能其中一个软件包中发生的修改。
但首先,我们需要安装插件安全性
# yum -y install yum-plugin-security ... --> Running transaction check ---> Package yum-plugin-security.noarch 0:1.1.30-42.el6_10 will be installed
然后我们可以显示所有安全更新
# yum updateinfo list security all
然后我们可以安装安全更新
# yum update --security
或者我们可以选择升级至少安全信息到上次安全更新的所有包
# yum --security update-minimal