Linux YUM命令详解:Useful yum Command Examples For Package Management In...
YellowdogUpdater Modifier (yum) is an RPM based package manager which is used to installand update packages in various Linux distributions including CentOS, RHEL andFedora.Yum is quitepowerful as it’s capable of automatically resolving dependency issues, and issimilar to other package managers such as ‘apt-get’ in Debian baseddistributions.These examplesshould serve as a useful introduction, guide or cheat sheet style resource forhow to use the yum command in Linux.How To Use yum – Command Examples· 1. Install New Package From RepositoryThe yum command can be used to installpackages from a repository with the ‘install’ argument, followed by the packagename. In the example below, we are installing Apache, which is provided in the‘httpd’ package.# yum install httpdResolving Dependencies--> Running transaction check---> Package httpd.x86_640:2.4.6-40.el7.centos.4 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 Transaction Summary========================================================================Install 1 Package Total download size: 2.7 MInstalled size: 9.4 MIs this ok : yDownloading packages:httpd-2.4.6-40.el7.centos.4.x86_64.rpm | 2.7 MB00:00:00Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : httpd-2.4.6-40.el7.centos.4.x86_64 1/1 Verifying:httpd-2.4.6-40.el7.centos.4.x86_64 1/1 Installed: httpd.x86_64 0:2.4.6-40.el7.centos.4 Complete!Note that after confirming the packagesthat will be installed, you will be asked to provide input. In this case weentered ‘y’ for yes to proceed with the installation, which then completedsuccessfully.· 2. Assume YesIn the first example we were prompted topress the ‘y’ key in order to proceed with the installation. Rather than beingprompted each time for user input, we can simply specify the ‘-y’ option in ourcommand for assume yes. This way we will not be prompted for any input and yumwill assume that everything will be answered with yes.# yum install httpd-manual -yResolving Dependencies--> Running transaction check---> Package httpd-manual.noarch0:2.4.6-40.el7.centos.4 will be installed--> Finished Dependency Resolution Dependencies Resolved ========================================================================== Package Arch Version Repository Size==========================================================================Installing: httpd-manual noarch 2.4.6-40.el7.centos.4 updates 1.3 M Transaction Summary==========================================================================Install 1 Package Total download size: 1.3 MInstalled size: 5.5 MDownloading packages:httpd-manual-2.4.6-40.el7.centos.4.noarch.rpm | 1.3 MB 00:00:01Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : httpd-manual-2.4.6-40.el7.centos.4.noarch 1/1Verifying:httpd-manual-2.4.6-40.el7.centos.4.noarch 1/1 Installed: httpd-manual.noarch 0:2.4.6-40.el7.centos.4 Complete!
Note that in this example we were notprompted for input, as assume yes has been provided, so the package wasinstalled fully without any further user interaction.· 3. Check For Available UpdatesWe can run yum with the ‘check-update’argument which will check in with our enabled repositories for any updates thatmay be available for packages that we have installed. This does not actuallyperform any updates, it simply gives us a list of packages that have updatesavailable for installation.# yum check-updateLoaded plugins: fastestmirrorLoading mirror speeds from cachedhostfile bash.x86_64 4.2.46-20.el7_2 updatesdbus.x86_64 1:1.6.12-14.el7_2 updatesdbus-libs.x86_64 1:1.6.12-14.el7_2 updatesdevice-mapper-persistent-data.x86_64 0.6.2-1.el7_2 updatesglibc.x86_64 2.17-106.el7_2.8 updatesglibc-common.x86_64 2.17-106.el7_2.8 updatesinitscripts.x86_64 9.49.30-1.el7_2.3 updateskernel.x86_64 3.10.0-327.28.3.el7 updateskernel-tools.x86_64 3.10.0-327.28.3.el7 updateskernel-tools-libs.x86_64 3.10.0-327.28.3.el7 updates...
· 4. Update New Package From RepositoryIn the previous example we have beenprovided with a list of packages that currently have package updates available.We can perform an update to a specific package by specifying it after the‘update’ argument, as shown below.# yum update bash -yResolving Dependencies--> Running transaction check---> Package bash.x86_640:4.2.46-19.el7 will be updated---> Package bash.x86_640:4.2.46-20.el7_2 will be an update--> Finished Dependency Resolution Dependencies Resolved ================================================================= Package Arch Version Repository Size=================================================================Updating: bash x86_64 4.2.46-20.el7_2 updates 1.0 M Transaction Summary=================================================================Upgrade 1 Package Total download size: 1.0 MDownloading packages:Delta RPMs disabled because/usr/bin/applydeltarpm not installed.bash-4.2.46-20.el7_2.x86_64.rpm | 1.0 MB00:00:00Running transaction checkRunning transaction testTransaction test succeededRunning transaction Updating :bash-4.2.46-20.el7_2.x86_64 1/2 Cleanup :bash-4.2.46-19.el7.x86_64 2/2 Verifying:bash-4.2.46-20.el7_2.x86_64 1/2 Verifying:bash-4.2.46-19.el7.x86_64 2/2 Updated: bash.x86_64 0:4.2.46-20.el7_2 Complete!
Note that we do not have to runcheck-update prior to this.While we can update specified packagesin this manner, it is often easier to simply apply all available updates forall packages on the system with ‘yum update’.· 5. Download Package RPM FileRather than installing a package fromthe repository as demonstrated previously, we can optionally download the .rpmfile from the repository with the ‘yumdownloader’ command.The ‘yumdownloader’ command comes fromthe ‘yum-utils’ package, so we install this first.# yum install yum-utils -y
Now we can download the RPM file to thelocal system.# yumdownloader unboundLoaded plugins: fastestmirrorLoading mirror speeds from cachedhostfile *base: mirror.ventraip.net.au *extras: mirror.optus.net *updates: mirror.optus.netunbound-1.4.20-26.el7.x86_64.rpm# ls -la unbound-1.4.20-26.el7.x86_64.rpm-rw-r--r--. 1 root root 483136 Nov262015 unbound-1.4.20-26.el7.x86_64.rpm
· 6. Perform Local Install Of RPM FileIn this example we will install the .rpmfile that we just downloaded with ‘yumdownloader’. While we could have justinstalled this package from the repository directly with ‘yum install’,sometimes you may be provided with .rpm files directly for installation so it’sworth knowing how to install them.While .rpm files can be installed withthe ‘rpm’ command, using yum has the advantage of automatically detecting andinstalling any other package dependencies that may also be required.# yum localinstall unbound-1.4.20-26.el7.x86_64.rpmLoaded plugins: fastestmirrorExaminingunbound-1.4.20-26.el7.x86_64.rpm: unbound-1.4.20-26.el7.x86_64Marking unbound-1.4.20-26.el7.x86_64.rpmto be installedResolving Dependencies--> Running transaction check---> Package unbound.x86_640:1.4.20-26.el7 will be installed--> Processing Dependency:unbound-libs(x86-64) = 1.4.20-26.el7 for package: unbound-1.4.20-26.el7.x86_64Loading mirror speeds from cachedhostfile *base: mirror.ventraip.net.au *extras: mirror.optus.net *updates: mirror.optus.net--> Processing Dependency: ldns >=1.6.13 for package: unbound-1.4.20-26.el7.x86_64--> Processing Dependency:libevent-2.0.so.5()(64bit) for package: unbound-1.4.20-26.el7.x86_64--> Processing Dependency: libldns.so.1()(64bit)for package: unbound-1.4.20-26.el7.x86_64--> Processing Dependency:libunbound.so.2()(64bit) for package: unbound-1.4.20-26.el7.x86_64--> Running transaction check---> Package ldns.x86_640:1.6.16-7.el7 will be installed---> Package libevent.x86_640:2.0.21-4.el7 will be installed---> Package unbound-libs.x86_640:1.4.20-26.el7 will be installed--> Finished Dependency Resolution Dependencies Resolved ========================================================================================= Package Arch Version Repository Size=========================================================================================Installing: unbound x86_64 1.4.20-26.el7 /unbound-1.4.20-26.el7.x86_64 1.6 MInstalling for dependencies: ldns x86_64 1.6.16-7.el7 base 473 k libevent x86_64 2.0.21-4.el7 base 214 k unbound-libs x86_64 1.4.20-26.el7 base 296 k Transaction Summary=========================================================================================Install 1 Package (+3 Dependent packages) Total size: 2.6 MTotal download size: 982 kInstalled size: 4.4 MIs this ok : yDownloading packages:(1/3):unbound-libs-1.4.20-26.el7.x86_64.rpm | 296 kB00:00:00(2/3): ldns-1.6.16-7.el7.x86_64.rpm | 473 kB00:00:00(3/3):libevent-2.0.21-4.el7.x86_64.rpm | 214 kB00:00:00-----------------------------------------------------------------------------------------Total 1.5 MB/s | 982 kB00:00Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : ldns-1.6.16-7.el7.x86_64 1/4 Installing : libevent-2.0.21-4.el7.x86_64 2/4 Installing : unbound-libs-1.4.20-26.el7.x86_64 3/4 Installing : unbound-1.4.20-26.el7.x86_64 4/4 Verifying:unbound-1.4.20-26.el7.x86_64 1/4 Verifying:unbound-libs-1.4.20-26.el7.x86_64 2/4 Verifying:libevent-2.0.21-4.el7.x86_64 3/4 Verifying:ldns-1.6.16-7.el7.x86_64 4/4 Installed: unbound.x86_64 0:1.4.20-26.el7 Dependency Installed: ldns.x86_64 0:1.6.16-7.el7 libevent.x86_640:2.0.21-4.el7 unbound-libs.x86_64 0:1.4.20-26.el7 Complete!
· 7. Uninstall PackagePackages can be uninstalled with the‘remove’ argument, as shown below.# yum remove httpdLoaded plugins: fastestmirrorResolving Dependencies--> Running transaction check---> Package httpd.x86_640:2.4.6-40.el7.centos.4 will be erased--> Processing Dependency: httpd =2.4.6-40.el7.centos.4 for package: httpd-manual-2.4.6-40.el7.centos.4.noarch--> Running transaction check---> Package httpd-manual.noarch0:2.4.6-40.el7.centos.4 will be erased--> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================= Package Arch Version Repository Size=============================================================================================================================================================================================Removing: httpd x86_64 2.4.6-40.el7.centos.4 @updates 9.4 MRemoving for dependencies: httpd-manual noarch 2.4.6-40.el7.centos.4 @updates 5.5 M Transaction Summary=============================================================================================================================================================================================Remove 1 Package (+1 Dependent package) Installed size: 15 MIs this ok : yDownloading packages:Running transaction checkRunning transaction testTransaction test succeededRunning transaction Erasing :httpd-manual-2.4.6-40.el7.centos.4.noarch 1/2 Erasing :httpd-2.4.6-40.el7.centos.4.x86_64 2/2 Verifying:httpd-manual-2.4.6-40.el7.centos.4.noarch 1/2 Verifying:httpd-2.4.6-40.el7.centos.4.x86_64 2/2 Removed:httpd.x86_640:2.4.6-40.el7.centos.4 Dependency Removed: httpd-manual.noarch 0:2.4.6-40.el7.centos.4 Complete!
Note that in order to remove a package,all other packages which depend on it will also be removed. For that reason itis recommended not to use the -y option to ‘assumeyes’ when removing a packageso that you get a chance to read what will be removed before proceeding, as itmay be more than you expect.In the above example, the httpd-manualpackage that we installed previously requires the httpd package. Therefore whenwe remove the httpd package, httpd-manual must go too.· 8. Reinstall PackageIf you’re having a problem with apackage, you can easily reinstall it with the ‘reinstall’ argument.# yum reinstall httpd -yLoaded plugins: fastestmirrorLoading mirror speeds from cachedhostfile *base: mirror.ventraip.net.au *extras: mirror.optus.net *updates: mirror.optus.netResolving Dependencies--> Running transaction check---> Package httpd.x86_640:2.4.6-40.el7.centos.4 will be reinstalled--> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================= Package Arch Version Repository Size=============================================================================================================================================================================================Reinstalling: httpd x86_64 2.4.6-40.el7.centos.4 updates 2.7 M Transaction Summary=============================================================================================================================================================================================Reinstall1 Package Total download size: 2.7 MInstalled size: 9.4 MDownloading packages:httpd-2.4.6-40.el7.centos.4.x86_64.rpm |2.7 MB00:00:00Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : httpd-2.4.6-40.el7.centos.4.x86_64 1/1 Verifying:httpd-2.4.6-40.el7.centos.4.x86_64 1/1 Installed: httpd.x86_64 0:2.4.6-40.el7.centos.4 Complete!
This is similar to performing a ‘yumremove’ followed by a ‘yum install’, however here it’s done in the one commandand custom configuration is not removed, as a ‘yum remove’ will delete allconfiguration files managed by the package.· 9. View Repository InformationBy default we have many repositoriesconfigured in the /etc/yum.repos.d/ directory, these can be viewed with the ‘repolist’and ‘repoinfo’ arguments.# yum repolistLoaded plugins: fastestmirrorLoading mirror speeds from cachedhostfile *base: mirror.ventraip.net.au *extras: mirror.optus.net *updates: mirror.optus.netrepo id repo name statusbase/7/x86_64 CentOS-7 - Base 9,007extras/7/x86_64 CentOS-7 - Extras 375updates/7/x86_64 CentOS-7 - Updates 2,303repolist: 11,685
By default ‘repolist’ displaysinformation for enabled repositories only, however we can specify ‘all’afterwards to list even disabled repositories.# yum repolist all repo id repo name statusbase/7/x86_64 CentOS-7 - Base enabled: 9,007base-debuginfo/x86_64 CentOS-7 - Debuginfo disabledbase-source/7 CentOS-7 - Base Sources disabledc7-media CentOS-7 - Media disabledcentosplus/7/x86_64 CentOS-7 - Plus disabled...
Further information on a specificrepository can then be viewed with ‘repoinfo’ followed by the repo name thatyou want to query.# yum repoinfo CentOS-7 - BaseLoaded plugins: fastestmirrorLoading mirror speeds from cachedhostfile *base: mirror.ventraip.net.au *extras: mirror.optus.net *updates: mirror.optus.netRepo-id : base/7/x86_64Repo-name : CentOS-7 - BaseRepo-status: enabledRepo-revision: 1449700451Repo-updated : Thu Dec 10 09:35:45 2015Repo-pkgs : 9,007Repo-size : 6.5 GRepo-mirrors :http://mirrorlist.centos.org/?re ... repo=os&infra=stockRepo-baseurl :http://mirror.ventraip.net.au/CentOS/7.2.1511/os/x86_64/ (9 more)Repo-expire: 21,600 second(s) (last: Mon Aug 29 14:25:352016)Repo-filename:/etc/yum.repos.d/CentOS-Base.repo repolist: 9,007
· 10. Add New RepositoryWe can specify a new repository bymanually editing or creating a new .repo file in the /etc/yum.repos.ddirectory, however we can much more easily create a persistent repository withthe yum-config-manager command. This command comes from the ‘yum-utils’ packagewhich we installed previously, so if you wish to use ‘yum-config-manager’ besure to first install ‘yum-utils’.# yum-config-manager--add-repo="https://mirror.aarnet.edu.au/pub/centos/7"Loaded plugins: fastestmirroradding repo from:https://mirror.aarnet.edu.au/pub/centos/7 name=added from:https://mirror.aarnet.edu.au/pub/centos/7baseurl=https://mirror.aarnet.edu.au/pub/centos/7enabled=1
We simply provide a known repository URLin the –add-repo option, and this creates a .repo file based on the name of therepository URL specified. In this case,/etc/yum.repos.d/mirror.aarnet.edu.au_pub_centos_7.repo has been created withthe contents specified above in the output of the command. We can now installor update packages from this newly specified mirror.· 11. Enable Or Disable Specific RepositoryThere may be times when we want toperform an action while enabling or disabling a specific repository. Forexample if we want to install a particular package, we may want to ensure thatit comes from a specific mirror.yum --enablerepo=disabled-repo installpackage-example
This would enabled a previously disabledrepo named ‘disabled-repo’ in order to install ‘package-example’ from it.If we have the same packages availableat multiple repositories we may wish to disable one and update or install froma specific repository. This can be done by disabling the repository with--disablerepo.yum --disablerepo=enabled-repo installpackage-example
· 12. Search For Packages In RepositoryThe ‘search’ argument is a very usefulyum feature, as it allows us to search through packages on the repository whichcan be useful if we don’t know exactly what we’re looking to install.# yum search php=====================================================================================N/S matched: php======================================================================================graphviz-php.x86_64 : PHP extension forgraphvizphp.x86_64 : PHP scripting language forcreating dynamic web sitesphp-bcmath.x86_64 : A module for PHPapplications for using the bcmath libraryphp-cli.x86_64 : Command-line interfacefor PHPphp-common.x86_64 : Common files for PHPphp-dba.x86_64 : A database abstractionlayer module for PHP applicationsphp-devel.x86_64 : Files needed forbuilding PHP extensionsphp-embedded.x86_64 : PHP library forembedding in applicationsphp-enchant.x86_64 : Enchant spellingextension for PHP applicationsphp-fpm.x86_64 : PHP FastCGI ProcessManagerphp-gd.x86_64 : A module for PHPapplications for using the gd graphics library... Name and summary matches only, use "search all" foreverything.
In this example we search package namesand the summary for the term “PHP” and see a number of available packages atthe repositories ready for us to install or download.· 13. Find Which Package Provides SomethingThis is one of my favourite features ofthe yum command as it’s very useful. Have you ever wanted to run a command, butyou’re not sure which package is required for it? This is where the ‘provides’argument comes in use.In this example we want to create aniSCSI initiator, this is done with the ‘iscsiadm’ command. We can use‘provides’ followed by the name of the command to search for packages thatprovide this command.# yum provides iscsiadmiscsi-initiator-utils-6.2.0.873-32.el7.i686: iSCSI daemon and utility programsRepo : baseMatched from:Filename : /usr/sbin/iscsiadm
As shown the ‘iscsi-initiator-utils’package can be installed to provide the ‘iscsiadm’ command.This can also be used against a file onthe file system to check which package provides that file.# yum provides /etc/httpd/conf/httpd.confhttpd-2.4.6-40.el7.centos.x86_64 :Apache HTTP ServerRepo : baseMatched from:Filename : /etc/httpd/conf/httpd.conf
In this example we can see that the/etc/httpd/conf/httpd.conf file comes from the ‘httpd’ package.· 14. View Package InformationFurther detailed information of apackage can be viewed with the ‘info’ argument. As shown below, this providesus with useful information such as the architecture, version number, releasenumber, total size, as well as a description of what the package actually does.# yum info httpdAvailable PackagesName : httpdArch : x86_64Version : 2.4.6Release : 40.el7.centos.4Size : 2.7 MRepo : updates/7/x86_64Summary : Apache HTTP ServerURL : http://httpd.apache.org/License : ASL 2.0Description : The Apache HTTP Server isa powerful, efficient, and extensible : web server.
This can be used before a package isinstalled or after installation, the same information will be provided in bothsituations.· 15. View Transaction HistoryThe yum history contains all actionsthat have been performed by the yum command. This allows us to see what hashappened to our Linux system over time, and even undo, redo, or roll back atransaction.# yum historyID | Login user | Date andtime | Action(s) | Altered------------------------------------------------------------------------ 15 | root | 2016-08-2915:20 | Erase | 1 14 | root | 2016-08-2914:55 | Install | 1 13 | root | 2016-08-2914:55 | Erase | 1 EE 12 | root | 2016-08-2914:54 | Reinstall | 1 11 | root | 2016-08-2914:54 | Install | 31 10 | root | 2016-08-2914:53 | Install | 1 9 | root | 2016-08-2914:47 | Erase | 2 8 | root | 2016-08-2914:45 | Install | 4 7 | root | 2016-08-2914:40 | Install | 3 6 | root | 2016-08-2914:37 | Update | 1 5 | root | 2016-08-2914:32 | Install | 1 4 | root | 2016-08-2914:28 | Install | 1 3 | root | 2016-08-2914:28 | Erase | 1 2 | root | 2016-08-2914:28 | Install | 5 1 | System | 2016-08-2914:17 | Install |298history list
The newest transactions appear at thetop of the list, and we can view further information of a transaction byspecifying its ID, as shown below.# yum history info 15Transaction ID : 15Begin time : Mon Aug 29 15:20:57 2016Begin rpmdb :341:c62a7835aef7986909a89bc51d654acf51b8d756End time : (0 seconds)End rpmdb :340:8701e1360e8e5403f96a5cc61994740ba7d8daf8User : root Return-Code : SuccessCommand Line : remove httpd -yTransaction performed with: Installed rpm-4.11.3-17.el7.x86_64 @anaconda Installed yum-3.4.3-132.el7.centos.0.1.noarch @anaconda Installed yum-plugin-fastestmirror-1.1.31-34.el7.noarch @anacondaPackages Altered: Erase httpd-2.4.6-40.el7.centos.4.x86_64 @updateshistory info
So the most recent transaction with theID of 15 was used to remove the httpd package. Now we can undo this transactionif we want, which should install the httpd package again, as this will undo theremoval.# yum history undo 15 -yUndoing transaction 15, from Mon Aug 2915:20:57 2016 Erase httpd-2.4.6-40.el7.centos.4.x86_64 @updatesResolving Dependencies--> Running transaction check---> Package httpd.x86_640:2.4.6-40.el7.centos.4 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 Transaction Summary============================================================================Install 1 Package Total download size: 2.7 MInstalled size: 9.4 MDownloading packages:httpd-2.4.6-40.el7.centos.4.x86_64.rpm | 2.7 MB00:00:00Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : httpd-2.4.6-40.el7.centos.4.x86_64 1/1 Verifying:httpd-2.4.6-40.el7.centos.4.x86_64 1/1 Installed: httpd.x86_64 0:2.4.6-40.el7.centos.4 Complete!
We can then run the removal again with‘yum history redo’.# yum history redo 15 -yRepeating transaction 15, from Mon Aug29 15:20:57 2016 Erase httpd-2.4.6-40.el7.centos.4.x86_64 @updatesResolving Dependencies--> Running transaction check---> Package httpd.x86_640:2.4.6-40.el7.centos.4 will be erased--> Finished Dependency Resolution Dependencies Resolved ========================================================================== Package Arch Version Repository Size==========================================================================Removing: httpd x86_64 2.4.6-40.el7.centos.4@updates 9.4 M Transaction Summary==========================================================================Remove 1 Package Installed size: 9.4 MDownloading packages:Running transaction checkRunning transaction testTransaction test succeededRunning transaction Erasing :httpd-2.4.6-40.el7.centos.4.x86_641/1 Verifying:httpd-2.4.6-40.el7.centos.4.x86_641/1 Removed: httpd.x86_64 0:2.4.6-40.el7.centos.4 Complete!
It’s also worth noting that packageinstallations, updates, or removals are also logged in the /var/log/yum.logfile, here’s an example of what’s logged here.# tail /var/log/yum.logAug 29 14:28:24 Installed:httpd-2.4.6-40.el7.centos.4.x86_64Aug 29 14:28:29 Erased:httpd-2.4.6-40.el7.centos.4.x86_64Aug 29 14:28:41 Installed:httpd-2.4.6-40.el7.centos.4.x86_64Aug 29 14:32:55 Installed:httpd-manual-2.4.6-40.el7.centos.4.noarchAug 29 14:37:28 Updated:bash-4.2.46-20.el7_2.x86_64
· 16. Clear Cached InformationBy default yum will cache data to the/var/cache/yum directory, such as package and repository data. This speeds upyum so that it doesn’t have to keep querying this information from theInternet.There are times when you may want todelete this cached data, such as if a repository has updated packages but yourLinux system has incorrect or stale cached data which may cause variousproblems when attempting to install a package.# yum clean allLoaded plugins: fastestmirrorCleaning repos: base extras updatesCleaning up everythingCleaning up list of fastest mirrors
This will remove all cached items, it ispossible to otherwise specify specific elements to clear (such as packages,metadata, expire-cache, rpmdb or plugins) however this saves a lot of time ifyou aren’t sure what should be removed.· 17. Build CacheThe yum cache will be automaticallybuilt up over time when you perform various yum queries such as installing orupdating packages, however we have the option to manually make the cache sothat future actions will be quicker with the ‘makecache’ argument.# time yum makecacheLoaded plugins: fastestmirror, langpacksbase |3.6 kB00:00:00extras | 3.4kB00:00:00updates | 3.4 kB 00:00:00(1/12): base/7/x86_64/group_gz | 155 kB00:00:00(2/12): extras/7/x86_64/prestodelta | 72 kB00:00:00(3/12):extras/7/x86_64/filelists_db |426 kB00:00:00(4/12): extras/7/x86_64/primary_db | 160 kB00:00:00(5/12): base/7/x86_64/filelists_db | 6.2 MB00:00:01(6/12): extras/7/x86_64/other_db | 652 kB00:00:00(7/12): base/7/x86_64/other_db | 2.3 MB00:00:01(8/12): updates/7/x86_64/prestodelta | 698 kB00:00:00(9/12): base/7/x86_64/primary_db | 5.3 MB00:00:01(10/12):updates/7/x86_64/filelists_db |4.4 MB00:00:01(11/12):updates/7/x86_64/primary_db |7.1 MB00:00:01(12/12): updates/7/x86_64/other_db | 61 MB00:00:07Determining fastest mirrors *base: centos.mirror.crucial.com.au *extras: centos.mirror.crucial.com.au *updates: centos.mirror.crucial.com.auMetadata Cache Created real 0m40.785suser 0m32.504ssys 0m1.937s
We can also add the ‘fast’ option to theend, which will take less time but only cache fewer repositories.# time yum makecache fastLoaded plugins: fastestmirror, langpacksbase | 3.6 kB 00:00:00extras | 3.4kB00:00:00updates | 3.4 kB00:00:00(1/4): base/7/x86_64/group_gz | 155 kB00:00:00(2/4): extras/7/x86_64/primary_db | 160 kB00:00:00(3/4): base/7/x86_64/primary_db | 5.3 MB00:00:00(4/4): updates/7/x86_64/primary_db | 7.1 MB 00:00:01Determining fastest mirrors *base: mirror.nsw.coloau.com.au *extras: mirror.nsw.coloau.com.au *updates: mirror.nsw.coloau.com.auMetadata Cache Created real 0m8.879suser 0m3.451ssys 0m0.343s
As we can see the fast cache wassignificantly faster.· 18. List Installed PackagesWe have the ability to list all packagesthat are currently installed on our Linux system with ‘yum list installed’. Ihave not included the full output here as I have hundreds of packages installedon my system, however this small snippet should give you an idea of what toexpect.# yum list installed | head -n 15Installed PackagesNetworkManager.x86_64 1:1.0.6-30.el7_2 @anacondaNetworkManager-libnm.x86_64 1:1.0.6-30.el7_2 @anacondaNetworkManager-team.x86_64 1:1.0.6-30.el7_2 @anacondaNetworkManager-tui.x86_64 1:1.0.6-30.el7_2 @anacondaNetworkManager-wifi.x86_64 1:1.0.6-30.el7_2 @anacondaacl.x86_64 2.2.51-12.el7 @anacondaaic94xx-firmware.noarch 30-6.el7 @anacondaalsa-firmware.noarch 1.0.28-2.el7 @anacondaalsa-lib.x86_64 1.0.28-2.el7 @anacondaalsa-tools-firmware.x86_64 1.0.28-2.el7 @anacondaapr.x86_64 1.4.8-3.el7 @baseapr-util.x86_64 1.5.2-6.el7 @baseaudit.x86_64 2.4.1-5.el7 @anaconda...
When piped into the grep command thisbecomes extremely powerful as it allows us to search for a specific package tosee if it is installed.# yum list installed | grep httpdhttpd.x86_64 2.4.6-40.el7.centos.4 @updateshttpd-tools.x86_64 2.4.6-40.el7.centos.4 @updates
· 19. List Available PackagesIn the above example we learned how tolist currently installed packages on our Linux system with the yum command, nowwe want to be able to query a repository and list its available packagesinstead.This is quite similar, except that wereplace ‘installed’ with ‘available’ to instead list all available packages.# yum list installed | wc -l343# yum list available | wc -l8972
As we can see we have over 300 packagescurrently installed, however in the repository there are almost 9000 packagesin total for us to install.We can also run ‘yum list all’ to listboth installed and available packages together, or ‘yum list updates’ to onlylist packages which currently have updates available, the output of which issimilar to the ‘yum check-update’ command covered previously.· 20. Package GroupsPackages that are related to each othermay be grouped together into a package group. This allows us to install,update, or remove the package group which is actually made up or many otherindividual packages.For example we can run ‘yum grouplist’to view the groups that are available for us to use by default.# yum grouplistThere is no installed groups file.Maybe run: yum groups mark convert (seeman yum)Available Environment Groups:Minimal InstallCompute NodeInfrastructure ServerFile and Print ServerBasic Web ServerVirtualization HostServer with GUIGNOME DesktopKDE Plasma WorkspacesDevelopment and Creative WorkstationAvailable Groups:Compatibility LibrariesConsole Internet ToolsDevelopment ToolsGraphical Administration ToolsLegacy UNIX CompatibilityScientific SupportSecurity ToolsSmart Card SupportSystem Administration ToolsSystem ManagementDone
Once we have the list of availablegroups, we can use ‘yum groupinfo’ on any of them to get an idea of thepackages that are included as part of the group.# yum groupinfo "Basic Web Server"There is no installed groups file.Maybe run: yum groups mark convert (seeman yum) Environment Group: Basic Web Server Environment-Id: web-server-environment Description: Server for serving static anddynamic internet content. Mandatory Groups:+base+core+web-server # yum groupinfo "web-server"There is no installed groups file.Maybe run: yum groups mark convert (seeman yum) Group: Web Server Group-Id: web-server Description: Allows the system to act as a webserver, and run Perl and Python web applications. Mandatory Packages: httpd Default Packages:+crypto-utils+httpd-manual+mod_fcgid+mod_ssl
Here we can see the “Basic Web Server”group is actually made up of three other groups, base, core, and web-server. Ifwe look closer at the web-server group we can see that it uses the httpdpackage as well as some additional ones that we do not yet have installed butare available, as denoted by the ‘+’ symbol.Finally we can install the group withthe ‘yum groupinstall’ command, as shown below.# yum groupinstall "web server" -yThere is no installed groups file.Maybe run: yum groups mark convert (seeman yum)Resolving Dependencies--> Running transaction check---> Package crypto-utils.x86_640:2.4.1-42.el7 will be installed--> Processing Dependency: perl(Newt)for package: crypto-utils-2.4.1-42.el7.x86_64---> Package httpd-manual.noarch0:2.4.6-40.el7.centos.4 will be installed---> Package mod_fcgid.x86_640:2.3.9-4.el7 will be installed---> Package mod_ssl.x86_641:2.4.6-40.el7.centos.4 will be installed--> Running transaction check---> Package perl-Newt.x86_640:1.08-36.el7 will be installed--> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================= Package Arch Version Repository Size=============================================================================================================================================================================================Installing for group install "WebServer": crypto-utils x86_64 2.4.1-42.el7 base 78 k httpd-manual noarch 2.4.6-40.el7.centos.4 updates 1.3 M mod_fcgid x86_64 2.3.9-4.el7 base 79 k mod_ssl x86_64 1:2.4.6-40.el7.centos.4 updates 104 kInstalling for dependencies: perl-Newt x86_64 1.08-36.el7 base 64 k Transaction Summary=============================================================================================================================================================================================Install 4 Packages (+1 Dependent package) Total download size: 1.6 MInstalled size: 6.2 MDownloading packages:(1/5):crypto-utils-2.4.1-42.el7.x86_64.rpm |78 kB 00:00:00(2/5):perl-Newt-1.08-36.el7.x86_64.rpm |64 kB00:00:00(3/5): mod_fcgid-2.3.9-4.el7.x86_64.rpm |79 kB00:00:00(4/5):mod_ssl-2.4.6-40.el7.centos.4.x86_64.rpm |104 kB00:00:00(5/5):httpd-manual-2.4.6-40.el7.centos.4.noarch.rpm | 1.3 MB00:00:00---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Total 2.5MB/s | 1.6 MB00:00:00Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : perl-Newt-1.08-36.el7.x86_64 1/5 Installing : crypto-utils-2.4.1-42.el7.x86_64 2/5 Installing : 1:mod_ssl-2.4.6-40.el7.centos.4.x86_64 3/5 Installing : httpd-manual-2.4.6-40.el7.centos.4.noarch 4/5 Installing : mod_fcgid-2.3.9-4.el7.x86_64 5/5 Verifying:perl-Newt-1.08-36.el7.x86_64 1/5 Verifying:mod_fcgid-2.3.9-4.el7.x86_64 2/5 Verifying:crypto-utils-2.4.1-42.el7.x86_64 3/5 Verifying:httpd-manual-2.4.6-40.el7.centos.4.noarch 4/5 Verifying:1:mod_ssl-2.4.6-40.el7.centos.4.x86_64 5/5 Installed: crypto-utils.x86_64 0:2.4.1-42.el7 httpd-manual.noarch0:2.4.6-40.el7.centos.4 mod_fcgid.x86_64 0:2.3.9-4.el7 mod_ssl.x86_64 1:2.4.6-40.el7.centos.4 Dependency Installed: perl-Newt.x86_64 0:1.08-36.el7 Complete!
The group can also be updated with ‘yumgroupupdate’, or all packages within the group removed with ‘yum groupremove’.· 21. Install Security Updates OnlyWhile we can simply perform a ‘yumupdate’ to update all of our installed packages to the latest versions, thismay not be ideal in a production environment. Being able to install securityonly updates during patching may be a requirement in your environment, and canbe done with ‘yum update --security’.# yum update --security
· 22. Exclude Specific PackageWe can optionally exclude a specificpackage from update, installation or removal with the -x command which willexclude that package. In this example, we perform a ‘yum check-update’ andconfirm that a kernel update is available, however we do not wish to installthis package at this time. With the help of -x, we ignore the kernel update andinstead perform a ‘yum update’ which states that it is going to updateeverything except the kernel, which is not listed.# yum check-update device-mapper-persistent-data.x86_64 0.6.2-1.el7_2 updatesinitscripts.x86_64 9.49.30-1.el7_2.3 updateskernel.x86_64 3.10.0-327.28.3.el7 updateskernel-tools.x86_64 3.10.0-327.28.3.el7 updateskernel-tools-libs.x86_64 3.10.0-327.28.3.el7 updatesmicrocode_ctl.x86_64 2:2.1-12.el7_2.1 updatespython-perf.x86_64 3.10.0-327.28.3.el7 updates # yum update -x kernelResolving Dependencies--> Running transaction check---> Packagedevice-mapper-persistent-data.x86_64 0:0.5.5-1.el7 will be updated---> Packagedevice-mapper-persistent-data.x86_64 0:0.6.2-1.el7_2 will be an update---> Package initscripts.x86_640:9.49.30-1.el7_2.2 will be updated---> Package initscripts.x86_640:9.49.30-1.el7_2.3 will be an update---> Package kernel-tools.x86_640:3.10.0-327.22.2.el7 will be updated---> Package kernel-tools.x86_640:3.10.0-327.28.3.el7 will be an update---> Package kernel-tools-libs.x86_640:3.10.0-327.22.2.el7 will be updated---> Package kernel-tools-libs.x86_640:3.10.0-327.28.3.el7 will be an update---> Package microcode_ctl.x86_642:2.1-12.el7 will be updated---> Package microcode_ctl.x86_642:2.1-12.el7_2.1 will be an update---> Package python-perf.x86_640:3.10.0-327.22.2.el7 will be updated---> Package python-perf.x86_640:3.10.0-327.28.3.el7 will be an update--> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================= Package Arch Version Repository Size=============================================================================================================================================================================================Updating: device-mapper-persistent-data x86_64 0.6.2-1.el7_2 updates 366 k initscripts x86_64 9.49.30-1.el7_2.3 updates 429 k kernel-tools x86_64 3.10.0-327.28.3.el7 updates 2.4 M kernel-tools-libs x86_64 3.10.0-327.28.3.el7 updates 2.3 M microcode_ctl x86_64 2:2.1-12.el7_2.1 updates 535 k python-perf x86_64 3.10.0-327.28.3.el7 updates 2.4 M Transaction Summary=============================================================================================================================================================================================Upgrade 6 Packages Total download size: 8.4 MIs this ok :
· 23. Add Random Wait TimeThere are times where you may want toinsert a random period of time before performing some yum command, for exampleif you manage hundreds of servers and go to update them all on a particular dayyou probably don’t want them all to hit the repository at the same time as thismay cause the process to run very slow. With the -R option we can specify amaximum random time in minutes that yum will wait before executing the command,somewhere within this time period will be selected at random.# time yum remove httpd -y -R 2Loaded plugins: fastestmirrorResolving Dependencies--> Running transaction check---> Package httpd.x86_640:2.4.6-40.el7.centos.4 will be erased--> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================= Package Arch Version Repository Size=============================================================================================================================================================================================Removing: httpd x86_64 2.4.6-40.el7.centos.4 @updates 9.4 M Transaction Summary=============================================================================================================================================================================================Remove 1 Package Installed size: 9.4 MDownloading packages:Running transaction checkRunning transaction testTransaction test succeededRunning transaction Erasing :httpd-2.4.6-40.el7.centos.4.x86_64 1/1 Verifying:httpd-2.4.6-40.el7.centos.4.x86_64 1/1 Removed: httpd.x86_64 0:2.4.6-40.el7.centos.4 Complete! real 1m21.954suser 0m0.409ssys 0m0.156s
In this example I appended the ‘time’command to the ‘yum’ command so that we could see how long it took to complete,in this example we can see that this took approximately 1 minute and 22 secondsto complete, which is a random period from 0 to 2 minutes as was specified.· 24. Hide All OutputAs you have seen in the above examples,the yum command can provide quite a bit of output. If we don’t want to displaythe output messages we can simply specify the -q option for quiet which willhide all of the output. In the example below we completely uninstall a packagewith yum and have no messages output.# yum install httpd -y -q#
· 25. Display Verbose InformationAlternatively if you’re troubleshootingyou can specify the -v option to enable verbose output. In the below verboseexample we can see additional information in the output that was not previouslyavailable.# yum remove httpd -y -vLoading "fastestmirror" pluginConfig time: 0.007Yum version: 3.4.3rpmdb time: 0.000Resolving Dependencies--> Running transaction check---> Package httpd.x86_640:2.4.6-40.el7.centos.4 will be erasedChecking deps for httpd.x86_640:2.4.6-40.el7.centos.4 - e--> Finished Dependency ResolutionDependency Process endingDepsolve time: 0.220 Dependencies Resolved ========================================================================= Package Arch Version Repository Size=========================================================================Removing: httpd x86_64 2.4.6-40.el7.centos.4@updates 9.4 M Transaction Summary=========================================================================Remove 1 Package Installed size: 9.4 MDownloading packages:Member: httpd.x86_640:2.4.6-40.el7.centos.4 - eRemoving Packagehttpd-2.4.6-40.el7.centos.4.x86_64Running transaction checkTransaction check time: 0.001Running transaction testTransaction test succeededTransaction test time: 0.014Running transaction Erasing :httpd-2.4.6-40.el7.centos.4.x86_64 1/1 Verifying:httpd-2.4.6-40.el7.centos.4.x86_64 1/1VerifyTransaction time: 0.049Transaction time: 0.425 Removed: httpd.x86_64 0:2.4.6-40.el7.centos.4 Complete!
SummaryThis guide hascovered how to use yum with 25 different examples in which you can practicallyput the yum command to use.
页:
[1]