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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM