在这篇文章中,我们将学习如何使用由红帽(RedHat)开发的YUM(Yellowdog Updater Modified)工具,在Linux系统上安装、更新、移除、查找软件包,以及管理软件包和仓库。
本文中显示的示例命令经过了在我们的RHEL 8服务器上的实际测试,你可以将这些材料用于学习目的、RHEL认证,或者只是探索安装新软件包并保持系统最新的方法。
本文的基本要求是,你必须具备命令的基本理解和一个可工作的Linux操作系统,在其中你可以探索和实践下面列出的所有命令。
什么是YUM?
YUM(Yellowdog Updater Modified)是一个开源的基于命令行和图形界面的软件包管理工具,用于基于RPM(RedHat Package Manager)的Linux系统。
它允许用户和系统管理员在系统上轻松安装、更新、删除或搜索软件包。它是由Seth Vidal根据GPL(General Public License)作为开放源代码开发和发布的,这意味着任何人都可以下载和访问代码以修复错误并开发定制的软件包。
YUM使用众多第三方仓库来通过解决其依赖问题自动安装软件包。
更新:yum命令被dnf命令取代,dnf是yum的下一代版本,在大多数新的RPM-based发行版中被视为yum的替代品。
1. 使用YUM安装软件包
要安装一个名为Firefox的软件包,只需运行下面的命令,它将自动查找并安装Firefox的所有所需依赖项。
# yum install firefox Loaded plugins: fastestmirror Dependencies Resolved ================================================================================================ Package Arch Version Repository Size ================================================================================================ Updating: firefox i686 10.0.6-1.el6.centos updates 20 M Updating for dependencies: xulrunner i686 10.0.6-1.el6.centos updates 12 M Transaction Summary ================================================================================================ Install 0 Package(s) Upgrade 2 Package(s) Total download size: 32 M Is this ok [y/N]: y Downloading Packages: (1/2): firefox-10.0.6-1.el6.centos.i686.rpm | 20 MB 01:10 (2/2): xulrunner-10.0.6-1.el6.centos.i686.rpm | 12 MB 00:52 ------------------------------------------------------------------------------------------------ Total 63 kB/s | 32 MB 02:04 Updated: firefox.i686 0:10.0.6-1.el6.centos Dependency Updated: xulrunner.i686 0:10.0.6-1.el6.centos Complete!
上述命令将在安装任何软件包之前要求确认。如果你想自动安装软件包而不需要任何确认,请使用以下示例中显示的-y
选项。
# yum -y install firefox
2. 使用YUM删除软件包
要完全删除一个软件包及其所有依赖项,只需运行以下命令,如下所示。
# yum remove firefox Loaded plugins: fastestmirror Setting up Remove Process Resolving Dependencies --> Running transaction check ---> Package firefox.i686 0:10.0.6-1.el6.centos set to be erased --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================== Package Arch Version Repository Size ==================================================================================================== Removing: firefox i686 10.0.6-1.el6.centos @updates 23 M Transaction Summary ==================================================================================================== Remove 1 Package(s) Reinstall 0 Package(s) Downgrade 0 Package(s) Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : firefox-10.0.6-1.el6.centos.i686 1/1 Removed: firefox.i686 0:10.0.6-1.el6.centos Complete!
同样,上述命令在删除软件包之前会要求确认。要禁用确认提示,只需添加选项-y
,如下所示。
# yum -y remove firefox
3. 使用YUM更新软件包
假设您有一个过时的MySQL软件包版本,并且您想将其更新到最新的稳定版本。只需运行以下命令,它将自动解决所有依赖性问题并安装它们。
# yum update mysql Loaded plugins: fastestmirror Dependencies Resolved ============================================================================================================ Package Arch Version Repository Size ============================================================================================================ Updating: vsftpd i386 2.0.5-24.el5_8.1 updates 144 k Transaction Summary ============================================================================================================ Install 0 Package(s) Upgrade 1 Package(s) Total size: 144 k Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Updating : vsftpd 1/2 Cleanup : vsftpd 2/2 Updated: vsftpd.i386 0:2.0.5-24.el5_8.1 Complete!
4. 使用YUM列出软件包
使用list功能搜索具有特定名称的软件包。例如,要搜索名为OpenSSH的软件包,请使用命令。
# yum list openssh Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.neu.edu.cn * epel: mirror.neu.edu.cn * extras: mirror.neu.edu.cn * rpmforge: mirror.nl.leaseweb.net * updates: mirror.nus.edu.sg Installed Packages openssh.i386 4.3p2-72.el5_6.3 installed Available Packages 4.3p2-82.el5 base
为了使搜索更准确,如果知道的话,可以定义带有其版本的软件包名称。例如,要搜索特定版本的OpenSSH-4.3p2软件包,请使用命令。
# yum list openssh-4.3p2
[ 您可能还喜欢:如何保护和加固OpenSSH服务器 ]
5. 使用YUM搜索软件包
如果您不记得软件包的确切名称,那么请使用搜索功能来搜索所有可用的软件包以匹配您指定的软件包名称。例如,要搜索所有与单词匹配的软件包。
# yum search vsftpd Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.neu.edu.cn * epel: mirror.neu.edu.cn * extras: mirror.neu.edu.cn * rpmforge: mirror.nl.leaseweb.net * updates: ftp.iitm.ac.in ============================== Matched: vsftpd ======================== ccze.i386 : A robust log colorizer pure-ftpd-selinux.i386 : SELinux support for Pure-FTPD vsftpd.i386 : vsftpd - Very Secure Ftp Daemon
6. 使用YUM获取软件包信息
假设您想在安装之前了解有关软件包的信息。要获取有关软件包的信息,只需发出以下命令。
# yum info firefox Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.neu.edu.cn * epel: mirror.neu.edu.cn * extras: mirror.neu.edu.cn * rpmforge: mirror.nl.leaseweb.net * updates: ftp.iitm.ac.in Available Packages Name : firefox Arch : i386 Version : 10.0.6 Release : 1.el5.centos Size : 20 M Repo : updates Summary : Mozilla Firefox Web browser URL : http://www.mozilla.org/projects/firefox/ License : MPLv1.1 or GPLv2+ or LGPLv2+ Description: Mozilla Firefox is an open-source web browser, designed for standards : compliance, performance and portability.
7. 使用YUM列出所有可用软件包
要列出Yum数据库中的所有可用软件包,请使用以下命令。
# yum list | less
8. 使用YUM列出所有已安装的软件包
要列出系统上所有已安装的软件包,只需发出以下命令,它将显示所有已安装的软件包。
# yum list installed | less
9. Yum Provides功能
Yum 提供功能用于查找特定文件所属的软件包。例如,如果您想知道拥有/etc/httpd/conf/httpd.conf的软件包的名称。
# yum provides /etc/httpd/conf/httpd.conf Loaded plugins: fastestmirror httpd-2.2.3-63.el5.centos.i386 : Apache HTTP Server Repo : base Matched from: Filename : /etc/httpd/conf/httpd.conf httpd-2.2.3-63.el5.centos.1.i386 : Apache HTTP Server Repo : updates Matched from: Filename : /etc/httpd/conf/httpd.conf httpd-2.2.3-65.el5.centos.i386 : Apache HTTP Server Repo : updates Matched from: Filename : /etc/httpd/conf/httpd.conf httpd-2.2.3-53.el5.centos.1.i386 : Apache HTTP Server Repo : installed Matched from: Other : Provides-match: /etc/httpd/conf/httpd.conf
10. 使用Yum检查可用更新
要查找系统上已安装的软件包有多少更新可用,请使用以下命令进行检查。
# yum check-update
11. 使用Yum更新系统
要使您的系统保持最新状态,包括所有安全性和二进制软件包更新,请运行以下命令。它将安装所有最新的补丁和安全更新到您的系统。
# yum update
12. 列出所有可用的组软件包
在Linux中,多个软件包被捆绑成特定的组。您可以使用yum而不是逐个安装软件包,您可以安装一个特定的组,该组将安装属于该组的所有相关软件包。例如,要列出所有可用的组,只需发出以下命令。
# yum grouplist Installed Groups: Administration Tools DNS Name Server Dialup Networking Support Editors Engineering and Scientific FTP Server Graphics Java Development Legacy Network Server Available Groups: Authoring and Publishing Base Beagle Cluster Storage Clustering Development Libraries Development Tools Eclipse Educational Software KDE (K Desktop Environment) KDE Software Development
13. 安装组软件包
要安装特定的软件包组,我们使用选项groupinstall。例如,要安装“MySQL数据库”,只需执行以下命令。
# yum groupinstall 'MySQL Database' Dependencies Resolved ================================================================================================= Package Arch Version Repository Size ================================================================================================= Updating: unixODBC i386 2.2.11-10.el5 base 290 k Installing for dependencies: unixODBC-libs i386 2.2.11-10.el5 base 551 k Transaction Summary ================================================================================================= Install 1 Package(s) Upgrade 1 Package(s) Total size: 841 k Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing : unixODBC-libs 1/3 Updating : unixODBC 2/3 Cleanup : unixODBC 3/3 Dependency Installed: unixODBC-libs.i386 0:2.2.11-10.el5 Updated: unixODBC.i386 0:2.2.11-10.el5 Complete!
14. 更新组软件包
要更新任何已安装的组软件包,只需运行以下命令,如下所示。
# yum groupupdate 'DNS Name Server' Dependencies Resolved ================================================================================================================ Package Arch Version Repository Size ================================================================================================================ Updating: bind i386 30:9.3.6-20.P1.el5_8.2 updates 981 k bind-chroot i386 30:9.3.6-20.P1.el5_8.2 updates 47 k Updating for dependencies: bind-libs i386 30:9.3.6-20.P1.el5_8.2 updates 864 k bind-utils i386 30:9.3.6-20.P1.el5_8.2 updates 174 k Transaction Summary ================================================================================================================ Install 0 Package(s) Upgrade 4 Package(s) Total size: 2.0 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Updating : bind-libs 1/8 Updating : bind 2/8 Updating : bind-chroot 3/8 Updating : bind-utils 4/8 Cleanup : bind 5/8 Cleanup : bind-chroot 6/8 Cleanup : bind-utils 7/8 Cleanup : bind-libs 8/8 Updated: bind.i386 30:9.3.6-20.P1.el5_8.2 bind-chroot.i386 30:9.3.6-20.P1.el5_8.2 Dependency Updated: bind-libs.i386 30:9.3.6-20.P1.el5_8.2 bind-utils.i386 30:9.3.6-20.P1.el5_8.2 Complete!
15. 删除组软件包
要删除或从系统中删除任何已安装的组,只需使用以下命令。
# yum groupremove 'DNS Name Server' Dependencies Resolved =========================================================================================================== Package Arch Version Repository Size =========================================================================================================== Removing: bind i386 30:9.3.6-20.P1.el5_8.2 installed 2.1 M bind-chroot i386 30:9.3.6-20.P1.el5_8.2 installed 0.0 Transaction Summary =========================================================================================================== Remove 2 Package(s) Reinstall 0 Package(s) Downgrade 0 Package(s) Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Erasing : bind 1/2 warning: /etc/sysconfig/named saved as /etc/sysconfig/named.rpmsave Erasing : bind-chroot 2/2 Removed: bind.i386 30:9.3.6-20.P1.el5_8.2 bind-chroot.i386 30:9.3.6-20.P1.el5_8.2 Complete!
16. 列出启用的Yum仓库
要列出系统中所有启用的Yum仓库,请使用以下选项。
# yum repolist repo id repo name status base CentOS-5 - Base enabled: 2,725 epel Extra Packages for Enterprise Linux 5 - i386 enabled: 5,783 extras CentOS-5 - Extras enabled: 282 mod-pagespeed mod-pagespeed enabled: 1 rpmforge RHEL 5 - RPMforge.net - dag enabled: 11,290 updates CentOS-5 - Updates enabled: 743 repolist: 20,824
16. 列出所有启用的和禁用的Yum仓库
以下命令将显示系统上所有启用的和禁用的yum仓库。
# yum repolist all repo id repo name status C5.0-base CentOS-5.0 - Base disabled C5.0-centosplus CentOS-5.0 - Plus disabled C5.0-extras CentOS-5.0 - Extras disabled base CentOS-5 - Base enabled: 2,725 epel Extra Packages for Enterprise Linux 5 - i386 enabled: 5,783 extras CentOS-5 - Extras enabled: 282 repolist: 20,824
17. 从特定仓库安装软件包
要从特定的启用或禁用仓库安装特定软件包,必须在yum命令中使用--enablerepo
选项。例如,要安装PhpMyAdmin软件包,只需执行该命令。
# yum --enablerepo=epel install phpmyadmin Dependencies Resolved ============================================================================================= Package Arch Version Repository Size ============================================================================================= Installing: phpMyAdmin noarch 3.5.1-1.el6 epel 4.2 M Transaction Summary ============================================================================================= Install 1 Package(s) Total download size: 4.2 M Installed size: 17 M Is this ok [y/N]: y Downloading Packages: phpMyAdmin-3.5.1-1.el6.noarch.rpm | 4.2 MB 00:25 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : phpMyAdmin-3.5.1-1.el6.noarch 1/1 Verifying : phpMyAdmin-3.5.1-1.el6.noarch 1/1 Installed: phpMyAdmin.noarch 0:3.5.1-1.el6 Complete!
18. 交互式Yum Shell
Yum工具提供了一个自定义的shell,您可以在其中执行多个命令。
# yum shell Loaded plugins: fastestmirror Setting up Yum Shell > update httpd Loading mirror speeds from cached hostfile * base: mirrors.sin3.sg.voxel.net * epel: ftp.riken.jp * extras: mirrors.sin3.sg.voxel.net * updates: mirrors.sin3.sg.voxel.net Setting up Update Process >
19. 清理Yum缓存
默认情况下,yum将所有启用的仓库包数据保存在/var/cache/yum/中的每个子目录中,为了清理所有来自启用的仓库的缓存文件,您需要定期运行以下命令来清理所有缓存,并确保没有不必要的内容占用空间。我们不想给出以下命令的输出,因为我们希望保持缓存数据不变。
# yum clean all
20. 查看Yum历史记录
要查看yum命令的所有过去的交易记录,只需使用以下命令。
# yum history Loaded plugins: fastestmirror ID | Login user | Date and time | Action(s) | Altered ------------------------------------------------------------------------------- 10 | root | 2012-08-11 15:19 | Install | 3 9 | root | 2012-08-11 15:11 | Install | 1 8 | root | 2012-08-11 15:10 | Erase | 1 EE 7 | root | 2012-08-10 17:44 | Install | 1 6 | root | 2012-08-10 12:19 | Install | 2 5 | root | 2012-08-10 12:14 | Install | 3 4 | root | 2012-08-10 12:12 | I, U | 13 E< 3 | root | 2012-08-09 13:01 | Install | 1 > 2 | root | 2012-08-08 20:13 | I, U | 292 EE 1 | System | 2012-08-08 17:15 | Install | 560 history list
我们已经尝试涵盖了从基础到高级的yum命令及其示例。如果有任何与yum命令相关的内容可能被遗漏,请通过我们的评论框更新我们。因此,我们将根据收到的反馈不断更新相同的内容。
Source:
https://www.tecmint.com/20-linux-yum-yellowdog-updater-modified-commands-for-package-mangement/