当前位置:网站首页>RPM包管理

RPM包管理

2022-04-23 18:41:00 师莹

RPM包概述

什么是rpm包

由红帽开发的软件、升级、安装、卸载、查询工具

为什么要使用rpm

就像在windows系统中一样,如果你想要安装一个QQ,安装一个微信,安装一款游戏,首先要去该软件的官网上去下载相关的软件包,通常都是.exe的安装包。还有那种绿色软件包,一下载下来,解压开就能使用无需安装。

那么Linux中也有很多种类的安装包,我们在Linux上也需要安装软件,软件包的种类也有很多,但是最方便,最简单,最简易,速度最快的,也就是rpm包了。

rpm包命名规则

      
      
## tree-1.6.0-10.el7.x86_64.rpm
tree:软件名称
1.6.0:版本号
10:该版本的发布次数
el7:适用系统版本(RedHat7,CentOS7)向下兼容
x86_64:系统平台64 x86:32位 i386
rpm:包后缀名
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

Linux中软件的安装方式

      
      
rpm安装:已经编译好的源码包,下载下来后,直接使用rpm命令安装即可 版本会低于官方版本
源码安装:先编译,将源代码翻译成系统认识的软件,再安装 步骤复杂繁琐
二进制安装:绿色软件(免安装) 不是所有程序都有二进制包
  • 1.
  • 2.
  • 3.

RPM包获取途径

  • 系统光盘镜像/CentOS官网: TP

1.检查光盘是否放入光驱

挂载 mount 卸载umount语法

      
      
# 挂载语法
mount 源设备文件 目标路径
[r[email protected] ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only

# 查看磁盘信息
df -h查看磁盘分区情况,磁盘使用情况,也能查看到挂载设备及目录
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda3 18G 1.6G 16G 10% /
/dev/sda1 497M 125M 373M 25% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sr0 4.4G 4.4G 0 100% /mnt

# 卸载语法
umount 目标路径
umount /mnt
[[email protected] ~]# umount /mnt

## 挂载报错[[email protected] yjc ~ ] # mount /dev/cdrom /mnt
mount: no medium found on /dev/sr0
原因:设备文件是空的,里面没有任何内容,光驱没有连接

## 卸载报错
[[email protected] mnt]# umount /mnt
umount: /mnt: target is busy.
(In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))
[[email protected] mnt]# cd
[[email protected] ~]# umount /mnt
原因:设备繁忙


## 查看挂载结果

[[email protected] ~]# ll /mnt
total 696
-rw-r--r--. 3 root root 14 Oct 30 2020 CentOS_BuildTag
drwxr-xr-x. 3 root root 2048 Oct 27 2020 EFI
-rw-rw-r--. 21 root root 227 Aug 30 2017 EULA
-rw-rw-r--. 21 root root 18009 Dec 10 2015 GPL
drwxr-xr-x. 3 root root 2048 Oct 27 2020 images
drwxr-xr-x. 2 root root 2048 Nov 3 2020 isolinux
drwxr-xr-x. 2 root root 2048 Oct 27 2020 LiveOS
drwxr-xr-x. 2 root root 673792 Nov 4 2020 Packages
drwxr-xr-x. 2 root root 4096 Nov 4 2020 repodata
-rw-rw-r--. 21 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r--. 21 root root 1690 Dec 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root 2883 Nov 4 2020 TRANS.TBL

如果卸载了zip文件
[[email protected] ~]# yum remove zip -y

切换到/mnt/Packages/目录下
[[email protected] ~]# /mnt/Packages/
[[email protected] ~]# cd /mnt/Packages/
rpm包安装
[[email protected] Packages]# rpm -ivh zip-3.0-11.el7.x86_64.rpm

/mnt/Packages里面有很多rpm包
[[email protected] Packages]# ls -l |wc -l
4072
[[email protected] Packages]# ll /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.

rpm 命令使用-安装

      
      
# 语法:
rpm [选项] rpm包名
rpm包名:
-本地包名:在系统中某个目录下的rpm包
-远程包名:不用下载,直接使用网站上的rpm包
# 选项:
-i:install安装
-v:显示安装的过程
-h:显示安装的进度条
--test:仅测试,不安装
--force:强制安装
--nodeps:忽略依赖关系

举例:
# 本地包名 -i rpm指定路径可以安装
[[email protected] Packages]# yum remove tree -y
[[email protected] Packages]# cd
[[email protected] ~]# rpm -i /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm
[[email protected] ~]# echo $?
0
[[email protected] ~]# tree
.
├── 111
├── 123tar.gz
├── 1.txt
├── 2.txt
├── 3.txt
└── tmp

## -v
[[email protected] ~]# rpm -iv /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm
Preparing packages...
tree-1.6.0-10.el7.x86_64

## -h
[[email protected] ~]# rpm -ivh /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:tree-1.6.0-10.el7 ################################# [100%]

## --test 只是测试
[[email protected] ~]# cd /mnt/Packages/
[[email protected] Packages]# rpm -ivh --test tree-1.6.0-10.el7.x86_64.rpm
Preparing... ################################# [100%]
package tree-1.6.0-10.el7.x86_64 is already installed

## --force --nodeps配合使用 强制执行安装 忽略依赖关系的有bug

如果zip卸载 可以远程aliyun镜像站找包名 复制网址安装
[[email protected] ~]# rpm -ivh https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/zip-3.0-11.el7.x86

## 注意:rpm命令管理rpm包的缺点,无法解决依赖关系https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/zip-3.0-11.el7.x86
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • RPM包查询官方网站: tp
  • 其他软件官方网站

rpm命令使用-查询

      
      
# 语法
rpm [选项] rpm包名
# 选项
-q:查看指定的安装包是否安装 query查询

---以下选项皆要配合-q使用---
-a:全部安装过的包 all
-i:详细信息 infor
-l:列出rpm包相关的配置文件和目录
-c:只查看该软件包相关的配置文件
-d:只查看该软件的帮助文档文件
-f:反向查找,根据配置文件,目录,帮助文档,查找该文件属于哪个包
--scripts:查看安装包,安装之前之后,和卸载之前之后要执行的脚本

-----和上面选项配合使用
-p:查询未安装包

-qip:查看没有安装包的详细信息(指定一个已存在的完整包名或者远程包名)
-qlp:查看没有安装包会生成那些文件(指定一个已存在的完整包名或者远程包名)
-qcp:查看没有安装包会生成哪些配置文件(指定一个已存在的完整包名或者远程包名)
-qdp:查看没有安装包会生成哪些帮助文件(指定一个已存在的完整包名或者远程包名

##举例:
## -q
[[email protected] Packages]# rpm -q tree
tree-1.6.0-10.el7.x86_64
[[email protected] Packages]# rpm -q vim-common
vim-common-7.4.629-8.el7_9.x86_64

## -q -a 查看含有net安装过的包
[[email protected] Packages]# rpm -q net
package net is not installed
[[email protected] Packages]# rpm -qa |grep 'net'
libnfnetlink-1.0.1-4.el7.x86_64
libnetfilter_conntrack-1.0.6-1.el7_3.x86_64
dracut-network-033-572.el7.x86_64

后面不加包名 查看所有的
[[email protected] Packages]# rpm -qa

## -i 后面要接完整的包名
[[email protected] Packages]# rpm -qi net-tools

[[email protected] Packages]# rpm -qa |grep 'net'|xargs rpm qi
[[email protected] Packages]# rpm -qi `rpm -qa | grep 'net'`
[[email protected] Packages]# rpm -qi $(rpm -qa | grep 'net')

## -l
[[email protected] Packages]# rpm -ql zip
/usr/bin/zip
/usr/bin/zipcloak
/usr/bin/zipnote

[[email protected] Packages]# rpm -ql tree
/usr/bin/tree
/usr/share/doc/tree-1.6.0
## -c 客户端配置文件
[[email protected] Packages]# rpm -qc mariadb
/etc/my.cnf.d/client.cnf
服务端配置文件
[[email protected] Packages]# rpm -qc mariadb-server
/etc/logrotate.d/mariadb
/etc/my.cnf.d/server.cnf
/var/log/mariadb/mariadb.log
## -d 只查看帮助
[[email protected] Packages]# rpm -qd zip
/usr/share/doc/zip-3.0/CHANGES
/usr/share/doc/zip-3.0/LICENSE
/usr/share/doc/zip-3.0/README
/usr/share/doc/zip-3.0/README.CR

## -f 反向查找是哪个包
[[email protected] Packages]# rpm -qf /etc/passwd
setup-2.8.71-11.el7.noarch

[[email protected] Packages]# rpm -qf /etc/hostname
systemd-219-78.el7.x86_64

[[email protected] Packages]# rpm -ql setup
/etc/aliases
/etc/bashrc
/etc/csh.cshrc
/etc/csh.login
# 查找属于哪个包
[[email protected] Packages]# rpm -qf /etc/hostname
systemd-219-78.el7.x86_64
# 包里有哪些配置文件和目录
[[email protected] ~]# rpm -ql systemd
# 过滤含有bin的文件
[[email protected] ~]# rpm -ql systemd |grep 'bin'

rpm -ql $(rpm -qf /etc/hostname)

[[email protected] Packages]# ll /etc/hostname
-rw-r--r--. 1 root root 22 Mar 14 22:34 /etc/hostname
[[email protected] Packages]# cat /etc/hostname
localhost.localdomain
更改用户名
[[email protected] Packages]# hostnamectl set-hostname shiying
[[email protected] ~]# cat /etc/hostname
shiying

## --scripts
[[email protected] ~]# rpm -q --scripts mariadb-server


## -p qip查看未安装nginx详细信息
[[email protected] ~]# rpm -qip https://mirrors.aliyun.com/epel/7/x86_64/Packages/n/nginx-1.20.1-9.el7.x86_64.rpm
warning: https://mirrors.aliyun.com/epel/7/x86_64/Packages/n/nginx-1.20.1-9.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Name : nginx
Epoch : 1
Version : 1.20.1
Release : 9.el7
Architecture: x86_64

#-qlp查看要装哪些包
[[email protected] ~]# rpm -qlp https://mirrors.aliyun.com/epel/7/x86_64/Packages/n/nginx-1.20.1-9.el7.x86_64.rpm
warning: https://mirrors.aliyun.com/epel/7/x86_64/Packages/n/nginx-1.20.1-9.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.

rpm命令使用-升级

      
      
选项:
-U:update升级,如果老版本不存在,则安装,如果老版本存在则升级
-f:必须有老版本存在才可以升级

##-U

[[email protected] Packages]# rpm -qip tree-1.6.0-10.el7.x86_64.rpm
Name : tree
Version : 1.6.0
Release : 10.el7
Architecture: x86_64
Install Date: (not installed)
Group : Applications/File
Size : 89505

[[email protected] Packages]# rpm -Uvh tree-1.6.0-10.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:tree-1.6.0-10.el7 ################################# [100%]

## 下载nginx包
[[email protected] ]# wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.10.0-1.el7.ngx.x86_64.rpm

[[email protected] ]# wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.12.0-1.el7.ngx.x86_64.rpm

## 升级安装nginx的包
[[email protected] ~]# rpm -Uvh nginx-1.10.0-1.el7.ngx.x86_64.rpm
warning: nginx-1.10.0-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:nginx-1:1.10.0-1.el7.ngx ################################# [100%]
------------------------------------------------------
## 查询当前系统中安装nginx的包(1.10版本
[[email protected] ~]# rpm -qa |grep nginx
nginx-1.10.0-1.el7.ngx.x86_64


## 使用ivh安装nginx 1.12版本的包
[[email protected] ~]# rpm -ivh nginx-1.12.0-1.el7.ngx.x86_64.rpm

warning: nginx-1.12.0-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing... ################################# [100%]
file /etc/logrotate.d/nginx from install of nginx-1:1.12.0-1.el7.ngx.x86_64 conflicts with file from package nginx-1:1.10.0-1.el7.ngx.x86_64
file /usr/sbin/nginx from install of nginx-1:1.12.0-1.el7.ngx.x86_64 conflicts with file from package nginx-1:1.10.0-1.el7.ngx.x86_64
file /usr/sbin/nginx-debug from install of nginx-1:1.12.0-1.el7.ngx.x86_64 conflicts with file from package nginx-1:1.10.0-1.el7.ngx.x86_64
file /usr/lib/systemd/system/nginx.service from install of nginx-1:1.12.0-1.el7.ngx.x86_64 conflicts with file from package nginx-1:1.10.0-1.el7.ngx.x86_64
file /usr/libexec/initscripts/legacy-actions/nginx/upgrade from install of nginx-1:1.12.0-1.el7.ngx.x86_64 conflicts with file from package nginx-1:1.10.0-1.el7.ngx.x86_64

# 以上报错:因为系统中已存在1.10版本的nginx所以无法使用ivh安装

## -Uvh升级 旧版本卸载
[[email protected] ~]# rpm -Uvh nginx-1.12.0-1.el7.ngx.x86_64.rpm
warning: nginx-1.12.0-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:nginx-1:1.12.0-1.el7.ngx ################################# [ 50%]
Cleaning up / removing...
2:nginx-1:1.10.0-1.el7.ngx ################################# [100%]

[[email protected] ~]# rpm -qa nginx
nginx-1.12.0-1.el7.ngx.x86_64

## -f老版本存在才可以升级
[[email protected] ~]# rpm -fvh nginx-1.12.0-1.el7.ngx.x86_64.rpm
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.

rpm命令使用-卸载

      
      
# 选项
-e:后面接卸载的包名即可
[[email protected] ~]# rpm -qa|grep nginx
nginx-1.12.0-1.el7.ngx.x86_64
[[email protected] ~]# rpm -e nginx
[[email protected] ~]# rpm -qa |grep nginx

批量删除 如有依赖则卸载不成功
[[email protected] ~]# rpm -qa|grep mariadb
mariadb-server-5.5.68-1.el7.x86_64
mariadb-5.5.68-1.el7.x86_64
mariadb-libs-5.5.68-1.el7.x86_64
[[email protected] ~]# rpm -e $(rpm -qa|grep mariadb)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

yum 报错

      
      
## 1.报错
http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/Packages/python-srpm-macros-3-34.el7.noarch.rpm: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com;Unknown error

原因是aliyun的Base源中有另外两个baseurl=那两个网址不是咱们使用的,可以删除

## 2.报错
[[email protected] ~]# yum install -y tr
Loaded plugins: fastestmirror
Existing lock /var/run/yum.pid:
another copy is running as pid 37209.Another app is currently holding the yum lock; waiting for it to exit...
The other application is: yum Memory : 24 M RSS (769 MB VSZ)
Started: Thu Apr 2121:33:34 2022-00:06 ago
State : Sleeping, pid: 37209

原因:后台已经有yum进程在安装服务,要么就等安装结束,要么kill PID

## 3.网络波动
多执行几次

## 4.环境变量损坏baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/

$releasever:7
$basearch: x86_64
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.


版权声明
本文为[师莹]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_15548673/5248716