Going further with DNF in Fedora

April 29, 2021, updated May 5, 2021 Fedora

Fedo­ra’s DNF pack­age man­ager is easy to use for every­day tasks, but it’s also easy to miss some very use­ful lesser known fea­tures. This arti­cle will look at how to accom­plish more with the typ­i­cal DNF com­mands you already use.

Running without the network

DNF reg­u­larly con­nects to the net­work to refresh metadataThe default in Fedora is seven days for base repos­i­to­ries and six hours for update repos­i­to­ries. This can be changed with the metadata_expire con­fig­u­ra­tion option. . This slows down even com­mands that do not need infor­ma­tion from a repos­i­tory to work.

You can run with the -C/--cacheonly option to skip the network:

dnf -C list installed 'gtk*'
Installed Packages
gtk-update-icon-cache.x86_64      3.24.28-2.fc33        @updates
gtk-vnc2.x86_64                   1.0.0-3.fc33          @fedora
gtk2.x86_64                       2.24.33-1.fc33        @updates
gtk3.x86_64                       3.24.28-2.fc33        @updates
gtk3-devel.x86_64                 3.24.28-2.fc33        @updates
gtkmm30.x86_64                    3.24.4-1.fc33         @updates
gtksourceview3.x86_64             3.24.11-4.fc33        @fedora
gtksourceview4.x86_64             4.8.1-1.fc33          @updates
gtkspell3.x86_64                  3.0.10-6.fc33         @fedora

This option always uses the sys­tem cache (the cache of the root user), which is pre­sum­ably more up-­to-­date than the cache of the local user. You will how­ever get an error if you try to install a pack­age with this, since the cache nor­mally never con­tains pack­ages wait­ing to be installed.

Fedora’s archive repository

The nor­mal Fedora repos­i­to­ries only keep two ver­sions of the same pack­age: the lat­est (in the updates repos­i­to­ry), and the first one for the cur­rent ver­sion of Fedora (in the fedora repos­i­to­ry). This can be seen by list­ing dupli­cates of a package:

Note the only two options under Avail­able Packages: 246.6-3 at the time Fedora 33 was released, and 246.13-1, the cur­rent ver­sion.

dnf -C list --showduplicates systemd.x86_64
Installed Packages
systemd.x86_64         246.13-1.fc33      @updates
Available Packages
systemd.x86_64         246.6-3.fc33       fedora
systemd.x86_64         246.13-1.fc33      updates

Downgrading a package

As a result, down­grad­ing a pack­age in Fedo­ra, while the­o­ret­i­cally easy, can cause sur­pris­es. For exam­ple, try­ing to down­grade systemd will go all the way down seven (mi­nor) ver­sions, to the ver­sion from the Fedora 33 release:

sudo dnf downgrade systemd
Dependencies resolved.
===============================================================================
 Package                  Architecture Version              Repository    Size
===============================================================================
Downgrading:
 systemd                  x86_64       246.6-3.fc33         fedora       4.0 M
 systemd-container        x86_64       246.6-3.fc33         fedora       498 k
 systemd-devel            x86_64       246.6-3.fc33         fedora       429 k
 systemd-libs             x86_64       246.6-3.fc33         fedora       578 k
 systemd-networkd         x86_64       246.6-3.fc33         fedora       467 k
 systemd-pam              x86_64       246.6-3.fc33         fedora       308 k
 systemd-rpm-macros       noarch       246.6-3.fc33         fedora        29 k
 systemd-udev             x86_64       246.6-3.fc33         fedora       1.5 M

Transaction Summary
===============================================================================
Downgrade  8 Packages

That usu­ally isn’t what you want when try­ing to down­grade a recently updated package.

Using the archive repository

Edit (May 5, 2021): The repos­i­tory is now enabled by default when installed; the sec­tion was changed to reflect this.

For­tu­nately Fedora includes a repos­i­tory that con­tains all pack­ages that ever reached the updates repos­i­to­ry. After installing it, and set­ting it as dis­abled by default:since this update all ver­sions of Fedora (ex­cept rawhide) have it enabled by default. The ratio­nal is that it is nor­mally only installed on edi­tions that actu­ally need it (like Fedora Silverblue).

sudo dnf install fedora-repos-archive
sudo dnf config-manager --set-disabled updates-archive

It becomes avail­able for use, and con­tains every­thing you might expect:

dnf list --showduplicates --enablerepo=updates-archive systemd.x86_64
Installed Packages
systemd.x86_64         246.13-1.fc33      @updates
Available Packages
systemd.x86_64         246.6-3.fc33       fedora
systemd.x86_64         246.7-1.fc33       updates-archive
systemd.x86_64         246.7-2.fc33       updates-archive
systemd.x86_64         246.10-1.fc33      updates-archive
systemd.x86_64         246.13-1.fc33      updates
systemd.x86_64         246.13-1.fc33      updates-archive

The more recent 246.10-1 ver­sion is now avail­able!

sudo dnf downgrade --enablerepo=updates-archive systemd
Dependencies resolved.
===============================================================================
 Package                Arch       Version           Repository           Size
===============================================================================
Downgrading:
 systemd                x86_64     246.10-1.fc33     updates-archive     4.1 M
 systemd-container      x86_64     246.10-1.fc33     updates-archive     494 k
 systemd-devel          x86_64     246.10-1.fc33     updates-archive     425 k
 systemd-libs           x86_64     246.10-1.fc33     updates-archive     579 k
 systemd-networkd       x86_64     246.10-1.fc33     updates-archive     464 k
 systemd-pam            x86_64     246.10-1.fc33     updates-archive     307 k
 systemd-rpm-macros     noarch     246.10-1.fc33     updates-archive      24 k
 systemd-udev           x86_64     246.10-1.fc33     updates-archive     1.5 M

Transaction Summary
===============================================================================
Downgrade  8 Packages

Undoing history

This also allows to suc­cess­fully undo trans­ac­tions with dnf his­tory undo and dnf his­tory rollback:

sudo dnf history undo last 
Error: The following problems occurred while running a transaction:
  Cannot find rpm nevra "fedpkg-1.40-5.fc33.noarch".
  Cannot find rpm nevra "vim-X11-2:8.2.2637-1.fc33.x86_64".
  Cannot find rpm nevra "vim-common-2:8.2.2637-1.fc33.x86_64".
  Cannot find rpm nevra "vim-enhanced-2:8.2.2637-1.fc33.x86_64".
  Cannot find rpm nevra "vim-filesystem-2:8.2.2637-1.fc33.noarch".
  Cannot find rpm nevra "vim-minimal-2:8.2.2637-1.fc33.x86_64".
sudo dnf history undo last --enablerepo=updates-archive
Dependencies resolved.
===============================================================================
 Package            Arch       Version               Repository           Size
===============================================================================
Downgrading:
 fedpkg             noarch     1.40-5.fc33           updates-archive      88 k
 vim-X11            x86_64     2:8.2.2637-1.fc33     updates-archive     1.9 M
 vim-common         x86_64     2:8.2.2637-1.fc33     updates-archive     6.7 M
 vim-enhanced       x86_64     2:8.2.2637-1.fc33     updates-archive     1.8 M
 vim-filesystem     noarch     2:8.2.2637-1.fc33     updates-archive      23 k
 vim-minimal        x86_64     2:8.2.2637-1.fc33     updates-archive     685 k

Transaction Summary
===============================================================================
Downgrade  6 Packages

Note that the updates-archive repos­i­tory should prob­a­bly be kept dis­abled by default, because query­ing it is sig­nif­i­cantly slower than other repositoriesBecause it has a lot more meta­data than nor­mal: it only ever keeps grow­ing in size, while the updates repos­i­tory is cleaned of old pack­ages to keep it fast. . If you don’t mind the meta­data down­load times, you can remove the --enable-repo argu­ments above after enabling the repos­i­tory with:

sudo dnf config-manager --set-enabled updates-archive

Locking a package in place

Keep­ing a down­graded pack­age around can be annoy­ing if DNF tries to upgrade it back again all the time.

How­ev­er, there is a DNF com­mand for this, though you might have to install it first:

Yes, this can be used to locate and install DNF com­mands, see below.

sudo dnf install 'dnf-command(versionlock)'

Then it can be used to lock pack­ages in place:

sudo dnf -C versionlock add 'vim*'
Adding versionlock on: vim-minimal-2:8.2.2735-1.fc33.*
Adding versionlock on: vim-powerline-0:2.8.2-1.fc33.*
Adding versionlock on: vim-filesystem-2:8.2.2735-1.fc33.*
Adding versionlock on: vim-common-2:8.2.2735-1.fc33.*
Adding versionlock on: vim-X11-2:8.2.2735-1.fc33.*
Adding versionlock on: vim-enhanced-2:8.2.2735-1.fc33.*

You can for exam­ple use it to lock a pack­age to minor updates:

sudo dnf -C versionlock add --raw 'firefox-88.*'

Or on the con­trary exclude minor updates and wait for the next major one:

sudo dnf -C versionlock exclude --raw 'firefox-88.*'

This can be used to include or exclude pack­ages in a more pre­cise way, com­pared to adding exclude entries in the DNF configuration.

Paths instead of package names

Sev­eral DNF com­mands sup­port using paths on the file-sys­tem instead of a pack­age name. The obvi­ous exam­ple is provide, which dis­plays pack­ages that pro­vide the file at that path:

dnf provides /etc/vimrc
Last metadata expiration check: 1:55:07 ago on Mon 26 Apr 2021 10:19:03 AM CEST.
vim-common-2:8.2.1770-1.fc33.x86_64 : The common files needed by any version of the VIM editor
Repo        : fedora
Matched from:
Filename    : /etc/vimrc

vim-common-2:8.2.2787-1.fc33.x86_64 : The common files needed by any version of the VIM editor
Repo        : updates
Matched from:
Filename    : /etc/vimrc

As a spe­cial con­ve­nience, dnf provide will prefixthis only works with provide! the search term with /usr/bin/ if there is no match (and then /usr/sbin), so the fol­low­ing will work:

dnf provides bzcat

But the path does­n’t actu­ally have to exist locally at all. For exam­ple, when installing a package:

Note that it needs to be an absolute path here, oth­er­wise you should use a wild­card.

sudo dnf install /usr/bin/gvim

Wild­cards can also be used:

dnf provides '*/libbz2.so*'
Last metadata expiration check: 1 day, 18:45:49 ago on Sat 17 Apr 2021 07:43:37 PM CEST.
bzip2-devel-1.0.8-4.fc33.i686 : Libraries and header files for apps which will use bzip2
Repo        : fedora
Matched from:
Filename    : /usr/lib/libbz2.so

bzip2-devel-1.0.8-4.fc33.x86_64 : Libraries and header files for apps which will use bzip2
Repo        : fedora
Matched from:
Filename    : /usr/lib64/libbz2.so

bzip2-libs-1.0.8-4.fc33.i686 : Libraries for applications using bzip2
Repo        : fedora
Matched from:
Filename    : /usr/lib/libbz2.so.1
Filename    : /usr/lib/libbz2.so.1.0.8

bzip2-libs-1.0.8-4.fc33.x86_64 : Libraries for applications using bzip2
Repo        : fedora
Matched from:
Filename    : /usr/lib64/libbz2.so.1
Filename    : /usr/lib64/libbz2.so.1.0.8

This can be par­tic­u­larly use­ful to re-in­stall locally mod­i­fied files:

Note that this will not work for most con­fig­u­ra­tion files in /etc since pack­ages are usu­ally instructed to leave local con­fig­u­ra­tion changes alone.

sudo dnf reinstall '/usr/share/themes/*'
sudo dnf reinstall '/boot/*'

Rich dependencies

Pack­ages in Fedora can pro­vide more that file paths. They also expose richer infor­ma­tion about what becomes avail­able once the pack­age is installed.

Missing libraries

Pack­ages expose native library depen­den­cies direct­ly, so an error mes­sage like this:

error while loading shared libraries: libbz2.so.1: \
  cannot open shared object file: No such file or directory

Can be diag­nosed with:

dnf provides 'libbz2.so.*'
Last metadata expiration check: 2:03:46 ago on Tue Apr 20 08:19:21 2021.
bzip2-libs-1.0.8-4.fc33.i686 : Libraries for applications using bzip2
Repo        : fedora
Matched from:
Provide    : libbz2.so.1

bzip2-libs-1.0.8-4.fc33.x86_64 : Libraries for applications using bzip2
Repo        : @System
Matched from:
Provide    : libbz2.so.1()(64bit)

bzip2-libs-1.0.8-4.fc33.x86_64 : Libraries for applications using bzip2
Repo        : fedora
Matched from:
Provide    : libbz2.so.1()(64bit)

Build dependencies

When man­u­ally com­pil­ing pro­grams, build sys­tems often use pkgconfig to locate depen­den­cies. The same depen­dency names can be used with DNF, regard­less of the actual name of the package:

# before compiling a program that requires dbus
sudo dnf install 'pkgconfig(dbus-1)'
Dependencies resolved.
===============================================================================
 Package                 Arch        Version                Repository    Size
===============================================================================
Installing:
 dbus-devel              x86_64      1:1.12.20-2.fc33       fedora        36 k
Installing dependencies:
 libpkgconf              x86_64      1.7.3-5.fc33           updates       36 k
 pkgconf                 x86_64      1.7.3-5.fc33           updates       41 k
 pkgconf-m4              noarch      1.7.3-5.fc33           updates       14 k
 pkgconf-pkg-config      x86_64      1.7.3-5.fc33           updates       10 k
 xml-common              noarch      0.6.3-55.fc33          fedora        31 k

Transaction Summary
===============================================================================
Install  6 Packages

Fonts

Pack­aged fonts can be installed by name:

sudo dnf install font(firasans)
# or all the fira fonts:
sudo dnf install font(fira*)

Thus you can list all fonts pack­aged in Fedora with:

dnf provides 'font(*)' | grep Provide | sort -u

DNF commands

You some­times see DNF com­mands that are not avail­able on a sys­tem by default­—in blog posts for exam­ple. Those can be installed by DNF with:

sudo dnf install 'dnf-command(torproxy)'

Libraries from various ecosystems

It is some­times use­ful to have a library installed in the stan­dard Python/Ruby/… envi­ron­ment pro­vided by Fedo­ra. The nam­ing of pack­ages is usu­ally con­sis­tent enough that you can guess where to find some­thing, but you can also use the following:

sudo dnf install 'rubygem(bundler)'
sudo dnf install 'perl(Socket)'
sudo dnf install 'python3dist(httplib2)'
sudo dnf install 'ocaml(Rope)'

MIME handlers

DNF can be used to find appli­ca­tions capa­ble of open­ing a file, using its mime-type:

# what is that unknown file?
file -b --mime-type pres.ppt
application/vnd.ms-powerpoint
# how can I open it?
dnf provides 'mimehandler(application/vnd.ms-powerpoint)'
calligra-okular-odpgenerator-3.2.1-4.fc33.i686 : OpenDocument presenter support for okular
Repo        : fedora
Matched from:
Provide    : mimehandler(application/vnd.ms-powerpoint)

calligra-okular-odpgenerator-3.2.1-4.fc33.x86_64 : OpenDocument presenter support for okular
Repo        : fedora
Matched from:
Provide    : mimehandler(application/vnd.ms-powerpoint)

calligra-stage-3.2.1-4.fc33.x86_64 : A full-featured presentation program
Repo        : fedora
Matched from:
Provide    : mimehandler(application/vnd.ms-powerpoint)

libreoffice-impress-1:7.0.1.2-5.fc33.x86_64 : LibreOffice Presentation Application
Repo        : fedora
Matched from:
Provide    : mimehandler(application/vnd.ms-powerpoint)

libreoffice-impress-1:7.0.5.2-1.fc33.x86_64 : LibreOffice Presentation Application
Repo        : updates
Matched from:
Provide    : mimehandler(application/vnd.ms-powerpoint)

Wrapping up

These are just some of DNF’s less used fea­tures that can help man­ag­ing your sys­tem. Hope­fully this has been use­ful to make your life on the com­mand line easier.

For a more com­plete tour of DNF see the DNF documentation and the Core DNF Plu­g­ins Documentation.

Going further with DNF in Fedora - April 29, 2021 - Antoine Gourlay