linux中20个yum包管理命令

什么是YUM

YUM(Yellowdog Updater Modified) 是一个开源命令行以及基于图形的包管理工具,用于RPM(RedHat Package Manager) 基于 Linux 系统。它允许用户和系统管理员轻松地在系统上安装、更新、删除或搜索软件包。它是由开发和发布的Seth Vidal在下面GPL(General Public License) 作为开源,意味着任何人都可以下载和访问代码以修复错误和开发定制包。YUM使用众多第三方存储库通过解决依赖关系问题来自动安装软件包。

1. 用YUM安装一个包

要安装名为Firefox 14的软件包,只需运行以下命令,它将自动查找并安装 Firefox 所需的所有依赖项。

  1. # yum install firefox
  2. Loaded plugins: fastestmirror
  3. Dependencies Resolved
  4. ================================================================================================
  5. Package Arch Version Repository Size
  6. ================================================================================================
  7. Updating:
  8. firefox i686 10.0.6-1.el6.centos updates 20 M
  9. Updating for dependencies:
  10. xulrunner i686 10.0.6-1.el6.centos updates 12 M
  11. Transaction Summary
  12. ================================================================================================
  13. Install 0 Package(s)
  14. Upgrade 2 Package(s)
  15. Total download size: 32 M
  16. Is this ok [y/N]: y
  17. Downloading Packages:
  18. (1/2): firefox-10.0.6-1.el6.centos.i686.rpm | 20 MB 01:10
  19. (2/2): xulrunner-10.0.6-1.el6.centos.i686.rpm | 12 MB 00:52
  20. ------------------------------------------------------------------------------------------------
  21. Total 63 kB/s | 32 MB 02:04
  22. Updated:
  23. firefox.i686 0:10.0.6-1.el6.centos
  24. Dependency Updated:
  25. xulrunner.i686 0:10.0.6-1.el6.centos
  26. Complete!

在你的系统上安装任何软件包之前,上述命令将要求确认。如果你想在不要求任何确认的情况下自动安装软件包,请使用选项-y如下例所示。

  1. # yum -y install firefox

2. 使用YUM删除包

要完全删除一个包及其所有依赖项,只需运行以下命令,如下所示。

  1. # yum remove firefox
  2. Loaded plugins: fastestmirror
  3. Setting up Remove Process
  4. Resolving Dependencies
  5. --> Running transaction check
  6. ---> Package firefox.i686 0:10.0.6-1.el6.centos set to be erased
  7. --> Finished Dependency Resolution
  8. Dependencies Resolved
  9. ====================================================================================================
  10. Package Arch Version Repository Size
  11. ====================================================================================================
  12. Removing:
  13. firefox i686 10.0.6-1.el6.centos @updates 23 M
  14. Transaction Summary
  15. ====================================================================================================
  16. Remove 1 Package(s)
  17. Reinstall 0 Package(s)
  18. Downgrade 0 Package(s)
  19. Is this ok [y/N]: y
  20. Downloading Packages:
  21. Running rpm_check_debug
  22. Running Transaction Test
  23. Transaction Test Succeeded
  24. Running Transaction
  25. Erasing : firefox-10.0.6-1.el6.centos.i686 1/1
  26. Removed:
  27. firefox.i686 0:10.0.6-1.el6.centos
  28. Complete!

以同样的方式,上述命令将在删除包之前要求确认。要禁用确认提示,只需添加选项 -y,如下所示。

  1. # yum -y remove firefox

3. 使用YUM更新包

假设你有过时版本的MySQL软件包,并且你想将其更新到最新的稳定版本。只需运行以下命令,它将自动解决所有依赖问题并安装它们。

  1. # yum update mysql
  2. Loaded plugins: fastestmirror
  3. Dependencies Resolved
  4. ============================================================================================================
  5. Package Arch Version Repository Size
  6. ============================================================================================================
  7. Updating:
  8. vsftpd i386 2.0.5-24.el5_8.1 updates 144 k
  9. Transaction Summary
  10. ============================================================================================================
  11. Install 0 Package(s)
  12. Upgrade 1 Package(s)
  13. Total size: 144 k
  14. Is this ok [y/N]: y
  15. Downloading Packages:
  16. Running rpm_check_debug
  17. Running Transaction Test
  18. Finished Transaction Test
  19. Transaction Test Succeeded
  20. Running Transaction
  21. Updating : vsftpd 1/2
  22. Cleanup : vsftpd 2/2
  23. Updated:
  24. vsftpd.i386 0:2.0.5-24.el5_8.1
  25. Complete!

4. 使用YUM列出一个包

使用list函数来搜索具有名称的特定包。例如搜索一个名为的包openssh,使用命令。

  1. # yum list openssh
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: mirror.neu.edu.cn
  5. * epel: mirror.neu.edu.cn
  6. * extras: mirror.neu.edu.cn
  7. * rpmforge: mirror.nl.leaseweb.net
  8. * updates: mirror.nus.edu.sg
  9. Installed Packages
  10. openssh.i386 4.3p2-72.el5_6.3 installed
  11. Available Packages 4.3p2-82.el5 base

为了使你的搜索更准确,如果你知道,请使用它们的版本定义包名称。例如搜索特定版本openssh-4.3p2包,使用命令。

  1. # yum list openssh-4.3p2

5. 使用YUM搜索包

如果你不记得包的确切名称,请使用search函数来搜索所有可用的包以匹配你指定的包的名称。例如,要搜索与单词匹配的所有包.

  1. # yum search vsftpd
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: mirror.neu.edu.cn
  5. * epel: mirror.neu.edu.cn
  6. * extras: mirror.neu.edu.cn
  7. * rpmforge: mirror.nl.leaseweb.net
  8. * updates: ftp.iitm.ac.in
  9. ============================== Matched: vsftpd ========================
  10. ccze.i386 : A robust log colorizer
  11. pure-ftpd-selinux.i386 : SELinux support for Pure-FTPD
  12. vsftpd.i386 : vsftpd - Very Secure Ftp Daemon

6. 使用YUM获取包的信息

假设你想在安装之前了解软件包的信息。要获取包的信息,只需发出以下命令。

  1. # yum info firefox
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: mirror.neu.edu.cn
  5. * epel: mirror.neu.edu.cn
  6. * extras: mirror.neu.edu.cn
  7. * rpmforge: mirror.nl.leaseweb.net
  8. * updates: ftp.iitm.ac.in
  9. Available Packages
  10. Name : firefox
  11. Arch : i386
  12. Version : 10.0.6
  13. Release : 1.el5.centos
  14. Size : 20 M
  15. Repo : updates
  16. Summary : Mozilla Firefox Web browser
  17. URL : http://www.mozilla.org/projects/firefox/
  18. License : MPLv1.1 or GPLv2+ or LGPLv2+
  19. Description: Mozilla Firefox is an open-source web browser, designed for standards
  20. : compliance, performance and portability.

7. 使用YUM列出所有可用的包

要列出 Yum 数据库中的所有可用包,请使用以下命令。

  1. # yum list | less

8. 使用YUM列出所有已安装的包

要列出系统上所有已安装的软件包,只需发出以下命令,它将显示所有已安装的软件包。

  1. # yum list installed | less

9. Yum provides

Yumprovides函数用于查找特定文件属于哪个包。例如,如果你想知道具有/etc/httpd/conf/httpd.conf.

  1. # yum provides /etc/httpd/conf/httpd.conf
  2. Loaded plugins: fastestmirror
  3. httpd-2.2.3-63.el5.centos.i386 : Apache HTTP Server
  4. Repo : base
  5. Matched from:
  6. Filename : /etc/httpd/conf/httpd.conf
  7. httpd-2.2.3-63.el5.centos.1.i386 : Apache HTTP Server
  8. Repo : updates
  9. Matched from:
  10. Filename : /etc/httpd/conf/httpd.conf
  11. httpd-2.2.3-65.el5.centos.i386 : Apache HTTP Server
  12. Repo : updates
  13. Matched from:
  14. Filename : /etc/httpd/conf/httpd.conf
  15. httpd-2.2.3-53.el5.centos.1.i386 : Apache HTTP Server
  16. Repo : installed
  17. Matched from:
  18. Other : Provides-match: /etc/httpd/conf/httpd.conf

10. 使用 Yum 检查可用更新

要查找系统上有多少已安装的软件包有可用更新,请使用以下命令进行检查。

  1. # yum check-update

11. 使用 Yum 更新系统

要通过所有安全和二进制包更新使你的系统保持最新,请运行以下命令。它将为你的系统安装所有最新的补丁和安全更新。

  1. # yum update

12. 列出所有可用的组包

在 Linux 中,许多包被捆绑到特定的组。你可以安装特定的组,而不是使用 yum 安装单个软件包,该组将安装属于该组的所有相关软件包。例如要列出所有可用的组,只需发出以下命令。

  1. # yum grouplist
  2. Installed Groups:
  3. Administration Tools
  4. DNS Name Server
  5. Dialup Networking Support
  6. Editors
  7. Engineering and Scientific
  8. FTP Server
  9. Graphics
  10. Java Development
  11. Legacy Network Server
  12. Available Groups:
  13. Authoring and Publishing
  14. Base
  15. Beagle
  16. Cluster Storage
  17. Clustering
  18. Development Libraries
  19. Development Tools
  20. Eclipse
  21. Educational Software
  22. KDE (K Desktop Environment)
  23. KDE Software Development

13. 安装组包

要安装特定的软件包组,我们使用选项作为groupinstall.例如,要安装MySQL Database,只需执行以下命令。

  1. # yum groupinstall 'MySQL Database'
  2. Dependencies Resolved
  3. =================================================================================================
  4. Package Arch Version Repository Size
  5. =================================================================================================
  6. Updating:
  7. unixODBC i386 2.2.11-10.el5 base 290 k
  8. Installing for dependencies:
  9. unixODBC-libs i386 2.2.11-10.el5 base 551 k
  10. Transaction Summary
  11. =================================================================================================
  12. Install 1 Package(s)
  13. Upgrade 1 Package(s)
  14. Total size: 841 k
  15. Is this ok [y/N]: y
  16. Downloading Packages:
  17. Running rpm_check_debug
  18. Running Transaction Test
  19. Finished Transaction Test
  20. Transaction Test Succeeded
  21. Running Transaction
  22. Installing : unixODBC-libs 1/3
  23. Updating : unixODBC 2/3
  24. Cleanup : unixODBC 3/3
  25. Dependency Installed:
  26. unixODBC-libs.i386 0:2.2.11-10.el5
  27. Updated:
  28. unixODBC.i386 0:2.2.11-10.el5
  29. Complete!

14. 更新组包

要更新任何现有的已安装组包,只需运行以下命令,如下所示。

  1. # yum groupupdate 'DNS Name Server'
  2. Dependencies Resolved
  3. ================================================================================================================
  4. Package Arch Version Repository Size
  5. ================================================================================================================
  6. Updating:
  7. bind i386 30:9.3.6-20.P1.el5_8.2 updates 981 k
  8. bind-chroot i386 30:9.3.6-20.P1.el5_8.2 updates 47 k
  9. Updating for dependencies:
  10. bind-libs i386 30:9.3.6-20.P1.el5_8.2 updates 864 k
  11. bind-utils i386 30:9.3.6-20.P1.el5_8.2 updates 174 k
  12. Transaction Summary
  13. ================================================================================================================
  14. Install 0 Package(s)
  15. Upgrade 4 Package(s)
  16. Total size: 2.0 M
  17. Is this ok [y/N]: y
  18. Downloading Packages:
  19. Running rpm_check_debug
  20. Running Transaction Test
  21. Finished Transaction Test
  22. Transaction Test Succeeded
  23. Running Transaction
  24. Updating : bind-libs 1/8
  25. Updating : bind 2/8
  26. Updating : bind-chroot 3/8
  27. Updating : bind-utils 4/8
  28. Cleanup : bind 5/8
  29. Cleanup : bind-chroot 6/8
  30. Cleanup : bind-utils 7/8
  31. Cleanup : bind-libs 8/8
  32. Updated:
  33. bind.i386 30:9.3.6-20.P1.el5_8.2 bind-chroot.i386 30:9.3.6-20.P1.el5_8.2
  34. Dependency Updated:
  35. bind-libs.i386 30:9.3.6-20.P1.el5_8.2 bind-utils.i386 30:9.3.6-20.P1.el5_8.2
  36. Complete!

15. 删除组包

要从系统中删除或删除任何现有的已安装组,只需使用以下命令。

  1. # yum groupremove 'DNS Name Server'
  2. Dependencies Resolved
  3. ===========================================================================================================
  4. Package Arch Version Repository Size
  5. ===========================================================================================================
  6. Removing:
  7. bind i386 30:9.3.6-20.P1.el5_8.2 installed 2.1 M
  8. bind-chroot i386 30:9.3.6-20.P1.el5_8.2 installed 0.0
  9. Transaction Summary
  10. ===========================================================================================================
  11. Remove 2 Package(s)
  12. Reinstall 0 Package(s)
  13. Downgrade 0 Package(s)
  14. Is this ok [y/N]: y
  15. Downloading Packages:
  16. Running rpm_check_debug
  17. Running Transaction Test
  18. Finished Transaction Test
  19. Transaction Test Succeeded
  20. Running Transaction
  21. Erasing : bind 1/2
  22. warning: /etc/sysconfig/named saved as /etc/sysconfig/named.rpmsave
  23. Erasing : bind-chroot 2/2
  24. Removed:
  25. bind.i386 30:9.3.6-20.P1.el5_8.2 bind-chroot.i386 30:9.3.6-20.P1.el5_8.2
  26. Complete!

16. 列出启用的 Yum 存储库

要列出系统中所有启用的 Yum 存储库,请使用以下选项。

  1. # yum repolist
  2. repo id repo name status
  3. base CentOS-5 - Base enabled: 2,725
  4. epel Extra Packages for Enterprise Linux 5 - i386 enabled: 5,783
  5. extras CentOS-5 - Extras enabled: 282
  6. mod-pagespeed mod-pagespeed enabled: 1
  7. rpmforge RHEL 5 - RPMforge.net - dag enabled: 11,290
  8. updates CentOS-5 - Updates enabled: 743
  9. repolist: 20,824

16. 列出所有启用和禁用的 Yum 存储库

以下命令将显示系统上所有启用和禁用的 yum 存储库。

  1. # yum repolist all
  2. repo id repo name status
  3. C5.0-base CentOS-5.0 - Base disabled
  4. C5.0-centosplus CentOS-5.0 - Plus disabled
  5. C5.0-extras CentOS-5.0 - Extras disabled
  6. base CentOS-5 - Base enabled: 2,725
  7. epel Extra Packages for Enterprise Linux 5 - i386 enabled: 5,783
  8. extras CentOS-5 - Extras enabled: 282
  9. repolist: 20,824

17. 从特定存储库安装包

要从特定启用或禁用的存储库安装特定软件包,你必须使用–enablerepo yum 命令中的选项。例如安装PhpMyAdmin 3.5.2包,只需执行命令。

  1. # yum --enablerepo=epel install phpmyadmin
  2. Dependencies Resolved
  3. =============================================================================================
  4. Package Arch Version Repository Size
  5. =============================================================================================
  6. Installing:
  7. phpMyAdmin noarch 3.5.1-1.el6 epel 4.2 M
  8. Transaction Summary
  9. =============================================================================================
  10. Install 1 Package(s)
  11. Total download size: 4.2 M
  12. Installed size: 17 M
  13. Is this ok [y/N]: y
  14. Downloading Packages:
  15. phpMyAdmin-3.5.1-1.el6.noarch.rpm | 4.2 MB 00:25
  16. Running rpm_check_debug
  17. Running Transaction Test
  18. Transaction Test Succeeded
  19. Running Transaction
  20. Installing : phpMyAdmin-3.5.1-1.el6.noarch 1/1
  21. Verifying : phpMyAdmin-3.5.1-1.el6.noarch 1/1
  22. Installed:
  23. phpMyAdmin.noarch 0:3.5.1-1.el6
  24. Complete!

18. 交互式 Yum Shell

Yum实用程序提供了一个自定义 shell,你可以在其中执行多个命令。

  1. # yum shell
  2. Loaded plugins: fastestmirror
  3. Setting up Yum Shell
  4. > update httpd
  5. Loading mirror speeds from cached hostfile
  6. * base: mirrors.sin3.sg.voxel.net
  7. * epel: ftp.riken.jp
  8. * extras: mirrors.sin3.sg.voxel.net
  9. * updates: mirrors.sin3.sg.voxel.net
  10. Setting up Update Process
  11. >

19. 清理 Yum 缓存

默认情况下,yum 将所有启用了存储库的包数据保存在/var/cache/yum/对于每个子目录,要从启用的存储库中清除所有缓存文件,你需要定期运行以下命令以清除所有缓存并确保没有任何不必要的空间正在使用。我们不想给出以下命令的输出,因为我们喜欢保持缓存数据的原样。

  1. # yum clean all

20. 查看yum安装历史

要查看 yum 命令历史执行,只需使用以下命令。

  1. # yum history
  2. Loaded plugins: fastestmirror
  3. ID | Login user | Date and time | Action(s) | Altered
  4. -------------------------------------------------------------------------------
  5. 10 | root | 2012-08-11 15:19 | Install | 3
  6. 9 | root | 2012-08-11 15:11 | Install | 1
  7. 8 | root | 2012-08-11 15:10 | Erase | 1 EE
  8. 7 | root | 2012-08-10 17:44 | Install | 1
  9. 6 | root | 2012-08-10 12:19 | Install | 2
  10. 5 | root | 2012-08-10 12:14 | Install | 3
  11. 4 | root | 2012-08-10 12:12 | I, U | 13 E<
  12. 3 | root | 2012-08-09 13:01 | Install | 1 >
  13. 2 | root | 2012-08-08 20:13 | I, U | 292 EE
  14. 1 | System | 2012-08-08 17:15 | Install | 560
  15. history list

返回笔记列表
入门小站