Linux软件包管理

成功不易,加倍努力!

      • 1 软件运行和编译
        • 1.1 软件相关概念
        • 1.2 C语言程序的实现过程
        • 1.3 软件模块的静态和动态链接
      • 2 软件包和包管理器
        • 2.1 软件包介绍
      • 3 包管理器rpm
        • 3.1 安装
        • 3.2 升级和降级
        • 3.3 包查询
        • 3.4 包卸载
        • 3.5 包校验
        • 3.6 数据库
      • 4 yum和dnf
        • 4.1 yum/dnf 工作原理
        • 4.2 yum客户端配置
        • 4.3 yum命令
      • 5 程序包编译
        • 5.1 源码编译介绍
        • 5.2 开源程序源代码的获取
        • 5.3 编译源码的项目工具
        • 5.4 C语言源代码编译安装过程
      • 6 Ubuntu 软件管理
        • 6.1 APT工作原理
        • 6.2 dpkg 包管理器
        • 6.3 apt
        • 6.4 ubuntu建议安装的常用包

1 软件运行和编译

1.1 软件相关概念

1.1.1 ABI
ABI即 Application Binary Interface

Windows与Linux不兼容

  • ELF(Executable and Linkable - Format)
  • PE(Portable Executable)

1.1.2 API

API即Application Programming Interface

API可以在各种不同的操作系统上实现给应用程序提供完全相同的接口,而它们本身在这些系统上的实现却可能迥异,主流的操作系统有两种,一种是Windows系统,另一种是Linux系统。

由于操作系统的不同,API又分为Windows API和Linux API。

在Windows平台开发出来的软件在Linux上无法运行,在Linux上开发的软件在Windows上又无法运行,这就导致了软件移植困难,POSIX标准的出现就是为了解决这个问题

POSIX:Portable Operating System Interface 可移植操作系统接口,定义了操作系统应该为应用程序提供的接口标准,是IEEE为要在各种UNIX操作系统上运行的软件而定义的一系列API标准的总称。

Linux和windows都要实现基本的posix标准,程序就在源代码级别可移植了

1.1.3 开发语言

系统级开发

  • 汇编语言
  • C
  • C++

应用级开发

  • java Python go php perl delphi basic ruby bash

1.2 C语言程序的实现过程

C 程序源代码 --> 预处理 --> 编译 --> 汇编 --> 链接

C语言的程序编译主要经过四个过程:

  • 预处理(Pre-Processing)
    1)将所有的#define删除,并且展开所有的宏定义
    2)处理所有的条件预编译指令,比如#if #ifdef #elif #else #endif等
    3)处理#include 预编译指令,将被包含的文件插入到该预编译指令的位置。
    4)删除所有注释 “//”和”/* */”.
    5)添加行号和文件标识,以便编译时产生调试用的行号及编译错误警告行号。
    6)保留所有的#pragma编译器指令,因为编译器需要使用它们
  • 编译 (Compiling)
    编译过程就是把预处理完的文件进行一系列的词法分析,语法分析,语义分析及优化后,最后生成相应的汇编代码
  • 汇编 (Assembling)
    汇编器是将汇编代码转变成机器可以执行的命令,每一个汇编语句几乎都对应一条机器指令。汇编相对于编译过程比较简单,根据汇编指令和机器指令的对照表一一翻译即可
  • 链接 (Linking)
    通过调用链接器ld来链接程序运行需要的一大堆目标文件,以及所依赖的其它库文件,最后生成可执行文件

范例:gcc 编译过程

#分步骤编译运行
gcc -E hello.c -o hello.i 对hello.c文件进行预处理,生成了hello.i 文件
gcc -S hello.i -o hello.s 对预处理文件进行编译,生成了汇编文件
gcc -c hello.s -o hello.o 对汇编文件进行编译,生成了目标文件
gcc hello.o -o hello 对目标文件进行链接,生成可执行文件
#一步实现编译过程
gcc hello.c -o hello 直接编译链接成可执行目标文件

1.3 软件模块的静态和动态链接

链接主要作用是把各个模块之间相互引用的部分处理好,使得各个模块之间能够正确地衔接,分为静态和动态链接

1.3.1 静态链接

  • 把程序对应的依赖库复制一份到包
  • 生成模块文件libxxx.a
  • 嵌入程序包
  • 升级难,需重新编译
  • 占用较多空间,迁移容易

1.3.2 动态链接

  • 只把依赖加做一个动态链接
  • 生成模块文件libxxx.so
  • 连接指向
  • 占用较少空间,升级方便

1.3.3 模块(库)文件

查看二进制程序所依赖的库文件

ldd /PATH/TO/BINARY_FILE

管理及查看本机装载的库文件

#加载配置文件中指定的库文件
ldconfig
#显示本机已经缓存的所有可用库文件名及文件路径映射关系
/sbin/ldconfig –p

配置文件:

/etc/ld.so.conf,
/etc/ld.so.conf.d/*.conf

缓存文件:

/etc/ld.so.cache

2 软件包和包管理器

2.1 软件包介绍

开源软件最初只提供了.tar.gz的打包的源码文件,用户必须自已编译每个想在GNU/Linux上运行的软件。
用户急需系统能提供一种更加便利的方法来管理这些软件,当Debian诞生时,这样一个管理工具dpkg也就应运而生,可用来管理deb后缀的"包"文件。
从而著名的“package”概念第一次出现在GNU/Linux系统中,稍后Red Hat才开发自己的rpm包管理系统

2.1.1 软件包中的文件分类

  • 二进制文件
  • 库文件
  • 配置文件
  • 帮助文件

范例:利用 cpio工具查看包文件列表

rpm2cpio 包文件|cpio –itv 预览包内文件
rpm2cpio 包文件|cpio –id “*.conf” 释放包内文件

2.1.2 程序包管理器

软件包管理器功能:

将编译好的应用程序的各组成文件打包一个或几个程序包文件,利用包管理器可以方便快捷地实现程序包的安装、卸载、查询、升级和校验等管理操作

主流的程序包管理器

  • redhat:rpm文件, rpm 包管理器
    rpm:Redhat Package Manager,RPM Package Manager
  • debian:deb文件, dpkg 包管理器

2.1.3 包命名

源代码打包文件命名:

name-VERSION.tar.gz|bz2|xz
VERSION: major.minor.release

rpm包命名方式:

name-VERSION-release.arch.rpm
VERSION: major.minor.release
release:release.OS

常见的arch:

  • x86: i386, i486, i586, i686
  • x86_64: x64, x86_64, amd64
  • powerpc: ppc
  • 跟平台无关:noarch

2.1.4 分类和拆包

软件包为了管理和使用的便利,会将一个大的软件分类,放在不同的子包中。

包的分类

  • Application-VERSION-ARCH.rpm: 主包
  • Application-devel-VERSION-ARCH.rpm 开发子包
  • Application-utils-VERSION-ARHC.rpm 其它子包
  • Application-libs-VERSION-ARHC.rpm 其它子包

2.1.5 包的依赖

软件包之间可能存在依赖关系,甚至循环依赖,即:A包依赖B包,B包依赖C包,C包依赖A包

安装软件包时,会因为缺少依赖的包,而导致安装包失败。

解决依赖包管理工具:

  • yum:rpm包管理器的前端工具
  • dnf:Fedora 18+ rpm包管理器前端管理工具,CentOS 8 版代替 yum
  • apt:deb包管理器前端工具
  • zypper:suse上的rpm前端管理工具

2.1.6 程序包管理器相关文件

1.包文件组成 (每个包独有)

  • 包内的文件
  • 元数据,如:包的名称,版本,依赖性,描述等
  • 可能会有包安装或卸载时运行的脚本

2.数据库(公共):/var/lib/rpm

  • 程序包名称及版本
  • 依赖关系
  • 功能说明
  • 包安装后生成的各文件路径及校验码信息

2.1.7 获取程序包的途径

软件包需要事先将源码进行编译后打包形成,获取包的途径如下:

1 系统发版的光盘或官方网站

  • CentOS镜像:
    https://www.centos.org/download/
    http://mirrors.aliyun.com
    http://mirrors.sohu.com
    http://mirrors.163.com

  • Ubuntu 镜像:
    http://cdimage.ubuntu.com/releases/
    http://releases.ubuntu.com

2 第三方组织提供

  • Fedora-EPEL:Extra Packages for Enterprise Linux
    https://fedoraproject.org/wiki/EPEL
    https://mirrors.aliyun.com/epel/?spm=a2c6h.13651104.0.0.3bc47dfaZpesAr
  • Rpmforge:官网:http://repoforge.org/, RHEL推荐,包很全,即将关闭
  • Community Enterprise Linux Repository:http://www.elrepo.org,支持最新的内核和硬件相关包

3 软件项目官方站点

  • http://yum.mariadb.org/10.4/centos8-amd64/rpms/
  • http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/

4 搜索引擎

  • http://pkgs.org
  • http://rpmfind.net
  • http://rpm.pbone.net
  • https://sourceforge.net/

注意:第三方包建议要检查其合法性,来源合法性,程序包的完整性

5 自己制作

将源码文件,利用工具,如:rpmbuild,fpm等工具制作成rpm包文件

3 包管理器rpm

CentOS系统上使用rpm命令管理程序包

功能: 安装、卸载、升级、查询、校验、数据库维护

3.1 安装

有可能会丢失属性

格式:

rpm {-i|--install} [install-options] PACKAGE_FILE…

选项:

-v: verbose 显示指令执行过程
-vv:详细显示指令执行过程,便于排错
-h: 以#显示程序包管理执行进度

常用组合:

rpm -ivh PACKAGE_FILE ...

rpm包安装[install-options]

--test:         测试安装,但不真正执行安装,即dry run模式
--nodeps:		忽略依赖关系
--replacepkgs | replacefiles 覆盖安装
--nosignature:  不检查来源合法性
--nodigest:	不检查包完整性
--noscripts:	不执行程序包脚本
%pre: 			安装前脚本 --nopre
%post: 			安装后脚本 --nopost
%preun: 		卸载前脚本 --nopreun
%postun: 		卸载后脚本 --nopostun

3.2 升级和降级

rpm包升级

rpm {-U|--upgrade} [install-options] PACKAGE_FILE...
rpm {-F|--freshen} [install-options] PACKAGE_FILE...

upgrade:安装有旧版程序包,则“升级”,如果不存在旧版程序包,则“安装”

freshen:安装有旧版程序包,则“升级”,如果不存在旧版程序包,则不执行升级操作
–oldpackage:降级
–force: 强制安装

常用组合

rpm -Uvh PACKAGE_FILE ...
rpm -Fvh PACKAGE_FILE ...

升级注意项
(1) 不要对内核做升级操作;Linux支持多内核版本并存,因此直接安装新版本内核
(2) 如果原程序包的配置文件安装后曾被修改,升级时,新版本提供的同一个配置文件不会直接覆盖老版本的配置文件,而把新版本文件重命名(FILENAME.rpmnew)后保留

3.3 包查询

#查看包是否安装
rpm {-q|--query} [select-options] [query-options]
[select-options]
-a:		 所有包
-f:		 查看指定的文件由哪个程序包安装生成
-p rpmfile: 针对尚未安装的程序包文件做查询操作,p后面跟文件名,需要路径
[query-options]
--changelog:查询rpm包的changelog
-c:		 查询程序的配置文件
-d:		 查询程序的文档
-i:		 information
-l:		 查看指定的程序包安装后生成的所有文件
--scripts:  程序包自带的脚本,分为四类:安装前后,卸载前后脚本,pre前,post后

#和CAPABILITY相关
--whatprovides CAPABILITY:查询指定的CAPABILITY由哪个包所提供
#并不实用,意义不大,不能解决包的依赖性
--whatrequires CAPABILITY:查询指定的CAPABILITY被哪个包所依赖
--provides:列出指定程序包所提供的CAPABILITY
-R:		查询指定的程序包所依赖的CAPABILITY

常用查询用法

-qa 		查询所有安装过的包
-q PACKAGE  查询软件是否安装
-qi PACKAGE 查询软件的基本信息
-qc PACKAGE 查询程序的配置文件
-ql PACKAGE 装包之后查看有哪些文件
-qd PACKAGE 查询程序的文档
-q --scripts PACKAGE 查看程序包自带的脚本
-qf FILE 	查看磁盘上的某个文件来自于那个包,利用的是rpm的数据库查询
-qpi PACKAGE_FILE 装包之前查看,p后面跟文件名,需要路径
-qpl PACKAGE_FILE, ... 装包之前查询装完之后会有哪些文件

3.4 包卸载

格式:

rpm {-e|--erase} [--allmatches] [--nodeps] [--noscripts] [--notriggers] [--test]
PACKAGE_NAME ...

注意:当包卸载时,对应的配置文件不会删除,以FILENAME.rpmsave形式保留,无法解决包的依赖性

范例:强行删除rpm包,并恢复

[root@centos7 ~]#rpm -e rpm --nodeps
#重启进入rescue模式
#mkdir /mnt/cdrom
#mount /dev/sr0 /mnt/cdrom
#rpm -ivh /mnt/cdrom/Packages/rpm-4.11.3-40.el7.x86_64.rpm --root=/mnt/sysimage
#reboot

3.5 包校验

在安装包时,系统也会检查包的来源是否是合法的
检查包的完整性和签名

rpm -K|--checksig rpmfile

在检查包的来源和完整性前,必须导入所需要公钥

范例:

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
rpm -qa “gpg-pubkey*”

范例:

[root@centos8 ~]#rpm -K /misc/cd/AppStream/Packages/httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64.rpm 
/misc/cd/AppStream/Packages/httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64.rpm: digests signatures NOT OK
[root@centos8 ~]#cat /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2.0.22 (GNU/Linux)

mQINBFzMWxkBEADHrskpBgN9OphmhRkc7P/YrsAGSvvl7kfu+e9KAaU6f5MeAVyn
rIoM43syyGkgFyWgjZM8/rur7EMPY2yt+2q/1ZfLVCRn9856JqTIq0XRpDUe4nKQ
8BlA7wDVZoSDxUZkSuTIyExbDf0cpw89Tcf62Mxmi8jh74vRlPy1PgjWL5494b3X
5fxDidH4bqPZyxTBqPrUFuo+EfUVEqiGF94Ppq6ZUvrBGOVo1V1+Ifm9CGEK597c
aevcGc1RFlgxIgN84UpuDjPR9/zSndwJ7XsXYvZ6HXcKGagRKsfYDWGPkA5cOL/e
f+yObOnC43yPUvpggQ4KaNJ6+SMTZOKikM8yciyBwLqwrjo8FlJgkv8Vfag/2UR7
JINbyqHHoLUhQ2m6HXSwK4YjtwidF9EUkaBZWrrskYR3IRZLXlWqeOi/+ezYOW0m
vufrkcvsh+TKlVVnuwmEPjJ8mwUSpsLdfPJo1DHsd8FS03SCKPaXFdD7ePfEjiYk
nHpQaKE01aWVSLUiygn7F7rYemGqV9Vt7tBw5pz0vqSC72a5E3zFzIIuHx6aANry
Gat3aqU3qtBXOrA/dPkX9cWE+UR5wo/A2UdKJZLlGhM2WRJ3ltmGT48V9CeS6N9Y
m4CKdzvg7EWjlTlFrd/8WJ2KoqOE9leDPeXRPncubJfJ6LLIHyG09h9kKQARAQAB
tDpDZW50T1MgKENlbnRPUyBPZmZpY2lhbCBTaWduaW5nIEtleSkgPHNlY3VyaXR5
QGNlbnRvcy5vcmc+iQI3BBMBAgAhBQJczFsZAhsDBgsJCAcDAgYVCAIJCgsDFgIB
Ah4BAheAAAoJEAW1VbOEg8ZdjOsP/2ygSxH9jqffOU9SKyJDlraL2gIutqZ3B8pl
Gy/Qnb9QD1EJVb4ZxOEhcY2W9VJfIpnf3yBuAto7zvKe/G1nxH4Bt6WTJQCkUjcs
N3qPWsx1VslsAEz7bXGiHym6Ay4xF28bQ9XYIokIQXd0T2rD3/lNGxNtORZ2bKjD
vOzYzvh2idUIY1DgGWJ11gtHFIA9CvHcW+SMPEhkcKZJAO51ayFBqTSSpiorVwTq
a0cB+cgmCQOI4/MY+kIvzoexfG7xhkUqe0wxmph9RQQxlTbNQDCdaxSgwbF2T+gw
byaDvkS4xtR6Soj7BKjKAmcnf5fn4C5Or0KLUqMzBtDMbfQQihn62iZJN6ZZ/4dg
q4HTqyVpyuzMXsFpJ9L/FqH2DJ4exGGpBv00ba/Zauy7GsqOc5PnNBsYaHCply0X
407DRx51t9YwYI/ttValuehq9+gRJpOTTKp6AjZn/a5Yt3h6jDgpNfM/EyLFIY9z
V6CXqQQ/8JRvaik/JsGCf+eeLZOw4koIjZGEAg04iuyNTjhx0e/QHEVcYAqNLhXG
rCTTbCn3NSUO9qxEXC+K/1m1kaXoCGA0UWlVGZ1JSifbbMx0yxq/brpEZPUYm+32
o8XfbocBWljFUJ+6aljTvZ3LQLKTSPW7TFO+GXycAOmCGhlXh2tlc6iTc41PACqy
yy+mHmSv
=kkH7
-----END PGP PUBLIC KEY BLOCK-----
[root@centos8 ~]#rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[root@centos8 ~]#rpm -K /misc/cd/AppStream/Packages/httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64.rpm 
/misc/cd/AppStream/Packages/httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64.rpm: digests signatures OK

[root@centos8 ~]#rpm -qa "gpg-pubkey"
gpg-pubkey-8483c65d-5ccc5b19
[root@centos8 ~]#rpm -qi gpg-pubkey-8483c65d-5ccc5b19
Name        : gpg-pubkey
Version     : 8483c65d
Release     : 5ccc5b19
Architecture: (none)
Install Date: Thu 19 Mar 2020 07:35:48 PM CST
Group       : Public Keys
Size        : 0
License     : pubkey
Signature   : (none)
Source RPM  : (none)
Build Date  : Fri 03 May 2019 11:15:37 PM CST
Build Host  : localhost
Relocations : (not relocatable)
Packager    : CentOS (CentOS Official Signing Key) <[email protected]>
Summary     : gpg(CentOS (CentOS Official Signing Key) <[email protected]>)
Description :
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: rpm-4.14.2 (NSS-3)

mQINBFzMWxkBEADHrskpBgN9OphmhRkc7P/YrsAGSvvl7kfu+e9KAaU6f5MeAVyn
rIoM43syyGkgFyWgjZM8/rur7EMPY2yt+2q/1ZfLVCRn9856JqTIq0XRpDUe4nKQ
8BlA7wDVZoSDxUZkSuTIyExbDf0cpw89Tcf62Mxmi8jh74vRlPy1PgjWL5494b3X
5fxDidH4bqPZyxTBqPrUFuo+EfUVEqiGF94Ppq6ZUvrBGOVo1V1+Ifm9CGEK597c
aevcGc1RFlgxIgN84UpuDjPR9/zSndwJ7XsXYvZ6HXcKGagRKsfYDWGPkA5cOL/e
f+yObOnC43yPUvpggQ4KaNJ6+SMTZOKikM8yciyBwLqwrjo8FlJgkv8Vfag/2UR7
JINbyqHHoLUhQ2m6HXSwK4YjtwidF9EUkaBZWrrskYR3IRZLXlWqeOi/+ezYOW0m
vufrkcvsh+TKlVVnuwmEPjJ8mwUSpsLdfPJo1DHsd8FS03SCKPaXFdD7ePfEjiYk
nHpQaKE01aWVSLUiygn7F7rYemGqV9Vt7tBw5pz0vqSC72a5E3zFzIIuHx6aANry
Gat3aqU3qtBXOrA/dPkX9cWE+UR5wo/A2UdKJZLlGhM2WRJ3ltmGT48V9CeS6N9Y
m4CKdzvg7EWjlTlFrd/8WJ2KoqOE9leDPeXRPncubJfJ6LLIHyG09h9kKQARAQAB
tDpDZW50T1MgKENlbnRPUyBPZmZpY2lhbCBTaWduaW5nIEtleSkgPHNlY3VyaXR5
QGNlbnRvcy5vcmc+iQI3BBMBAgAhBQJczFsZAhsDBgsJCAcDAgYVCAIJCgsDFgIB
Ah4BAheAAAoJEAW1VbOEg8ZdjOsP/2ygSxH9jqffOU9SKyJDlraL2gIutqZ3B8pl
Gy/Qnb9QD1EJVb4ZxOEhcY2W9VJfIpnf3yBuAto7zvKe/G1nxH4Bt6WTJQCkUjcs
N3qPWsx1VslsAEz7bXGiHym6Ay4xF28bQ9XYIokIQXd0T2rD3/lNGxNtORZ2bKjD
vOzYzvh2idUIY1DgGWJ11gtHFIA9CvHcW+SMPEhkcKZJAO51ayFBqTSSpiorVwTq
a0cB+cgmCQOI4/MY+kIvzoexfG7xhkUqe0wxmph9RQQxlTbNQDCdaxSgwbF2T+gw
byaDvkS4xtR6Soj7BKjKAmcnf5fn4C5Or0KLUqMzBtDMbfQQihn62iZJN6ZZ/4dg
q4HTqyVpyuzMXsFpJ9L/FqH2DJ4exGGpBv00ba/Zauy7GsqOc5PnNBsYaHCply0X
407DRx51t9YwYI/ttValuehq9+gRJpOTTKp6AjZn/a5Yt3h6jDgpNfM/EyLFIY9z
V6CXqQQ/8JRvaik/JsGCf+eeLZOw4koIjZGEAg04iuyNTjhx0e/QHEVcYAqNLhXG
rCTTbCn3NSUO9qxEXC+K/1m1kaXoCGA0UWlVGZ1JSifbbMx0yxq/brpEZPUYm+32
o8XfbocBWljFUJ+6aljTvZ3LQLKTSPW7TFO+GXycAOmCGhlXh2tlc6iTc41PACqy
yy+mHmSv
=kkH7
-----END PGP PUBLIC KEY BLOCK-----

范例:校验包文件

[root@centos8 ~]#rpm -K /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm 
/misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm: digests signatures OK
[root@centos8 ~]#cp /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm /data
[root@centos8 ~]#cd /data
[root@centos8 data]#ll
total 60
-r--r--r--. 1 root root 60780 Apr  8 23:25 tree-1.7.0-15.el8.x86_64.rpm
[root@centos8 data]#echo >>tree-1.7.0-15.el8.x86_64.rpm
[root@centos8 data]#ll tree-1.7.0-15.el8.x86_64.rpm
-r--r--r--. 1 root root 60781 Apr  8 23:25 tree-1.7.0-15.el8.x86_64.rpm
[root@centos8 data]#cd
[root@centos8 ~]#rpm -K /data/tree-1.7.0-15.el8.x86_64.rpm 
/data/tree-1.7.0-15.el8.x86_64.rpm: DIGESTS SIGNATURES NOT OK
[root@centos8 ~]#vim -b /data/tree-1.7.0-15.el8.x86_64.rpm
#扩展命令模式下输入%!xxd编辑二进制文件,删除末行新加入的空格0a,再输入%!xxd -r退出二进制模式
[root@centos8 ~]#rpm -K /data/tree-1.7.0-15.el8.x86_64.rpm 
/data/tree-1.7.0-15.el8.x86_64.rpm: digests signatures OK
[root@centos8 ~]#ll /data/
total 60
-r--r--r--. 1 root root 60780 Apr  8 23:33 tree-1.7.0-15.el8.x86_64.rpm

软件在安装时,会将包里的每个文件的元数据,如:大小,权限,所有者,时间等记录至rpm相关的数据库中,可以用来检查包中的文件是否和当初安装时有所变化

rpm {-V|--verify} [select-options] [verify-options]
#示例:
[root@centos8 ~]#rpm -V centos-release
S.5....T. c /etc/issue
S file Size differs
M Mode differs (includes permissions and file type)
5 digest (formerly MD5 sum) differs
D Device major/minor number mismatch
L readLink(2) path mismatch
U User ownership differs
G Group ownership differs
T mTime differs
P capabilities differ
. () 表示当前位置代表的字符含义一致
c 所在的位置表示文件类型
c 配置文件
d 文件数据文件
g 该文件不属于某个文件(极少情况)
l 许可证文件(license file)
r 自述文件(READ ME)

3.6 数据库

rpm包安装时生成的信息,都放在rpm数据库中

/var/lib/rpm

可以重建数据库

rpm {--initdb|--rebuilddb}
initdb: 初始化,如果事先不存在数据库,则新建之,否则,不执行任何操作
rebuilddb:重建已安装的包头的数据库索引目录

4 yum和dnf

CentOS 使用 yum, dnf 解决rpm的包依赖关系

YUM: Yellowdog Update Modifier,rpm的前端程序,可解决软件包相关依赖性,可在多个库之间定位软件包,up2date的替代工具,CentOS 8 用dnf 代替了yum ,不过保留了和yum的兼容性,配置也是通用的

4.1 yum/dnf 工作原理

yum/dnf 是基于C/S 模式

  • yum 服务器存放rpm包和相关包的元数据库
  • yum 客户端访问yum服务器进行安装或查询等

yum 实现过程

先在yum服务器上创建 yum repository(仓库),在仓库中事先存储了众多rpm包,以及包的相关的元数据文件(放置于特定目录repodata下),当yum客户端利用yum/dnf工具进行安装时包时,会自动下载repodata中的元数据,查询远数据是否存在相关的包及依赖关系,自动从仓库中找到相关包下载并安装。

yum服务器的仓库可以多种形式存在:

  • file:// 本地路径
  • http://
  • https://
  • ftp://

注意:yum仓库指向的路径一定必须是repodata目录所在目录

4.2 yum客户端配置

yum客户端配置文件

/etc/yum.conf #为所有仓库提供公共配置
/etc/yum.repos.d/*.repo: #为每个仓库的提供配置文件

帮助参考: man 5 yum.conf

相关变量 :

yum的repo配置文件中可用的变量:
$releasever: 当前OS的发行版的主版本号,如:8,7,6
$arch: 		 CPU架构,如:aarch64, i586, i686,x86_64等
$basearch:  系统基础平台;i386, x86_64
$contentdir:表示目录,比如:centos-8,centos-7
$YUM0-$YUM9: 自定义变量

范例:

http://server/centos/$releasever/$basearch/
http://server/centos/7/x86_64
http://server/centos/6/i386

范例:CentOS 8 配置文件

[root@centos8 ~]#ll /etc/yum.conf 
lrwxrwxrwx. 1 root root 12 Nov 12 23:35 /etc/yum.conf -> dnf/dnf.conf

[root@centos8 ~]#cat /etc/yum.conf
[main]
gpgcheck=1
installonly_limit=3
clean_requirements_on_remove=True
best=True

范例:CentOS 7的配置文件

[root@centos7 ~]#ll /etc/yum.conf 
-rw-r--r--. 1 root root 970 Aug  8  2019 /etc/yum.conf
[root@centos7 ~]#cat /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release


#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

repo仓库配置文件指向的定义

[repositoryID]
name=Some name for this repository
baseurl=url://path/to/repository/
enabled={1|0}
gpgcheck={1|0}
gpgkey=URL
enablegroups={1|0}
failovermethod={roundrobin|priority}
roundrobin:意为随机挑选,默认值
priority:按顺序访问
cost= 默认为1000

baseurl指向的路径
阿里云提供了写好的CentOS和ubuntu的仓库文件下载链接

http://mirrors.aliyun.com/repo/

CentOS系统的yum源

#阿里云
https://mirrors.aliyun.com/centos/$releasever/os/x86_64/
#华为云
https://mirrors.huaweicloud.com/
#清华大学
https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/x86_64/

EPEL的yum源

#阿里云
https://mirrors.aliyun.com/epel/$releasever/x86_64

阿里巴巴开源软件

https://opsx.alibaba.com/

范例:为CentOS7用系统安装光盘作的本地yum仓库

#挂载光盘至某目录,如/mnt/cdrom
mount /dev/cdrom /mnt/cdrom

#创建配置文件
[root@centos7 ~]#vim /etc/yum.repos.d/centos7.repo
[CentOS7]
name=CentOS 7
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1

范例:为CentOS 8 配置 yum 的系统和EPEL源仓库

[root@centos8 ~]#cat /etc/yum.repos.d/base.repo 
[BaseOS]
name=BaseOS
baseurl=file:///misc/cd/BaseOS
gpgcheck=1
gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[AppStream]
name=AppStream
baseurl=file:///misc/cd/AppStream
gpgcheck=0

[epel]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch/
gpgcheck=0
enabled=1

[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os/
gpgcheck=0

注意:与之前的版本不同,CentOS 8 系统有两个yum 源:BaseOS和AppStream ,需要分别设置两个仓库

范例:用脚本实现创建yum仓库配置文件

[root@centos7 ~]#cat yum.sh 
#!/bin/bash

mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.aliyun.com/centos/\$releasever/os/\$basearch
gpgcheck=0
EOF


[root@centos6 ~]#cat /etc/yum.repos.d/base.repo 
[base]
name=base
baseurl=file:///misc/cd/   #本地仓库
		https://mirrors.aliyun.com/centos/$releasever/os/$basearch
gpgcheck=0

[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch/
gpgcheck=0
enabled=0

[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch
gpgcheck=0
enabled=0

神奇文件夹

[root@centos6 ~]#yum -y install autofs ; chkconfig autofs on ; service autofs start

yum-config-manager命令

可以生成yum仓库的配置文件及启用或禁用仓库,来自于yum-utils包

格式:

#增加仓库
yum-config-manager --add-repo URL或file
#禁用仓库
yum-config-manager --disable “仓库名"
#启用仓库
yum-config-manager --enable “仓库名”

范例:启用和禁用仓库

[root@centos8 ~]#yum repolist
BaseOS                                     3.8 MB/s | 3.9 kB     00:00    
AppStream                                  4.2 MB/s | 4.3 kB     00:00    
EPEL                                        34 kB/s | 4.7 kB     00:00    
extras                                      13 kB/s | 1.5 kB     00:00    
repo id                           repo name                          status
AppStream                         AppStream                          4,755
BaseOS                            BaseOS                             1,659
epel                              EPEL                               5,219
extras                            extras

[root@centos8 ~]#yum-config-manager --disable epel
[root@centos8 ~]#cat /etc/yum.repos.d/base.repo 
[BaseOS]
name=BaseOS
baseurl=file:///misc/cd/BaseOS
gpgcheck=0
gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[AppStream]
name=AppStream
baseurl=file:///misc/cd/AppStream
gpgcheck=0
[epel]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch/
gpgcheck=0
enabled=0
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os/
gpgcheck=0
enabled=1
[root@centos8 ~]#yum repolist
BaseOS                                     3.8 MB/s | 3.9 kB     00:00    
AppStream                                  4.2 MB/s | 4.3 kB     00:00    
extras                                      13 kB/s | 1.5 kB     00:00    
repo id                           repo name                          status
AppStream                         AppStream                          4,755
BaseOS                            BaseOS                             1,659
extras                            extras                                12
[root@centos8 ~]#yum-config-manager --disable extras
[root@centos8 ~]#yum repolist
BaseOS                                     3.8 MB/s | 3.9 kB     00:00    
AppStream                                  4.2 MB/s | 4.3 kB     00:00    
repo id                           repo name                          status
AppStream                         AppStream                          4,755
BaseOS                            BaseOS                             1,659
[root@centos8 ~]#yum-config-manager --enable extras
[root@centos8 ~]#yum repolist
BaseOS                                     3.8 MB/s | 3.9 kB     00:00    
AppStream                                  4.2 MB/s | 4.3 kB     00:00    
extras                                     3.6 kB/s | 1.5 kB     00:00    
repo id                           repo name                          status
AppStream                         AppStream                          4,755
BaseOS                            BaseOS                             1,659
extras                            extras                                12

4.3 yum命令

yum命令的用法:

yum [options] [command] [package ...]

yum的命令行选项:

-y 						 #自动回答为“yes”
-q 						 #静默模式
--nogpgcheck 			 #禁止进行gpg check
--enablerepo=repoidglob  #临时启用此处指定的repo,支持通配符,如:”*“
--disablerepo=repoidglob #临时禁用此处指定的repo,和上面语句同时使用,放在后面的生效

4.3.1 显示仓库列表

yum repolist [all|enabled|disabled]

范例:

[root@centos8 ~]#yum repolist
BaseOS                                     3.8 MB/s | 3.9 kB     00:00    
AppStream                                  4.2 MB/s | 4.3 kB     00:00    
extras                                     3.6 kB/s | 1.5 kB     00:00    
repo id                           repo name                          status
AppStream                         AppStream                          4,755
BaseOS                            BaseOS                             1,659
extras                            extras                                12
[root@centos8 ~]#yum repolist all
BaseOS                                     3.8 MB/s | 3.9 kB     00:00    
AppStream                                  4.2 MB/s | 4.3 kB     00:00    
extras                                     6.4 kB/s | 1.5 kB     00:00    
repo id                       repo name                      status
AppStream                     AppStream                      enabled: 4,755
BaseOS                        BaseOS                         enabled: 1,659
epel                          EPEL                           disabled
extras                        extras                         enabled:    12
[root@centos8 ~]#yum --enablerepo=ep* --disablerepo=A* repolist
EPEL                                        29 kB/s | 4.7 kB     00:00    

repo id                            repo name                         status
BaseOS                             BaseOS                            1,661
epel                               EPEL                              5,219
extras                             extras                               12

4.3.2 显示程序包

yum list
yum list [all | glob_exp1] [glob_exp2] [...]
yum list {available|installed|updates} [glob_exp1] [...]

范例:

#列出包的版本
[root@centos8 ~]#dnf list mariadb-server
Last metadata expiration check: 0:06:56 ago on Thu 09 Apr 2020 05:42:11 PM CST.
Installed Packages
mariadb-server.x86_64  3:10.3.17-1.module_el8.1.0+217+4d875839   @AppStream

#列出包的所有版本
[root@centos8 ~]#dnf list mariadb-server --showduplicates
Last metadata expiration check: 0:03:17 ago on Thu 09 Apr 2020 06:16:49 PM CST.
Installed Packages
mariadb-server.x86_64  3:10.3.17-1.module_el8.1.0+217+4d875839   @AppStream
Available Packages
MariaDB-server.x86_64  10.4.10-1.el8                             mariadb   
MariaDB-server.x86_64  10.4.11-1.el8                             mariadb   
MariaDB-server.x86_64  10.4.12-1.el8                             mariadb   
mariadb-server.x86_64  3:10.3.17-1.module_el8.1.0+217+4d875839   AppStream 

#禁用某个仓库后,列出包的版本
[root@centos8 ~]#dnf list mariadb-server --disablerepo=AppStream
Last metadata expiration check: 0:03:27 ago on Thu 09 Apr 2020 06:16:49 PM CST.
Available Packages
MariaDB-server.x86_64  10.4.12-1.el8                             mariadb   

#禁用某个仓库后,列出包的所有版本
[root@centos8 ~]#dnf list mariadb-server --showduplicates --disablerepo=AppStream
Last metadata expiration check: 0:03:59 ago on Thu 09 Apr 2020 06:16:49 PM CST.
Available Packages
MariaDB-server.x86_64  10.4.10-1.el8                             mariadb   
MariaDB-server.x86_64  10.4.11-1.el8                             mariadb   
MariaDB-server.x86_64  10.4.12-1.el8                             mariadb

4.3.3 安装程序包

yum install package1 [package2] [...]
yum reinstall package1 [package2] [...] #重新安装

范例:

[root@centos8 ~]#rm /usr/bin/tree
rm: remove regular file '/usr/bin/tree'? y
[root@centos8 ~]#yum reinstall tree
[root@centos8 ~]#tree
.
├── anaconda-ks.cfg
└── initial-setup-ks.cfg

0 directories, 2 files

#运行安装sl程序,来不及解释了,快上车!
[root@centos7 ~]#sl -a

Linux软件包管理_第1张图片
范例:利用elrepo源在CentOS 7 安装新版内核

[root@centos7 ~]yum install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
[root@centos7 ~]#rpm -ql elrepo-release-7.0-4.el7.elrepo
/etc/pki/elrepo
/etc/pki/elrepo/SECURE-BOOT-KEY-elrepo.org.der
/etc/pki/rpm-gpg
/etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
/etc/yum.repos.d
/etc/yum.repos.d/elrepo.repo
[root@centos7 ~]#yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * elrepo: ftp.yz.yamagata-u.ac.jp
elrepo                                              | 2.9 kB     00:06     
elrepo/primary_db              FAILED                                          
http://ftp.yz.yamagata-u.ac.jp/pub/linux/RPMS/elrepo/elrepo/el7/x86_64/repodata/d96ab65364081828186069a3ac814c5737a5163cc4cb1b7a009a12c94d625730-primary.sqlite.bz2: [Errno 12] Timeout on http://ftp.yz.yamagata-u.ac.jp/pub/linux/RPMS/elrepo/elrepo/el7/x86_64/repodata/d96ab65364081828186069a3ac814c5737a5163cc4cb1b7a009a12c94d625730-primary.sqlite.bz2: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
Trying other mirror.
elrepo/primary_db                                     | 456 kB   00:37     
repo id         repo name                                            status
base/7/x86_64   base                                                 10,097
elrepo          ELRepo.org Community Enterprise Linux Repository - e    149
epel/7/x86_64   epel                                                 13,230
extras/7/x86_64 extras                                                  341
repolist: 23,817
[root@centos7 ~]#cat /etc/yum.repos.d/elrepo.repo
### Name: ELRepo.org Community Enterprise Linux Repository for el7
### URL: http://elrepo.org/

[elrepo]
name=ELRepo.org Community Enterprise Linux Repository - el7
baseurl=http://elrepo.org/linux/elrepo/el7/$basearch/
	http://mirrors.coreix.net/elrepo/elrepo/el7/$basearch/
	http://mirror.rackspace.com/elrepo/elrepo/el7/$basearch/
	http://repos.lax-noc.com/elrepo/elrepo/el7/$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo.el7
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0

[elrepo-testing]
name=ELRepo.org Community Enterprise Linux Testing Repository - el7
baseurl=http://elrepo.org/linux/testing/el7/$basearch/
	http://mirrors.coreix.net/elrepo/testing/el7/$basearch/
	http://mirror.rackspace.com/elrepo/testing/el7/$basearch/
	http://repos.lax-noc.com/elrepo/testing/el7/$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo-testing.el7
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0

[elrepo-kernel]
name=ELRepo.org Community Enterprise Linux Kernel Repository - el7
baseurl=http://elrepo.org/linux/kernel/el7/$basearch/
	    http://mirrors.coreix.net/elrepo/kernel/el7/$basearch/
		http://mirror.rackspace.com/elrepo/kernel/el7/$basearch/
		http://repos.lax-noc.com/elrepo/kernel/el7/$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo-kernel.el7
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0

[elrepo-extras]
name=ELRepo.org Community Enterprise Linux Extras Repository - el7
baseurl=http://elrepo.org/linux/extras/el7/$basearch/
		http://mirrors.coreix.net/elrepo/extras/el7/$basearch/
		http://mirror.rackspace.com/elrepo/extras/el7/$basearch/
		http://repos.lax-noc.com/elrepo/extras/el7/$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo-extras.el7
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0
[root@centos7 ~]#yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * elrepo-kernel: mirror-hk.koddos.net
elrepo-kernel/primary_db                              | 1.9 MB   00:41     
Available Packages
kernel-lt.x86_64                     4.4.218-1.el7.elrepo     elrepo-kernel
kernel-lt-devel.x86_64               4.4.218-1.el7.elrepo     elrepo-kernel
kernel-lt-doc.noarch                 4.4.218-1.el7.elrepo     elrepo-kernel
kernel-lt-headers.x86_64             4.4.218-1.el7.elrepo     elrepo-kernel
kernel-lt-tools.x86_64               4.4.218-1.el7.elrepo     elrepo-kernel
kernel-lt-tools-libs.x86_64          4.4.218-1.el7.elrepo     elrepo-kernel
kernel-lt-tools-libs-devel.x86_64    4.4.218-1.el7.elrepo     elrepo-kernel
kernel-ml.x86_64                     5.6.3-1.el7.elrepo       elrepo-kernel
kernel-ml-devel.x86_64               5.6.3-1.el7.elrepo       elrepo-kernel
kernel-ml-doc.noarch                 5.6.3-1.el7.elrepo       elrepo-kernel
kernel-ml-headers.x86_64             5.6.3-1.el7.elrepo       elrepo-kernel
kernel-ml-tools.x86_64               5.6.3-1.el7.elrepo       elrepo-kernel
kernel-ml-tools-libs.x86_64          5.6.3-1.el7.elrepo       elrepo-kernel
kernel-ml-tools-libs-devel.x86_64    5.6.3-1.el7.elrepo       elrepo-kernel
perf.x86_64                          5.6.3-1.el7.elrepo       elrepo-kernel
python-perf.x86_64                   5.6.3-1.el7.elrepo       elrepo-kernel
[root@centos7 ~]#yum -y --enablerepo="elrepo-kernel" install kernel-ml
[root@centos7 ~]#ls /boot
config-3.10.0-1062.el7.x86_64
config-5.6.3-1.el7.elrepo.x86_64
efi
grub
grub2
initramfs-0-rescue-547d5e166e8c484da460c079ac824df6.img
initramfs-3.10.0-1062.el7.x86_64.img
initramfs-3.10.0-1062.el7.x86_64kdump.img
initramfs-5.6.3-1.el7.elrepo.x86_64.img
symvers-3.10.0-1062.el7.x86_64.gz
symvers-5.6.3-1.el7.elrepo.x86_64.gz
System.map-3.10.0-1062.el7.x86_64
System.map-5.6.3-1.el7.elrepo.x86_64
vmlinuz-0-rescue-547d5e166e8c484da460c079ac824df6
vmlinuz-3.10.0-1062.el7.x86_64
vmlinuz-5.6.3-1.el7.elrepo.x86_64
[root@centos7 ~]#ls /lib/modules
3.10.0-1062.el7.x86_64  5.6.3-1.el7.elrepo.x86_64
[root@centos7 ~]#uname -r
5.6.3-1.el7.elrepo.x86_64

4.3.4 卸载程序包

yum remove | erase package1 [package2] [...]

4.3.5 升级和降级

升级和降级

yum update [package1] [package2] [...]
yum downgrade package1 [package2] [...] (降级)

范例:

[root@centos7 ~]#cat /etc/yum.repos.d/base.repo
[base]
name=base
baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-$releasever
enabled=1

[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch/
gpgcheck=0

[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch
gpgcheck=0

[update]
name=aliyun update
baseurl=https://mirrors.aliyun.com/centos/7/updates/x86_64/
gpgcheck=0
[root@centos7 ~]#yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * elrepo: elrepo.org
base                                                | 3.6 kB     00:00     
epel                                                | 4.7 kB     00:00     
extras                                              | 2.9 kB     00:00     
update                                              | 2.9 kB     00:00     
update/primary_db                                     | 7.6 MB   00:06     
repo id         repo name                                            status
base/7/x86_64   base                                                 10,097
elrepo          ELRepo.org Community Enterprise Linux Repository - e    149
epel/7/x86_64   epel                                                 13,230
extras/7/x86_64 extras                                                  341
update          aliyun update                                         1,787
repolist: 25,604
[root@centos7 ~]#yum --disablerepo=* --enablerepo=update list available
[root@centos7 ~]#yum info samba
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * elrepo: mirror-hk.koddos.net
Available Packages
Name        : samba
Arch        : x86_64
Version     : 4.9.1
Release     : 10.el7_7
Size        : 685 k
Repo        : update
Summary     : Server and Client software to interoperate with Windows
            : machines
URL         : http://www.samba.org/
License     : GPLv3+ and LGPLv3+
Description : Samba is the standard Windows interoperability suite of
            : programs for Linux and Unix.

[root@centos7 ~]#yum info samba --showduplicates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * elrepo: mirror-hk.koddos.net
Available Packages
Name        : samba
Arch        : x86_64
Version     : 4.9.1
Release     : 6.el7
Size        : 685 k
Repo        : base/7/x86_64
Summary     : Server and Client software to interoperate with Windows
            : machines
URL         : http://www.samba.org/
License     : GPLv3+ and LGPLv3+
Description : Samba is the standard Windows interoperability suite of
            : programs for Linux and Unix.

Name        : samba
Arch        : x86_64
Version     : 4.9.1
Release     : 10.el7_7
Size        : 685 k
Repo        : update
Summary     : Server and Client software to interoperate with Windows
            : machines
URL         : http://www.samba.org/
License     : GPLv3+ and LGPLv3+
Description : Samba is the standard Windows interoperability suite of
            : programs for Linux and Unix.
[root@centos7 ~]#yum install samba --disablerepo=update
[root@centos7 ~]#yum update samba
[root@centos7 ~]# yum update

检查可用升级:

yum check-update

4.3.6 查询

查看程序包information:

yum info [...]

查看指定的特性(可以是某文件)是由哪个程序包所提供:

yum provides | whatprovides feature1 [feature2] [...]

范例:

[root@centos8 ~]#ll /etc/vsftpd/vsftpd.conf
ls: cannot access '/etc/vsftpd/vsftpd.conf': No such file or directory
[root@centos8 ~]#yum provides /etc/vsftpd/vsftpd.conf
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:00:56 ago on Thu 09 Apr 2020 09:58:13 PM CST.
vsftpd-3.0.3-28.el8.x86_64 : Very Secure Ftp Daemon
Repo        : @System
Matched from:
Filename    : /etc/vsftpd/vsftpd.conf

vsftpd-3.0.3-28.el8.x86_64 : Very Secure Ftp Daemon
Repo        : AppStream
Matched from:
Filename    : /etc/vsftpd/vsftpd.conf

以指定的关键字搜索程序包名及summary信息

yum search string1 [string2] [...]

查看指定包所依赖的capabilities:

yum deplist package1 [package2] [...]

范例;

[root@centos8 ~]#dnf info bash
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:02:32 ago on Thu 09 Apr 2020 09:58:13 PM CST.
Installed Packages
Name         : bash
Version      : 4.4.19
Release      : 10.el8
Architecture : x86_64
Size         : 6.6 M
Source       : bash-4.4.19-10.el8.src.rpm
Repository   : @System
From repo    : anaconda
Summary      : The GNU Bourne Again shell
URL          : https://www.gnu.org/software/bash
License      : GPLv3+
Description  : The GNU Bourne Again shell (Bash) is a shell or command
             : language interpreter that is compatible with the Bourne
             : shell (sh). Bash incorporates useful features from the Korn
             : shell (ksh) and the C shell (csh). Most sh scripts can be
             : run by bash without modification.

范例:

[root@centos8 ~]#dnf list bash*
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:03:32 ago on Thu 09 Apr 2020 09:58:13 PM CST.
Installed Packages
bash.x86_64                          4.4.19-10.el8                @anaconda
bash-completion.noarch               1:2.7-5.el8                  @anaconda
Available Packages
bash-doc.x86_64                      4.4.19-10.el8                BaseOS 

范例:

[root@centos8 ~]#dnf provides /bin/ls
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:04:06 ago on Thu 09 Apr 2020 09:58:13 PM CST.
coreutils-8.30-6.el8.x86_64 : A set of basic GNU tools commonly used in
                            : shell scripts
Repo        : @System
Matched from:
Provide    : /bin/ls

范例:

[root@centos8 ~]#rpm -ivh /misc/cd/AppStream/Packages/httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64.rpm
error: Failed dependencies:
	httpd-filesystem is needed by httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
	httpd-filesystem = 2.4.37-16.module_el8.1.0+256+ae790463 is needed by httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
	httpd-tools = 2.4.37-16.module_el8.1.0+256+ae790463 is needed by httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
	libapr-1.so.0()(64bit) is needed by httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
	libaprutil-1.so.0()(64bit) is needed by httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
	mod_http2 is needed by httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
	system-logos-httpd is needed by httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64

[root@centos8 ~]#yum deplist httpd
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:08:04 ago on Thu 09 Apr 2020 09:58:13 PM CST.
package: httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
  dependency: /bin/sh
   provider: bash-4.4.19-10.el8.x86_64
  dependency: /etc/mime.types
   provider: mailcap-2.1.48-3.el8.noarch
  dependency: httpd-filesystem
   provider: httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch
  dependency: httpd-filesystem = 2.4.37-16.module_el8.1.0+256+ae790463
   provider: httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch
  dependency: httpd-tools = 2.4.37-16.module_el8.1.0+256+ae790463
   provider: httpd-tools-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
  dependency: libapr-1.so.0()(64bit)
   provider: apr-1.6.3-9.el8.x86_64
  dependency: libaprutil-1.so.0()(64bit)
   provider: apr-util-1.6.1-6.el8.x86_64
  dependency: libbrotlienc.so.1()(64bit)
   provider: brotli-1.0.6-1.el8.x86_64
  dependency: libc.so.6(GLIBC_2.14)(64bit)
   provider: glibc-2.28-72.el8.x86_64
  dependency: libcrypt.so.1()(64bit)
   provider: libxcrypt-4.1.1-4.el8.x86_64
  dependency: libcrypt.so.1(XCRYPT_2.0)(64bit)
   provider: libxcrypt-4.1.1-4.el8.x86_64
  dependency: libdl.so.2()(64bit)
   provider: glibc-2.28-72.el8.x86_64
  dependency: libexpat.so.1()(64bit)
   provider: expat-2.2.5-3.el8.x86_64
  dependency: liblua-5.3.so()(64bit)
   provider: lua-libs-5.3.4-11.el8.x86_64
  dependency: libm.so.6()(64bit)
   provider: glibc-2.28-72.el8.x86_64
  dependency: libpcre.so.1()(64bit)
   provider: pcre-8.42-4.el8.x86_64
  dependency: libpthread.so.0()(64bit)
   provider: glibc-2.28-72.el8.x86_64
  dependency: libpthread.so.0(GLIBC_2.2.5)(64bit)
   provider: glibc-2.28-72.el8.x86_64
  dependency: libselinux.so.1()(64bit)
   provider: libselinux-2.9-2.1.el8.x86_64
  dependency: libsystemd.so.0()(64bit)
   provider: systemd-libs-239-18.el8.x86_64
  dependency: libsystemd.so.0(LIBSYSTEMD_209)(64bit)
   provider: systemd-libs-239-18.el8.x86_64
  dependency: libz.so.1()(64bit)
   provider: zlib-1.2.11-10.el8.x86_64
  dependency: mod_http2
   provider: mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86_64
  dependency: rtld(GNU_HASH)
   provider: glibc-2.28-72.el8.i686
   provider: glibc-2.28-72.el8.x86_64
  dependency: system-logos-httpd
   provider: centos-logos-httpd-80.5-2.el8.noarch
  dependency: systemd-units
   provider: systemd-239-18.el8.i686
   provider: systemd-239-18.el8.x86_64

4.3.7 仓库缓存
清除目录/var/cache/yum/缓存

yum clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]

构建缓存:

yum makecache

范例:管理yum缓存

[root@centos7 ~]#du -sh /var/cache/yum
353M	/var/cache/yum
[root@centos7 ~]#ls /var/cache/yum/x86_64/7/
base                  C7.3.1611-centosplus  c7-media
base-debuginfo        C7.3.1611-extras      centosplus
base-source           C7.3.1611-fasttrack   centosplus-source
C7.0.1406-base        C7.3.1611-updates     cr
C7.0.1406-centosplus  C7.4.1708-base        elrepo
C7.0.1406-extras      C7.4.1708-centosplus  elrepo-kernel
C7.0.1406-fasttrack   C7.4.1708-extras      epel
C7.0.1406-updates     C7.4.1708-fasttrack   epel-debuginfo
C7.1.1503-base        C7.4.1708-updates     epel-source
C7.1.1503-centosplus  C7.5.1804-base        extras
C7.1.1503-extras      C7.5.1804-centosplus  extras-source
C7.1.1503-fasttrack   C7.5.1804-extras      fasttrack
C7.1.1503-updates     C7.5.1804-fasttrack   timedhosts
C7.2.1511-base        C7.5.1804-updates     timedhosts.txt
C7.2.1511-centosplus  C7.6.1810-base        update
C7.2.1511-extras      C7.6.1810-centosplus  updates
C7.2.1511-fasttrack   C7.6.1810-extras      updates-source
C7.2.1511-updates     C7.6.1810-fasttrack
C7.3.1611-base        C7.6.1810-updates
[root@centos7 ~]#yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base elrepo epel extras update
Cleaning up list of fastest mirrors
Other repos take up 103 M of disk space (use --verbose for details)
[root@centos7 ~]#du -sh /var/cache/yum
103M	/var/cache/yum
[root@centos7 ~]#yum makecache
。。。。。。
Metadata Cache Created
[root@centos7 ~]#du -sh /var/cache/yum
481M	/var/cache/yum

4.3.8 查看yum事务历史

yum 执行安装卸载命令会记录到相关日志中

日志 文件:

/var/log/yum.log
/var/log/dnf.log

日志命令

yum history [info|list|packages-list|packages-info|summary|addoninfo|
redo|undo|rollback|new|sync|stats]

范例;

[root@centos8 ~]#dnf history
Repository epel is listed more than once in the configuration
Modular dependency problems:

 Problem 1: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBD-MySQL:4.046:8010020191114030811:073fa5fe-0.x86_64
 Problem 2: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBD-SQLite:1.58:8010020191114033549:073fa5fe-0.x86_64
 Problem 3: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBI:1.641:8010020191113222731:16b3ab4d-0.x86_64
ID     | Command line             | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
    11 | install vsftpd           | 2020-04-09 21:58 | Install        |    1   
    10 | reinstall tree           | 2020-04-09 18:34 | R              |    2   
     9 | install epel-release.noa | 2020-04-09 18:31 | Install        |    1   
     8 | remove epel-release.noar | 2020-04-09 18:30 | Removed        |    1 EE
     7 | install yum-utils        | 2020-04-09 16:21 | Install        |    1   
     6 | -y install mariadb-serve | 2020-04-09 14:24 | Install        |   10   
     5 | install sl               | 2020-04-09 09:46 | Install        |    1   
     4 | install autofs           | 2020-04-08 23:09 | Install        |    1   
     3 | install -y tmux          | 2020-03-19 19:35 | Install        |    1   
     2 | install -y https://mirro | 2020-03-19 15:26 | Install        |    1   
     1 |                          | 2020-03-14 14:49 | Install        | 1348 EE
[root@centos8 ~]#dnf history info 5
Repository epel is listed more than once in the configuration
Modular dependency problems:

 Problem 1: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBD-MySQL:4.046:8010020191114030811:073fa5fe-0.x86_64
 Problem 2: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBD-SQLite:1.58:8010020191114033549:073fa5fe-0.x86_64
 Problem 3: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBI:1.641:8010020191113222731:16b3ab4d-0.x86_64
Transaction ID : 5
Begin time     : Thu 09 Apr 2020 09:46:47 AM CST
Begin rpmdb    : 1334:8bf8975897c44112519776e02960166efaba080d
End time       : Thu 09 Apr 2020 09:46:47 AM CST (0 seconds)
End rpmdb      : 1335:e939fc033be9e8a4af1e248f34ea2d6e8440cd72
User           : root <root>
Return-Code    : Success
Releasever     : 8
Command Line   : install sl
Packages Altered:
    Install sl-5.02-1.el8.x86_64 @epel
[root@centos8 ~]#dnf history undo 5 -y
Removed:
  sl-5.02-1.el8.x86_64                               
Complete!
[root@centos8 ~]#dnf history redo 5 -y

4.3.9 安装及升级本地程序包

yum localinstall|install rpmfile1 [rpmfile2] [...]
yum localupdate|update rpmfile1 [rpmfile2] [...]

4.3.10 包组管理的相关命令

yum grouplist [hidden] [groupwildcard] [...]
yum groupinstall group1 [group2] [...]
yum groupupdate group1 [group2] [...]
yum groupremove group1 [group2] [...]
yum groupinfo group1 [...]

范例:最小化安装的系统安装图形环境

[root@centos8 ~]#yum grouplist
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:17:46 ago on Thu 09 Apr 2020 09:58:13 PM CST.
Available Environment Groups:
   Server
   Minimal Install
   Workstation
   KDE Plasma Workspaces
   Custom Operating System
   Virtualization Host
Installed Environment Groups:
   Server with GUI
Installed Groups:
   Container Management
   Headless Management
Available Groups:
   Legacy UNIX Compatibility
   Development Tools
   .NET Core Development
   Graphical Administration Tools
   Network Servers
   RPM Development Tools
   Scientific Support
   Security Tools
   Smart Card Support
   System Tools
   Fedora Packager
   Xfce
[root@centos8 ~]#yum groupinfo "Server with GUI"
Repository epel is listed more than once in the configuration
Last metadata expiration check: 0:18:16 ago on Thu 09 Apr 2020 09:58:13 PM CST.
Environment Group: Server with GUI
 Description: An integrated, easy-to-manage server with a graphical interface.
no group 'dns-server' from environment 'graphical-server-environment'
 Mandatory Groups:
   Common NetworkManager submodules
   Container Management
   Core
   Fonts
   GNOME
   Guest Desktop Agents
   Hardware Monitoring Utilities
   Hardware Support
   Headless Management
   Internet Browser
   Multimedia
   Printing Client
   Server product core
   Standard
   base-x
 Optional Groups:
   Basic Web Server
   Debugging Tools
   FTP Server
   File and Storage Server
   Guest Agents
   Infiniband Support
   Mail Server
   Network File System Client
   Network Servers
   Performance Tools
   Remote Desktop Clients
   Remote Management for Linux
   Virtualization Client
   Virtualization Hypervisor
   Virtualization Tools
   Windows File Server
[root@centos8 ~]#dnf groupinstall GNOME -y
[root@centos8 ~]#init 5

4.3.11 实现私用 yum仓库

下载所有yum仓库的相关包和meta 数据

#CentOS 8 dnf 工具集成
dnf reposync --help #查看帮助

#默认只下载rpm包,不下载meta数据,需要指定--download-metadata 才能下载meta
dnf reposync --repoid=REPOID --download-metadata -p /path

#CentOS 7 以前版本,reposync工具来自于yum-utils包
reposync --repoid=REPOID --download-metadata -p /path

创建私有yum仓库:

createrepo [options] <directory>

范例:创建局域网的基于Base的私有yum源

#仓库服务器配置
[root@centos8 ~]#yum -y install httpd
[root@centos8 ~]#systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@centos8 ~]#mkdir /var/www/html/centos/8 -pv
mkdir: created directory '/var/www/html/centos'
mkdir: created directory '/var/www/html/centos/8'
[root@centos8 ~]#systemctl stop firewalld
[root@centos8 ~]#systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

Apr 08 22:46:03 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Apr 08 22:46:05 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Apr 09 22:51:25 centos8 systemd[1]: Stopping firewalld - dynamic firewall daemon...
Apr 09 22:51:26 centos8 systemd[1]: Stopped firewalld - dynamic firewall daemon.
[root@centos8 ~]#systemctl disable firewalld

#yum客户端配置
[root@centos8client ~]# cat /etc/yum.repos.d/test.repo 
[BaseOS]
name=BaseOS
baseurl=http://10.0.0.201/centos/8/BaseOS
gpgkey=file:////etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[AppStream]
name=Appstream
baseurl=http://10.0.0.201/centos/8/AppStream/
gpgkey=file:////etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

范例:下载阿里云的extras源,制作私有yum源

[root@centos8 ~]#yum repolist
Repository epel is listed more than once in the configuration
BaseOS                                                                                               3.8 MB/s | 3.9 kB     00:00    
AppStream                                                                                            4.2 MB/s | 4.3 kB     00:00    
EPEL                                                                                                  26 kB/s | 4.7 kB     00:00    
extras                                                                                               9.6 kB/s | 1.5 kB     00:00    
Extra Packages for Enterprise Linux Modular 8 - x86_64                                               5.2 kB/s | 9.4 kB     00:01    
MariaDB                                                                                              1.6 kB/s | 2.9 kB     00:01    
repo id                                   repo name                                                                            status
AppStream                                 AppStream                                                                            4,755
BaseOS                                    BaseOS                                                                               1,659
epel                                      EPEL                                                                                 5,219
epel-modular                              Extra Packages for Enterprise Linux Modular 8 - x86_64                                   0
extras                                    extras                                                                                  12
mariadb                                   MariaDB                                                                                 74

[root@centos8 ~]#dnf reposync --repoid=extras --download-metadata -p /var/www/html/centos
Repository epel is listed more than once in the configuration
extras                                     7.5 kB/s | 1.5 kB     00:00    
Modular dependency problems:

 Problem 1: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBD-MySQL:4.046:8010020191114030811:073fa5fe-0.x86_64
 Problem 2: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBD-SQLite:1.58:8010020191114033549:073fa5fe-0.x86_64
 Problem 3: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBI:1.641:8010020191113222731:16b3ab4d-0.x86_64
extras                                     6.5 kB/s | 7.7 kB     00:01    
(1/12): centos-release-gluster6-1.0-1.el8.  17 kB/s | 9.3 kB     00:00    
(2/12): centos-release-configmanagement-1-  13 kB/s | 8.7 kB     00:00    
(3/12): centos-release-ansible-29-1-1.el8.  11 kB/s | 8.3 kB     00:00    
(4/12): centos-release-nfs-ganesha30-1.0-2  43 kB/s | 8.6 kB     00:00    
(5/12): centos-release-gluster7-1.0-2.el8.  13 kB/s | 9.5 kB     00:00    
(6/12): centos-release-nfs-ganesha28-1.0-3  14 kB/s | 8.7 kB     00:00    
(7/12): centos-release-storage-common-2-2.  16 kB/s | 9.4 kB     00:00    
(8/12): centos-release-stream-8.1-1.1911.0  16 kB/s |  11 kB     00:00    
(9/12): centos-release-opstools-1-10.el8.n 7.6 kB/s | 9.9 kB     00:01    
(10/12): elrepo-release-8.0-2.el8.elrepo.n  21 kB/s |  13 kB     00:00    
(11/12): cpaste-1.0.0-3.el8.x86_64.rpm      41 kB/s |  42 kB     00:01    
(12/12): epel-release-8-8.el8.noarch.rpm    32 kB/s |  23 kB     00:00    
[23:40:21 root@centos8 ~]#ls /var/www/html/centos/
8  extras
[23:40:30 root@centos8 ~]#ls /var/www/html/centos/extras/
Packages  repodata

#yum客户端配置
[root@centos8client ~]# yum repolist
BaseOS                                     916 kB/s | 3.9 kB     00:00    
Appstream                                  2.0 MB/s | 4.3 kB     00:00    
extras                                     1.0 MB/s | 4.9 kB     00:00    
repo id                           repo name                          status
AppStream                         Appstream                          4,755
BaseOS                            BaseOS                             1,659
extras                            extras                                12
[root@centos8client ~]# cat /etc/yum.repos.d/test.repo
[BaseOS]
name=BaseOS
baseurl=http://10.0.0.201/centos/8/BaseOS
gpgkey=file:////etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[AppStream]
name=Appstream
baseurl=http://10.0.0.201/centos/8/AppStream/
gpgkey=file:////etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[extras]
name=extras
baseurl=http://10.0.0.201/centos/extras/
[root@centos8client ~]# yum --disablerepo=* --enablerepo=extras list available #只查看extras的包
Last metadata expiration check: 0:00:28 ago on Thu 09 Apr 2020 11:42:47 PM CST.
Available Packages
centos-release-ansible-29.noarch            1-1.el8                  extras
centos-release-configmanagement.noarch      1-1.el8                  extras
centos-release-gluster6.noarch              1.0-1.el8                extras
centos-release-gluster7.noarch              1.0-2.el8                extras
centos-release-nfs-ganesha28.noarch         1.0-3.el8                extras
centos-release-nfs-ganesha30.noarch         1.0-2.el8                extras
centos-release-opstools.noarch              1-10.el8                 extras
centos-release-storage-common.noarch        2-2.el8                  extras
centos-release-stream.x86_64                8.1-1.1911.0.7.el8       extras
cpaste.x86_64                               1.0.0-3.el8              extras
elrepo-release.noarch                       8.0-2.el8.elrepo         extras
epel-release.noarch                         8-8.el8                  extras
[root@centos8client ~]# yum -y install epel-release
Last metadata expiration check: 0:00:40 ago on Thu 09 Apr 2020 11:42:47 PM CST.
Dependencies resolved.
===========================================================================
 Package              Architecture   Version          Repository      Size
===========================================================================
Installing:
 epel-release         noarch         8-8.el8          extras          23 k

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

Total download size: 23 k
Installed size: 32 k
Downloading Packages:
epel-release-8-8.el8.noarch.rpm            5.4 MB/s |  23 kB     00:00    
---------------------------------------------------------------------------
Total                                      2.2 MB/s |  23 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                   1/1 
  Installing       : epel-release-8-8.el8.noarch                       1/1 
  Running scriptlet: epel-release-8-8.el8.noarch                       1/1 
  Verifying        : epel-release-8-8.el8.noarch                       1/1 

Installed:
  epel-release-8-8.el8.noarch                                              

Complete!

范例:下载阿里云的EPEL源,制作私有yum源

[root@centos8 ~]#cat /etc/yum.repos.d/base.repo
[epel]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
gpgcheck=0

[root@centos8 ~]#dnf repolist
Last metadata expiration check: 0:07:40 ago on Sun 22 Dec 2019 03:14:16 PM CST.
repo id repo name status
AppStream AppStream 4,681
BaseOS BaseOS 1,655
epel EPEL 3,707

#下载相关仓库包和元数据
[root@centos8 ~]#dnf reposync --repoid=epel --download-metadata -p
/var/www/html
#--download-metadata 加此选项可以下载元数据
#下载相关的key文件
[root@repo-server ~]#wget -P /var/www/html/epel/
https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8

#下面两个步骤只有没meta数据才需要执行
#[root@centos8 ~]#dnf -y install createrepo httpd
#[root@centos8 ~]#createrepo /var/www/html/epel/

[root@centos8 ~]#ls /var/www/html/epel/
Packages repodata
[root@centos8 ~]#systemctl start httpd
[root@repo-client ~]#cat /etc/yum.repos.d/test.repo
[BaseOS]
name=BaseOS
baseurl=http://10.0.0.8/centos/8/BaseOS
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[AppStream]
name=Appstream
baseurl=http://10.0.0.8/centos/8/AppStream/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[extras]
name=extras
baseurl=http://10.0.0.8/centos/extras/

[epel]
name=epel
baseurl=http://10.0.0.8/epel/
gpgkey=http://10.0.0.8/epel/RPM-GPG-KEY-EPEL-8

[root@repo-client ~]#yum repolist
extras 1.6 MB/s | 4.9 kB 00:00
epel 88 MB/s | 6.2 MB 00:00
repo id repo name status
AppStream Appstream 4,755
BaseOS BaseOS 1,659
epel epel 5,206
extras extras 12

[root@repo-client ~]#dnf install open

4.3.12 DNF 介绍

DNF,即DaNdiFied,是新一代的RPM软件包管理器。
DNF 发行日期是2015年5月11日,DNF 包管理器采用Python 编写,发行许可为GPL v2,首先出现在Fedora 18 发行版中。
在 RHEL 8.0 版本正式取代了YUM,DNF包管理器克服了YUM包管理器的一些瓶颈,提升了包括用户体验,内存占用,依赖分析,运行速度等

配置文件:

/etc/dnf/dnf.conf

仓库文件:

/etc/yum.repos.d/ *.repo

日志:

/var/log/dnf.rpm.log
/var/log/dnf.log

4.3.13 yum Troubleshooting

yum 和 dnf 失败最主要原因

  • yum的配置文件格式或路径错误
    解决方法:检查/etc/yum.repos.d/*.repo文件格式
  • yum cache
    解决方法:yum clean all
  • 网络不通:
    解决方法:网卡配置

5 程序包编译

5.1 源码编译介绍

程序包编译安装:

源代码–>预处理–>编译–>汇编–>链接–>执行
多文件:文件中的代码之间,很可能存在跨文件依赖关系

虽然有很多开源软件将软件打成包,供人们使用,但并不是所有源代码都打成包,如果想使用开源软件,可能需要自已下载源码,进行编译安装。另外即使提供了包,但是生产中需要用于软件的某些特性,仍然需要自行编译安装。但是利用源代码编译安装是比较繁琐的,庆幸的是有相关的项目管理工具可以大大减少编译过程的复杂度

5.2 开源程序源代码的获取

项目官方自建站点:

  • apache.org (ASF:Apache Software Foundation)
  • mariadb.org

代码托管:

  • Github.com
  • gitee.com
  • SourceForge.net
  • code.google.com

5.3 编译源码的项目工具

  • C、C++的源码编译:使用 make 项目管理器
    configure脚本 --> Makefile.in --> Makefile
    相关开发工具:
    autoconf: 生成configure脚本
    automake:生成Makefile.in
  • java的源码编译: 使用 maven

5.4 C语言源代码编译安装过程

利用编译工具,通常只需要三大步骤

  • ./configure
    (1) 通过选项传递参数,指定安装路径、启用特性等;执行时会参考用户的指定以及Makefile.in文
    件生成Makefile
    (2) 检查依赖到的外部环境,如依赖的软件包
  • make 根据Makefile文件,会检测依赖的环境,进行构建应用程序,
  • make install 复制文件到相应路径
    注意:安装前可以通过查看README,INSTALL获取帮助

5.4.1 编译安装准备

准备:安装相关的依赖包

  • 开发工具:make, gcc (c/c++编译器GNU C Complier)
  • 开发环境:开发库(glibc:标准库),头文件,可安装开发包组 Development Tools
  • 软件相关依赖包

生产实践:基于最小化安装的系统建议安装下面相关包

yum install -y gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages

5.4.2 编译安装

第一步:运行 configure 脚本,生成Makefile 文件
其选项主要功能:

  • 可以指定安装位置
  • 指定启用的特性

获取其支持使用的选项

./configure --help

选项分类:

  • 安装路径设定:
    –prefix=/PATH:指定默认安装位置,默认为/usr/local/
    –sysconfdir=/PATH:配置文件安装位置
    System types:支持交叉编译
  • 软件特性和相关指定:
    Optional Features: 可选特性
    –disable-FEATURE
    –enable-FEATURE[=ARG]
    Optional Packages: 可选包
    –with-PACKAGE[=ARG] 依赖包
    –without-PACKAGE 禁用依赖关系

注意:通常被编译操作依赖的程序包,需要安装此程序包的“开发”组件,其包名一般类似于namedevel-
VERSION

第二步:make
第三步:make install

5.4.3 安装后的配置

1.二进制程序目录导入至PATH环境变量中
编辑文件/etc/profile.d/NAME.sh

export PATH=/PATH/TO/BIN:$PATH

2.相关用户及文件
有些开源软件编译完成后,还需要创建相关的用户及文件

3.导入帮助手册
编辑/etc/man.config|man_db.conf文件,添加一个MANPATH

范例:CentOS 7 编译安装 tree

#1 安装相关的依赖包
[root@centos7 ~]#yum install gcc make

#2 下载源码并解压
[root@centos7 ~]#tar xvf tree-1.8.0.tgz -C /usr/local/src

#3 进入解压缩的目录,README和INSTALL
[root@centos7 ~]#cd /usr/local/src/tree-1.8.0/
[root@centos7 tree-1.8.0]#cat README
[root@centos7 tree-1.8.0]#cat INSTALL

#4 修改源码的版本号
[root@centos7 tree-1.8.0]#sed -i 's#v1\.8\.0#v.8.8.8#' tree.c

#5 编译准备
[root@centos7 tree-1.8.0]#vim Makefile
prefix = /apps/tree

#6 编译
[root@centos7 tree-1.8.0]#make

#7 安装
[root@centos7 tree-1.8.0]#make install

#8 修改PATH变量
#默认无法直接运行tree
[root@centos7 ~]#tree
-bash: tree: command not found

[root@centos7 ~]#echo 'PATH=/apps/tree/bin:$PATH' > /etc/profile.d/tree.sh
. /etc/profile.d/tree.sh
#或者利用软链接实现
[root@centos7 ~]#ln -s /apps/tree/bin/tree /usr/local/bin

#9 验证结果
[root@centos7 ~]#tree --version
tree v8.8.8 (c) 1996 - 2018 by Steve Baker, Thomas Moore, Francesc Rocher,
Florian Sesser, Kyosuke Tokoro

#10 添加man帮助
#默认无法查看man
[root@centos7 ~]#man tree
No manual entry for tree

[root@centos7 ~]#vim /etc/man_db.conf
MANDATORY_MANPATH /apps/tree/man

[root@centos7 ~]#man tree

#11 运行tree查看生成的文件列表
[root@centos7 ~]#tree /apps/tree
/apps/tree
├── bin
│ └── tree
└── man
└── man1
└── tree.1
3 directories, 2 files

范例:CentOS 8 编译安装 cmatrix

#1 安装相关包
[root@centos8 ~]#dnf install gcc make autoconf ncurses-devel

#2 下载并解压缩包
[root@centos8 ~]#cd /usr/local/src
[root@centos8 src]#wget https://github.com/abishekvashok/cmatrix/releases/download/v2.0/cmatrix-v2.0-Butterscotch.tar
[root@centos8 src]#tar vxf cmatrix-v2.0-Butterscotch.tar

#3配置
[root@centos8 src]#ls
cmatrix  cmatrix-v2.0-Butterscotch.tar
[root@centos8 src]#cd cmatrix/
[root@centos8 cmatrix]#./configure --prefix=/apps/cmatrix

#4编译并安装
[root@centos8 cmatrix]#make -j 2 && make install

#5配置环境
[root@centos8 ~]#echo 'PATH=/apps/cmatrix/bin:$PATH' > /etc/profile.d/cmatrix.sh
[root@centos8 ~]#. /etc/profile.d/cmatrix.sh

#或者用软连接
[root@centos8 ~]#ln -sv /apps/cmatrix/bin/cmatrix /usr/local/bin/
[root@centos8 ~]#ll /usr/local/bin/
total 0
lrwxrwxrwx. 1 root root 25 Apr 11 15:28 cmatrix -> /apps/cmatrix/bin/cmatrix
[root@centos8 ~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

#6运行
[root@centos8 ~]#cmatrix -b -C yellow

#7帮助
[root@centos8 ~]#vim /etc/man_db.conf
MANDATORY_MANPATH /apps/cmatrix/share/man
[root@centos8 ~]#man cmatrix
[root@centos8 cmatrix]#tree /apps/cmatrix
/apps/cmatrix
├── bin
│   └── cmatrix
└── share
    └── man
        └── man1
            └── cmatrix.1

4 directories, 2 files

范例:centos8 编译安装 httpd-2.4.43

#安装前准备:关闭防火墙和SELinux
#1 安装包
[root@centos8 ~]#dnf install gcc make apr-devel apr-util-devel pcre-devel
openssl-devel redhat-rpm-config

#2 下载并解压缩包
[root@centos8 ~]#tar xvf httpd-2.4.43.tar.bz2 -C /usr/local/src

#3 配置
[root@centos8 ~]#cd /usr/local/src/httpd-2.4.43/
[root@centos8 httpd-2.4.43]#./configure --prefix=/apps/httpd24 --
sysconfdir=/etc/httpd24 --enable-ssl

#4 编译并安装
[root@centos8 httpd-2.4.43]#make -j 4 && make install

#5 配置环境
[root@centos8 ~]#echo 'PATH=/apps/httpd24/bin:$PATH' > /etc/profile.d/httpd24.sh
[root@centos8 ~]#. /etc/profile.d/httpd24.sh

#6运行
[root@centos8 ~]#apachectl

#7 指定用apache用户运行
[root@centos8 ~]#useradd -r -s /sbin/nologin -d /var/www -c Apache -u 48 apache
[root@centos8 ~]#vim /etc/httpd24/httpd.conf
user apache
group apache

#8生效和验证
[root@centos8 ~]#apachectl restart

#查看
[root@centos8 ~]#ps aux

6 Ubuntu 软件管理

Debian软件包通常为预编译的二进制格式的扩展名“.deb”,类似rpm文件,因此安装快速,无需编译软
件。包文件包括特定功能或软件所必需的文件、元数据和指令

  • dpkg:package manager for Debian,类似于rpm, dpkg是基于Debian的系统的包管理器。可
    以安装,删除和构建软件包,但无法自动下载和安装软件包或其依赖项
  • apt:Advanced Packaging Tool,功能强大的软件管理工具,甚至可升级整个Ubuntu的系统,基
    于客户/服务器架构,类似于yum

6.1 APT工作原理

在服务器上先复制所有DEB包,然后用APT的分析工具genbasedir根据每个DEB 包的包头(Header)
信息对所有的DEB包进行分析,并将该分析结果记录在文件夹base内的一个DEB 索引清单文件中,一
旦APT 服务器内的DEB有所变动,要使用genbasedir产生新的DEB索引清单。客户端在进行安装或升级
时先要查询DEB索引清单,从而获知所有具有依赖关系的软件包,并一同下载到客户端以便安装。当客
户端需要安装、升级或删除某个软件包时,客户端计算机取得DEB索引清单压缩文件后,会将其解压置
放于 /var/cache/apt/,而客户端使用apt-get install或apt-get upgrade命令的时候,就会将这个文件夹
内的数据和客户端计算机内的DEB数据库比对,知道哪些DEB已安装、未安装或是可以升级的

6.2 dpkg 包管理器

帮助参看:man dpkg

dpkg 常见用法

#安装包
dpkg -i package.deb

#删除包,不建议,不自动卸载依赖于它的包
dpkg -r package

#删除包(包括配置文件)
dpkg -P package

#列出当前已安装的包,类似rpm -qa
dpkg -l

#显示该包的简要说明,类似rpm –qi
dpkg -l package

#列出该包的状态,包括详细信息,类似rpm –qi
dpkg -s package

#列出该包中所包含的文件,类似rpm –ql
dpkg -L package

#搜索包含pattern的包,类似rpm –qf
dpkg -S <pattern>

#配置包,-a 使用,配置所有没有配置的软件包
dpkg --configure package

#列出 deb 包的内容,类似rpm –qpl
dpkg -c package.deb

#解开 deb 包的内容
dpkg --unpack package.deb

范例:

#列出系统上安装的所有软件包
dpkg -l

#列出软件包安装的文件
dpkg -L bash

#查看/bin/bash来自于哪个软件包
dpkg -S /bin/bash

#安装本地的 .deb 文件
dpkg -i /mnt/cdrom/pool/main/z/zip/zip_3.0-11build1_amd64.deb

#卸载软件包
dpkg -r zip

注意:一般建议不要使用dpkg卸载软件包。因为删除包时,其它依赖它的包不会卸载,并且可能无法再正常运行

6.3 apt

Debian 使用apt 工具集来管理包系统,apt-get 是其中一个常用的命令行工具,另外一款较为流行的命令行与 GUI 兼顾的工具是 aptitude ,之前最常用的 Linux 包管理命令都被分散在了 apt-get、aptcache和 apt-config 这三条命令中

在 2014 年apt 命令发布第一个稳定版,Ubuntu 16.04 引入新特性之一便是 apt 命令,apt 命令解决了命令过于分散的问题,它包括 apt-get 命令出现以来使用最广泛的功能选项,以及 apt-cache 和 aptconfig命令中很少用到的功能。在使用 apt 命令时,用户不必再由 apt-get 转到 apt-cache 或 aptconfig,提供管理软件包所需的必要选项

apt 相当于 apt-get、apt-cache 和 apt-config 中最常用命令选项的集合

apt 具有更精减但足够的命令选项,而且参数选项的组织方式更为有效。此外,启用的几个特性也非常有帮助。例如:可以在使用 apt 命令安装或删除程序时看到进度条,apt 还会在更新存储库数据库时提示用户可升级的软件包个数

apt 与 apt-get 有一些类似的命令选项,但它并不能完全向下兼容 apt-get 命令,也即可用 apt 替换部分
apt-get 系列命令,但不是全部

apt 命令用法

查看帮助:apt help n

apt 命令 命令的功能
apt install 安装软件包
apt remove 移除软件包
apt purge 移除软件包及配置文件
apt update 刷新存储库索引
apt upgrade 升级所有可升级的软件包
apt autoremove 自动删除不需要的包
apt full-upgrade 在升级软件包时自动处理依赖关系
apt search 搜索应用程序
apt show 显示安装细节

apt 特有的命令

apt list 列出包含条件的包(已安装,可升级等)
apt edit-sources 编辑源列表

APT包索引配置文件

/etc/apt/sources.list
/etc/apt/sources.list.d

可以修改上面文件为国内的安装源,提高速度
参考链接:https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.53322f70fghx56
apt命令操作(如安装和删除软件包)日志文件

/var/log/dpkg.log

范例:

#安装包:
apt install tree zip

#安装图形桌面
apt install ubuntu-desktop

#删除包:
apt remove tree zip
#说明:apt remove中添加--purge选项会删除包配置文件,谨慎使用
#更新包索引,相当于yum clean all;yum makecache
apt update
#升级包:要升级系统,请首先更新软件包索引,再升级
apt upgrade

#apt列出仓库软件包,等于yum list
apt list

#搜索安装包
apt search nginx

#查看某个安装包的详细信息
apt show apache2

#在线安装软件包
apt install apache2

#卸载单个软件包但是保留配置⽂件
apt remove apache2

#删除安装包并解决依赖关系
apt autoremove apache2

#更新本地软件包列表索引,修改了apt仓库后必须执⾏
apt update

#卸载单个软件包删除配置⽂件
apt purge apache2

#升级所有已安装且可升级到新版本的软件包
apt upgrade

#升级整个系统,必要时可以移除旧软件包。
apt full-upgrade

#编辑source源⽂件
apt edit-sources

#查看仓库中软件包有哪些版本可以安装
apt-cache madison nginx

#安装软件包的时候指定安装具体的版本
apt install nginx=1.14.0-0ubuntu1.6

6.4 ubuntu建议安装的常用包

apt install -y iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfscommon lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc openssh-server iotop unzip zip purge ufw lxd lxd-client lxcfs liblxc-common

你可能感兴趣的:(Linux基础)