linux之 rpm yum dnf

#############################

rpm工具:

  •  RPM 全名 RedHat Package Managerment,是由Red Hat公司提出,被众多Linux发行版本所采用,是一种数据库记录的方式来将所需要的软件安装到到Linux系统的一套软件管理机制。
  • 它最大的特点就是将你要安装的软件先编译过,并且打包成为 RPM 机制的文件,通过打包好的软件里面默认的数据库,记录这个软件要安装的时候必须具备的依赖属性软件。
  • 当在你的 Linux 主机安装时,RPM 会先依照软件里面的数据查询Linux 主机的依赖属性软件是否满足,若满足则子以安装,若不满足则不子安装。那么安装的时候就将该软件的信息整个写入 RPM 的数据库中,以便未来的查询、验证与反安装。

 

 

通常,RPM 包采用系统默认的安装路径。安装文件会按照类别分别安装到不同的目录,如下所示:

RPM 包默认安装路径含 义
/etc/ 配置文件安装目录,如/etc/dnf
/usr/bin/ 可执行的命令安装目录
/usr/lib/ 程序所使用的函数库保存位置
/usr/share/doc/ 基本的软件使用手册保存位置
/usr/share/man/ 帮助文件(man page文件)保存位置

 

 

 

 

 

RPM 包还可以手动指定安装路径(不推荐)。

获取指定安装包的安装路径:
  • 先找到rpm安装包:rpm -qa  rpm_pakage_fullname
  • 再根据已经安装的安装包全称获得该安装包的默认安装路径:rpm -ql rpm_pakage_fullname(rpm-ql 只能获取已经用rpm或yum或dnf安装过的软件包的全称,意味着你用源码安装的软件就查不到,另外你没有安装过这个软件包也查不到的)

 

 

[root@igoodful yum.repos.d]rpm -qa *rz*
lrzsz-0.12.20-43.el8.x86_64
[root@igoodful yum.repos.d]rpm -qa lrzsz
lrzsz-0.12.20-43.el8.x86_64
[root@igoodful yum.repos.d]rpm -ql lrzsz-0.12.20-43.el8.x86_64
/usr/bin/rb
/usr/bin/rx
/usr/bin/rz
/usr/bin/sb
/usr/bin/sx
/usr/bin/sz
/usr/lib/.build-id
/usr/lib/.build-id/b2
/usr/lib/.build-id/b2/25f21df8a410853ff0ca69c273bfd6166a36a0
/usr/lib/.build-id/b2/25f21df8a410853ff0ca69c273bfd6166a36a0.1
/usr/lib/.build-id/b2/25f21df8a410853ff0ca69c273bfd6166a36a0.2
/usr/lib/.build-id/f2
/usr/lib/.build-id/f2/29412bed6b8d68690555e59b6910cf19b44a19
/usr/lib/.build-id/f2/29412bed6b8d68690555e59b6910cf19b44a19.1
/usr/lib/.build-id/f2/29412bed6b8d68690555e59b6910cf19b44a19.2
/usr/share/locale/de/LC_MESSAGES/lrzsz.mo
/usr/share/man/man1/rz.1.gz
/usr/share/man/man1/sz.1.gz
[root@igoodful yum.repos.d]rpm -ql lrzsz*
package lrzsz* is not installed
[root@igoodful yum.repos.d]

 

 

 

 

 

 

 

 

 

 

 

官方文档:https://dnf.readthedocs.io/en/latest/index.html

yum和dnf都是管理rmp包的,也只管理rpm包,源码包,它是无法管理的。

CentOS 6和CentOS 7系统中默认使用yum来管理软件包安装,但CentOS  Stream 8开始默认使用DNF来管理软件包安装, Fedora 22 已经默认使用DNF,DNF也是

 

  • dnf缓存文件为:/var/cache/dnf
  • dnf全局配置文为:/etc/dnf/dnf.conf
  • dnf仓库配置文件为:/etc/yum.repos.d/*

 

CentOS 6和CentOS 7系统中安装 DNF:

(1)CentOS 6和CentOS 7系统中为了安装 DNF ,您必须先安装并启用 epel-release 依赖。
 
yum  -y install epel-release
 
(2)然后再使用 epel-release 依赖中的 YUM 命令来安装 DNF 包。在系统中执行以下命令:
 
yum -y install dnf

 

 dnf命令总览:

[root@192.168.31.225:~]# 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                   [deprecated, use repoquery --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

List of Plugin Commands:

builddep                  Install build dependencies for package or spec file
changelog                 Show changelog data of packages
config-manager            manage dnf configuration options and repositories
copr                      Interact with Copr repositories.
debug-dump                dump information about installed rpm packages to file
debug-restore             restore packages recorded in debug-dump file
debuginfo-install         install debuginfo packages
download                  Download package to current directory
groups-manager            create and edit groups metadata file
kpatch                    Toggles automatic installation of kpatch-patch packages
needs-restarting          determine updated binaries that need restarting
offline-distrosync        Prepare offline distrosync of the system
offline-upgrade           Prepare offline upgrade of the system
playground                Interact with Playground repository.
repoclosure               Display a list of unresolved dependencies for repositories
repodiff                  List differences between two sets of repositories
repograph                 Output a full package dependency graph in dot format
repomanage                Manage a directory of rpm packages
reposync                  download all packages from remote repo
system-upgrade            Prepare system for upgrade to a new release

General DNF options:
  -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.
  --nobest              do not limit the transaction to the best candidate
  -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]   Enable additional repositories. List option. Supports
                        globs, can be specified multiple times.
  --disablerepo [repo]  Disable repositories. List option. Supports globs, can
                        be specified multiple times.
  --repo [repo], --repoid [repo]
                        enable just specific repositories by an id or a glob,
                        can be specified multiple times
  --enable              enable repos with config-manager command
                        (automatically saves)
  --disable             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 an additional repository to use
                        (same path as in a baseurl), 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
  --bz BUGZILLA, --bzs BUGZILLA
                        Include packages needed to fix the given BZ, in
                        updates
  --cve CVES, --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
[root@192.168.31.225:~]# 

 

 
 

dnf(Dandified YUM)是Linux系统中常用的包管理器,可以用于安装、升级、删除软件包以及管理软件仓库。以下是一些dnf命令的最佳实践:

列出所有可用的仓库
dnf repolist
搜索软件包
dnf search <package_name>
安装软件包

dnf install <package_name>
升级软件包
dnf upgrade
删除软件包

dnf remove <package_name>
清理dnf缓存

dnf clean all
列出已安装的软件包
dnf list installed
列出软件包依赖

dnf repoquery --requires <package_name>
列出软件包提供的功能

dnf repoquery --provides <package_name>
查看软件包详细信息

dnf info <package_name>
同时安装多个软件包

dnf install <package_name1> <package_name2> <package_name3>
安装指定版本的软件包

dnf install <package_name>-<version>
禁用软件仓库

dnf --disablerepo=<repository_name> install <package_name>
启用软件仓库

dnf --enablerepo=<repository_name> install <package_name>
更新软件仓库元数据
dnf makecache
总之,dnf命令非常强大,可以通过它来完成许多任务,例如软件包管理、仓库管理、软件包版本控制等。上述最佳实践只是其中的一部分,但它们足以满足日常工作中的大部分需求。

 

 
 
 
 
 
 
 

 搜索rpm软件包:

包含:dnf管理的所有rpm软件包、已经安装过了的rpm安装包,dnf管理的但还没有安装到系统的rpm软件包
 
(1)dnf管理的所有rpm软件包:
dnf list
 
(2)已经安装过了的rpm安装包:
dnf list installed
 
(3)dnf管理的但还没有安装到系统的rpm软件包:
dnf list available

 

(4)根据软件包名称xxx匹配查找rpm软件包:dnf search rpm_name

dnf search xxx

 

(5)根据可执行程序文件名匹配查找rpm软件包:dnf provides exe_name

场景:比如你看见张三执行了某个神秘的命令,然后你也想要安装这个命令来玩,那你就可以找到他执行这个命令的文件位置,然后使用dnf provides  /xxx/yyy/zzz获得rpm安装包名称,然后再去自己的机器上安装即可,但是张三若是源码安装了这个神秘命令,那dnf就无能为力了

dnf provides /bin/bash

 

(6)获取指定rpm软件包的信息:dnf info  rpm_name

[root@igoodful ~]dnf info lrzsz
Last metadata expiration check: 0:03:54 ago on Wed 01 Mar 2023 10:57:18 PM CST.
Available Packages
Name         : lrzsz
Version      : 0.12.20
Release      : 43.el8
Architecture : x86_64
Size         : 84 k
Source       : lrzsz-0.12.20-43.el8.src.rpm
Repository   : baseos
Summary      : The lrz and lsz modem communications programs
URL          : http://www.ohse.de/uwe/software/lrzsz.html
License      : GPLv2+
Description  : Lrzsz (consisting of lrz and lsz) is a cosmetically modified
             : zmodem/ymodem/xmodem package built from the public-domain version of
             : the rzsz package. Lrzsz was created to provide a working GNU
             : copylefted Zmodem solution for Linux systems.

[root@igoodful ~]

 

 

 安装软件包:

 (1)直接安装:dnf -y install  rpm_name
[root@igoodful ~]dnf -y install lrzsz
Last metadata expiration check: 0:17:23 ago on Wed 01 Mar 2023 10:57:18 PM CST.
Dependencies resolved.
=======================================================================================================================================================================================================================================================================================================================================
 Package                                                                      Architecture                                                                  Version                                                                                Repository                                                                     Size
=======================================================================================================================================================================================================================================================================================================================================
Installing:
 lrzsz                                                                        x86_64                                                                        0.12.20-43.el8                                                                         baseos                                                                         84 k

Transaction Summary
=======================================================================================================================================================================================================================================================================================================================================
Install  1 Package

Total download size: 84 k
Installed size: 190 k
Downloading Packages:
lrzsz-0.12.20-43.el8.x86_64.rpm                                                                                                                                                                                                                                                                         52 kB/s |  84 kB     00:01    
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                                                                                   42 kB/s |  84 kB     00:02     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                                                                                                                                               1/1 
  Installing       : lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                   1/1 
  Running scriptlet: lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                   1/1 
  Verifying        : lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                   1/1 

Installed:
  lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                                          

Complete!
[root@igoodful ~]

 

 
 

dnf -y reinstall lrzsz:

 
[root@192.168.31.225:~]# dnf -y reinstall lrzsz
Last metadata expiration check: 0:22:15 ago on Wed 08 Mar 2023 06:28:39 PM CST.
Dependencies resolved.
=======================================================================================================================================================================================================================================================================================================================================
 Package                                                                      Architecture                                                                  Version                                                                                Repository                                                                     Size
=======================================================================================================================================================================================================================================================================================================================================
Reinstalling:
 lrzsz                                                                        x86_64                                                                        0.12.20-43.el8                                                                         baseos                                                                         84 k

Transaction Summary
=======================================================================================================================================================================================================================================================================================================================================

Total download size: 84 k
Installed size: 190 k
Downloading Packages:
lrzsz-0.12.20-43.el8.x86_64.rpm                                                                                                                                                                                                                                                                        451 kB/s |  84 kB     00:00    
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                                                                                  135 kB/s |  84 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                                                                                                                                               1/1 
  Reinstalling     : lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                   1/2 
  Cleanup          : lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                   2/2 
  Running scriptlet: lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                   2/2 
  Verifying        : lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                   1/2 
  Verifying        : lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                   2/2 

Reinstalled:
  lrzsz-0.12.20-43.el8.x86_64                                                                                                                                                                                                                                                                                                          

Complete!
[root@192.168.31.225:~]# 

 

 
 
 
[root@igoodful ~]dnf provides lrzsz
Last metadata expiration check: 0:19:19 ago on Wed 01 Mar 2023 10:57:18 PM CST.
lrzsz-0.12.20-43.el8.x86_64 : The lrz and lsz modem communications programs
Repo        : @System
Matched from:
Provide    : lrzsz = 0.12.20-43.el8

lrzsz-0.12.20-43.el8.x86_64 : The lrz and lsz modem communications programs
Repo        : baseos
Matched from:
Provide    : lrzsz = 0.12.20-43.el8

[root@igoodful ~]

 

 

 卸载软件:

 卸载gnome桌面环境:
# dnf groupremove "GNOME"

 

 
 

 

 

 dnf history:

  1. 你经常安装centos系统的时候,有一个很大的痛点,那就是每次重新安装系统后,都要重新安装一遍原来的安装包,而且你还不知道安装了哪些安装包,这会让人抓狂;
  2. 这个命令非常有用,通常我们安装系统后,经过一段时间后,你自己也不知道安装了些啥,你可能通过history命令记录,但是这个记录了所有正确与不正确的命令,你也可能包装一下yum或dnf成一个shell脚本来记录自己安装了哪些安装包,但都不如这个命令高效通用;
  3. 这个命令只记录你真正安装了或更新了的安装包,而你执行失败了的安装事不会记录下来的,你多次执行相同的安装命令,也只会记录第一次安装成功的记录,通过这个手段您可以知道在自您使用dnf开始,有什么软件什么时候被安装和卸载
  4. dnf history是不是都能找到你安装过的所有安装包呢?显然不是的,它只能追踪你用yum或dnf安装过的或更新过的软件包。如果你用源码安装过的软件包,那它就没办法来记录了,这个得你自己记住咯;

 

 

 

下面就是我本人弄了一个低质量的简单的用于记录安装了哪些包的脚本,也就是包装了dnf命令,但和dnf history比起来还是差远了

[root@igoodful scripts] vim dnff.sh

#!/bin/bash
arg_all="$*"
function dnff(){
        if [ ! -d /data/log ];then
        mkdir -p /data/log
        fi
        echo "dnff ${arg_all}" >> /data/log/yum_rpm.log
        /usr/bin/dnf ${arg_all}
        echo "${arg_all}"
}
function main(){
        dnff
}
main

 

 

[root@192.168.31.225:~]# dnf history  |awk -F'|' '{print $2}'
 Command line                                                                                                                                                                                                                                                                     

 -y remove Percona-Server-MongoDB-36-server.x86_64                                                                                                                                                                                                                                
 -y install Percona-Server-MongoDB-36-server.x86_64                                                                                                                                                                                                                               
 -y install e2fsprogs-devel                                                                                                                                                                                                                                                       
 install -y bzip2-devel.x86*                                                                                                                                                                                                                                                      
 -y install uuid                                                                                                                                                                                                                                                                  
 install python39                                                                                                                                                                                                                                                                 
 install python3-devel openssl-devel                                                                                                                                                                                                                                              
 install -y mailx                                                                                                                                                                                                                                                                 
 groupremove GNOME                                                                                                                                                                                                                                                                
 install ftp                                                                                                                                                                                                                                                                      
 install https://repo.percona.com/yum/percona-release-latest.noarch.rpm                                                                                                                                                                                                           
 -y install lrzsz                                                                                                                                                                                                                                                                 
 -y remove lrzsz                                                                                                                                                                                                                                                                  
 -y install lrzsz                                                                                                                                                                                                                                                                 
 remove lrzsz                                                                                                                                                                                                                                                                     
 install dstat                                                                                                                                                                                                                                                                    
 groupinstall Server                                                                                                                                                                                                                                                              
 -y install libcurl-devel                                                                                                                                                                                                                                                         
 install libtirpc-devel                                                                                                                                                                                                                                                           
 install -y openldap openldap-clients openldap-devel                                                                                                                                                                                                                              
 -y install pam-devel                                                                                                                                                                                                                                                             
 install -y numactl git lrzsz libaio libaio-devel automake autoconf bison libtool ncurses-devel ncurses vim numactl-devel sendmail perl-CPAN perl-Time-HiRes patch cpan perl-DBD-MySQL perl-YAML perl-File-Which perl-DBI bind-utils zlib-devel perl-JSON.noarch openssh-server o 
 remove -y java cvs libselinux-devel postgresql mysql ecj jna sinjdoc                                                                                                                                                                                                             
 -y install libevent-devel                                                                                                                                                                                                                                                        
 remove cmake                                                                                                                                                                                                                                                                     
 -y install make cmake gcc gcc-c++ wget ncurses ncurses-devel openssl openssl-devel libaio libaio-devel bison perl perl-devel                                                                                                                                                     
 -y install cmake                                                                                                                                                                                                                                                                 
 -y install lrzsz                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                  
[root@192.168.31.225:~]# 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#######################

posted @ 2023-03-01 22:00  igoodful  阅读(251)  评论(0编辑  收藏  举报