21 Useful Yum Command Examples on RHEL 7 and CentOS 7 Servers

Yum stands for Yellowdog Updater Modified and it is a package manager, which helps us to install, remove, and update rpm packages on CentOS and RHEL systems. Package manager according to wiki are tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer’s operating system in a consistent manner.

Yum helps to resolve dependencies during installation of any software (rpms). Let’s understand it by an example, let’s assume we want to setup MySQL database in our system. In order to use MySQL in our system we need install software or rpm with name MySQL . Now we have two options to install mysql rpm either with rpm command or yum command, if we try to install mysql using rpm command then it will through dependencies errors. But if we are using yum command then you need not to be worry about dependencies because yum resolves dependencies  by searching packages in its own repository.

In this article we will discuss 21 useful yum command examples,

Configuration file of Yum :

“/etc/yum.conf” is the main configuration file for yum command.As we know yum command is used for various purposes like installing, removing ,updating rpms. we need to create a repository i.e  central location in which data/packages/software are stored .

  • A repository can be created  by creating a file  in path :  /etc/yum.repos.d/ {yum repo name which we want to create) and then manually  entering the details.
  • By using “config-manager command”

Note: On CentOS 7 Server there is no need to configure repositories because default package repositories are configured automatically during the installation, for RHEL 7 Systems we have to create local repository in case it is not subscribed with Red Hat Satellite Server (RHN)

Let’s see YUM commands with examples:

1. Installing packages using yum:

In order to install a new package like samba we use command “ yum install”

Syntax: # yum  install package name

[root@server ~]# yum install samba -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
Resolving Dependencies
--> Running transaction check
---> Package samba.x86_64 0:4.2.3-10.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
 Package        Arch            Version               Repository           Size
================================================================================
Installing:
 samba          x86_64          4.2.3-10.el7          root_Repo_          601 k
Transaction Summary
================================================================================
Install  1 Package
Total download size: 601 k
Installed size: 1.8 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : samba-4.2.3-10.el7.x86_64                                    1/1
  Verifying  : samba-4.2.3-10.el7.x86_64                                    1/1
Installed:
samba.x86_64 0:4.2.3-10.el7                                                                                                                         
Complete!

Note: -y is for yes. It is optional.  If you don’t put -y option then while installing the package it will ask for confirmation before installing the package at that time you need  to press y if you want the software to be installed.

2. Updating package using ‘yum update’

Let’s assume we want to update a specific package like “httpd”, then use yum update command followed by package name. If no package name is mentioned then it will update every installed package.

Syntax: # yum update {pkg_name}

Let’s take an example. Let’s say we want to update httpd to the latest version then we will run following commands:

# yum update httpd

3. Re-installation of a package

Sometimes it might happen that package might not get install properly and we do not want to remove the package and install it again. Then in that case we can use reinstall.

Syntax: yum reinstall {pkg_name}

[root@server ~]# yum reinstall firefox -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
Resolving Dependencies
--> Running transaction check
---> Package firefox.x86_64 0:38.3.0-2.el7_1 will be reinstalled
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================================================================
Package                          Arch                            Version                                   Repository                           Size
======================================================================================================================================================
Reinstalling:
firefox                          x86_64                          38.3.0-2.el7_1                            root_Repo_                           72 M
Transaction Summary
======================================================================================================================================================
Reinstall  1 Package
Total download size: 72 M
Installed size: 131 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 Installing : firefox-38.3.0-2.el7_1.x86_64                                                                                                      1/1
 Verifying  : firefox-38.3.0-2.el7_1.x86_64                                                                                                      1/1
Installed:
firefox.x86_64 0:38.3.0-2.el7_1                                                                                                                    
Complete!

4. Removing a rpm package

To remove any rpm package then use ‘yum remove’ command followed by the package name

Syntax: # yum remove {pkg_name}

[root@server ~]# yum remove samba -y
Resolving Dependencies
--> Running transaction check
---> Package samba.x86_64 0:4.2.3-10.el7 will be erased
--> Processing Dependency: samba = 4.2.3-10.el7 for package: samba-python-4.2.3-10.el7.x86_64
--> Running transaction check
---> Package samba-python.x86_64 0:4.2.3-10.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================================================================================
 Package Arch Version Repository Size
============================================================================================================================================
Removing:
 samba x86_64 4.2.3-10.el7 @root_Repo_ 1.8 M
Removing for dependencies:
 samba-python x86_64 4.2.3-10.el7 @root_Repo_ 11 M
Transaction Summary
============================================================================================================================================
Remove 1 Package (+1 Dependent package)
Installed size: 12 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 Erasing : samba-python-4.2.3-10.el7.x86_64 1/2Erasing : samba-4.2.3-10.el7.x86_64 2/2
 Verifying : samba-4.2.3-10.el7.x86_64 1/2
 Verifying : samba-python-4.2.3-10.el7.x86_64 2/2
Removed:
 samba.x86_64 0:4.2.3-10.el7 
Dependency Removed:
 samba-python.x86_64 0:4.2.3-10.el7 
Complete!

5.To view all available packages from repository

In order to see what packages are available in the yum repository, we use “yum list” command

Syntax: # yum list

[root@server ~]# yum list | less
Installed Packages
GConf2.x86_64                           3.2.6-8.el7                @anaconda/7.2
ModemManager.x86_64                     1.1.0-8.git20130913.el7    @anaconda/7.2
ModemManager-glib.x86_64                1.1.0-8.git20130913.el7    @anaconda/7.2
.........................................
PyYAML.x86_64                           3.10-11.el7                @root_Repo_ 
Red_Hat_Enterprise_Linux-Release_Notes-7-en-US.noarch
                                        7-2.el7                    @anaconda/7.2
SDL.x86_64                              1.2.15-14.el7              @root_Repo_ 
abattis-cantarell-fonts.noarch          0.0.16-3.el7               @anaconda/7.2
abrt.x86_64                             2.1.11-34.el7              @anaconda/7.2
abrt-addon-ccpp.x86_64                  2.1.11-34.el7              @anaconda/7.2
abrt-addon-kerneloops.x86_64            2.1.11-34.el7              @anaconda/7.2
abrt-addon-pstoreoops.x86_64            2.1.11-34.el7              @anaconda/7.2
abrt-addon-python.x86_64                2.1.11-34.el7              @anaconda/7.2

6. Clean yum cache using ‘clean’ option

Option ‘clean all’ in yum command is used to clean yum cache directory which uses unnecessary space. When we query something then yum checks it in cache directory if it finds the result then it doesn’t have to keep querying this information from the Internet thus helps to speed up the yum.  By default yum caches data to /var/cache/yum directory, such as package and repository data Internet. But there are situations where we would want to delete this cached data, such as if a repository has updated packages but Linux system has incorrect cached data which may cause various problems during installation of the package.

# yum clean  “argument which can be  [headers|packages|metadata|dbcache|plugins|expire-cache|all]”

If we want to clean entire yum cache directory then we use “yum clean all”

[root@server ~]# yum clean all
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Cleaning repos: root_Repo_
Cleaning up everything

7. To downgrade the package

There might be situation where we don’t want the latest version of the software. And we want the older version of software then we use downgrade command but there is one condition i.e It will work only if the repository has a previous version of the mentioned package.

Syntax: # yum downgrade {pkg_name}

[root@server ~]# yum downgrade firefox

yum-downgrade-command

8. View Package Group list

yum has a concept of group in which related packages are grouped together. Instead of searching and installing all the individual packages for a particular purpose, we can simply install the group, which will install all the packages that belongs to that group. Run the below command to view names of installed and available groups

[root@server ~]# yum grouplist
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: del-mirrors.extreme-ix.org
 * extras: del-mirrors.extreme-ix.org
 * updates: del-mirrors.extreme-ix.org
Available Environment Groups:
 Minimal Install
 Compute Node
 Infrastructure Server
 File and Print Server
 Basic Web Server
 Virtualization Host
 Server with GUI
 GNOME Desktop
 KDE Plasma Workspaces
 Development and Creative Workstation
Available Groups:
 Compatibility Libraries
 Console Internet Tools
 Development Tools
 Graphical Administration Tools
 Legacy UNIX Compatibility
 Scientific Support
 Security Tools
 Smart Card Support
 System Administration Tools
 System Management
Done
[root@server ~]#

9. Install Packages with groupinstall

To install all the packages that are related to a group, in that case we can use groupinstall option in yum command. Let’s assume we want install all development related packages on Linux server,  then use the command ‘yum groupinstall’ followed by the groupname (Development Tools)

[root@server ~]# yum groupinstall 'Development Tools'

10. Removing Packages with ‘groupremove’ option

With yum command, we can remove all the package that are related to a group.

Syntax: # yum groupremove {group_name}

[root@server ~]# yum groupremove 'Development Tools'

11. View all enabled  repositories using “repolist” option

If we want to list all the enabled repositories then use ‘yum repolist’ command, example is shown below

[root@server ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: del-mirrors.extreme-ix.org
 * extras: del-mirrors.extreme-ix.org
 * updates: del-mirrors.extreme-ix.org
repo id                                        repo name                                                             status
base/7/x86_64                                  CentOS-7 - Base                                                       9,591
extras/7/x86_64                                CentOS-7 - Extras                                                       388
icinga-stable-release/7                        ICINGA (stable release for epel)                                        327
updates/7/x86_64                               CentOS-7 - Updates                                                    1,929
repolist: 12,235
[root@server ~]#

12.View both enabled and disabled repositories

In order to see both enabled and disabled repositories we use “repolist all”

[root@server ~]# yum repolist all

yum-repolist-all-command

Read Also : 8 Useful mkdir Command Examples for Linux Users

13. To check information about the package

‘yum info’ followed by package name gives us detailed information about a package such as its version, release and also give an idea how much disk space required for the installation.

Synatx: # yum info pkg_name

[root@server ~]# yum info icinga
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: del-mirrors.extreme-ix.org
 * extras: del-mirrors.extreme-ix.org
 * updates: del-mirrors.extreme-ix.org
Available Packages
Name        : icinga
Arch        : x86_64
Version     : 1.14.0
Release     : 0.el7.centos
Size        : 346 k
Repo        : icinga-stable-release/7
Summary     : Open Source host, service and network monitoring program
URL         : http://www.icinga.org/
License     : GPLv2
Description : Icinga is an application, system and network monitoring application.
            : It can escalate problems by email, pager or any other medium. It is
            : also useful for incident or SLA reporting.
            :
            : Icinga is written in C and is designed as a background process,
            : intermittently running checks on various services that you specify.
            :
            : The actual service checks are performed by separate "plugin" programs
            : which return the status of the checks to Icinga.
            :
            : Icinga is a fork of the nagios project.
 [root@server ~]#

14. Check which package does a file belongs to

It gives the package name that matches the pathname in other way we can say it helps to find out which package does file belongs to.

Syntax: # yum provides pathname

let’s assume we want find which package provides the file “/etc/inittab”

[root@server ~]# yum provides /etc/inittab
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: del-mirrors.extreme-ix.org
 * extras: del-mirrors.extreme-ix.org
 * updates: del-mirrors.extreme-ix.org
initscripts-9.49.39-1.el7.x86_64 : The inittab file and the /etc/init.d scripts
Repo : base
Matched from:
Filename : /etc/inittab

initscripts-9.49.39-1.el7_4.1.x86_64 : The inittab file and the /etc/init.d scripts
Repo : updates
Matched from:
Filename : /etc/inittab

initscripts-9.49.30-1.el7.x86_64 : The inittab file and the /etc/init.d scripts
Repo : @anaconda
Matched from:
Filename : /etc/inittab
[root@server ~]#

15. To  disable repository using  yum-config-manager

We can disable repository using yum-config-manager.

Syntax: Yum-config-manager  –disable  {repo_name}

[root@server ~]# yum-config-manager --disable "Extra Packages for Enterprise Linux 7 - x86_64"

16. To  enable  repository using  yum-config-manager

We can disable repository using yum-config-manager.

Syntax: # yum-config-manager  –enable  {repo_name}

Let’s assume we want to enable EPEL repositories,

[root@server ~]# yum-config-manager --enable "Extra Packages for Enterprise Linux 7 - x86_64"

Now verify whether repository is enabled or not

[root@server ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: del-mirrors.extreme-ix.org
 * epel: del-mirrors.extreme-ix.org
 * extras: del-mirrors.extreme-ix.org
 * updates: del-mirrors.extreme-ix.org
repo id                                           repo name                                                                       status
base/7/x86_64                                     CentOS-7 - Base                                                                  9,591
epel/x86_64                                       Extra Packages for Enterprise Linux 7 - x86_64                                  12,299
extras/7/x86_64                                   CentOS-7 - Extras                                                                  388
updates/7/x86_64                                  CentOS-7 - Updates                                                               1,929
repolist: 24,207
[root@server ~]#

17.  View all available updates

It gives us the list of packages that have updates available for the installation. It will check in our enabled repository.

Syntax: # yum check-update

[root@server ~]# yum check-update

To view Security related updates only

[root@server ~]# yum check-update --security

18. Searching rpm package

Sometime it happens like  we may not remember the exact name of the package, and then there we can just write the name that we remember and search option in yum will search for all the packages available that matches the name of the package that we have mentioned.

Syntax: yum search “package that we want to search(exact name may not matter)

[root@server ~]# yum search "nfs"

19. To run yum interactively

There is “shell” option in yum that provide us interactive way to run multiple commands

Syntax: # yum shell

[root@server ~]# yum shell
Loaded plugins: fastestmirror
> search postfix
Loading mirror speeds from cached hostfile
 * base: del-mirrors.extreme-ix.org
 * epel: del-mirrors.extreme-ix.org
 * extras: del-mirrors.extreme-ix.org
 * updates: del-mirrors.extreme-ix.org
========================================================= N/S matched: postfix =========================================================
pcp-pmda-postfix.x86_64 : Performance Co-Pilot (PCP) metrics for the Postfix (MTA)
postfix.x86_64 : Postfix Mail Transport Agent
postfix-perl-scripts.x86_64 : Postfix utilities written in perl
postfix-sysvinit.noarch : SysV initscript for postfix
spamass-milter-postfix.noarch : Postfix support for spamass-milter
postgrey.noarch : Postfix Greylisting Policy Server
pypolicyd-spf.noarch : SPF Policy Server for Postfix (Python implementation)
sqlgrey.noarch : Postfix grey-listing policy service

Name and summary matches only, use "search all" for everything.
>

20. Roll back yum updates using transactions id

There are some scenarios where we have might have to rollback yum updates, this can be easily done with yum command,

First find the transaction id against we have applied the updates, run yum history command,

[root@server ~]# yum history list all
Loaded plugins: fastestmirror
ID     | Login user               | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
     9 | root <root>              | 2018-02-24 00:30 | Install        |    1
     8 | root <root>              | 2018-02-24 00:27 | I, U           |    7
     7 | root <root>              | 2018-02-24 00:00 | I, U           |  108 EE
     6 | root <root>              | 2018-02-23 21:09 | I, U           |   60
     5 | root <root>              | 2018-01-28 01:09 | Install        |    5
     4 | root <root>              | 2018-01-27 23:45 | Install        |    1
     3 | root <root>              | 2018-01-27 23:14 | Install        |    9
     2 | root <root>              | 2018-01-27 23:13 | Install        |    1
     1 | System <unset>           | 2018-01-27 22:58 | Install        |  297
history list
[root@server ~]#

Let’s suppose we want to rollback updates which were applied against ID ‘9’, We can view which packages were installed for ID 9,

[root@server ~]# yum history info 9

Now rollback updates using below command,

[root@server ~]# yum history undo 9

21. Download rpm packages without installing

There are some scenarios where we want to install rpm packages but don’t want to install. So to download the rpm package using yum command, first we have install “yum-utils” package

[root@server ~]# yum install yum-utils

Now use the yumdownloader command to download packages along with its dependencies,

[root@server ~]# yumdownloader postfix --resolve --destdir /tmp

That’s all from this article, Please do share your feedback and comments on it. For more details please refer to man page i.e man yum.

2 thoughts on “21 Useful Yum Command Examples on RHEL 7 and CentOS 7 Servers”

Leave a Comment

eleven − four =