DNF Package Management-CentOS 8 - 遠離塵世の方舟

DNF Package Management-CentOS 8

1. The syntax of the DNF command 语法

dnf [Option] [Command] [Package_Name]

2. Location of the DNF Configuration Files 相关配置文件

Main Configuration: /etc/dnf/dnf.conf

Repository: /etc/yum.repos.d/

Cache Files: /var/cache/dnf

3. Popular DNF Commands are as follows 常用命令

+---------------------+-------------------------------------------------------------------------------------------+
|      Command        |                                         Description                                       |
+---------------------+-------------------------------------------------------------------------------------------+
|repolist             | Display the configured software repositories                                              |
|install              | Install a single or multiple packages on your system                                      |
|upgrade              | Upgrade a package or packages on your system                                              |
|upgrade-minimal      | Upgrade, but only \'newest\' package match which fixes a problem that affects your system   |
|list                 | List a package or groups of packages                                                      |
|info                 | Display details about a package or group of packages                                      |
|updateinfo           | Display advisories about packages                                                         |
|search               | Search package details for the given string                                               |
|check-update         | Check for available package upgrades                                                      |
|remove               | Remove a package or packages from your system                                             |
|reinstall            | Reinstall a package                                                                       |
|downgrade            | Downgrade a package                                                                       |
|autoremove           | Remove all unneeded packages that were originally installed as dependencies               |
|distro-sync          | Synchronize installed packages to the latest available versions                           |
|makecache            | Generate the metadata cache                                                               |
|repository-packages  | Run commands on top of all packages in given repository                                   |
|provides             | Find what package provides the given value                                                |
|group                | Display, or use, the groups information                                                   |
|history              | Display, or use, the transaction history                                                  |
|clean                | Remove cached data                                                                        |
|help                 | Display a helpful usage message                                                           |
+---------------------+-------------------------------------------------------------------------------------------+

4. List repositories that are enabled on the system. 查看当前服务器上已启用的repositories

# dnf repolist

This provides more detailed information when adding the -v option with the dnf command.

# dnf repolist -v

Alternatively, you can run the following command to list the enabled repositories on the system.

# dnf repolist enabled

# dnf repolist --enabled

5. List the disabled repositories on the system. 查看当前已禁用的Repositories

# dnf repolist disabled

# dnf repolist --disabled

6. List all repositories (enabled and disabled) on the system. 查看所有的Repositories,包含启用的和禁用的

# dnf repolist all

# dnf repolist --all

[root@centos8 ~]# dnf repolist
repo id                                                      repo name
AppStream                                                    CentOS-8 - AppStream
BaseOS                                                       CentOS-8 - Base
extras                                                       CentOS-8 - Extras
[root@centos8 ~]# dnf repolist all
repo id                                       repo name                                                                     status
AppStream                                     CentOS-8 - AppStream                                                          enabled
AppStream-source                              CentOS-8 - AppStream Sources                                                  disabled
BaseOS                                        CentOS-8 - Base                                                               enabled
BaseOS-source                                 CentOS-8 - BaseOS Sources                                                     disabled
Devel                                         CentOS-8 - Devel WARNING! FOR BUILDROOT USE ONLY!                             disabled
HighAvailability                              CentOS-8 - HA                                                                 disabled
PowerTools                                    CentOS-8 - PowerTools                                                         disabled
base-debuginfo                                CentOS-8 - Debuginfo                                                          disabled
c8-media-AppStream                            CentOS-AppStream-8 - Media                                                    disabled
c8-media-BaseOS                               CentOS-BaseOS-8 - Media                                                       disabled
centosplus                                    CentOS-8 - Plus                                                               disabled
centosplus-source                             CentOS-8 - Plus Sources                                                       disabled
cr                                            CentOS-8 - cr                                                                 disabled
extras                                        CentOS-8 - Extras                                                             enabled
extras-source                                 CentOS-8 - Extras Sources                                                     disabled
fasttrack                                     CentOS-8 - fasttrack                                                          disabled
[root@centos8 ~]# 

7. Install a Package or Packages Using the dnf Command. 使用dnf命令来安装软件包

To install a single package 安装单个软件包

#dnf install nano

To install multiple packages 安装多个软件包

#dnf install MariaDB-server MariaDB-client

Add the -y option with dnf to install a package without your confirmation. 加-y参数直接安装,无须确认

#dnf install nano -y

在安装一个软件包之前,可以使用下面的命令先查看是否已安装过。

[root@centos8 ~]# dnf list installed | grep nano
nano.x86_64                                        2.9.8-1.el8                                    @anaconda 

或者:

[root@centos8 ~]# dnf list firefox 已安装,并且给出可用的更新包
Last metadata expiration check: 1:12:24 ago on Sat 08 Aug 2020 05:00:31 PM +04.
Installed Packages
firefox.x86_64                                              68.7.0-2.el8_1                                                @AppStream
Available Packages
firefox.x86_64                                              68.10.0-1.el8_2                                               AppStream 
[root@centos8 ~]# dnf list installed firefox 只显示已安装的软件包
Installed Packages
firefox.x86_64                                               68.7.0-2.el8_1                                               @AppStream
[root@centos8 ~]# dnf list installed mysql 只显示未安装mysql
Error: No matching Packages to list
[root@centos8 ~]# dnf list mysql 显示未安装,并且给出可用的软件包
Last metadata expiration check: 1:13:18 ago on Sat 08 Aug 2020 05:00:31 PM +04.
Available Packages
mysql.x86_64                                     8.0.17-3.module_el8.0.0+181+899d6349                                      AppStream
[root@centos8 ~]# dnf list htop 显示没有相关的软件包。表明Repositories中并不包含htop相关的软件包
Last metadata expiration check: 1:13:38 ago on Sat 08 Aug 2020 05:00:31 PM +04.
Error: No matching Packages to list

Install a package from a specific repository. 使用指定的Repository来安装软件包

In this case, we are installing the “htop” package from the EPEL repository.

# dnf --disablerepo="*" --enablerepo=epel install htop

To install local rpm file. 安装本地的rpm软件包

#dnf install /path/to/file.rpm

To install an rpm package directly from the URL. 安装URL指定的软件包

#dnf install https://xxx.com/file.rpm

To install a package that provides /usr/bin/[xxx] file. 安装能提供某个文件的软件包

#dnf install /usr/bin/xxx

先使用dnf provides来查看哪个Package提供所需文件:

The dnf provides command will find packages that provide the given file. This is useful if you want to find out which package (installed or not) provides this file.

# dnf provides xxx

[root@centos8 ~]# dnf provides /usr/sbin/httpd
Last metadata expiration check: 0:35:35 ago on Sat 08 Aug 2020 05:00:31 PM +04.
httpd-2.4.37-21.module_el8.2.0+382+15b0afa8.x86_64 : Apache HTTP Server
Repo        : @System
Matched from:
Filename    : /usr/sbin/httpd

httpd-2.4.37-21.module_el8.2.0+382+15b0afa8.x86_64 : Apache HTTP Server
Repo        : AppStream
Matched from:
Filename    : /usr/sbin/httpd

8. Remove a Package or Packages. 删除软件包

# dnf remove nano -y

Dletes the old version of the duplicate packages. 删除旧版本的软件包

# dnf remove --duplicates

Remove All “leaf” Packages.

This removes all the packages that were originally installed as dependencies for other packages that are no longer needed. 删除已安装的不需要的依赖包

# dnf autoremove

先使用下面的命令来查看哪些Packages将会被自动清除

List packages which will be removed by dnf auto-remove command.

# dnf list autoremove

9. Update a Package or Packages. 更新软件包

Update all installed packages to the latest available version. 更新所有软件包,通常情况下,在系统刚安装完成之后,使用该命令dnf upgrade -y 来更新系统。更新完成之后,使用命令dnf check-update 或者dnf list updates 来查看。

# dnf upgrade

The below command performs similar functions as above. This will install all available updates on your computer. 同上,更新所有软件包。

# dnf distro-sync

Update a given package or packages to the latest available version. 更新指定的软件包

# dnf upgrade [Package_Name]

# dnf upgrade nano

Upgrade a given package or packages to the specified versions. 更新指定的软件包到某一版本

# dnf upgrade [Package_Version]

# dnf upgrade nano-2.9.8-1

10. Check Information about Update Advisories 查看更新建议

The below commands will display information about update advisories. This shows how many package updates are available for Security, Bug Fixes and Enhancement.

These packages can be upgraded through “dnf upgrade” command.

Run the command below to display the number of advisory types. 查看建议更新的汇总信息

# dnf updateinfo summary

Displays list of advisories. 查看建议更新的详细信息

# dnf updateinfo list

Displays detailed information about the given advisory ID. 根据上述列出来的Advisory ID查看建议更新的详细信息

# dnf updateinfo info [advisory ID]

11. Update Only Advisories. 只更新建议的软件包

Updates each package to the latest version that provides a bugfix, enhancement or security fix. 更新所有建议的软件包

# dnf upgrade-minimal

Updates the given package or packages to the latest version that provides a bugfix, enhancement or security fix. 更新指定的软件包

# dnf upgrade-minimal [Package_Name]

12. Check Available Package Updates查看可更新的软件包

This command checks weather any package updates are available on the system. 查看所有可更新的软件包

# dnf check-update

# dnf list updates

Check available updates to the specified package. 查看某个软件包是否可以更新

# dnf check-update [Package_Name]

# dnf check-update nano

Run the following command if you want to quickly check what changes have been made to the given package before updating. 这个命令很有用。在更新前某个软件包前,可以先查看有哪些变更。

# dnf check-update firefox.x86_64 --changelog

[root@centos8 ~]# dnf check-update firefox.x86_64 --changelog 
Last metadata expiration check: 0:49:45 ago on Sat 08 Aug 2020 05:00:31 PM +04.

firefox.x86_64                                               68.10.0-1.el8_2                                               AppStream
Changelogs for firefox-68.10.0-1.el8_2.x86_64
* Mon Jul 06 12:00:00 AM 2020 CentOS Sources <bugs@centos.org> - 68.10.0-1.el8.centos
- Apply debranding changes

* Fri Jun 26 12:00:00 AM 2020 Jan Horak <jhorak@redhat.com>
- Update to 68.10.0 build1

* Fri May 29 12:00:00 AM 2020 Jan Horak <jhorak@redhat.com>
- Update to 68.9.0 build1
- Added patch for pipewire 0.3

* Mon May 11 12:00:00 AM 2020 Jan Horak <jhorak@redhat.com>
- Added s390x specific patches

* Wed Apr 29 12:00:00 AM 2020 Jan Horak <jhorak@redhat.com>
- Update to 68.8.0 build1

* Thu Apr 23 12:00:00 AM 2020 Martin Stransky <stransky@redhat.com> - 68.7.0-3
- Added fix for rhbz#1821418

This command checks the local packaging and generates information on any issues it has detected. You can limit the “packagedb” check by passing the options --dependencies, --duplicates, --obsoleted or --provides. 检查已安装的软件包是否有问题。

# dnf check

13.List Installed Packages 查看已安装的软件包

Run the below command to list all packages, present in the RPMDB, in a repo or in both.该命令查看所有存在于RPMDB以及Repositories中的软件包(即已安装的以及可用的软件包)

# dnf list

or

# dnf list all

[root@centos8 ~]# dnf list | wc -l
6712
[root@centos8 ~]# dnf list all | wc -l
6712

Run the below command to list only installed packages in RPMDB. 查看当前服务器上已安装的软件包

# dnf list installed

[root@centos8 ~]# dnf list installed | wc -l
1698

在安装一个软件包之前,可以先用该命令查看是否已经安装过:

[root@centos8 ~]# dnf list installed | grep htop
[root@centos8 ~]# dnf list installed | grep firefox
firefox.x86_64                                     68.7.0-2.el8_1                                 @AppStream
[root@centos8 ~]# 

Run the below command to list available packages from repos, excluding installed packages.列出repository里所有可用的安装包,排除已经安装的软件包,但是不排除可更新的软件包。

# dnf list available

[root@centos8 ~]# dnf list installed | grep firefox
firefox.x86_64                                     68.7.0-2.el8_1                                 @AppStream
[root@centos8 ~]# dnf list available | grep firefox
firefox.x86_64                                       68.10.0-1.el8_2                                  AppStream
[root@centos8 ~]# dnf list available | wc -l
5014

dnf list all 结果等于dnf list installed 加上dnf list available

Run the command below to see if a given package is installed. If yes, it shows an output like the one below. Otherwise, it displays the following error message “Error: No matching packages to list” 列出已安装的软件 包。

# dnf list installed httpd

List packages installed on the system that are obsoleted. 列出无效的软件包

# dnf list obsoletes

List packages recently added into the repositories. 列出最近安装的软件包

# dnf list recent

List upgrades available for the installed packages. 查看当前服务器上已安装的所有软件包中,哪些可以进行更新升级

# dnf list upgrades

[root@centos8 ~]# dnf list upgrades | wc -l
89
[root@centos8 ~]# dnf list upgrades | grep firefox
firefox.x86_64                                68.10.0-1.el8_2                            AppStream
[root@centos8 ~]# 

list packages which will be removed by dnf auto-remove command. 查看服务器上的哪些软件包会被自动清除。

# dnf list autoremove

14. Search a Package 查询软件包

#dnf search [KeyWords]

[root@centos8 ~]# dnf search httpd
Last metadata expiration check: 1:51:17 ago on Sat 08 Aug 2020 05:00:31 PM +04.
=================================================== Name Exactly Matched: httpd ====================================================
httpd.x86_64 : Apache HTTP Server
================================================== Name & Summary Matched: httpd ===================================================
centos-logos-httpd.noarch : CentOS-related icons and pictures used by httpd
keycloak-httpd-client-install.noarch : Tools to configure Apache HTTPD as Keycloak client
python3-keycloak-httpd-client-install.noarch : Tools to configure Apache HTTPD as Keycloak client
======================================================= Name Matched: httpd ========================================================
httpd-tools.x86_64 : Tools for use with the Apache HTTP Server
httpd-devel.x86_64 : Development interfaces for the Apache HTTP server
httpd-manual.noarch : Documentation for the Apache HTTP server
libmicrohttpd.i686 : Lightweight library for embedding a webserver in applications
libmicrohttpd.x86_64 : Lightweight library for embedding a webserver in applications
httpd-filesystem.noarch : The basic directory layout for the Apache HTTP server
====================================================== Summary Matched: httpd ======================================================
mod_dav_svn.x86_64 : Apache httpd module for Subversion server
mod_auth_mellon.x86_64 : A SAML 2.0 authentication module for the Apache Httpd Server
[root@centos8 ~]# dnf search fuck
Last metadata expiration check: 1:51:22 ago on Sat 08 Aug 2020 05:00:31 PM +04.
No matches found.

15. Display Installed Package Information 查看已安装的软件包信息

# dnf info httpd

[root@centos8 ~]# dnf info httpd
Last metadata expiration check: 1:52:20 ago on Sat 08 Aug 2020 05:00:31 PM +04.
Installed Packages
Name         : httpd
Version      : 2.4.37
Release      : 21.module_el8.2.0+382+15b0afa8
Architecture : x86_64
Size         : 4.9 M
Source       : httpd-2.4.37-21.module_el8.2.0+382+15b0afa8.src.rpm
Repository   : @System
From repo    : AppStream
Summary      : Apache HTTP Server
URL          : https://httpd.apache.org/
License      : ASL 2.0
Description  : The Apache HTTP Server is a powerful, efficient, and extensible
             : web server.

[root@centos8 ~]# dnf info firefox
Last metadata expiration check: 1:52:27 ago on Sat 08 Aug 2020 05:00:31 PM +04.
Installed Packages
Name         : firefox
Version      : 68.7.0
Release      : 2.el8_1
Architecture : x86_64
Size         : 342 M
Source       : firefox-68.7.0-2.el8_1.src.rpm
Repository   : @System
From repo    : AppStream
Summary      : Mozilla Firefox Web browser
URL          : https://www.mozilla.org/firefox/
License      : MPLv1.1 or GPLv2+ or LGPLv2+
Description  : Mozilla Firefox is an open-source web browser, designed for standards
             : compliance, performance and portability.

Available Packages
Name         : firefox
Version      : 68.10.0
Release      : 1.el8_2
Architecture : x86_64
Size         : 94 M
Source       : firefox-68.10.0-1.el8_2.src.rpm
Repository   : AppStream
Summary      : Mozilla Firefox Web browser
URL          : https://www.mozilla.org/firefox/
License      : MPLv1.1 or GPLv2+ or LGPLv2+
Description  : Mozilla Firefox is an open-source web browser, designed for standards
             : compliance, performance and portability.

16. Use the “dnf makecache” Command

Makecache is used to download and make usable all the metadata for the currently enabled repository on your system.

# dnf makecache

[root@centos8 ~]# dnf makecache
CentOS-8 - AppStream                                                                                6.2 kB/s | 4.3 kB     00:00    
CentOS-8 - Base                                                                                     4.0 kB/s | 3.9 kB     00:00    
CentOS-8 - Extras                                                                                   2.1 kB/s | 1.5 kB     00:00    
Metadata cache created.

17. Downgrade a Package下载一个软件包

# dnf downgrade nano

18. Re-Install a Package 重新安装某个软件包

This command is used to reinstall if they package has already installed. If it’s not installed, you will be getting the following message.

Package nano available, but not installed.

No match for argument: nano

Error: No packages marked for reinstall.

Run the following command to re-install the given package.

# dnf reinstall nano

19. List Package Groups 列出软件包组

# dnf group list

or

# dnf grouplist

[root@centos8 ~]# dnf group list
Last metadata expiration check: 0:02:40 ago on Sat 08 Aug 2020 06:54:02 PM +04.
Available Environment Groups:
   Server
   Minimal Install
   Workstation
   Virtualization Host
   Custom Operating System
Installed Environment Groups:
   Server with GUI
Installed Groups:
   Container Management
   Development Tools
   Graphical Administration Tools
   Headless Management
   Scientific Support
   Security Tools
   System Tools
Available Groups:
   .NET Core Development
   RPM Development Tools
   Legacy UNIX Compatibility
   Network Servers
   Smart Card Support

20. How to View Summary of Groups

This display overview of how many groups are installed and available on your system.

# dnf group summary

[root@centos8 ~]# dnf group summary
Last metadata expiration check: 0:03:32 ago on Sat 08 Aug 2020 06:54:02 PM +04.
Installed Groups: 7
Available Groups: 5

To View a Specified Group Info. 查看指定的软件包组信息

This command displays the list of packages available in this group.

Each group has three parts and the details are as follows:

Mandatory Packages

Default Packages

Optional Packages

# dnf group info \'Development Tools\'

[root@centos8 ~]# dnf group info \'Development Tools\'
Last metadata expiration check: 0:04:37 ago on Sat 08 Aug 2020 06:54:02 PM +04.

Group: Development Tools
 Description: A basic development environment.
 Mandatory Packages:
   autoconf
   automake
   binutils
   bison
   flex
   gcc
   gcc-c++
   gdb
   glibc-devel
   libtool
   make
   pkgconf
   pkgconf-m4
   pkgconf-pkg-config
   redhat-rpm-config
   rpm-build
   rpm-sign
   strace
 Default Packages:
   asciidoc
   byacc
   ctags
   diffstat
   git
   intltool
   jna
   ltrace
   patchutils
   perl-Fedora-VSP
   perl-generators
   pesign
   source-highlight
   systemtap
   valgrind
   valgrind-devel
 Optional Packages:
   cmake
   expect
   rpmdevtools
   rpmlint

21. Install a Package Group安装某一个软件包组

# dnf group install \'Development Tools\' -y

or

# dnf groupinstall \'Development Tools\' -y

22. Update a Package Group 升级某个软件包组

# dnf group update \'Development Tools\' -y

or

# dnf groupupdate \'Development Tools\' -y

23. Remove a Package Group 删除某个软件包组

# dnf group remove \'Development Tools\'

or

# dnf group erase \'Development Tools\'

24. Clear Cached Data 删除缓存数据

By default dnf will cache data such as package and repository metadata to the “/var/cache/dnf” directory when you perform varies dnf operation. This cache can take up a lot space over the period of time. This will allow you to remove all cached data.

Run the below command to remove cache files generated from the repository metadata.

# dnf clean dbcache

Run the below command to mark the repository metadata expired.

# dnf clean expire-cache

Run the below command to remove repository metadata.

# dnf clean metadata

Run the below command to remove any cached packages from the system.

# dnf clean packages

This command does all of the above action in one go.

# dnf clean all

[root@centos8 ~]# dnf clean all
29 files removed
[root@centos8 ~]# dnf makecache
CentOS-8 - AppStream                                                                                1.6 MB/s | 5.8 MB     00:03    
CentOS-8 - Base                                                                                     646 kB/s | 2.2 MB     00:03    
CentOS-8 - Extras                                                                                   6.1 kB/s | 7.0 kB     00:01    
Metadata cache created.

25. Print dnf History 查看dnf 历史记录

The dnf history command allows the user to view what action has happened in past transactions. All transactions showed in a table.

# dnf history

# dnf history list

It shows you detailed information about the given transactions. When no transaction is specified, describe what happened during the latest transaction.

# dnf history info 3

Repeat the same action for the specified transaction ID.

# dnf history redo 3

Perform the opposite operation to all operations performed in the specified transaction.

# dnf history undo 3

Undo all transactions performed after the specified transaction.

# dnf history rollback 7

26. Enable DNF Automatic Updates 启用自动更新

You can enable automatic package update by installing the dnf-automatic package. 先安装dnf-automatic软件包

# dnf install dnf-automatic

After installaing the pacakge, make sure you edit the /etc/dnf/automatic.conf file and replace apply_updates = yes instead of apply_updates = no. 安装完成之后,修改配置文件参数为yes.

Enable the “dnf-automatic-timer” service once you made the changes in the configuration file. 设置该服务自动启动。

# systemctl enable dnf-automatic.timer

Finally start the service. 启动该服务

#systemctl start dnf-automatic.timer

27. Mark/Un-Mark a Package

The dnf mark command allows you to always keep the specified package on the system, and doesn’t remove this package from the system when the auto-removal command is running.

Run the following command to mark the specified packages as installed by user.

# dnf mark install nano

nano-2.9.8-1.el8.x86_64 marked as user installed.

Run the following command to unmark the specified packages as installed by user.

# dnf mark remove nano

nano-2.9.8-1.el8.x86_64 unmarked as user installed.

28. Query a Package From the Enabled Repos

It searches the given package on the enabled repositories and displays the information. This is equivalent to the rpm “-q” command.

# dnf repoquery httpd

29. dnf帮助命令

# dnf -help

or

# man dnf