# Linux软件管理(yum工具的使用)

Linux软件管理(yum工具的使用)

@

yum和rpm

1. rpm的弊端

前面我们讲了下rpm,那么rpm有什么弊端呢?其弊端是显而易见的,当用rpm安装软件时,若遇到有依赖关系的软件,必须先安装依赖的软件才能继续安装我们要安装的软件,当依赖关系很复杂的情况下,这种安装方式就很让人头疼,所以我们需要另一种安装方式来解决这个问题,今天我们要来学习的就是这样一个工具--yum

2. yum的优劣势

yum有什么优势呢?yum最大的优势就是能够解决rpm的依赖问题,yum能够自动解决软件安装时的依赖关系。
当然了,有优势就有劣势,人无完人嘛,软件也是一样的,yum的缺陷就是如果在未完成安装的情况下强行中止安装过程,下次再安装时将无法解决依赖关系,Fedora22+、redhat7和centos7等可以通过手动安装dnf工具来解决此问题。
dnf是redhat7上用来代替yum的一个工具,其存在的意义就是处理yum的缺陷,但其用法与yum是完全一样的,甚至连选项都是一样的,你可以理解为dnf就是yum,只是换了个名字而已。所以说只要学会了yum就自然会dnf,大家不用担心学了yum又要去学dnf之类的问题

3. 什么是yum及其作用

那么什么是yum呢?yum是yellowdog update manager的简称,它能够实现rpm管理的所有操作,并能够自动解决各rpm包之间的依赖关系。yum是rpm的前端工具,是基于rpm来实现软件的管理的一个工具。
你不能用yum去管理windows的exe程序包,也不能用yum去管理ubuntu的deb程序包,只能用yum来管理redhat系列的rpm包

挂载光盘

[root@hostnamectl ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载

yum的配置文件

  • 配置文件有哪些:
    • /etc/yum.conf 作用:为所有仓库提供公共配置
    • /etc/yum.repos.d/*.repo 作用:为仓库的指向提供配置
  • yum的repo配置文件中可用的变量:
    • $releaseversion:当前OS的发行版的主版本号
    • $arch:平台类型
    • $basearch:基础平台

配置本地yum仓库

  • 创建一个文件夹(也可以直接用mnt文件夹)
[root@hostnamectl ~]# mkdir /opt/myrepo
[root@hostnamectl ~]# ls /opt/
myrepo
  • 将光盘文件cp到目录下
[root@hostnamectl ~]# cp -r /mnt/* /opt/myrepo/
[root@hostnamectl ~]# ls /opt/myrepo/
addons  EULA              GPL     isolinux  media.repo  repodata                 RPM-GPG-KEY-redhat-release
EFI     extra_files.json  images  LiveOS    Packages    RPM-GPG-KEY-redhat-beta  TRANS.TBL
  • 本地仓库配置
[Repo_Name]:仓库名称  
name:描述信息  
  baseurl:仓库的具体路径,接受以下三种类型  
    ftp://  
    http://  
    file:///  
enabled:可选值{1|0},1为启用此仓库,0为禁用此仓库  
gpgcheck:可选值{1|0},1为检查软件包来源合法性,0为不检查来源  
    如果gpgcheck设为1,则必须用gpgkey定义密钥文件的具体路径  
    gpgkey=/PATH/TO/KEY  
[root@hostnamectl ~]# touch /etc/yum.repos.d/myrepo.repo
[root@hostnamectl ~]# vi /etc/yum.repos.d/myrepo.repo 
[root@hostnamectl ~]# cat /etc/yum.repos.d/myrepo.repo 
[myrepo]
name:myrepo.repo
baseurl:file:///opt/myrepo
enabled:1
gpgcheck:0
  • 清除本地缓存
[root@hostnamectl ~]# yum clean all
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在清理软件源: myrepo       //显示这个表示myrepo仓库可以使用了
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from dis
  • 检验yum本地仓库
[root@hostnamectl ~]# yum list all 
compat-db-headers.noarch                4.7.25-28.el7              myrepo 
compat-glibc-headers.x86_64             1:2.12-4.el7               myrepo 
glibc-headers.x86_64                    2.17-196.el7               myrepo 
java-1.7.0-openjdk-headless.x86_64      1:1.7.0.141-2.6.10.5.el7   myrepo 
java-1.8.0-openjdk-headless.i686        1:1.8.0.131-11.b12.el7     myrepo 
java-1.8.0-openjdk-headless.x86_64      1:1.8.0.131-11.b12.el7     myrepo 
kernel-headers.x86_64                   3.10.0-693.el7             myrepo 
........

yum网络仓库

  • 官方网络yum仓库(国外)
  • 阿里云yum仓库
  • 163yum仓库
  • xx大学yum仓库
  • epel源

阿里云yum仓库:
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl:linux自带的下载软件
阿里云epel源
[root@localhost ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

  • 源查找方式基本一致,zabbix,mysql,saltstack,openstack等等,上官网找
    [root@localhost ~]# vim /etc/yum.repos.d/nginx.repo
    [nginx]
    name=nginx repo
    baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
    gpgcheck=0
    enabled=1

  • redhat7使用centos7的yum源

1.卸载红帽yum源
[root@localhost ~]# rpm -e $(rpm -qa|grep yum) --nodeps

2.删除所有repo相关文件
[root@localhost ~]# rm -f /etc/yum.conf
[root@localhost ~]# rm -rf /etc/yum.repos.d/
[root@localhost ~]# rm -rf /var/cache/yum

3.下载centos相关yum组件(安装wget,[root@hostnamectl ~]# rpm -ivh /mnt/Packages/wget-1.14-15.el7.x86_64.rpm    最后一个版本可能不一样,自己tabel)
[root@localhost ~]# wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-3.4.3-163.el7.centos.noarch.rpm
[root@localhost ~]# wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
[root@localhost ~]# wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-52.el7.noarch.rpm
[root@localhost ~]# wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-updateonboot-1.1.31-52.el7.noarch.rpm
[root@localhost ~]# wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-utils-1.1.31-52.el7.noarch.rpm
//如果链接因服务器跟新而失效了就自己看着包名找,2019年9月28日18:11:53

//如果没有wget命令则使用curl命令(建议使用wget)
[root@localhost ~]# curl -o yum-utils-1.1.31-50.el7.noarch.rpm  http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-utils-1.1.31-50.el7.noarch.rpm
[root@localhost ~]# curl -o yum-3.4.3-161.el7.centos.noarch.rpm  http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-3.4.3-161.el7.centos.noarch.rpm
[root@localhost ~]# curl -o yum-metadata-parser-1.1.4-10.el7.x86_64.rpm  http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
[root@localhost ~]# curl -o yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm   http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm
[root@localhost ~]# curl -o yum-updateonboot-1.1.31-50.el7.noarch.rpm http://mirror.centos.org/centos/7/os/x86_64/Packages/yum-updateonboot-1.1.31-50.el7.noarch.rpm

3.安装CentOS所有相关组件
[root@hostnamectl ~]# rpm -ivh yum-* --nodeps
警告:yum-3.4.3-163.el7.centos.noarch.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:yum-metadata-parser-1.1.4-10.el7 ################################# [ 20%]
   2:yum-plugin-fastestmirror-1.1.31-5################################# [ 40%]
   3:yum-3.4.3-163.el7.centos         ################################# [ 60%]
   4:yum-updateonboot-1.1.31-52.el7   ################################# [ 80%]
   5:yum-utils-1.1.31-52.el7          ################################# [100%]
//五个包缺一不可


4.下载base和epel仓库
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# sed -i 's#$releasever#7#g' /etc/yum.repos.d/CentOS-Base.repo

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum管理软件

yum命令语法:

yum [options] [command] [package ...]
  • 常用的options:
ist            //列表
    all         //默认项
    available   //列出仓库中有的,但尚未安装的所有可用的包
    installed   //列出已经安装的包
    updates     //可用的升级
    
clean           //清理缓存
    packages
    headers
    metadata
    dbcache
    all         //一般只会用这个
    
repolist        //显示repo列表及其简要信息
    all
    enabled     //默认项
    disabled
    
install         //安装
    yum install packages [...]
[root@hostnamectl ~]# yum -y install httpd      //-y 安装的时候就不许要自己输入y了

    
update          //升级
    yum update packages [...]
[root@hostnamectl ~]# yum -y update vim

update_to       //升级为指定版本

downgrade package1 [package2 ...]   //降级

remove|erase    //卸载,卸载的时候他所依赖的包不会卸载所以一般情况不要随便卸载

info    //显示rpm -qi package的结果
    yum info packages
    
provides|whatprovides   //查看指定的文件或特性是由哪个包安装生成的

search string1 [string2 ...]    //以指定的关键字搜索程序包名及summary信息

deplist package [package2 ...]  //显示指定包的依赖关系

history     //查看yum的历史事务信息

localinstall    //安装本地rpm包,自动解决依赖关系

grouplist       //列出可用的组

groupinstall "group name"   //安装一组软件
        
createrepo命令    //创建yum仓库的元数据信息
[root@localhost ~]# yum install createrepo -y
[root@localhost ~]# createrepo [options] <directory

命令实践

搜索软件包

列出软件仓库中可用的软件
[root@localhost ~]# yum list all

进行模糊查找
[root@localhost ~]# yum list|grep ftp

列出软件包详情
[root@localhost ~]# yum info ftp

安装软件包

安装软件只需要给出软件名称
[root@localhost ~]# yum install traceroute


安装过程中分析依赖关系后, 直接安装, 无需交互
[root@localhost ~]# yum -y install php

安装本地的rpm包, 如果有依赖关系, 会自动从软件仓库中下载所需依赖(非来自.repo定义的软件仓库)
[root@localhost ~]# yum localinstall /mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm

安装网络上rpm包
[root@hostnamectl ~]# yum -y install https://mirrors.aliyun.com/centos/7.7.1908/os/x86_64/Packages/wget-1.14-18.el7_6.1.x86_64.rpm

重装软件包

检查软件是否存在
[root@hostnamectl ~]# rpm -q httpd
httpd-2.4.6-90.el7.centos.x86_64


[root@hostnamectl ~]# rpm -qc httpd
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd

//不小心删除httpd配置文件
[root@hostnamectl ~]# rm -f /etc/httpd/conf.d/autoindex.conf

[root@hostnamectl ~]# rpm -qc httpd
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd

更新软件包

//对比Linux已安装的软件和yum仓库中的软件, 有哪些需要升级
[root@localhost ~]# yum check-update

//更新软件
[root@localhost ~]#  yum update vim -y

删除软件包

//删除该软件包,不会删除依赖, 但是我们尽可能不要使用删除软件操作
[root@localhost ~]# yum -y erase vim
[root@localhost ~]# yum -y remove vim

与仓库相关的命令

列出yum源可用的软件仓库
[root@hostnamectl ~]# yum repolist
已加载插件:fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
源标识                                                   源名称                                                                                   状态
base/x86_64                                              CentOS-7 - Base - mirrors.aliyun.com                                                     10,097
epel/x86_64                                              Extra Packages for Enterprise Linux 7 - x86_64                                           13,415
extras/x86_64                                            CentOS-7 - Extras - mirrors.aliyun.com                                                      304
updates/x86_64                                           CentOS-7 - Updates - mirrors.aliyun.com                                                     319
repolist: 24,135


列出全部yum源可用和禁用的仓库
[root@localhost ~]# yum repolist all

//查看这个文件或命令属于哪个包
[root@localhost ~]# yum provides /etc/my.cnf
[root@localhost ~]# yum provides cd
[root@localhost ~]# yum provides *bin/pstree

与缓存相关的命令

缓存yum源软件仓库, xml元数据文件
[root@localhost ~]# yum makecache

缓存软件包, 修改yum全局配置文件
[root@localhost ~]# vim /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=1                     //启动缓存


查看缓存的xml文件
[root@localhost ~]# ls /var/cache/yum/x86_64/7/base/

查看缓存软件包路径
[root@localhost ~]# ls /var/cache/yum/x86_64/7/


另一种缓存rpm包方式

1.安装插件支持只下载软件包不安装
[root@localhost ~]# yum -y install yum-plugin-downloadonly
2.将软件下载至指定目录
[root@localhost ~]# yum -y install --downloadonly --downloaddir=/tmp httpd


清除所有yum缓存
[root@localhost ~]# yum clean all

只清除缓存的软件包
[root@localhost ~]# yum clean packages

与包组相关的命令

列出已经安装和所有可使用的软件组
[root@localhost ~]# yum groups list

安装一整个组的软件
[root@localhost ~]# yum groups install Development tools 
Compatibility libraries 
Base Debugging Tools

yum删除包组
[root@localhost ~]# yum groups remove  -y Base

与历史记录相关的命令

查看历史执行yum命令
[root@localhost ~]# yum history

查询历史执行yum命令ID详细信息
[root@localhost ~]# yum history info N

撤销历史执行过的yum命令
[root@localhost ~]# yum history undo N

YUM签名检查机制

  • rpm软件提供组织redhat在构建rpm包时, 使用其私钥private key对 rpm进行签名

  • 客户端在使用rpm为了验证其合法性, 可以使用redhat提供的公钥publickey进行签名检查

  • 方式1:指定公钥位置

[root@localhost ~]# vim /etc/yum.repos.d/CentOS-Base.repo
[base]
name=CentOS-$releasever - Base mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  • 方式2:提前导入公钥
[root@localhost ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@localhost ~]# vim /etc/yum.repos.d/CentOS-Base.repo
[base]
name=CentOS-$releasever - Base 
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
  • 方式3:不进行签名验证
不检查软件包的签名
[root@localhost ~]# yum install httpd --nogpgcheck
原文地址:https://www.cnblogs.com/guilai/p/11604661.html