Linux系统之dnf包管理器的基本使用

一、dnf工具介绍

1. dnf工具简介

DNF(Dandified Yum)是Fedora Linux操作系统中的一个包管理器,它是基于Yum开发的。它的主要特点是快速、可靠、易用和优秀的用户体验。

2. dnf的功能

DNF提供了许多强大的功能和命令行工具,包括:

  • 安装和升级软件包:可以使用dnf install和dnf update命令来安装和升级软件包。

  • 查询和列出软件包:可以使用dnf search和dnf list命令来查询和列出软件包。

  • 删除软件包:可以使用dnf remove命令来删除已安装的软件包。

  • 清理系统:可以使用dnf clean命令来清理系统中的软件包缓存和元数据。

  • 版本管理:可以使用dnf history命令来查看软件包操作历史记录,并进行回滚或重做操作。

  • 软件包组管理:可以使用dnf group命令来安装、升级和删除软件包组。

  • 存储库管理:可以使用dnf repolist命令来列出系统中的存储库,并使用dnf config命令来配置存储库。

二、DNF的安装

1. 检查本地操作系统版本

检查本地操作系统版本

[root@jeven ~]# cat /etc/system-release
CentOS Linux release 7.6.1810 (Core) 

2. 安装epel

  • 配置epel,启动epel的yum仓库。
yum install epel-release -y
  • 或者使用阿里云的epel源
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

3. 检查本地yum仓库状态

检查本地yum仓库状态

[root@jeven ~]# yum repolist all |grep enable 
!base/7/x86_64        CentOS-7 - Base - mirrors.aliyun.com       enabled: 10,072
epel/x86_64           Extra Packages for Enterprise Linux 7 - x8 enabled: 13,753
!extras/7/x86_64      CentOS-7 - Extras - mirrors.aliyun.com     enabled:    515
!updates/7/x86_64     CentOS-7 - Updates - mirrors.aliyun.com    enabled:  5,053

4. 安装dnf包

使用yum安装dnf包

yum install dnf -y

在这里插入图片描述

三、dnf的使用帮助

1. 查看dnf版本

查看dnf包管理器版本

[root@jeven ~]# dnf --version
4.0.9
  Installed: dnf-0:4.0.9.2-2.el7_9.noarch at Mon 03 Jul 2023 11:24:14 AM CST
  Built    : CentOS BuildSystem <http://bugs.centos.org> at Wed 07 Apr 2021 03:52:38 PM CST

  Installed: rpm-0:4.11.3-48.el7_9.x86_64 at Sat 24 Jun 2023 10:08:53 PM CST
  Built    : CentOS BuildSystem <http://bugs.centos.org> at Wed 24 Nov 2021 04:33:39 PM CST

2. 查看dnf命令的帮助信息

查看DNF在Linux的help帮助信息

[root@jeven ~]# dnf --help
usage: dnf [options] COMMAND

List of Main Commands:

alias                     List or create command aliases
autoremove                remove all unneeded packages that were originally installed as dependencies
check                     check for problems in the packagedb
check-update              check for available package upgrades
clean                     remove cached data
deplist                   List package's dependencies and what packages provide them
distro-sync               synchronize installed packages to the latest available versions
downgrade                 Downgrade a package
group                     display, or use, the groups information
help                      display a helpful usage message
history                   display, or use, the transaction history
info                      display details about a package or group of packages
install                   install a package or packages on your system
list                      list a package or groups of packages
makecache                 generate the metadata cache
mark                      mark or unmark installed packages as installed by user.
module                    Interact with Modules.
provides                  find what package provides the given value
reinstall                 reinstall a package
remove                    remove a package or packages from your system
repolist                  display the configured software repositories
repoquery                 search for packages matching keyword
repository-packages       run commands on top of all packages in given repository
search                    search package details for the given string
shell                     run an interactive DNF shell
swap                      run an interactive dnf mod for remove and install one spec
updateinfo                display advisories about packages
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

Optional arguments:
  -c [config file], --config [config file]
                        config file location
  -q, --quiet           quiet operation
  -v, --verbose         verbose operation
  --version             show DNF version and exit
  --installroot [path]  set install root
  --nodocs              do not install documentations
  --noplugins           disable all plugins
  --enableplugin [plugin]
                        enable plugins by name
  --disableplugin [plugin]
                        disable plugins by name
  --releasever RELEASEVER
                        override the value of $releasever in config and repo
                        files
  --setopt SETOPTS      set arbitrary config and repo options
  --skip-broken         resolve depsolve problems by skipping packages
  -h, --help, --help-cmd
                        show command help
  --allowerasing        allow erasing of installed packages to resolve
                        dependencies
  -b, --best            try the best available package versions in
                        transactions.
  -C, --cacheonly       run entirely from system cache, don't update cache
  -R [minutes], --randomwait [minutes]
                        maximum command wait time
  -d [debug level], --debuglevel [debug level]
                        debugging output level
  --debugsolver         dumps detailed solving results into files
  --showduplicates      show duplicates, in repos, in list/search commands
  -e ERRORLEVEL, --errorlevel ERRORLEVEL
                        error output level
  --obsoletes           enables dnf's obsoletes processing logic for upgrade
                        or display capabilities that the package obsoletes for
                        info, list and repoquery
  --rpmverbosity [debug level name]
                        debugging output level for rpm
  -y, --assumeyes       automatically answer yes for all questions
  --assumeno            automatically answer no for all questions
  --enablerepo [repo]
  --disablerepo [repo]
  --repo [repo], --repoid [repo]
                        enable just specific repositories by an id or a glob,
                        can be specified multiple times
  --enable, --set-enabled
                        enable repos with config-manager command
                        (automatically saves)
  --disable, --set-disabled
                        disable repos with config-manager command
                        (automatically saves)
  -x [package], --exclude [package], --excludepkgs [package]
                        exclude packages by name or glob
  --disableexcludes [repo], --disableexcludepkgs [repo]
                        disable excludepkgs
  --repofrompath [repo,path]
                        label and path to additional repository, can be
                        specified multiple times.
  --noautoremove        disable removal of dependencies that are no longer
                        used
  --nogpgcheck          disable gpg signature checking (if RPM policy allows)
  --color COLOR         control whether color is used
  --refresh             set metadata as expired before running the command
  -4                    resolve to IPv4 addresses only
  -6                    resolve to IPv6 addresses only
  --destdir DESTDIR, --downloaddir DESTDIR
                        set directory to copy packages to
  --downloadonly        only download packages
  --comment COMMENT     add a comment to transaction
  --bugfix              Include bugfix relevant packages, in updates
  --enhancement         Include enhancement relevant packages, in updates
  --newpackage          Include newpackage relevant packages, in updates
  --security            Include security relevant packages, in updates
  --advisory ADVISORY, --advisories ADVISORY
                        Include packages needed to fix the given advisory, in
                        updates
  --bzs BUGZILLA        Include packages needed to fix the given BZ, in
                        updates
  --cves CVES           Include packages needed to fix the given CVE, in
                        updates
  --sec-severity {
    
    Critical,Important,Moderate,Low}, --secseverity {
    
    Critical,Important,Moderate,Low}
                        Include security relevant packages matching the
                        severity, in updates
  --forcearch ARCH      Force the use of an architecture

3. dnf命令的选项解释

dnf命令的选项解释

DNF(Dandified Yum)是Fedora、CentOS、RHEL等Linux发行版上最新的包管理工具,下面是DNF工具常用选项的解释:

1. `install`:安装一个或多个软件包
2. `remove`:卸载一个或多个软件包
3. `update`:更新现有的软件包
4. `upgrade`:更新系统中所有软件包(包括依赖项)
5. `info`:查询软件包详细信息
6. `search`:搜索软件包
7. `list`:列出系统中所有已安装的软件包
8. `clean`:清除系统中的缓存
9. `check-update`:检查系统中可用的更新
10. `repository`:管理 YUM 仓库
11. `module`:管理模块
12. `group`:管理软件包组
13. `config-manager`:管理 DNF 配置文件和库
14. `version`:查看 DNF 版本信息

四、dnf命令的基本使用

1. 查询dnf软件库

  • 查看系统中可用的DNF软件库
[root@jeven ~]# dnf repolist
Extra Packages for Enterprise Linux 7 - x86_64                                                                                         6.5 MB/s |  17 MB     00:02    
CentOS-7 - Base - mirrors.aliyun.com                                                                                                   7.0 MB/s |  10 MB     00:01    
CentOS-7 - Updates - mirrors.aliyun.com                                                                                                8.2 MB/s |  27 MB     00:03    
CentOS-7 - Extras - mirrors.aliyun.com                                                                                                 978 kB/s | 332 kB     00:00    
repo id                                                      repo name                                                                                           status
base                                                         CentOS-7 - Base - mirrors.aliyun.com                                                                10,072
epel                                                         Extra Packages for Enterprise Linux 7 - x86_64                                                      13,753
extras                                                       CentOS-7 - Extras - mirrors.aliyun.com                                                                 515
updates                                                      CentOS-7 - Updates - mirrors.aliyun.com                                                              5,053
  • 查询系统中所有的 DNF软件库,包括可用于不可用的。
[root@jeven ~]# dnf repolist all
Last metadata expiration check: 0:01:45 ago on Mon 03 Jul 2023 07:32:09 PM CST.
repo id                                                repo name                                                                                        status
base                                                   CentOS-7 - Base - mirrors.aliyun.com                                                             enabled: 10,072
centosplus                                             CentOS-7 - Plus - mirrors.aliyun.com                                                             disabled
contrib                                                CentOS-7 - Contrib - mirrors.aliyun.com                                                          disabled
epel                                                   Extra Packages for Enterprise Linux 7 - x86_64                                                   enabled: 13,753
epel-debuginfo                                         Extra Packages for Enterprise Linux 7 - x86_64 - Debug                                           disabled
epel-source                                            Extra Packages for Enterprise Linux 7 - x86_64 - Source                                          disabled
extras                                                 CentOS-7 - Extras - mirrors.aliyun.com                                                           enabled:    515
updates                                                CentOS-7 - Updates - mirrors.aliyun.com                                                          enabled:  5,053

2.查询系统的rpm包

  • 查询系统中所有的软件包,该命令用于列出用户系统上的所有来自软件库的可用软件包和所有已经安装在系统上的软件包。
dnf list

在这里插入图片描述

  • 查询已经安装的rpm包
dnf list installed

在这里插入图片描述

  • 查询可供安装的 RPM 包
dnf list available
  • 搜索软件库中的某个软件包
[root@jeven ~]# dnf search fdisk
Last metadata expiration check: 0:07:32 ago on Mon 03 Jul 2023 07:32:09 PM CST.
======================================================================= Summary Matched: fdisk ========================================================================
gdisk.x86_64 : An fdisk-like partitioning tool for GPT disks
  • 查询某个命令对应的软件名
dnf provides /bin/bash

3. 软件包的安装与卸载

  • 安装软件包
dnf install tree -y

在这里插入图片描述

  • 卸载软件包
dnf remove tree -y
  • 更新软件包
dnf update tree -y

在这里插入图片描述

4. 检查系统软件包的更新

  • 使用以下命令检查系统中所有软件包的更新
dnf check-update

5. 升级所有系统软件包

使用以下命令升级系统中所有有可用升级的软件包

dnf update 或 dnf upgrade

6. 查看dnf命令的执行历史

查看DNF命令的执行历史

[root@jeven ~]# dnf history
ID     | Command line             | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
     1 | install nginx -y         | 2023-07-03 19:47 | Install        |    3   

7. 查看所有的软件包组

查看所有的软件包组

dnf grouplist
  • 安装开发包组
dnf groupinstall "Development Tools" -y

8. 查询软件的详细信息

  • 查看某个软件的详细信息
[root@jeven ~]# dnf info tree
Last metadata expiration check: 0:21:56 ago on Mon 03 Jul 2023 07:32:09 PM CST.
Installed Packages
Name         : tree
Version      : 1.6.0
Release      : 10.el7
Arch         : x86_64
Size         : 87 k
Source       : tree-1.6.0-10.el7.src.rpm
Repo         : @System
Summary      : File system tree viewer
URL          : http://mama.indstate.edu/users/ice/tree/
License      : GPLv2+
Description  : The tree utility recursively displays the contents of directories in a
             : tree-like format.  Tree is basically a UNIX port of the DOS tree
             : utility.

五、dnf命令的使用总结

1. dnf命令的使用总结

  1. 安装软件包:使用dnf install命令,后跟要安装的软件包的名称。例如,dnf install firefox安装Firefox浏览器。
  2. 升级软件包:使用dnf upgrade命令升级所有已安装的软件包。dnf upgrade package-name升级单个软件包。
  3. 搜索软件包:使用dnf search命令搜索要安装的软件包。例如,dnf search mysql搜索MySQL软件包。
  4. 删除软件包:使用dnf remove命令删除已安装的软件包。例如,dnf remove firefox删除Firefox浏览器。
  5. 列出已安装的软件包:使用dnf list installed命令列出所有已安装的软件包。
  6. 列出可用的软件包:使用dnf list available命令列出所有可用的软件包。
  7. 列出软件包的详细信息:使用dnf info package-name命令列出软件包的详细信息。
  8. 清理缓存:使用dnf clean all命令清理DNF缓存。
  9. 禁用或启用软件包仓库:使用dnf config-manager命令禁用或启用软件包仓库。例如,dnf config-manager --disable repository-name禁用软件包仓库。
  10. 检查依赖关系:使用dnf check命令检查软件包之间的依赖关系。

2. dnf与yum包管理器比较

dnf相比于yum有以下几个优点:

  • 性能更好:dnf使用了更先进的依赖关系解决算法,因此速度更快。

  • 交互性更好:dnf提供更友好的命令行交互,支持自动补全、多个软件包同时安装、更好的错误信息提示等功能。

  • 依赖关系处理更加精确:dnf可以非常准确地处理软件包之间的依赖关系,避免因为缺少依赖而导致安装失败的问题。

  • 提供更多的包信息:dnf提供更多、更详细的软件包信息,包括依赖关系、提供的功能、安装的文件以及其他相关信息。

  • 具有扩展性:dnf是基于插件架构设计的,可以方便地扩展新的功能。

猜你喜欢

转载自blog.csdn.net/jks212454/article/details/131522044