如何在CentOS/RHEL上使用yum命令

时间:2020-01-09 10:43:19  来源:igfitidea点击:

如何使用yum命令通过RHN或者Internet更新和修补Red Hat Enterprise Linux/CentOS Linux版本5.x/6.x/7.x服务器?
我可以在RHEL v5.x下使用up2date命令吗?

up2date命令是RHEL v4.x或者更早版本的一部分。
您需要使用yum命令通过RHN或者Internet更新和修补系统。
使用yum command安装关键和非关键安全更新以及二进制软件包。
以root用户身份登录以安装和更新系统。
本教程显示可以在CentOS或者RHEL上使用yum命令执行的常见sysadmin任务的示例。

如何在RHN中注册基于RHEL的系统

要使用RHN注册RHEL 5.x系统,请执行以下命令,然后按照屏幕上的说明进行操作(CentOS用户跳至下一步):

# rhn_register

要向RHN注册RHEL 7.x系统,请输入folllwong

# subscription-manager register --username tom --password jerry --auto-attach

更新所有CentOS 6.x软件包,包括它们所依赖的软件包

您只能使用以下语法来应用与安全性相关的程序包更新:

$ sudo yum update --security

更新指定的软件包

更新指定的程序包。
语法是

$ yum update {package-name-1}

要检查并更新httpd软件包,请执行:

$ sudo yum update httpd

您可以使用以下语法将一个软件包更新为特定版本,例如nginx:

$ ## first find a list of all dups ##
$ yum --showduplicates list nginx
$ ## now update to particular version ##
$ sudo yum update-to nginx-version
$ sudo yum update-to nginx-1.12.2-1.el7

列出所有已安装的软件包

要列出所有已安装的软件包,请执行:

$ rpm -qa ## not recommend just use yum ##
$ yum list installed

输出示例:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.iitm.ac.in
* epel: mirror1.ku.ac.th
* extras: ftp.iitm.ac.in
* updates: ftp.iitm.ac.in
Installed Packages
GeoIP.x86_64                                       1.5.0-11.el7                                @base   
OpenEXR-libs.x86_64                                1.7.1-7.el7                                 @base   
SDL.x86_64                                         1.2.15-14.el7                               @base   
ShellCheck.x86_64                                  0.3.5-1.el7                                 @epel   
acl.x86_64                                         2.2.51-12.el7                               @base   
alsa-lib.x86_64                                    1.1.3-3.el7                                 @base   
apr.x86_64                                         1.4.8-3.el7_4.1                             @updates
apr-util.x86_64                                    1.5.2-6.el7                                 @base   
atk.x86_64                                         2.22.0-3.el7                                @base   
atk-devel.x86_64                                   2.22.0-3.el7                                @base   
atlas.x86_64                                       3.10.1-12.el7                               @base   
audit-libs.x86_64                                  2.7.6-3.el7                                 @base   
autoconf.noarch                                    2.69-11.el7                                 @base   
autogen-libopts.x86_64                             5.18-5.el7                                  @base   
....
..
...
yum-plugin-fastestmirror.noarch                    1.1.31-42.el7                               @base   
zip.x86_64                                         3.0-11.el7                                  @base   
zlib.x86_64                                        1.2.7-17.el7                                @base   
zlib-devel.x86_64                                  1.2.7-17.el7                                @base

找出系统上是否已安装httpd软件包

$ rpm -qa | grep httpd*
$ yum list installed httpd

输出示例:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* epel: repo.ugm.ac.id
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
Installed Packages
httpd.x86_64                               2.4.6-67.el7.centos.6                               @updates

列出所有可用于安装系统上安装的所有软件包的软件包

$ yum list
$ yum list
$ yum list | more ## [ Use more command to show info one screen at a time ] ##
$ yum list | grep bash ## [ Use grep command to filter out results ] ##

列出所有可用于安装的软件包

$ yum list available
$ yum list available | more
$ yum list available | grep nginx

如何按名称搜索软件包

您可以使用搜索命令在包装说明中搜索指定的字符串。

list命令也可以使用。
要搜索httpd和所有匹配的perl软件包,请执行:

$ yum list {package-name}
$ yum list {regex}
$ yum list httpd
$ yum list perl*
$ yum search {package-name}
$ yum search nginx
$ yum search nginx*

来自yum list perl *的示例输出:

Loading "installonlyn" plugin
Loading "security" plugin
Setting up repositories
Reading repository metadata in from local files
Installed Packages
perl.i386                                4:5.8.8-10.el5_0.2     installed       
perl-Archive-Tar.noarch                  1.30-1.fc6             installed       
perl-BSD-Resource.i386                   1.28-1.fc6.1           installed       
perl-Compress-Zlib.i386                  1.42-1.fc6             installed       
perl-DBD-MySQL.i386                      3.0007-1.fc6           installed       
perl-DBI.i386                            1.52-1.fc6             installed       
perl-Digest-HMAC.noarch                  1.01-15                installed       
perl-Digest-SHA1.i386                    2.11-1.2.1             installed       
perl-HTML-Parser.i386                    3.55-1.fc6             installed   
.....
.......
..
perl-libxml-perl.noarch                  0.08-1.2.1             base            
perl-suidperl.i386                       4:5.8.8-10.el5_0.2     updates

如何查看有关包装的详细信息

语法为:

$ yum info {pkg-1} {pkg-2}
$ yum info htop
$ yum info nginx htop

输出示例:显示有关RHEL/CentOS Linux服务器上软件包的详细信息。

如何显示软件包的依赖关系列表

语法为:

$ yum deplist {pkg}
## show for htop pkg
$ yum deplist htop

输出示例:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* epel: repo.ugm.ac.id
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
package: htop.x86_64 2.0.2-1.el7
  dependency: libc.so.6(GLIBC_2.15)(64bit)
   provider: glibc.x86_64 2.17-196.el7_4.2
  dependency: libgcc_s.so.1()(64bit)
   provider: libgcc.x86_64 4.8.5-16.el7_4.1
  dependency: libgcc_s.so.1(GCC_3.0)(64bit)
   provider: libgcc.x86_64 4.8.5-16.el7_4.1
  dependency: libgcc_s.so.1(GCC_3.3.1)(64bit)
   provider: libgcc.x86_64 4.8.5-16.el7_4.1
  dependency: libm.so.6()(64bit)
   provider: glibc.x86_64 2.17-196.el7_4.2
  dependency: libm.so.6(GLIBC_2.2.5)(64bit)
   provider: glibc.x86_64 2.17-196.el7_4.2
  dependency: libncursesw.so.5()(64bit)
   provider: ncurses-libs.x86_64 5.9-14.20130511.el7_4
  dependency: libtinfo.so.5()(64bit)
   provider: ncurses-libs.x86_64 5.9-14.20130511.el7_4
  dependency: rtld(GNU_HASH)
   provider: glibc.x86_64 2.17-196.el7_4.2
   provider: glibc.i686 2.17-196.el7_4.2

如何安装指定的软件包[RPM]

要安装名为httpd的软件包:

# yum install {package-name-1} {package-name-2}
# yum install httpd
# yum install nginx mariadb-server php-fpm
# yum install htop

输出示例:在CentOS/RHEL上安装名为htop的指定软件包。

要从名为foo.rpm或者http或者ftp镜像的本地文件安装软件包:

$ sudo yum localinstall foo.rpm
$ sudo yum https://server1.theitroad.local/foo.rpm

也可以安装特定版本。
在仓库/列表/搜索命令中查找或者显示重复项

$ yum --showduplicates list php
$ ## now install to particular version ##
$ sudo yum update-to php-version
$ sudo yum update-to php-7.0.2-1.nix

如何将软件包降级到较早版本

语法为:

# yum downgrade {pkg}
# yum downgrade nginx

如何再次重新安装软件包

语法为:

# yum reinstall {pkg}
# yum reinstall httpd

如何删除/卸载指定的软件包[RPM]

删除名为httpd的软件包,执行:

# yum remove {package-name-1} {package-name-2}
# yum erase {package-name-1} {package-name-2}
# yum remove httpd
# yum erase htop

如何删除不需要/不需要的程序包/deps

语法为:

# yum autoremove

输出示例:

Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be erased
--> Finished Dependency Resolution
--> Finding unneeded leftover dependencies
Found and removing 0 unneeded dependencies
 
Dependencies Resolved
 
=======================================================================================================
Package                        Arch                Version                   Repository          Size
=======================================================================================================
Removing:
tcp_wrappers-libs              x86_64              7.6-77.el7                @base              131 k
 
Transaction Summary
=======================================================================================================
Remove  1 Package
 
Installed size: 131 k
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : tcp_wrappers-libs-7.6-77.el7.x86_64                                                 1/1 
  Verifying  : tcp_wrappers-libs-7.6-77.el7.x86_64                                                 1/1 
 
Removed:
  tcp_wrappers-libs.x86_64 0:7.6-77.el7                                                                
 
Complete!

显示可用软件包列表

$ yum list all

显示组软件列表

执行以下命令:

$ yum grouplist

或者

$ yum groups list

输出:

Installed Groups:
   Engineering and Scientific
   MySQL Database
   Editors
   System Tools
   Text-based Internet
   Legacy Network Server
   DNS Name Server
   Dialup Networking Support
   FTP Server
   Network Servers
   Legacy Software Development
   Legacy Software Support
   Development Libraries
   Graphics
   Web Server
   Ruby
   Printing Support
   Mail Server
   Server Configuration Tools
   PostgreSQL Database
Available Groups:
   Office/Productivity
   Administration Tools
   Beagle
   Development Tools
   GNOME Software Development
   X Software Development
   Virtualization
   GNOME Desktop Environment
   Authoring and Publishing
   Mono
   Games and Entertainment
   XFCE-4.4
   Tomboy
   Java
   Java Development
   Emacs
   X Window System
   Windows File Server
   KDE Software Development
   KDE (K Desktop Environment)
   Horde
   Sound and Video
   FreeNX and NX
   News Server
   Yum Utilities
   Graphical Internet
Done

按组安装所有默认软件包

安装所有"开发工具"组软件包,执行:

# yum groupinstall "Development Tools"

或者

# yum groups install "Development Tools"

按组更新所有默认软件包

更新所有"开发工具"组软件包,执行:

# yum groupupdate "Development Tools"

或者

# yum groups update "Development Tools"

删除组中的所有软件包

除去所有"开发工具"组软件包,执行:

# yum groupremove "Development Tools"

或者

# yum groups remove "Development Tools"

显示软件包组的描述和内容

$ yum groupinfo 'Development Tools'

或者

$ yum groups info 'Development Tools'
$ yum groups info 'File and Print Server'

输出示例:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: del-mirrors.extreme-ix.org
* epel: ftp.riken.jp
* extras: del-mirrors.extreme-ix.org
* updates: del-mirrors.extreme-ix.org
 
Environment Group: File and Print Server
Environment-Id: file-print-server-environment
Description: File, print, and storage server for enterprises.
Mandatory Groups:
   +base
   +core
   +file-server
   +print-server
Optional Groups:
   +backup-client
   +backup-server
   +debugging
   +directory-client
   +guest-agents
   +ha
   +hardware-monitoring
   +java-platform
   +large-systems
   +network-file-system-client
   +performance
   +remote-system-management
   +resilient-storage

安装特定的体系结构软件包

如果您使用的是64位RHEL版本,则可以安装32个软件包(请注意,最新版本的CentOS/RHEL 7.x不再提供32位支持):

# yum install {package-name}.{architecture}
# yum install mysql.i386

显示软件包未通过RHN官方订阅库安装

显示所有通过订阅的频道或者存储库不可用的软件包,即显示通过其他存储库安装的软件包:

$ yum list extras

输出示例:

Loading "installonlyn" plugin
Loading "security" plugin
Setting up repositories
Reading repository metadata in from local files
Extra Packages
DenyHosts.noarch                         2.6-python2.4          installed       
VMwareTools.i386                         6532-44356             installed       
john.i386                                1.7.0.2-3.el5.rf       installed       
kernel.i686                              2.6.18-8.1.15.el5      installed       
kernel-devel.i686                        2.6.18-8.1.15.el5      installed       
lighttpd.i386                            1.4.18-1.el5.rf        installed       
lighttpd-fastcgi.i386                    1.4.18-1.el5.rf        installed       
psad.i386                                2.1-1                  installed       
rssh.i386                                2.3.2-1.2.el5.rf       installed

显示哪个包提供文件

您可以轻松地找到哪个RPM软件包提供了该文件。
例如,查找提供/etc/passwd文件的文件:

$ yum whatprovides /etc/passwd

或者

$ yum provides /etc/passwd

输出示例:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* epel: repo.ugm.ac.id
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
setup-2.8.71-7.el7.noarch : A set of system configuration and setup files
Repo        : base
Matched from:
Filename    : /etc/passwd

setup-2.8.71-7.el7.noarch : A set of system configuration and setup files
Repo        : @base
Matched from:
Filename    : /etc/passwd

您可以使用相同的命令列出满足依赖性的软件包:

$ yum whatprovides {dependency-1} {dependency-2}
$ yum provides string
$ yum provides '*bin/nginx*'

如何清理Yum餐饮

您可以删除所有缓存和下载的程序包,标头和其他信息:

# yum clean all

输出示例:

Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors

上面的命令可以帮助您清除由过时的标题,网络错误等引起的问题。
要检查本地RPM数据库是否存在问题:

# yum check

如何列出软件存储库

$ yum repolist

输出示例:

repo id                          repo name                                                      status
base/7/x86_64                    CentOS-7 - Base                                                 9591
epel/x86_64                      Extra Packages for Enterprise Linux 7 - x86_64                 12155
extras/7/x86_64                  CentOS-7 - Extras                                                327
nginx/x86_64                     nginx repo                                                       313
updates/7/x86_64                 CentOS-7 - Updates                                              1573
repolist: 23959

查看有关已启用的yum存储库的信息

要获取有关nginxRepo的信息,请运行:

$ yum repoinfo nginx

输出示例:

Repo-id      : nginx/x86_64
Repo-name    : nginx repo
Repo-status  : enabled
Repo-revision: 1511279401
Repo-updated : Tue Nov 21 15:50:07 2016
Repo-pkgs    : 313
Repo-size    : 143 M
Repo-baseurl : http://nginx.org/packages/mainline/centos/7/x86_64/
Repo-expire  : 21600 second(s) (last: Sat Dec 23 16:49:24 2016)
  Filter     : read-only:present
Repo-filename: /etc/yum.repos.d/nginx.repo
 
repolist: 313

如何使用给定的存储库

语法为:

# yum repo-pkgs repo_name_id <list|install|remove|upgrade|reinstall> [pkg]

列出nginx存储库中的所有软件包:

# yum repo-pkgs nginx list

从Nginx存储库中获取有关Nginx软件包的信息:

# yum repo-pkgs nginx info | more
# yum repo-pkgs nginx info nginx

从nginx存储库中安装名为nginx的软件包:

# yum repo-pkgs nginx install nginx

从Nginx存储库安装所有软件包:

# yum repo-pkgs nginx install

从nginx存储库中擦除/删除/删除名为nginx的软件包:

# yum repo-pkgs nginx remove nginx

从nginx信息库中擦除/删除/删除所有软件包:

# yum repo-pkgs nginx remove

从Nginx存储库升级Nginx软件包:

# yum repo-pkgs nginx upgrade nginx

从nginx存储库中重新安装nginx软件包:

# yum repo-pkgs nginx reinstall nginx

如何查看和使用Yum历史

语法为:

# yum history
# yum history list
# yum info ID
# yum undo ID
# yum redo ID

如何查看有关yum命令的帮助

有关更多信息,请参见yum命令手册页:

$ man yum
$ yum help
$ yum help install
$ yum help erase

输出示例:

Loaded plugins: fastestmirror
erase PACKAGE...

Remove a package or packages from your system

alias: remove

yum命令摘要

命令名称描述/用法
yum check-update显示可用软件包更新的列表
yum update
yum update pkg1
更新所有软件包或者更新pkg1软件包
yum install pkg安装软件包
yum localinstall pkg.rpm从名为pkg.rpm的文件安装软件包。
yum erase pkg删除软件包
yum remove pkg与上述相同
yum autoremove通过删除不需要的软件包来释放磁盘空间
yum reinstall pkg重新安装软件包
yum downgrade pkg将软件包降级到较旧的版本
yum check检查rpm数据库中的错误/问题
yum clean packages删除缓存的软件包数据库
yum clean all从磁盘缓存中删除所有软件包和元数据
yum list
yum list installed
yum list php
yum list available
列出软件包名称
yum deplist pkg1显示pkg1的依赖项
yum info pkg显示有关包装的信息
yum search pkg
yum search regex
搜索包名称
yum provides string
yum whatprovides string
提供给定文件或者其他信息的列表包
yum history list显示所有yum命令历史记录操作的列表,例如install/update/erase
yum history info ID获取yum操作ID的信息
yum history undo ID从ID撤消yum命令操作
yum history redor ID从ID中重做yum命令动作
yum grouplist列出软件包组
yum groupinstall 组名在给定组名中安装所有软件包
yum groupinfo 组名查看给定组名中的软件包
yum groupremove Group Name删除/删除所选组中的所有软件包
yum repolist显示所有已启用存储库的列表
yum repoinfo repoID显示有关repoID
yum repo-pkgs repoID list显示来自repoID repo的软件包
yum repo-pkgs repoID install从repoID repo安装所有软件包
yum repo-pkgs repoID remove从repoID repo中擦除所有软件包
yum repo-pkgs repoID reinstall从repoID repo重新安装所有软件包
yum help
yum help command
man yum
显示有关yum命令的帮助或者阅读yum命令手册页