Linux下rpm和yum

 Linux下使用rpm和yum都可以实现的软件的安装,一般rpm用于安装只有单个离线安装包的软件,yum用于安装有很多依赖的软件,它们的具体细节这里离暂时没做记录,先记录基本用法。

rpm

rpm是redhat package manager的缩写,是红帽开发的一款包管理工具,它的前身是Debian的dpkg。

优点

(1)不需要编译

(2)自动检测系统是否可以安装,如系统位数,系统版本,硬盘空间等

(3)可以通过命令查看软件的详细信息,如版本、帮助文档、配置文件等。

缺点

(1)如果安装和卸载,都需要解决软件依赖关系,最大的缺点。

(2)安装软件前需要根据提示,安装依赖软件

(3)卸载软件前需要根据提示,卸载依赖关系

安装后默认路径,以mysql为例,它有如下安装后路径。

[root@node01 ~]# find / -name mysql
# 程序使用的动态链接库存在/var/lib中
/var/lib/mysql /var/lib/mysql/mysql /var/lock/subsys/mysql /usr/share/mysql /usr/lib64/mysql
# 可执行的文件存在/usr/bin下
/usr/bin/mysql /etc/logrotate.d/mysql
# 配置的开机自启动,不是默认安装的
/etc/rc.d/init.d/mysql

此外/usr/share/doc下存放软件使用手册和说明文件,usr/share/man下存放linux帮助文件。

查询相关

rpm查询使用语法为 rpm [选项] 软件包名,其中常用选项如下,最多的还是前两个:

(1)-q:查询软件是否有安装

(2)-qa:查询linux下安装的所有软件名称,一般会有管道符一起使用, 如rpm -qa|grep mysql

(3)-qc:列出软件的所有配置文件

(4)-qd:列出软件的所有说明文件

(5)-ql:列出软件所有的文件和目录

(6)-qR:列出软件需要的依赖

# 查询lrzsz是否安装
[root@node01 /home]# rpm -q lrzsz lrzsz-0.12.20-27.1.el6.x86_64
# 查询所有安装的软件,并查询出lrzsz [root@node01
/home]# rpm -qa|grep lrzsz lrzsz-0.12.20-27.1.el6.x86_64
# 查看iptables防火墙的所有配置文件 [root@node01
/home]# rpm -qc iptables /etc/sysconfig/iptables-config
# 查看lrzsz的说明文件 [root@node01
/home]# rpm -qd lrzsz /usr/share/man/man1/rz.1.gz /usr/share/man/man1/sz.1.gz
# 列出lrzsz所有的文件和目录 [root@node01
/home]# rpm -ql lrzsz /usr/bin/rb /usr/bin/rx /usr/bin/rz /usr/bin/sb /usr/bin/sx /usr/bin/sz /usr/share/locale/de/LC_MESSAGES/lrzsz.mo /usr/share/man/man1/rz.1.gz /usr/share/man/man1/sz.1.gz
# 列出wget所有的依赖 [root@node01
/home]# rpm -qR wget /bin/sh /bin/sh /sbin/install-info /sbin/install-info config(wget) = 1.12-10.el6 libc.so.6()(64bit) libc.so.6(GLIBC_2.11)(64bit) libc.so.6(GLIBC_2.2.5)(64bit) libc.so.6(GLIBC_2.3)(64bit) libc.so.6(GLIBC_2.3.4)(64bit) libc.so.6(GLIBC_2.4)(64bit) libc.so.6(GLIBC_2.8)(64bit) libcrypto.so.10()(64bit) libcrypto.so.10(libcrypto.so.10)(64bit) libdl.so.2()(64bit) librt.so.1()(64bit) librt.so.1(GLIBC_2.2.5)(64bit) libssl.so.10()(64bit) libssl.so.10(libssl.so.10)(64bit) rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rtld(GNU_HASH) rpmlib(PayloadIsXz) <= 5.2-1

目前个人最常用的还是前面两个选项。

升级卸载相关

rpm跟卸载升级相关的使用语法为 rpm [选项] 软件包名,常用选项如下。

(1)-e:卸载软件

(2)-i:安装软件

(3)-v:显示处理过程

(4)-h:显示处理进度

(5)-U:升级软件,如果软件没有安装就安装,有的话就更新

(6)-F:升级软件,只有安装了软件才会更新

一般安装软件使用-ivh组合选项,升级软件使用-Uvh组合。

yum

yum就是为了解决rpm烦人依赖而出现的,yum通过分析rpm信息来进行软件的安装,升级和卸载,它的所有执行操作会依赖yum源,即repo文件,这些文件存储在/etc/yum.repos.d/目录下。

以上yum源是默认的,可以更换为全为yum源如阿里的yum源,以下是yum的常见操作。

yum查询

(1)yum search 软件名,用于查询软件名称或关键字

# 查询hadoop,发现不归yum管理
[root@node02 /etc/yum.repos.d]# yum search hadoop Loaded plugins: fastestmirror, security Repository base is listed more than once in the configuration Repository updates is listed more than once in the configuration Repository extras is listed more than once in the configuration Repository centosplus is listed more than once in the configuration Repository contrib is listed more than once in the configuration Loading mirror speeds from cached hostfile * base: mirror.lzu.edu.cn * extras: mirror.jdcloud.com * updates: mirror.jdcloud.com
# 找不到hadoop相关信息 Warning: No matches found
for: hadoop No Matches found
# 查询lrzsz [root@node02
/etc/yum.repos.d]# yum search lrzsz Loaded plugins: fastestmirror, security Repository base is listed more than once in the configuration Repository updates is listed more than once in the configuration Repository extras is listed more than once in the configuration Repository centosplus is listed more than once in the configuration Repository contrib is listed more than once in the configuration Loading mirror speeds from cached hostfile * base: mirror.lzu.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cn
# 找到一个软件信息
============================= N/S Matched: lrzsz ============================= lrzsz.x86_64 : The lrz and lsz modem communications programs Name and summary matches only, use "search all" for everything. # 查询wget [root@node02 /etc/yum.repos.d]# yum search wget Loaded plugins: fastestmirror, security Repository base is listed more than once in the configuration Repository updates is listed more than once in the configuration Repository extras is listed more than once in the configuration Repository centosplus is listed more than once in the configuration Repository contrib is listed more than once in the configuration Loading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cn
# 查询到一个归yum管理
============================= N/S Matched: wget ============================== wget.x86_64 : A utility for retrieving files using the HTTP or FTP protocols Name and summary matches only, use "search all" for everything. [root@node02 /etc/yum.repos.d]#

(2)yum list,可以列出yum所管理的所有软件和版本信息。

# 查询发现N多软件被yum所管理
[root@node02 /etc/yum.repos.d]# yum list Loaded plugins: fastestmirror, security Repository base is listed more than once in the configuration Repository updates is listed more than once in the configuration Repository extras is listed more than once in the configuration Repository centosplus is listed more than once in the configuration Repository contrib is listed more than once in the configuration Loading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cn Installed Packages ConsoleKit.x86_64 0.4.1-3.el6 @anaconda-CentOS-201311272149.x86_64/6.5 ConsoleKit-libs.x86_64 0.4.1-3.el6 @anaconda-CentOS-201311272149.x86_64/6.5 MAKEDEV.x86_64 3.24-6.el6 @anaconda-CentOS-201311272149.x86_64/6.5 SDL.x86_64 1.2.14-3.el6 @anaconda-CentOS-201311272149.x86_64/6.5 abrt.x86_64 2.0.8-21.el6.centos @anaconda-CentOS-201311272149.x86_64/6.5 abrt-addon-ccpp.x86_64 2.0.8-21.el6.centos @anaconda-CentOS-201311272149.x86_64/6.5 abrt-addon-kerneloops.x86_64 2.0.8-21.el6.centos @anaconda-CentOS-201311272149.x86_64/6.5 abrt-addon-python.x86_64 2.0.8-21.el6.centos @anaconda-CentOS-201311272149.x86_64/6.5 abrt-cli.x86_64 2.0.8-21.el6.centos @anaconda-CentOS-201311272149.x86_64/6.5 abrt-libs.x86_64 2.0.8-21.el6.centos @anaconda-CentOS-201311272149.x86_64/6.5 abrt-tui.x86_64 2.0.8-21.el6.centos @anaconda-CentOS-201311272149.x86_64/6.5 acl.x86_64 2.2.49-6.el6 @anaconda-CentOS-201311272149.x86_64/6.5 acpid.x86_64 1.0.10-2.1.el6 @anaconda-CentOS-201311272149.x86_64/6.5 aic94xx-firmware.noarch 30-2.el6 @anaconda-CentOS-201311272149.x86_64/6.5 alsa-lib.x86_64 1.0.22-3.el6 @anaconda-CentOS-201311272149.x86_64/6.5 alsa-utils.x86_64 1.0.22-5.el6 @anaconda-CentOS-201311272149.x86_64/6.5 apr.x86_64 1.3.9-5.el6_2 @anacon

...省略

yum安装

yum安装使用yum install 包名 [-y],如果没加-y会有确认提示,加了-y就是一路的yes安装下去。

# yum安装lrzsz示例
[root@node02 /etc/yum.repos.d]# yum install lrzsz -y Loaded plugins: fastestmirror, security Repository base is listed more than once in the configuration Repository updates is listed more than once in the configuration Repository extras is listed more than once in the configuration Repository centosplus is listed more than once in the configuration Repository contrib is listed more than once in the configuration Loading mirror speeds from cached hostfile * base: mirror.lzu.edu.cn * extras: mirror.jdcloud.com * updates: mirror.jdcloud.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package lrzsz.x86_64 0:0.12.20-27.1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================== Package Arch Version Repository Size ============================================================================== Installing: lrzsz x86_64 0.12.20-27.1.el6 base 71 k Transaction Summary ============================================================================== Install 1 Package(s) Total download size: 71 k Installed size: 159 k Downloading Packages: lrzsz-0.12.20-27.1.el6.x86_64.rpm | 71 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : lrzsz-0.12.20-27.1.el6.x86_64 1/1 Verifying : lrzsz-0.12.20-27.1.el6.x86_64 1/1 Installed: lrzsz.x86_64 0:0.12.20-27.1.el6 Complete!

yum卸载

yum卸载使用yum remove 包名。

# 卸载刚才安装的lrzsz
[root@node02 /etc/yum.repos.d]# yum remove lrzsz Loaded plugins: fastestmirror, security Setting up Remove Process Resolving Dependencies --> Running transaction check ---> Package lrzsz.x86_64 0:0.12.20-27.1.el6 will be erased --> Finished Dependency Resolution Repository base is listed more than once in the configuration Repository updates is listed more than once in the configuration Repository extras is listed more than once in the configuration Repository centosplus is listed more than once in the configuration Repository contrib is listed more than once in the configuration Dependencies Resolved ============================================================================== Package Arch Version Repository Size ============================================================================== Removing: lrzsz x86_64 0.12.20-27.1.el6 @base 159 k Transaction Summary ============================================================================== Remove 1 Package(s) Installed size: 159 k Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : lrzsz-0.12.20-27.1.el6.x86_64 1/1 Verifying : lrzsz-0.12.20-27.1.el6.x86_64 1/1 Removed: lrzsz.x86_64 0:0.12.20-27.1.el6 Complete!
# 查询已卸载 [root@node02 /etc/yum.repos.d]# rpm -qa lrzsz [root@node02 /etc/yum.repos.d]#

yum更新

yum的更新使用yum update 包名。

# 准备更新发现是最新版
[root@node02 /etc/yum.repos.d]# yum update lrzsz Loaded plugins: fastestmirror, security Repository base is listed more than once in the configuration Repository updates is listed more than once in the configuration Repository extras is listed more than once in the configuration Repository centosplus is listed more than once in the configuration Repository contrib is listed more than once in the configuration Loading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cn Setting up Update Process No Packages marked for Update [root@node02 /etc/yum.repos.d]#

yum运行机制 

yum每次执行install或search时,会去/etc/yum.repo.d/文件夹下读取repo文件,这些文件包含yum服务器url等关键信息。如果安装的软件这些服务器都没有,则无法安装,这个时候需要添加yum源,如hadoop默认是没有配置yum服务器的,需要添加一个hadoop的repo文件才可以。

网址https://archive.cloudera.com/cdh5/redhat/6/x86_64/cdh里包含了需要的rpm包

随便点一个可以查看rpm包信息。

因此可以将这个网址配置到repo的baseurl中。

# 在/etc/yum.repos.d/目录下添加hadoop相关的repo文件,里面baseurl就是上面我们访问的网址
[root@node02 /etc/yum.repos.d]# cat cloudera-cdh5.repo [cloudera-cdh5] # Packages for Cloudera's Distribution for Hadoop, Version 5, on RedHat or CentOS 6 x86_64 name=Cloudera's Distribution for Hadoop, Version 5 baseurl=https://archive.cloudera.com/cdh5/redhat/6/x86_64/cdh/5/ gpgkey =https://archive.cloudera.com/cdh5/redhat/6/x86_64/cdh/RPM-GPG-KEY-cloudera gpgcheck = 1

另外,yum安装完软件,可以设置将rpm包保存到cache缓存中,这个需要在/etc/yum.conf中进行设置,如图所示,cachedir目录即缓存保存的目录,设置keepcache=1则代表保存缓存。如果安装软件时缓存中没有这个rpm包,则会从yum服务器上下载,否则使用cache中的。

cachedir目录实际如下。

# cachedir目录为/var/cache/yum/x86_64/6
[root@node01 /var/cache/yum/x86_64/6]# ll total 24 drwxr-xr-x. 4 root root 4096 Oct 15 13:53 base drwxr-xr-x. 3 root root 4096 Oct 15 15:48 cloudera-cdh5 drwxr-xr-x. 3 root root 4096 Oct 15 13:49 epel drwxr-xr-x. 3 root root 4096 Oct 15 13:53 extras -rw-r--r--. 1 root root 492 Oct 11 11:34 timedhosts.txt drwxr-xr-x. 3 root root 4096 Oct 15 13:53 updates

centos6更新yum源

下面更新默认yum源为阿里yum源。

# 更新yum源为阿里yum源,并且文件命名为CentOS-Base.repo
[root@node02 /etc/yum.repos.d]# wget -O CentOS-Base.repo http://mirrors.aliyunm/repo/Centos-6.repo
--2019-10-21 17:44:06--  http://mirrors.aliyun.com/repo/Centos-6.repo
Resolving mirrors.aliyun.com... 61.162.48.182, 119.167.220.234, 119.167.168.224, ...
Connecting to mirrors.aliyun.com|61.162.48.182|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: “CentOS-Base.repo”

100%[====================================>] 2,523       --.-K/s   in 0s      

2019-10-21 17:44:06 (101 MB/s) - “CentOS-Base.repo” saved [2523/2523]

[root@node02 /etc/yum.repos.d]# ll
total 24
-rw-r--r--. 1 root root 1926 Oct 15 19:34 CentOS-Base_bak.repo
-rw-r--r--. 1 root root 2523 Jun 16  2018 CentOS-Base.repo
-rw-r--r--. 1 root root  638 Nov 27  2013 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  630 Nov 27  2013 CentOS-Media.repo
-rw-r--r--. 1 root root 3664 Nov 27  2013 CentOS-Vault.repo
-rw-r--r--. 1 root root  324 Oct 21 17:25 cloudera-cdh5.repo
# 查看 [root@node02
/etc/yum.repos.d]# cat 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]
# 已更新为阿里的yum源 name
=CentOS-$releasever - Base - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

... 省略

以上是对linux下rpm和yum的简单整理,均基于centos6.5。

参考博文:

(1)https://blog.csdn.net/shuaigexiaobo/article/details/79769175

(2)https://blog.csdn.net/ljl890705/article/details/78523367 yum安装cache的使用

猜你喜欢

转载自www.cnblogs.com/youngchaolin/p/11713079.html