软件包管理-yum私有仓库
阅读原文时间:2023年07月11日阅读:1

Linux软件安装

  包的依赖关系:

    软件包之间可能存在依赖关系,甚至循环依赖,即:A包依赖B包,B包依赖C包,C包依赖A包 安装软件包时,会因为缺少依赖的包,而导致安装包失败。 解决依赖包管理工具: yum:rpm包管理器的前端工具 dnf:Fedora 18+ rpm包管理器前端管理工具,CentOS 8 版代替 yum apt:deb包管理器前端工具 zypper:suse上的rpm前端管理工具

  获取程序包的路径

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

     Centos镜像

            Centosg官方网站

     Ubuntu镜像

            Ubuntu官方网站

     第三方组织提供

            阿里巴巴开源镜像

            centos | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

     软件项目官方站点

            Download - The Apache HTTP Server Project

            Download MariaDB Server - MariaDB.org

     自己制作

  1.1 rpm安装

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

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

    安装

格式:

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

    卸载

格式:
rpm {-e|--erase} [--allmatches] [--nodeps] [--noscripts] [--notriggers] [--test]PACKAGE_NAME …

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

    包查询

rpm {-q|--query} [select-options] [query-options]

[select-options]

-a:所有包
-f:查看指定的文件由哪个程序包安装生成
-p rpmfile:针对尚未安装的程序包文件做查询操作

[query-options]

--changelog:查询rpm包的changelog
-c:查询程序的配置文件
-d:查询程序的文档
-i:information
-l:查看指定的程序包安装后生成的所有文件
--scripts:程序包自带的脚本

#和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
-qpi PACKAGE_FILE
-qpl PACKAGE_FILE, …

    rpm包安装不能一次性解决依赖性关系,不适用于安装或卸载,依赖关系问题造成安装过程繁琐,用的较少,一般用于查询使用

  1.2yum和dnf安装

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

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

    1.2.1yum和dnf工作原理

    yum/dnf 是基于C/S 模式

      yum 服务器存放rpm包和相关包的元数据库

      yum 客户端访问yum服务器进行安装或查询等

    yum 实现过程

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

   1.2.2yum客户端配置

yum客户端配置文件

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

帮助参考: man 5 yum.conf

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

[repositoryID]                #仓库名称
name=Some name for this repository   #命令
baseurl=url://path/to/repository/     #安装包路径
enabled={1|0}                 #是否启用此repo仓库 1开启,0关闭
gpgcheck={1|0}                #安装包安全检查校验
gpgkey=URL                   #检查校验密钥路径

    yum服务器的baseurl形式

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

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

    相关变量

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: 自定义变量

范例:*.repo文件中

baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/

    网络源配置

    网络源配置完成后,下载更新软件依赖外网,没有外网无法工作

centos7网络yum源 配置文件 ,第三方yum源,阿里云

# CentOS-Base.repo

The mirror system uses the connecting IP address of the client and the

update status of each mirror to pick mirrors that are updated to and

geographically close to the client. You should use this for CentOS updates

unless you are manually picking other mirrors.

If the mirrorlist= does not work for you, as a fall back you can try the

remarked out baseurl= line instead.

#

[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

centos8.3.2011版本 yum源配置,此版本用阿里源。

# CentOS-Base.repo

The mirror system uses the connecting IP address of the client and the

update status of each mirror to pick mirrors that are updated to and

geographically close to the client. You should use this for CentOS updates

unless you are manually picking other mirrors.

If the mirrorlist= does not work for you, as a fall back you can try the

remarked out baseurl= line instead.

#

[base]
name=CentOS-8.5.2111 - Base - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/BaseOS/$basearch/os/
http://mirrors.aliyuncs.com/centos-vault/8.5.2111/BaseOS/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/BaseOS/$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that may be useful
[extras]
name=CentOS-8.5.2111 - Extras - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/extras/$basearch/os/
http://mirrors.aliyuncs.com/centos-vault/8.5.2111/extras/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/extras/$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-8.5.2111 - Plus - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/centosplus/$basearch/os/
http://mirrors.aliyuncs.com/centos-vault/8.5.2111/centosplus/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/centosplus/$basearch/os/
gpgcheck=0
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

[PowerTools]
name=CentOS-8.5.2111 - PowerTools - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/PowerTools/$basearch/os/
http://mirrors.aliyuncs.com/centos-vault/8.5.2111/PowerTools/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/PowerTools/$basearch/os/
gpgcheck=0
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

[AppStream]
name=CentOS-8.5.2111 - AppStream - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos-vault/8.5.2111/AppStream/$basearch/os/
http://mirrors.aliyuncs.com/centos-vault/8.5.2111/AppStream/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos-vault/8.5.2111/AppStream/$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official    

      baseurl指向的路径

      阿里云,清华云,腾讯云等提供了各种版本Linux换源的步骤

      注意:修改yum源前,原文件做好备份

      光盘本地yum源配置

      光盘本地yum源配置适用于本机,脱离外网,一样可用。

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源仓库

#挂载光盘至某目录,如/mnt/cdrom
[root@centos8~]$mount /dev/sr0 /mnt/cdrom/

[root@centos8 ~]#vim /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

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

配置好yum源后操作

yum clean all     清楚所有缓存
yum makecache        生成新的缓存
yum -y install epel-release   安装epel源包
yum update -y      更新数据包 \\可以省略不执行

    1.2.3yum命令

yum命令用法:

yum [options] [command] [package …]

yum的命令行选项:

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

    1.2.4 显示仓库列表

yum repolist [all|enabled|disabled]

    1.2.5显示程序包

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

    1.2.6安装程序包

yum install package1 [package2] […]
yum reinstall package1 [package2] […] #重新安装
--downloadonly #只下载相关包默认至/var/cache/yum/x86_64/7/目录下,而不执行
install/upgrade/erase
--downloaddir=, --destdir= #--downloaddir选项来指定下载的目录,如果不存在
自动创建

    1.2.7卸载程序包

yum remove | erase package1 [package2] […]

    1.2.8查询

yum info […]

    1.2.9包组管理的相关命令

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

    2.1dpkg包管理器

      dpkg作用同centos的rpm

    dpkg常见用法

#安装包
dpkg -i package.deb

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

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

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

#显示该包的简要说明
dpkg -l package

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

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

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

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

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

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

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

    2.2 apt

      apt作用同centos的yum/dnf

    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

_软件管理常用命令

#dpkg -S filename :在当前安装的包里查找文件

#显示系统安装包的统计信息,可以统计已经安装包的数量,大小,占用空间等
#apt-cache stats

#显示系统安装包的统计信息,可以统计已经安装包的数量,大小,占用空间等
#apt-cache stats

#查询软件xxx依赖哪些包
#apt depends xxx
#apt-cache depends xxx_

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]

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

#仓库服务器配置
[root@centos8 ~]#yum -y install httpd
[root@centos8 ~]#systemctl enable --now httpd
[root@centos8 ~]#mkdir /var/www/html/centos/8 -pv
[root@centos8 ~]#mount /dev/sr0 /mnt/
[root@centos8 ~]#cp -a /mnt/* /var/www/html/centos/8
#yum客户端配置
[root@centos8 ~]#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