25 Useful DNF Command Examples For Package Management In Linux

Dandified Yum (DNF) is an RPM based package manager which is used to install and update packages in various Linux distributions including CentOS, RHEL and Fedora.

DNF is the next major verison of Yum and as such it aims to maintain CLI compatibility with Yum, so a lot of the information here will be similar to our yum commands.

Like Yum, DNF is quite powerful as it’s capable of automatically resolving dependency issues, and is similar to other package managers such as ‘apt-get’ in Debian based distributions.

These examples should serve as a useful introduction, guide or cheat sheet style resource for how to use the dnf command in Linux.

If DNF is not yet used by default in your distribution but you’re interested in installing it check out our guide to installing DNF.

How To Use dnf – Command Examples

  • 1. Install New Package From Repository

    The dnf command can be used to install packages from a repository with the ‘install’ argument, followed by the package name. In the example below, we are installing Apache, which is provided in the ‘httpd’ package.

    [root@centos7 ~]# dnf install httpd
    Using metadata from Thu Dec 29 21:31:01 2016
    Dependencies resolved.
    ===========================================================
     Package    Arch     Version              Repository   Size
    ===========================================================
    Installing:
     httpd      x86_64   2.4.6-45.el7.centos  base        2.7 M
    
    Transaction Summary
    ===========================================================
    Install  1 Package
    
    Total download size: 2.7 M
    Installed size: 9.4 M
    Is this ok [y/N]: y
    Downloading Packages:
    httpd-2.4.6-45.el7.centos.x86_64.rpm         4.3 MB/s | 2.7 MB     00:00
    ------------------------------------------------------------------------
    Total                                        1.6 MB/s | 2.7 MB     00:01
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Installing  : httpd-2.4.6-45.el7.centos.x86_64              1/1
      Verifying   : httpd-2.4.6-45.el7.centos.x86_64              1/1
    
    Installed:
      httpd.x86_64 2.4.6-45.el7.centos
    
    Complete!
    

    Note that after confirming the packages that will be installed, you will be asked to provide input. In this case we entered ‘y’ for yes to proceed with the installation, which then completed successfully.

  • 2. Assume Yes

    In the first example we were prompted to press the ‘y’ key in order to proceed with the installation. Rather than being prompted each time for user input, we can simply specify the ‘-y’ option in our command for assume yes. This way we will not be prompted for any input and dnf will assume that everything will be answered with yes.

    [root@centos7 ~]# dnf install httpd-manual -y
    Using metadata from Thu Dec 29 21:31:01 2016
    Dependencies resolved.
    ======================================================================
     Package           Arch       Version                Repository   Size
    ======================================================================
    Installing:
     httpd-manual      noarch     2.4.6-45.el7.centos    base        1.3 M
    
    Transaction Summary
    ======================================================================
    Install  1 Package
    
    Total download size: 1.3 M
    Installed size: 5.5 M
    Downloading Packages:
    httpd-manual-2.4.6-45.el7.centos.noarch.rpm      2.6 MB/s | 1.3 MB     00:00
    ----------------------------------------------------------------------------
    Total                                            796 kB/s | 1.3 MB     00:01
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Installing  : httpd-manual-2.4.6-45.el7.centos.noarch             1/1
      Verifying   : httpd-manual-2.4.6-45.el7.centos.noarch             1/1
    
    Installed:
      httpd-manual.noarch 2.4.6-45.el7.centos
    
    Complete!
    

    Note that in this example we were not prompted for input, as assume yes has been provided, so the package was installed fully without any further user interaction.

  • 3. Check For Available Updates

    We can run dnf with the ‘check-update’ argument which will check in with our enabled repositories for any updates that may be available for packages that we have installed. This does not actually perform any updates, it simply gives us a list of packages that have updates available for installation.

    [root@centos7 ~]# dnf check-update
    bash.x86_64                             4.2.46-20.el7_2        updates
    dbus.x86_64                             1:1.6.12-14.el7_2      updates
    dbus-libs.x86_64                        1:1.6.12-14.el7_2      updates
    device-mapper-persistent-data.x86_64    0.6.2-1.el7_2          updates
    glibc.x86_64                            2.17-106.el7_2.8       updates
    glibc-common.x86_64                     2.17-106.el7_2.8       updates
    initscripts.x86_64                      9.49.30-1.el7_2.3      updates
    kernel.x86_64                           3.10.0-327.28.3.el7    updates
    kernel-tools.x86_64                     3.10.0-327.28.3.el7    updates
    kernel-tools-libs.x86_64                3.10.0-327.28.3.el7    updates
    ...
    
  • 4. Update New Package From Repository

    In the previous example we have been provided 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.

    [root@centos7 ~]# dnf update bash -y
    Resolving Dependencies
    --> Running transaction check
    ---> Package bash.x86_64 0:4.2.46-19.el7 will be updated
    ---> Package bash.x86_64 0: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 M
    Downloading packages:
    Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
    bash-4.2.46-20.el7_2.x86_64.rpm        | 1.0 MB  00:00:00
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running 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 run check-update prior to this.

    While we can update specified packages in this manner, it is often easier to simply apply all available updates for all packages on the system with ‘dnf update’.

  • 5. Download Package RPM File

    Rather than installing a package from the repository as demonstrated previously, we can optionally download the .rpm file from the repository with the DNF download plugin.

    This is an example of what happens if you attempt to perform an action without the appropriate plugin.

    [root@centos7 ~]# dnf download httpd
    No such command: download. Please use /usr/bin/dnf --help
    It could be a DNF plugin command.
    

    This plugin is part of the dnf-plugins-core, which we must first install.

    [root@centos7 ~]# dnf install dnf-plugins-core
    

    Now we can download the RPM file to the local system.

    [root@centos7 ~]# dnf download httpd
    Using metadata from Thu Dec 29 21:31:01 2016
    httpd-2.4.6-45.el7.centos.x86_64.rpm                               4.5 MB/s | 2.7 MB     00:00
    
    [root@centos7 ~]# ls -la httpd-2.4.6-45.el7.centos.x86_64.rpm
    -rw-r--r--. 1 root root 2827204 Dec 29 22:53 httpd-2.4.6-45.el7.centos.x86_64.rpm
    
  • 6. Perform Local Install Of RPM File

    In this exampler we will install the .rpm file that we just downloaded with the dnf download plugin. While we could have just installed this package from the repository directly with ‘dnf install’, sometimes you may be provided with .rpm files directly for installation so it’s worth knowing how to install them.

    While .rpm files can be installed with the ‘rpm’ command, using dnf has the advantage of automatically detecting and installing any other package dependencies that may also be required.

    [root@centos7 ~]# dnf install unbound-1.4.20-28.el7.x86_64.rpm
    Using metadata from Thu Dec 29 21:31:01 2016
    Dependencies resolved.
    =================================================================
     Package         Arch       Version          Repository      Size
    =================================================================
    Installing:
     ldns            x86_64     1.6.16-10.el7    base           476 k
     libevent        x86_64     2.0.21-4.el7     base           214 k
     unbound         x86_64     1.4.20-28.el7    @commandline   473 k
     unbound-libs    x86_64     1.4.20-28.el7    base           296 k
    
    Transaction Summary
    =================================================================
    Install  4 Packages
    
    Total size: 1.4 M
    Total download size: 985 k
    Installed size: 4.4 M
    Is this ok [y/N]: y
    Downloading Packages:
    (1/3): libevent-2.0.21-4.el7.x86_64.rpm       1.7 MB/s | 214 kB     00:00
    (2/3): ldns-1.6.16-10.el7.x86_64.rpm          3.4 MB/s | 476 kB     00:00
    (3/3): unbound-libs-1.4.20-28.el7.x86_64.rpm  2.0 MB/s | 296 kB     00:00
    -------------------------------------------------------------------------
    Total                                         965 kB/s | 985 kB     00:01
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Installing  : libevent-2.0.21-4.el7.x86_64                  1/4
      Installing  : ldns-1.6.16-10.el7.x86_64                     2/4
      Installing  : unbound-libs-1.4.20-28.el7.x86_64             3/4
      Installing  : unbound-1.4.20-28.el7.x86_64                  4/4
      Verifying   : unbound-1.4.20-28.el7.x86_64                  1/4
      Verifying   : ldns-1.6.16-10.el7.x86_64                     2/4
      Verifying   : libevent-2.0.21-4.el7.x86_64                  3/4
      Verifying   : unbound-libs-1.4.20-28.el7.x86_64             4/4
    
    Installed:
      ldns.x86_64 1.6.16-10.el7          unbound.x86_64 1.4.20-28.el7
      unbound-libs.x86_64 1.4.20-28.el7  libevent.x86_64 2.0.21-4.el7
    
    Complete!
    

    We can see that the source of the unbound package is listed as the commandline, as we have the file locally to install. The other packages that are installed as dependencies are listed as coming from the base repository though.

  • 7. Uninstall Package

    Packages can be uninstalled with the ‘remove’ argument, as shown below.

    [root@centos7 ~]# dnf remove httpd
    Using metadata from Thu Dec 29 21:31:01 2016
    Dependencies resolved.
    ===================================================================
     Package         Arch       Version               Repository   Size
    ===================================================================
    Removing:
     httpd           x86_64     2.4.6-45.el7.centos   @System     9.4 M
     httpd-manual    noarch     2.4.6-45.el7.centos   @System     5.5 M
    
    Transaction Summary
    ===================================================================
    Remove  2 Packages
    
    Installed size: 15 M
    Is this ok [y/N]: y
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Erasing     : httpd-manual-2.4.6-45.el7.centos.noarch                 1/2
      Erasing     : httpd-2.4.6-45.el7.centos.x86_64                        2/2
      Verifying   : httpd-2.4.6-45.el7.centos.x86_64                        1/2
      Verifying   : httpd-manual-2.4.6-45.el7.centos.noarch                 2/2
    
    Removed:
      httpd.x86_64 2.4.6-45.el7.centos    httpd-manual.noarch 2.4.6-45.el7.centos
    
    Complete!
    

    Note that in order to remove a package, all other packages which depend on it will also be removed. For that reason it is recommended not to use the -y option to ‘assumeyes’ when removing a package so that you get a chance to read what will be removed before proceeding, as it may be more than you expect.

    In the above example, the httpd-manual package that we installed previously requires the httpd package. Therefore when we remove the httpd package, httpd-manual must go too.

  • 8. Reinstall Package

    If you’re having a problem with a package, you can easily reinstall it with the ‘reinstall’ argument.

    [root@centos7 ~]# dnf reinstall httpd -y
    Using metadata from Thu Dec 29 21:31:01 2016
    Dependencies resolved.
    ==================================================================
     Package         Arch     Version               Repository    Size
    ==================================================================
    Reinstalling:
     httpd           x86_64   2.4.6-45.el7.centos   base         2.7 M
    
    Transaction Summary
    ==================================================================
    
    Total download size: 2.7 M
    Downloading Packages:
    httpd-2.4.6-45.el7.centos.x86_64.rpm  8.6 MB/s | 2.7 MB     00:00
    -----------------------------------------------------------------
    Total                                 1.9 MB/s | 2.7 MB     00:01
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Reinstalling: httpd-2.4.6-45.el7.centos.x86_64              1/2
    error: httpd-2.4.6-45.el7.centos.x86_64: erase failed
      Verifying   : httpd-2.4.6-45.el7.centos.x86_64              1/2
      Verifying   : httpd-2.4.6-45.el7.centos.x86_64              2/2
    
    Reinstalled:
      httpd.x86_64 2.4.6-45.el7.centos
    
    Complete!
    

    This is similar to performing a ‘dnf remove’ followed by a ‘dnf install’, however here it’s done in the one command and custom configuration is not removed, as a ‘dnf remove’ will delete all configuration files managed by the package.

  • 9. View Repository Information

    By default we have many repositories configured in the /etc/yum.repos.d/ directory which dnf reads, these can be viewed with the ‘repolist’ and ‘repoinfo’ arguments.

    [root@centos7 ~]# dnf repolist
    Using metadata from Thu Dec 29 21:31:01 2016
    repo id          repo name                                          status
    base             CentOS-7 - Base                                     9,363
    epel             Extra Packages for Enterprise Linux 7 - x86_64     10,976
    extras           CentOS-7 - Extras                                     435
    updates          CentOS-7 - Updates 
    

    By default ‘repolist’ displays information for enabled repositories only, however we can specify ‘all’ afterwards to list even disabled repositories.

    [root@centos7 ~]# dnf repolist all
    
    repo id                repo name                       status
    C7.0.1406-base         CentOS-7.0.1406 - Base          disabled
    C7.0.1406-centosplus   CentOS-7.0.1406 - CentOSPlus    disabled
    C7.0.1406-extras       CentOS-7.0.1406 - Extras        disabled
    C7.0.1406-fasttrack    CentOS-7.0.1406 - CentOSPlus    disabled
    C7.0.1406-updates      CentOS-7.0.1406 - Updates       disabled
    C7.1.1503-base         CentOS-7.1.1503 - Base          disabled
    ...
    
  • 10. Add New Repository

    We can specify a new repository by manually editing or creating a new .repo file in the /etc/yum.repos.d directory, however we can much more easily create a persistent repository with the ‘dnf config-manager’ command.

    [root@centos7 ~]# dnf config-manager --add-repo="https://mirror.aarnet.edu.au/pub/centos/7"
    Adding repo from: https://mirror.aarnet.edu.au/pub/centos/7
    
    [root@centos7 ~]# cat /etc/yum.repos.d/mirror.aarnet.edu.au_pub_centos_7.repo
    [mirror.aarnet.edu.au_pub_centos_7]
    name=created by dnf config-manager from https://mirror.aarnet.edu.au/pub/centos/7
    baseurl=https://mirror.aarnet.edu.au/pub/centos/7
    enabled=1
    

    We simply provide a known repository URL in the –add-repo option, and this creates a .repo file based on the name of the repository URL specified. In this case, /etc/yum.repos.d/mirror.aarnet.edu.au_pub_centos_7.repo has been created with the contents specified above in the output of the command. We can now install or update packages from this newly specified mirror.

  • 11. Enable Or Disable Specific Repository

    There may be times when we want to perform an action while enabling or disabling a specific repository. For example if we want to install a particular package, we may want to ensure that it comes from a specific mirror.

    dnf --enablerepo=disabled-repo install package-example
    

    This would enabled a previously disabled repo named ‘disabled-repo’ in order to install ‘package-example’ from it.

    If we have the same packages available at multiple repositories we may wish to disable one and update or install from a specific repository. This can be done by disabling the repository with --disablerepo.

    dnf --disablerepo=enabled-repo install package-example
    
  • 12. Search For Packages In Repository

    The ‘search’ argument is a very useful dnf feature, as it allows us to search through packages on the repository which can be useful if we don’t know exactly what we’re looking to install.

    [root@centos7 ~]# dnf search php
    Using metadata from Thu Dec 29 21:31:01 2016
    ====================== N/S Matched: php ==========================
    php.x86_64 : PHP scripting language for creating dynamic web sites
    php-punic.noarch : PHP-Unicode CLDR
    geos-php.x86_64 : PHP modules for GEOS
    php-pluf.noarch : PHP WebApp Framework
    php-idn.x86_64 : PHP API for GNU LibIDN
    php-jsonlint.noarch : JSON Lint for PHP
    php-common.x86_64 : Common files for PHP
    ...
    

    In this example we search package names and the summary for the term “PHP” and see a number of available packages at the repositories ready for us to install or download.

  • 13. Find Which Package Provides Something

    This is one of my favourite features of the dnf command as it’s very useful. Have you ever wanted to run a command, but you’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 an iSCSI initiator, this is done with the ‘iscsiadm’ command. We can use ‘provides’ followed by the name of the command to search for packages that provide this command.

    [root@centos7 ~]# dnf provides */iscsiadm
    Using metadata from Thu Dec 29 21:31:01 2016
    iscsi-initiator-utils-6.2.0.873-35.el7.x86_64 : iSCSI daemon and utility programs
    Repo        : @System
    

    As shown the ‘iscsi-initiator-utils’ package can be installed to provide the ‘iscsiadm’ command.

    This can also be used against a file on the file system to check which package provides that file.

    [root@centos7 ~]# dnf provides /etc/httpd/conf/httpd.conf
    Using metadata from Thu Dec 29 21:31:01 2016
    httpd-2.4.6-45.el7.centos.x86_64 : Apache HTTP Server
    Repo        : @System
    

    In this example we can see that the /etc/httpd/conf/httpd.conf file comes from the ‘httpd’ package.

  • 14. View Package Information

    Further detailed information of a package can be viewed with the ‘info’ argument. As shown below, this provides us with useful information such as the architecture, version number, release number, total size, as well as a description of what the package actually does.

    [root@centos7 ~]# dnf info httpd
    Using metadata from Thu Dec 29 21:31:01 2016
    Installed Packages
    Name        : httpd
    Arch        : x86_64
    Epoch       : 0
    Version     : 2.4.6
    Release     : 45.el7.centos
    Size        : 9.4 M
    Repo        : @System
    Summary     : Apache HTTP Server
    URL         : http://httpd.apache.org/
    License     : ASL 2.0
    Description : The Apache HTTP Server is a powerful, efficient, and extensible
                : web server.
    

    This can be used before a package is installed or after installation, the same information will be provided in both situations.

  • 15. View Transaction History

    The dnf history contains all actions that have been performed by the dnf command. This allows us to see what has happened to our Linux system over time, and even undo, redo, or roll back a transaction.

    [root@centos7 ~]# dnf history
    Using metadata from Thu Dec 29 21:31:01 2016
    ID     | Command line             | Date a | Action | Altere
    ---------------------------------------------------------------------------
        14 | reinstall httpd -y       | 2016-12-29 23:02 | Reinstall  |    1 E<
        13 | remove httpd             | 2016-12-29 23:00 | Erase      |    2 >
        12 | install httpd httpd-manu | 2016-12-29 23:00 | Install    |    2
        11 | remove httpd             | 2016-12-29 23:00 | Erase      |    1
        10 | install unbound-1.4.20-2 | 2016-12-29 22:58 | Install    |    4  <
         9 | install unbound-1.4.20-2 | 2016-12-29 22:57 | Install    |    1 >
         8 | install httpd-2.4.6-45.e | 2016-12-29 22:55 | Install    |    1
         7 | remove httpd -y          | 2016-12-29 22:55 | Erase      |    2
         6 | install dnf-plugins-core | 2016-12-29 22:52 | Install    |    1
         5 | install httpd-manual -y  | 2016-12-29 22:46 | Install    |    1
         4 | install httpd            | 2016-12-29 22:44 | Install    |    1
         3 | remove httpd -y          | 2016-12-29 22:43 | Erase      |    2  <
         2 | update                   | 2016-12-29 21:57 | Update     |    1 >
         1 | install httpd-2.4.6-45.e | 2016-12-29 21:34 | E, I       |    2
    

    The newest transactions appear at the top of the list, and we can view further information of a transaction by specifying its ID, as shown below.

    [root@centos7 ~]# dnf history info 13
    Using metadata from Thu Dec 29 21:31:01 2016
    Transaction ID : 13
    Begin time     : Thu Dec 29 23:00:49 2016
    Begin rpmdb    : 1336:80339f63f7d0b0833cb3f3a9561ded593a1181d6
    End time       :            23:00:50 2016 (1 seconds)
    End rpmdb      : 1334:21298913a58dbd9d10ea424fd201cf2e29a183fa
    User           : root 
    Return-Code    : Success
    Command Line   : remove httpd
    Transaction performed with:
        Installed     dnf-0.6.4-2.el7.noarch   (unknown)
        Installed     rpm-4.11.3-21.el7.x86_64 (unknown)
    Packages Altered:
        Erase httpd-2.4.6-45.el7.centos.x86_64        @base
        Erase httpd-manual-2.4.6-45.el7.centos.noarch @base
    

    So the transaction with the ID of 13 was used to remove the httpd package. Now we can undo this transaction if we want, which should install the httpd package again, as this will undo the removal.

    [root@centos7 ~]# dnf history undo 13 -y
    Using metadata from Thu Dec 29 21:31:01 2016
    Undoing transaction 13, from Thu Dec 29 23:00:49 2016
        Erase httpd-2.4.6-45.el7.centos.x86_64        @base
        Erase httpd-manual-2.4.6-45.el7.centos.noarch @base
    =====================================================================
     Package          Arch       Version                Repository   Size
    =====================================================================
    Installing:
     httpd            x86_64     2.4.6-45.el7.centos    base        2.7 M
     httpd-manual     noarch     2.4.6-45.el7.centos    base        1.3 M
    
    Transaction Summary
    =====================================================================
    Install  2 Packages
    
    Total size: 4.0 M
    Installed size: 15 M
    Downloading Packages:
    [SKIPPED] httpd-manual-2.4.6-45.el7.centos.noarch.rpm: Already downloaded
    [SKIPPED] httpd-2.4.6-45.el7.centos.x86_64.rpm: Already downloaded
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Installing  : httpd-2.4.6-45.el7.centos.x86_64              1/2
      Installing  : httpd-manual-2.4.6-45.el7.centos.noarch       2/2
      Verifying   : httpd-manual-2.4.6-45.el7.centos.noarch       1/2
      Verifying   : httpd-2.4.6-45.el7.centos.x86_64              2/2
    
    Installed:
      httpd.x86_64 2.4.6-45.el7.centos   httpd-manual.noarch 2.4.6-45.el7.centos
    
    Complete!
    

    We can then run the removal again with ‘dnf history redo’.

    [root@centos7 ~]# dnf history redo 13 -y
    Using metadata from Thu Dec 29 21:31:01 2016
    Repeating transaction 13, from Thu Dec 29 23:00:49 2016
        Erase httpd-2.4.6-45.el7.centos.x86_64        @base
        Erase httpd-manual-2.4.6-45.el7.centos.noarch @base
    =========================================================================
     Package             Arch       Version               Repository     Size
    =========================================================================
    Removing:
     httpd               x86_64     2.4.6-45.el7.centos   @System       9.4 M
     httpd-manual        noarch     2.4.6-45.el7.centos   @System       5.5 M
    
    Transaction Summary
    =========================================================================
    Remove  2 Packages
    
    Installed size: 15 M
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Erasing     : httpd-manual-2.4.6-45.el7.centos.noarch         1/2
      Erasing     : httpd-2.4.6-45.el7.centos.x86_64                2/2
      Verifying   : httpd-2.4.6-45.el7.centos.x86_64                1/2
      Verifying   : httpd-manual-2.4.6-45.el7.centos.noarch         2/2
    
    Removed:
      httpd.x86_64 2.4.6-45.el7.centos             httpd-manual.noarch 2.4.6-45.el7.centos
    
    Complete!
    

    It’s also worth noting that package installations, updates, or removals are also logged in the /var/log/dnf.log file, here’s an example of what’s logged here.

    [root@centos7 ~]# tail /var/log/dnf.log
    Dec 29 23:21:29 INFO =========================================================
     Package               Arch        Version                 Repository    Size
    ==============================================================================
    Removing:
     httpd                 x86_64      2.4.6-45.el7.centos     @System      9.4 M
     httpd-manual          noarch      2.4.6-45.el7.centos     @System      5.5 M
    
    Transaction Summary
    ==============================================================================
    Remove  2 Packages
    
    Dec 29 23:21:29 INFO Installed size: 15 M
    
  • 16. Clear Cached Information

    By default dnf will cache data to the /var/cache/dnf directory, such as package and repository data. This speeds up dnf so that it doesn’t have to keep querying this information from the Internet.

    There are times when you may want to delete this cached data, such as if a repository has updated packages but your Linux system has incorrect or stale cached data which may cause various problems when attempting to install a package.

    [root@centos7 ~]# dnf clean all
    Cleaning repos: epel base updates extras
    Cleaning up Everything
    

    This will remove all cached items, it is possible to otherwise specify specific elements to clear (such as packages, metadata, expire-cache, rpmdb or plugins) however this saves a lot of time if you aren’t sure what should be removed.

  • 17. Build Cache

    The dnf cache will be automatically built up over time when you perform various dnf queries such as installing or updating packages, however we have the option to manually make the cache so that future actions will be quicker with the ‘makecache’ argument.

    [root@centos7 ~]# time dnf makecache
    Extra Packages for Enterprise Linux 7 - x86_64     10 MB/s |  12 MB     00:01
    CentOS-7 - Base                                   9.9 MB/s | 9.2 MB     00:00
    CentOS-7 - Updates                                7.4 MB/s | 1.5 MB     00:00
    CentOS-7 - Extras                                 2.4 MB/s | 636 kB     00:00
    Metadata cache created.
    
    real    0m16.491s
    user    0m9.307s
    sys     0m1.198s
    
  • 18. List Installed Packages

    We have the ability to list all packages that are currently installed on our Linux system with ‘dnf list installed’. I have not included the full output here as I have hundreds of packages installed on my system, however this small snippet should give you an idea of what to expect.

    [root@centos7 ~]# dnf list installed
    Using metadata from Fri Dec 30 00:39:53 2016
    Installed Packages
    GConf2.x86_64                         3.2.6-8.el7            @System
    GeoIP.x86_64                          1.5.0-11.el7           @System
    ModemManager.x86_64                   1.6.0-2.el7            @System
    ModemManager-glib.x86_64              1.6.0-2.el7            @System
    NetworkManager.x86_64                 1:1.4.0-13.el7_3       @System
    NetworkManager-adsl.x86_64            1:1.4.0-13.el7_3       @System
    NetworkManager-bluetooth.x86_64       1:1.4.0-13.el7_3       @System
    NetworkManager-glib.x86_64            1:1.4.0-13.el7_3       @System
    NetworkManager-libnm.x86_64           1:1.4.0-13.el7_3       @System
    NetworkManager-team.x86_64            1:1.4.0-13.el7_3       @System
    ...
    

    When piped into the grep command this becomes extremely powerful as it allows us to search for a specific package to see if it is installed.

    [root@centos7 ~]# dnf list installed | grep httpd
    httpd.x86_64                  2.4.6-45.el7.centos            @System
    httpd-tools.x86_64            2.4.6-45.el7.centos            @System
    
  • 19. List Available Packages

    In the above example we learned how to list currently installed packages on our Linux system with the dnf command, now we want to be able to query a repository and list its available packages instead.

    This is quite similar, except that we replace ‘installed’ with ‘available’ to instead list all available packages.

    [root@centos7 ~]# dnf list installed | wc -l
    1336
    
    [root@centos7 ~]# dnf list available | wc -l
    19212
    

    As we can see we have over 1,300 packages currently installed, however in the repository there are over 19,000 packages in total for us to install.

    We can also run ‘dnf list all’ to list both installed and available packages together, or ‘dnf list updates’ to only list packages which currently have updates available, the output of which is similar to the ‘dnf check-update’ command covered previously.

  • 20. Package Groups

    Packages that are related to each other may 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 other individual packages.

    For example we can run ‘dnf grouplist’ to view the groups that are available for us to use by default.

    [root@centos7 ~]# dnf grouplist
    Using metadata from Fri Dec 30 00:39:53 2016
    Available environment groups:
       Minimal Install
       Compute Node
       Infrastructure Server
       File and Print Server
       MATE Desktop
       Basic Web Server
       Virtualization Host
       Server with GUI
       GNOME Desktop
       KDE Plasma Workspaces
       Development and Creative Workstation
    ...
    

    Once we have the list of available groups, we can use ‘dnf groupinfo’ on any of them to get an idea of the packages that are included as part of the group.

    [root@centos7 ~]# dnf groupinfo "Basic Web Server"
    Environment Group: Basic Web Server
     Description: Server for serving static and dynamic internet content.
     Mandatory Groups:
       Base
       Core
       Web Server
       
    [root@centos7 ~]# dnf groupinfo "Web Server"
    Group: Web Server
     Description: Allows the system to act as a web server, 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. If we look closer at the Web Server group we can see that it uses the httpd package as well as some additional ones that we do not yet have installed but are available.

    Finally we can install the group with the ‘dnf groupinstall’ command, as shown below.

    [root@centos7 ~]# dnf groupinstall "Web Server" -y
    Using metadata from Fri Dec 30 00:39:53 2016
    Dependencies resolved.
    ===================================================================================
     Group                  Packages
    ===================================================================================
    Marking installed:
     Web Server           mod_security_crs      mod_auth_mellon        mod_auth_kerb
                          squid                 mod_auth_openidc       perl-CGI-Session
                          mod_revocator         plone                  mod_fcgid
                          httpd                 mod_ssl                cherokee
                          zope                  memcached              libmemcached
                          mod_security          certmonger             crypto-utils
                          moin                  Pound                  mod_nss
                          perl-CGI              python-memcached       httpd-manual
    ====================================================================================
     Package                 Arch       Version                  Repository     Size
    ====================================================================================
    Installing:
     crypto-utils            x86_64     2.4.1-42.el7             base           78 k
     httpd-manual            noarch     2.4.6-45.el7.centos      base          1.3 M
     mod_fcgid               x86_64     2.3.9-4.el7              base           79 k
     mod_ssl                 x86_64     1:2.4.6-45.el7.centos    base          105 k
     perl-Newt               x86_64     1.08-36.el7              base           64 k
    
    Transaction Summary
    ====================================================================================
    Install  5 Packages
    
    Total download size: 1.7 M
    Installed size: 6.2 M
    Downloading Packages:
    (1/5): mod_ssl-2.4.6-45.el7.centos.x86_64.rpm            958 kB/s | 105 kB     00:00
    (2/5): crypto-utils-2.4.1-42.el7.x86_64.rpm              506 kB/s |  78 kB     00:00
    (3/5): mod_fcgid-2.3.9-4.el7.x86_64.rpm                  742 kB/s |  79 kB     00:00
    (4/5): perl-Newt-1.08-36.el7.x86_64.rpm                  599 kB/s |  64 kB     00:00
    (5/5): httpd-manual-2.4.6-45.el7.centos.noarch.rpm       4.8 MB/s | 1.3 MB     00:00
    ------------------------------------------------------------------------------------
    Total                                                    1.3 MB/s | 1.7 MB     00:01
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running 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  : mod_fcgid-2.3.9-4.el7.x86_64                          3/5
      Installing  : mod_ssl-1:2.4.6-45.el7.centos.x86_64                  4/5
      Installing  : httpd-manual-2.4.6-45.el7.centos.noarch               5/5
      Verifying   : httpd-manual-2.4.6-45.el7.centos.noarch               1/5
      Verifying   : crypto-utils-2.4.1-42.el7.x86_64                      2/5
      Verifying   : mod_ssl-1:2.4.6-45.el7.centos.x86_64                  3/5
      Verifying   : mod_fcgid-2.3.9-4.el7.x86_64                          4/5
      Verifying   : perl-Newt-1.08-36.el7.x86_64                          5/5
    
    Installed:
      crypto-utils.x86_64 2.4.1-42.el7     httpd-manual.noarch 2.4.6-45.el7.centos
      mod_fcgid.x86_64 2.3.9-4.el7         mod_ssl.x86_64 1:2.4.6-45.el7.centos
      perl-Newt.x86_64 1.08-36.el7
    
    Complete!
    

    The group can also be updated with ‘dnf group update’, or all packages within the group removed with ‘dnf group remove’.

  • 21. Exclude Specific Package

    We can optionally exclude a specific package from update, installation or removal with the -x command which will exclude that package. In this example, we perform a ‘dnf check-update’ and confirm that a kernel update is available, however we do not wish to install this package at this time. With the help of -x, we ignore the kernel update and instead perform a ‘dnf update’ which states that it is going to update everything except the kernel, which is not listed.

    [root@centos7 ~]# dnf check-update
    
    device-mapper-persistent-data.x86_64      0.6.2-1.el7_2                updates
    initscripts.x86_64                        9.49.30-1.el7_2.3            updates
    kernel.x86_64                     3.10.0-327.28.3.el7          updates
    kernel-tools.x86_64                       3.10.0-327.28.3.el7          updates
    kernel-tools-libs.x86_64                  3.10.0-327.28.3.el7          updates
    microcode_ctl.x86_64                      2:2.1-12.el7_2.1             updates
    python-perf.x86_64                        3.10.0-327.28.3.el7          updates
    
    [root@centos7 ~]# dnf update -x kernel
    Resolving Dependencies
    --> Running transaction check
    ---> Package device-mapper-persistent-data.x86_64 0:0.5.5-1.el7 will be updated
    ---> Package device-mapper-persistent-data.x86_64 0:0.6.2-1.el7_2 will be an update
    ---> Package initscripts.x86_64 0:9.49.30-1.el7_2.2 will be updated
    ---> Package initscripts.x86_64 0:9.49.30-1.el7_2.3 will be an update
    ---> Package kernel-tools.x86_64 0:3.10.0-327.22.2.el7 will be updated
    ---> Package kernel-tools.x86_64 0:3.10.0-327.28.3.el7 will be an update
    ---> Package kernel-tools-libs.x86_64 0:3.10.0-327.22.2.el7 will be updated
    ---> Package kernel-tools-libs.x86_64 0:3.10.0-327.28.3.el7 will be an update
    ---> Package microcode_ctl.x86_64 2:2.1-12.el7 will be updated
    ---> Package microcode_ctl.x86_64 2:2.1-12.el7_2.1 will be an update
    ---> Package python-perf.x86_64 0:3.10.0-327.22.2.el7 will be updated
    ---> Package python-perf.x86_64 0: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 M
    Is this ok [y/d/N]:
    
  • 22. Add Random Wait Time

    In order to use the random wait time function, we must install the dnf-automatic package first. This tool is used for automatic tasks such as updating regularly via cron jobs.

    [root@centos7 ~]# dnf install dnf-automatic -y
    

    By default dnf-automatic has a random sleep period of 300 seconds, which is the maximum delay that will take place prior to downloading. This may be useful if you manage many machines that all attempt to update at the same time and smash the repository or otherwise use up all available network bandwidth making the process go very slow.

    The 300 second default can be adjusted by adding ‘random_sleep’ into the /etc/dnf/automatic.conf configuration file.

  • 23. Install Security Updates Only

    While we can simply perform a ‘dnf update’ to update all of our installed packages to the latest versions, this may not be ideal in a production environment. Being able to install security only updates during patching may be a requirement in your environment.

    [root@centos7 ~]# dnf updateinfo list sec
    

    With dnf-automatic, we can also modify the /etc/dnf/automatic.conf file. By default the upgrade_type is set to default which will update all available packages, however this can be changed to security instead so that only security updates are installed.

  • 24. Hide All Output

    As you have seen in the above examples, the dnf command can provide quite a bit of output. If we don’t want to display the output messages we can simply specify the -q option for quiet which will hide all of the output. In the example below we completely uninstall a package with dnf and have no messages output.

    [root@centos7 ~]# dnf install httpd -y -q
    [root@centos7 ~]#
    
  • 25. Display Verbose Information

    Alternatively if you’re troubleshooting you can specify the -v option to enable verbose output. In the below verbose example we can see additional information in the output that was not previously available.

    [root@centos7 ~]# dnf remove httpd -y -v
    cachedir: /var/cache/dnf/x86_64/7/x86_64/7
    Loaded plugins: builddep, config-manager, debuginfo-install, download, generate_completion_cache, kickstart, needs-restarting, noroot, protected_packages, Query, reposync
    DNF version: 0.6.4
    repo: using cache for: epel
    not found deltainfo for: Extra Packages for Enterprise Linux 7 - x86_64
    repo: using cache for: base
    not found deltainfo for: CentOS-7 - Base
    not found updateinfo for: CentOS-7 - Base
    repo: using cache for: updates
    not found updateinfo for: CentOS-7 - Updates
    repo: using cache for: extras
    not found updateinfo for: CentOS-7 - Extras
    Using metadata from Fri Dec 30 00:39:53 2016
    --> Starting dependency resolution
    --> Finding unneeded leftover dependencies
    ---> Package httpd.x86_64 2.4.6-45.el7.centos will be erased
    --> Finished dependency resolution
    Dependencies resolved.
    ========================================================================
     Package           Arch      Version                 Repository     Size
    ========================================================================
    Removing:
     httpd             x86_64    2.4.6-45.el7.centos     @System       9.4 M
    
    Transaction Summary
    ========================================================================
    Remove  1 Package
    
    Installed size: 9.4 M
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Erasing     : httpd-2.4.6-45.el7.centos.x86_64             1/1
      Verifying   : httpd-2.4.6-45.el7.centos.x86_64             1/1
    
    Removed:
      httpd.x86_64 2.4.6-45.el7.centos
    
    Completion plugin: Generating completion cache...
    Complete!
    

Summary

This guide has covered how to use dnf with 25 different examples in which you can practically put the dnf command to use in Linux.

  1. I’ve just installed Fedora 26 and I want to secure it first, which means (in part) configuring automatic updates, which means learning about DNF. I found this page to be a very useful and actually more information than I needed but good to know just in case. Thank you for preparing this page!

  2. thank you much, very useful

  3. These examples and explanations are insightful-thank you. I have two question: (1) Can the user specify a destination directory for a package install? ;(2) Since dnf does not give info on where it installed, what is the best way to find that location?

  4. I’ve created a Files repository to copy to a second box to speed up
    it’s upgrade. But, there are superseded packages in my repository.
    How do I get rid of them auto-magically?

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>