> 文章列表 > Linux 包管理器

Linux 包管理器

Linux 包管理器

Linux 安装工具

  • Debian
    • 1. apt 和 apt-get
      • 1.1 概要
      • 1.2 常用命令
      • 1.3 apt 国内源(阿里云)
      • 1.4 关于ubuntu版本号
      • 1.5 Ubuntu 主要版本
    • 2. dpkg
  • Redhat
    • 3. yum
      • 3.1 概念
      • 3.2 语法
      • 3.3 yum 配置文件
      • 3.4 配置本地yum源
      • 4. 关于repo 文件的格式
      • 3.5 配置国内yum源
    • 4. RPM
      • 4.1 概念
      • 4.2 命名规格
      • 4.3 语法

Debian

1. apt 和 apt-get

apt-get 命令适用于 deb 包管理式的 Linux 操作系统(Debian、Ubuntu等),主要用于自动从互联网软件仓库中搜索、下载、安装、升级、卸载软件或操作系统。

Ubuntu 软件包查找: https://packages.ubuntu.com/

1.1 概要

  1. apt-get是老版的命令,apt是新版的命令。
  2. apt包含了apt-get、apt-cache等等,用起来更方便。
  3. apt刚刚出来,所以允许有apt-get和apt共存,向后兼容,以后apt-get就要淘汰了。

使用 apt 可以获得几乎所有的功能,它的主要目的就是让用户使用最简单、高效的方式使用包管理工具。

apt 默认启用一些对用户实际使用有益的操作,比如:可以在 apt 安装或删除操作过程中显示进度条。在更新软件包列表的时候还可以提示你可以升级的包的数量。虽然使用 apt-get 也可以实现这些功能,但是 apt 默认开启这些特性。

虽然 apt 有些命令和 apt-get 类似,但是 apt 并没有向后兼容 apt-get。这意味着不可能使用 apt 完全替代 apt-get 命令。下面列出了哪些 apt 命令替换了 apt-get 和 apt-cache 命令。

1.2 常用命令

apt命令 apt-get命令 说明
apt install apt-get install 安装新包
apt remove apt-get remove 卸载已安装的包(保留配置文件)
apt purge apt-get purge 卸载已安装的包(删除配置文件)
apt update apt-get update 更新软件包列表
apt upgrade apt-get upgrade 更新所有已安装的包
apt autoremove apt-get autoremove 卸载已不需要的包依赖
apt full-upgrade apt-get dist-upgrade 自动处理依赖包升级
apt search apt-cache search 查找软件包
apt show apt-cache show 显示指定软件包的详情
apt list 列出包含条件的包(已安装,可升级等)
apt edit-sources 编辑源列表

1.3 apt 国内源(阿里云)

配置文件:/etc/apt/sources.list /etc/apt/souces.list.d
日志文件:/var/log/dpkg.log

先备份/etc/apt/sources.list,然后再进行修改

  • ubuntu 16.04
#阿里云源
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
  • ubuntu 18.04
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
  • ubuntu 20.04
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiversedeb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

在终端执行

apt-get update				#更新软件列表,换源完成
apt-get upgrade				#upgrade 是升级已安装的所有软件包,升级之后的版本就是本地索引里的,#因此,在执行 upgrade 之前一定要执行 update, 这样才能是最新的。

1.4 关于ubuntu版本号

Ubuntu中,每个版本都有一个更为特色的名字(codename),这个名字由一个形容词和一个动物名称组成,并且,形容词和名词的首字母都是一致的。

Ubuntu版本的命名规则是根据正式版发布的年月命名,Ubuntu 16.04 也就意味着 2016年4月发布的Ubuntu,研发人员与用户可从版本号码就知道正式发布的时间。

codename的用途:如果你需要自己编辑更新列表文件 /etc/apt/sources.list ,你就会发现在 url 的后面紧跟着一个 codename 。

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

当前版本codename

root@ubuntu:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04 LTS
Release:        16.04
Codename:       xenial
版本 别名(codename) 发布日期
4.10 Warty Warthog(长疣的疣猪) 2004年10月20日
5.04 Hoary Hedgehog(灰白的刺猬) 2005年4月8日
5.10 Breezy Badger(活泼的獾) 2005年10月13日
6.06 Dapper Drake(整洁的公鸭) 2006年6月1日(LTS)
6.10 Edgy Eft(急躁的水蜥) 2006年10月6日
7.04 Feisty Fawn(坏脾气的小鹿) 2007年4月19日
7.10 Gutsy Gibbon(勇敢的长臂猿) 2007年10月18日
8.04 Hardy Heron(耐寒的苍鹭) 2008年4月24日(LTS)
8.10 Intrepid Ibex (勇敢的野山羊) 2008年10月30日
9.04 Jaunty Jackalope(得意洋洋的怀俄明野兔) 2009年4月23日
9.10 Karmic Koala(幸运的考拉) 2009年10月29日
10.04 Lucid Lynx(清醒的猞猁) 2010年4月29日
11.10 Oneiric Ocelot(梦幻的豹猫) 2010年10月13日
11.04 Natty Narwhal(敏捷的独角鲸) 2011年4月28日
12.04 Precise Pangolin(精准的穿山甲) 2012年的4月26日(LTS)
12.10 Quantal Quetzal(量子的绿咬鹃) 2012年的10月20日
13.04 Raring Ringtail(铆足了劲的猫熊) 2013年4月25日
13.10 Saucy Salamander(活泼的蝾螈) 2013年10月17日
14.04 Trusty Tahr (可靠的塔尔羊) 2014年4月18日 (LTS)
14.10 Utopic Unicorn(乌托邦独角兽) 2014年10月23日
15.04 Vivid Vervet (活泼的小猴) 2015年4月
15.10 Wily Werewolf (狡猾的狼人) 2015年10月
16.04 Xenial Xerus (好客的非洲地松鼠) 2016年4月 (LTS)
16.10 Yakkety Yak(牦牛) 2016年10月
17.04 Zesty Zapus(开心的跳鼠) 2017年4月
17.10 Artful Aardvark(机灵的土豚) 2017年10月
18.04 Bionic Beaver(仿生海狸) 2018年4月(LTS)
20.04 Focal Fossa(重要的马岛长尾狸猫)
22.04 Jammy Jellyfish(幸运水母)

1.5 Ubuntu 主要版本

最新发行版主要提供 desktop 和 server 两种版本。

  1. Desktop版本:desktop 是 live cd 的名字,什么是 live cd? live cd 是一个刻录在光盘上的操作系统,我们并不需要硬盘就可以直接在光盘上运行这个操作系统。里面也已经集成了很多软件,我们一般可以把这个系统安装到硬盘上,类似于ghost系统,安装速度会比较快。这个版本提供了向导的图形界面的方式,让你一步步来轻松地安装 ubuntu 。

  2. Alternate版本:是一个最标准的安装版本,采用文本安装界面,里面有很多高级选项,可以安装 desktop ,也可以安装 server 版,适合比较高要求的人来安装,安装速度比 desktop 版慢,但是运行时的效率比 desktop 版本快。

  3. Netbook版本:专门为上网本定制和优化,最近的版本采用了 Unity,为小屏幕提供了一种美观的用户界面,采用了大图标设计,让用户更容易寻找各种应用程序。

  4. DVD版本:里面包括 Desktop 和 Alternate 两种安裝模式,同时集成更多的软件和完整的语言包,如果你没有联网可以选择下这个版本。

  5. Minimal版本:安装 Ubuntu 的最小镜像版本。

2. dpkg

dpkg 即 package manager for Debian ,是 Debian 和基于 Debian 的系统中一个主要的包管理工具,可以用来安装、构建、卸载、管理 deb 格式的软件包。

和 apt(Advanced Package Tool)有所不同:

  • apt 命令并不直接操作 deb 安装包文件,而是从 /etc/apt/sources.list 配置文件中定义的软件镜像源里下载软件包并安装,使用时也只需指定软件的名称(或者也可以附加上版本号)。

  • dpkg 主要是用来安装已经下载到本地的 deb 软件包,或者对已经安装好的软件进行管理。而 apt 可以直接从远程的软件仓库里下载安装软件。

命令 描述
dpkg -i[–install] package_name.deb 安装软件包
dpkg -r[–remove] package_name.deb 卸载软件包,保留配置文件
dpkg -P[–purge] package_name.deb 卸载软件包的同时删除其配置文件
dpkg -L package_name.deb 显示与软件包关联的文件,查看软件安装到什么地方
dpkg -l package_name 显示已安装软件包列表
dpkg --unpack package_name.deb 解开软件包
dpkg -c package_name.deb 显示软件包内文件列表
dpkg --configure package_name.deb 配置软件包
dpkg -B[–auto-deconfigure] package_name.deb 卸载包时,自动把依赖于被卸载的包的所有包也卸载掉。

资源:
https://pkgs.org/
Launchpad
Getdeb
RPM Seek
Open Suse Software

Redhat

3. yum

3.1 概念

yum(全称为 Yellowdog Updater Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软体包,无须繁琐地一次次下载、安装。yum提供了查找、安装、删除某一个、一组甚至全部软件包的命令,而且命令简洁而又好记。

yum 的理念是使用一个中心仓库(repository)管理一部分甚至一个distribution 的应用程序相互关系,根据计算出来的软件依赖关系进行相关的升级、安装、删除等等操作,减少了Linux 用户一直头痛的dependencies 的问题。这一点上,yum 和apt 相同。apt 原为debian 的deb 类型软件管理所使用,但是现在也能用到RedHat 门下的rpm 了。

yum 主要功能是更方便的添加/删除/更新RPM 包,自动解决包的依赖性问题,便于管理大量系统的更新问题。

yum 可以同时配置多个资源库(Repository),简洁的配置文件(/etc/yum.conf),自动解决增加或删除rpm 包时遇到的依赖性问题,保持与RPM 数据库的一致性。

yum 的基础安装包包括:

    yum  //RPM installer/updateryum-fastestmirror  //Yum plugin which chooses fastest repository from a mirrorlistyum-metadata-parser  //A fast metadata parser for yum

3.2 语法

NAMEyum - Yellowdog Updater ModifiedSYNOPSISyum [options] command [package ...]

命令 command

命令 说明
check-update 检查可提供的包升级
clean 删除缓存数据
deplist 列出包依赖相关
erase 移除一个或多个包从你的系统
groupinfo 显示关于包组的细节
groupinstall 从组里安装软件包到你的系统
grouplist 列出当前的组
groupremove 在一个组中移除包从您的系统
help 显示帮助用法信息
info 显示关于包或一组包的细节信息
install 安装一个或多个软件包在你的系统
list 列出一个包或某组的包
localinstall 安装本地 RPM
makecache 生成原数据缓存
provides Find what package provides the given value
reinstall 重新安装一个包
repolist 显示软件仓库的配置
resolvedep 决定哪个包提供了给定的依赖
search 查找包细节居给定的字符串
shell 运行交互式的yum shell
update 更新一个或多个包在你的系统
upgrade Update packages taking obsoletes into account

选项 options

选项 说明
-h, --help show this help message and exit
-t, --tolerant be tolerant of errors
-C 从缓冲中运行,而不是升级缓存
-c [config file] config file location
-R [minutes] 最大化的命令等待时间
-d [debug level] 调试输出级别
–showduplicates 显示复制, 在repo,list/search命令下
-e [error level] 错误输出级别
-q, --quiet 安静的操作
-v, --verbose verbose operation
-y 回答所有的问题为是
–version 显示 YUM版本信息并退出
–installroot=[path] 设置为root安装
–enablerepo=[repo] 激活一个或多个仓库(支持通配符)
–disablerepo=[repo] 禁掉一个或多个仓库(支持通配符)
-x [package], --exclude=[package] 在名称或glob不包含包。
–disableexcludes=[repo] disable exclude from main, for a repo or for everything
–obsoletes enable obsoletes processing during updates
–noplugins 禁掉yum插件
–nogpgcheck 禁掉GPG验证检查
–disableplugin=[plugin] 用名称禁掉插件
–enableplugin=[plugin] enable plugins by name
–skip-broken 忽略包的依赖问题
–color=COLOR control whether color is used

① 升级所有包,改变软件设置和系统设置,系统版本内核都升级

yum -y update

② 升级所有包,不改变软件设置和系统设置,系统版本升级,内核不改变

yum -y upgrade

3.3 yum 配置文件

yum 的配置文件分为两部分:main 和repository

  • main 部分定义了全局配置选项,整个yum 配置文件应该只有一个main。常位于/etc/yum.conf 中。
  • repository 部分定义了每个源/服务器的具体配置,可以有一到多个。常位于/etc/yum.repo.d 目录下的各文件中。

yum.conf 文件一般位于/etc目录下,一般其中只包含main部分的配置选项。

[root@node1 ~]# cat /etc/yum.conf
[main]
cachedir=/var/cache/yum//yum 缓存的目录,yum 在此存储下载的rpm 包和数据库,默认设置为/var/cache/yum
keepcache=0//安装完成后是否保留软件包,0为不保留(默认为0),1为保留
debuglevel=2//Debug 信息输出等级,范围为0-10,缺省为2
logfile=/var/log/yum.log//yum 日志文件位置。用户可以到/var/log/yum.log 文件去查询过去所做的更新。
pkgpolicy=newest//包的策略。一共有两个选项,newest 和last,这个作用是如果你设置了多个repository,而同一软件在不同的repository 中同时存在,yum 应该安装哪一个,如果是newest,则yum 会安装最新的那个版本。如果是last,则yum 会将服务器id 以字母表排序,并选择最后的那个服务器上的软件安装。一般都是选newest。
distroverpkg=redhat-release//指定一个软件包,yum 会根据这个包判断你的发行版本,默认是redhat-release,也可以是安装的任何针对自己发行版的rpm 包。
tolerant=1//有1和0两个选项,表示yum 是否容忍命令行发生与软件包有关的错误,比如你要安装1,2,3三个包,而其中3此前已经安装了,如果你设为1,则yum 不会出现错误信息。默认是0。
exactarch=1//有1和0两个选项,设置为1,则yum 只会安装和系统架构匹配的软件包,例如,yum 不会将i686的软件包安装在适合i386的系统中。默认为1。
retries=6//网络连接发生错误后的重试次数,如果设为0,则会无限重试。默认值为6.
obsoletes=1//这是一个update 的参数,具体请参阅yum(8),简单的说就是相当于upgrade,允许更新陈旧的RPM包。
plugins=1//是否启用插件,默认1为允许,0表示不允许。我们一般会用yum-fastestmirror这个插件。
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum# Note: yum-RHN-plugin doesn't honor this.
metadata_expire=1hinstallonly_limit = 5# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

除了上述之外,还有一些可以添加的选项,如:

  exclude=selinux*  // 排除某些软件在升级名单之外,可以用通配符,列表中各个项目要用空格隔开,这个对于安装了诸如美化包,中文补丁的朋友特别有用。gpgcheck=1  // 有1和0两个选择,分别代表是否是否进行gpg(GNU Private Guard) 校验,以确定rpm 包的来源是有效和安全的。这个选项如果设置在[main]部分,则对每个repository 都有效。默认值为0。

关于变量

  • $releasever:代表发行版的版本,从[main]部分的distroverpkg获取,如果没有,则根据redhat-release包进行判断。
  • $arch:cpu体系,如i686,athlon等
  • $basearch:cpu的基本体系组,如i686和athlon同属i386,alpha和alphaev6同属alpha。

3.4 配置本地yum源

[root@node1 yum.repos.d]# ll /etc/yum.repos.d
total 76
-rw-r--r--. 1 root root 1572 Dec  1  2016 CentOS7-Base-163.repo
-rw-r--r--. 1 root root 1658 Mar  3 00:57 CentOS-Base.repo
-rw-r--r--. 1 root root 1706 Feb  9  2022 CentOS-Base.repo.bak
-rw-r--r--. 1 root root 1309 Nov 23  2020 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Nov 23  2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Nov 23  2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Nov 23  2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Mar  3 00:58 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 Nov 23  2020 CentOS-Vault.repo
-rw-r--r--. 1 root root  616 Nov 23  2020 CentOS-x86_64-kernel.repo
-rw-r--r--. 1 root root 2081 Jul 21  2022 docker-ce.repo
-rw-r--r--. 1 root root  951 Oct  3  2017 epel.repo
-rw-r--r--. 1 root root 1050 Oct  3  2017 epel-testing.repo
-rw-r--r--. 1 root root  307 Jul 21  2022 kubernetes.repo
-rw-r--r--. 1 root root 1838 Apr 27  2017 mysql-community.repo
-rw-r--r--. 1 root root 1885 Apr 27  2017 mysql-community-source.repo
-rw-r--r--. 1 root root  853 May 11  2020 zabbix.repo
  • CentOS-Base.repo 是yum 网络源的配置文件
  • CentOS-Media.repo 是yum 本地源的配置文件

本地安装

  • 1、挂载系统安装光盘
# mount /dev/cdrom /mnt/cdrom/
  • 2、修改CentOS-Media.repo
# cat CentOS-Media.repo
# CentOS-Media.repo
#
# This repo is used to mount the default locations for a CDROM / DVD on
#  CentOS-5.  You can use this repo and yum to install items directly off the
#  DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
#  yum --enablerepo=c5-media [command]
#  
# or for ONLY the media repo, do this:
#
#  yum --disablerepo=\\* --enablerepo=c5-media [command][c5-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/file:///mnt/cdrom/file:///media/cdrecorder/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5

在baseurl 中修改第2个路径为/mnt/cdrom(即为光盘挂载点)
将enabled=0改为1

  • 3、禁用默认的yum 网络源
    将yum 网络源配置文件改名为CentOS-Base.repo.bak,否则会先在网络源中寻找适合的包,改名之后直接从本地源读取。
  • 4、执行yum 命令
# yum install postgresql

4. 关于repo 文件的格式

所有repository 服务器设置都应该遵循如下格式:

[serverid]
name=Some name for this server
baseurl=url://path/to/repository/

serverid 是用于区别各个不同的repository,必须有一个独一无二的名称;
name 是对repository 的描述,支持像$releasever $basearch这样的变量;
baseurl 是服务器设置中最重要的部分,只有设置正确,才能从上面获取软件。它的格式是:

baseurl=url://server1/path/to/repository/url://server2/path/to/repository/url://server3/path/to/repository/

其中url 支持的协议有 http:// ftp:// file:// 三种。baseurl 后可以跟多个url,你可以自己改为速度比较快的镜像站,但baseurl 只能有一个,也就是说不能像如下格式:

baseurl=url://server1/path/to/repository/
baseurl=url://server2/path/to/repository/
baseurl=url://server3/path/to/repository/

其中url 指向的目录必须是这个repository header 目录的上一级,它也支持$releasever $basearch 这样的变量。
url 之后可以加上多个选项,如gpgcheck、exclude、failovermethod 等,比如:

[updates-released]
name=Fedora Core $releasever - $basearch - Released Updates
baseurl=http://download.atrpms.net/mirrors/fedoracore/updates/$releasever/$basearchhttp://redhat.linux.ee/pub/fedora/linux/core/updates/$releasever/$basearchhttp://fr2.rpmfind.net/linux/fedora/core/updates/$releasever/$basearch
gpgcheck=1
exclude=gaim
failovermethod=priority

其中gpgcheck,exclude 的含义和[main] 部分相同,但只对此服务器起作用
failovermethod 有两个选项roundrobin 和priority,意思分别是有多个url可供选择时,yum 选择的次序,roundrobin 是随机选择,如果连接失败则使用下一个,依次循环,priority 则根据url 的次序从第一个开始。如果不指明,默认是roundrobin。

3.5 配置国内yum源

系统默认的yum 源速度往往不尽人意,为了达到快速安装的目的,在这里修改yum源为国内源。

  1. 上海交通大学yum源
  • a. 修改/etc/yum.repos.d/CentOS-Base.repo为:
# 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
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5#released updates 
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
baseurl=http://ftp.sjtu.edu.cn/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
  • b. 导入GPG KEY

yum 可以使用gpg 对包进行校验,确保下载包的完整性,所以我们先要到各个repository 站点找到gpg key,一般都会放在首页的醒目位置,一些名字诸如RPM-GPG-KEY-CentOS-5 之类的纯文本文件,把它们下载下来,然后用rpm --import RPM-GPG-KEY-CentOS-5 命令将key 导入。

  • c. 执行yum 命令
  1. 其他国内yum源列表如下:
  • 企业贡献:
    搜狐开源镜像站:http://mirrors.sohu.com/
    网易开源镜像站:http://mirrors.163.com/

  • 大学教学:
    北京理工大学:
    http://mirror.bit.edu.cn (IPv4 only)
    http://mirror.bit6.edu.cn (IPv6 only)

    北京交通大学:
    http://mirror.bjtu.edu.cn (IPv4 only)
    http://mirror6.bjtu.edu.cn (IPv6 only)
    http://debian.bjtu.edu.cn (IPv4+IPv6)

    兰州大学:http://mirror.lzu.edu.cn/
    厦门大学:http://mirrors.xmu.edu.cn/

    清华大学:
    清华大学开源软件镜像站 | Tsinghua Open Source Mirror (IPv4+IPv6)
    http://mirrors.6.tuna.tsinghua.edu.cn/ (IPv6 only)

    天津大学:http://mirror.tju.edu.cn/

    中国科学技术大学:
    USTC Open Source Software Mirror (IPv4+IPv6)
    http://mirrors4.ustc.edu.cn/
    http://mirrors6.ustc.edu.cn/

    东北大学:
    http://mirror.neu.edu.cn/ (IPv4 only)
    http://mirror.neu6.edu.cn/ (IPv6 only)
    电子科技大学:http://ubuntu.uestc.edu.cn/

4. RPM

4.1 概念

rpm命令是RPM软件包的管理工具。rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎。逐渐受到其他发行版的采用。RPM套件管理方式的出现,让Linux易于安装,升级,间接提升了Linux的适用度。

优点:

由于已经编译完成并且打包完华,所以软件传输与安装上很方便(不需要再重新编译)。
RPM 在被安装之前,会先检查系统的硬盘容量、操作系统版本等,可避免文件被错误安装。
RPM 本身提供软件版本信息、依赖属性检查、软件用途说明、软件所含文件等信息;便于了解软件
RPM 管理使用数据库记录RPM文件的相关参数,便于查询、删除、升级与反安装。

缺点:

由于 RPM 文件是已经打包好的数据,也就是说,里面的数据已经都编译完成了,所以,该软件安装文件几乎只能安装在原本默认的硬件
与操作系统版本中。所以你的主机系统环境必须要与当初建立这个软件安装文件的主机环境相同才行。

所以,通常不同的 Linux 发行版所发布的RPM文件,并不能用在其他的 Linux 发行版上。但如果我们真的想要安装其他Linux发行版的RPM软件包怎么办?这时候就该用到我们SRMP。

SRPM (Source RPM)

SRPM文件里面含有原始码(Source Code),即SRPM所提供的软件内容并没有进行编译,提供的是源代码
SRPM的文件名是以 ***.src.rpm这种格式来命名虽然SRMP的内容是源代码,但是它仍然含有该软件所需的依赖性软件说明以及所有RPM文件所提供的数据,也提供了参数的配置文件,
所以如果我们用的是SRPM的话,安装时,需要先将该软件以RPM管理的方式进行编译,此时SRPM会被编译成RPM文件,然后再将RPM文件
安装到Linux系统当中。

RPM软件包

一般来说,在安装RPM类型的文件时,会先去读取文件内记录的设置参数内容,然后将该数据用来对比Linux系统的环境,以找出是否有属性依赖的软件尚未安装的问题。

若环境检查合格,那么RPM文件就被安装到Linux系统上。安装完毕后,该软件的相关信息就被写入到/var/lib/rpm目录下的数据库文件中了。

RPM的数据库:/var/lib/rpm

这个目录内的数据很重要,因为未来如果我们有任何软件升级的需求,查询系统已经安装软件的需求,查询软件详细信息的需求等都是由这个数据库记录提供的

故障原因:

非正常关机、误删除运行中的程序文件
RPM数据文件被误写或删除

当RPM数据库损坏时,需要进行数据库重建

格式:rpm  --rebuilddb 或者 rpm --initdb

4.2 命名规格

[root@kafka01 Packages]# ls openssh-askpass-8.0p1-5.el8.x86_64.rpm
openssh-askpass-8.0p1-5.el8.x86_64.rpm

一般命名格式:

软件名称 软件版本信息 发布的次数 企业版的Linux8 硬件平台 扩展名
openssh-askpass 8.0p1 5 .el8 x86_64 .rpm
  • el --> enterprise el8 --> 企业版的Linux 8
  • noarch --> no architecture --> 不区分架构,即没有任何硬件等级上的限制,一般这种类型的RPM文件中没有二进制程序文件存在,常出现的就是属于shell脚本方面的软件

4.3 语法

NAMErpm - RPM Package ManagerSYNOPSISQUERYING AND VERIFYING PACKAGES:rpm {-q|--query} [select-options] [query-options]rpm {-V|--verify} [select-options] [verify-options]INSTALLING, UPGRADING, AND REMOVING PACKAGES:rpm {-i|--install} [install-options] PACKAGE_FILE ...rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...rpm {-F|--freshen} [install-options] PACKAGE_FILE ...rpm {-e|--erase} [--allmatches] [--justdb] [--nodeps] [--noscripts][--notriggers] [--test] PACKAGE_NAME ...

最常用选项:

-i:安装(install);
-v:显示更详细的信息(verbose);
-h:打印 #,显示安装进度(hash);

命令 作用
参数 实现功能
-i 安装软件包
-v 提供安装时详细输出
-h 软件包安装时列出哈希标记
-e 卸载软件包
-U 升级软件包
-qa 查询已安装软件包
-qf 查询文件所属软件包,即查看某个文件是由哪一个包提供
-qi 显示已安装的rpm软件包信息
-ql 查看指定软件包的文件列表
-qd 查询软件的事例文档
–nodeps 安装时不验证依赖关系
–force 强制安装

常用命令组合

命令搭配 实现功能
rpm -ivh 包名 安装软件
rpm -Uvh 包名 升级软件
rpm -qpi 包名 查询软件的描述信息
rpm -qpl 包名 列出软件的文件信息