文章目录

展开

yum管理工具

yum概述

什么是yum

yum 也是一种 rpm 包管理工具,相比于rpm命令,优势是可以自动解决依赖关系。
自动解决依赖关系前提条件,你的yum源中要有这些依赖包

什么是yum源

yum源:可以理解为手机中的应用商店
yum源其他名称:镜像站,yum仓库,rpm仓库

配置yum

# 使用阿里云的yum源
http://mirrors.aliyun.com

# 系统中需要的基础yum源
base源:和镜像中的Linux基础rpm包差不多
epel源:一些扩展安装包

# yum源的配置文件存放目录
/etc/yum.repos.d/

# 1.删除所有官方yum源
[root@web ~]# rm -fr /etc/yum.repos.d/*

# 2.安全方式 :打成压缩包 方便恢复
gzip -r /etc/yum.repos.d/

# 3.下载Base源
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

[root@web yum.repos.d]# ll
total 4
-rw-r--r-- 1 root root 2523 Aug  4  2022 CentOS-Base.repo

# 4.下载epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

[root@web yum.repos.d]# ll
total 8
-rw-r--r-- 1 root root 2523 Aug  4  2022 CentOS-Base.repo
-rw-r--r-- 1 root root  664 Aug  4  2022 epel.repo
## 注意:在/etc/yum.repos.d/目录下所有的yum源配置,必须以.repo结尾

## 安装nginx服务,使用nginx官方yum源
# 1.打开官网 http://nginx.org/
# 2.找nginx的yum仓库
# 3.创建一个vim /etc/yum.repos.d/nginx.repo 写入以下源码
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

# 清华源里的替换yum源的的命令↓
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo

1.阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区 (aliyun.com)
2.欢迎访问网易开源镜像站 (163.com)
3.清华大学开源软件镜像站 | Tsinghua Open Source Mirror
4.USTC Open Source Software Mirror
5.华为开源镜像站_软件开发服务_华为云 (huaweicloud.com)

yum命令实践

yum 查询

## list:查看yum仓库中的所有可以安装的rpm包
[root@web yum.repos.d]# yum list
    包名                                  版本和发布次数                   这包在哪个仓库
zziplib.x86_64                          0.13.62-12.el7                     base     
zziplib-devel.i686                      0.13.62-12.el7                     base     
zziplib-devel.x86_64                    0.13.62-12.el7                     base     
zziplib-utils.x86_64                    0.13.62-12.el7                     base 
......

## 过滤看看yum仓库中是否有wget包
[root@web yum.repos.d]# yum list|grep wget
wget.x86_64                              1.14-18.el7_6.1               @base 

## info:查看指定包的详细信息
[root@web yum.repos.d]# yum info wget
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
Name        : wget
Arch        : x86_64
Version     : 1.14
Release     : 18.el7_6.1
Size        : 2.0 M
Repo        : installed
From repo   : base
Summary     : A utility for retrieving files using the HTTP or FTP protocols
URL         : http://www.gnu.org/software/wget/
License     : GPLv3+
Description : GNU Wget is a file retrieval utility which can use either the HTTP or
            : FTP protocols. Wget features include the ability to work in the
            : background while you are logged out, recursive retrieval of
            : directories, file name wildcard matching, remote file timestamp
            : storage and comparison, use of Rest with FTP servers and Range with
            : HTTP servers to retrieve files over slow or unstable connections,
            : support for Proxy servers, and configurability.

## 查看yum仓库中,所有安装包的详细信息
yum info

## provides:根据命令查找包
yum provides ifconfig
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
# 该命令隶属于的包名↓
net-tools-2.0-0.25.20131004git.el7.x86_64 : Basic networking tools
Repo        : @base # 该命令隶属于的仓库
Matched from:
Filename    : /usr/sbin/ifconfig# 装完后,命令会在/usr/sbin下叫ifconfig

## provides:根据命令查找属于哪个安装包,最好写命令的绝对路径
yum provides */ifconfig
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
epel/x86_64/filelists_db                                   |  12 MB  00:00:02     
extras/7/x86_64/filelists_db                               | 303 kB  00:00:00     
updates/7/x86_64/filelists_db                              |  12 MB  00:00:01     
net-tools-2.0-0.25.20131004git.el7.x86_64 : Basic networking tools
Repo        : base
Matched from:
Filename    : /sbin/ifconfig

net-tools-2.0-0.25.20131004git.el7.x86_64 : Basic networking tools
Repo        : @base
Matched from:
Filename    : /sbin/ifconfig

yum安装

yum安装方式:
     - 本地安装
      yum localinstall -y 包名 (前提该包已经下载在服务器上了)
     - yum源安装
      yum install -y tree
     - 网站上安装
      yum install -y 下载地址

自动解决依赖关系的前提条件:在你的所有yum源中都要有该软件的依赖包

yum卸载

# 以下两种方式 选择一种即可
     yum erase 包名 -y
     yum remove 包名 -y

yum重装

yum reinstall -y 包名

## 作用:误删除了该服务相关的任何一个文件,使用reinstall都可以恢复,但是恢复的是最初始的配置,只恢复被删除的文件 ,其他文件不变,还是重装前的配置

## reinstall重装的方式,必须跟最开始安装这个包的方式保持一致
   - 本地安装:重装时就得进入到包设置目录或加上包的绝对路径 并加上完整包名
   - 从源里装:重装就只要包名
例:
[root@web ~]# yum install -y nginx
[root@web ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
[root@web ~]# cd /etc/nginx/
[root@web nginx]# ll
total 68
-rw-r--r-- 1 root root 1077 Nov 11  2022 fastcgi.conf
-rw-r--r-- 1 root root 1077 Nov 11  2022 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Nov 11  2022 fastcgi_params
-rw-r--r-- 1 root root 1007 Nov 11  2022 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Nov 11  2022 koi-utf
-rw-r--r-- 1 root root 2223 Nov 11  2022 koi-win
-rw-r--r-- 1 root root 5231 Nov 11  2022 mime.types
-rw-r--r-- 1 root root 5231 Nov 11  2022 mime.types.default
-rw-r--r-- 1 root root 2336 Nov 11  2022 nginx.conf
-rw-r--r-- 1 root root 2656 Nov 11  2022 nginx.conf.default
-rw-r--r-- 1 root root  636 Nov 11  2022 scgi_params
-rw-r--r-- 1 root root  636 Nov 11  2022 scgi_params.default
-rw-r--r-- 1 root root  664 Nov 11  2022 uwsgi_params
-rw-r--r-- 1 root root  664 Nov 11  2022 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Nov 11  2022 win-utf
[root@web nginx]# rm -fr nginx.conf
[root@web nginx]# ll
total 64
-rw-r--r-- 1 root root 1077 Nov 11  2022 fastcgi.conf
-rw-r--r-- 1 root root 1077 Nov 11  2022 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Nov 11  2022 fastcgi_params
-rw-r--r-- 1 root root 1007 Nov 11  2022 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Nov 11  2022 koi-utf
-rw-r--r-- 1 root root 2223 Nov 11  2022 koi-win
-rw-r--r-- 1 root root 5231 Nov 11  2022 mime.types
-rw-r--r-- 1 root root 5231 Nov 11  2022 mime.types.default
-rw-r--r-- 1 root root 2656 Nov 11  2022 nginx.conf.default
-rw-r--r-- 1 root root  636 Nov 11  2022 scgi_params
-rw-r--r-- 1 root root  636 Nov 11  2022 scgi_params.default
-rw-r--r-- 1 root root  664 Nov 11  2022 uwsgi_params
-rw-r--r-- 1 root root  664 Nov 11  2022 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Nov 11  2022 win-utf
[root@web nginx]# yum reinstall -y nginx
[root@web nginx]# ll
total 68
-rw-r--r-- 1 root root 1077 Nov 11  2022 fastcgi.conf
-rw-r--r-- 1 root root 1077 Nov 11  2022 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Nov 11  2022 fastcgi_params
-rw-r--r-- 1 root root 1007 Nov 11  2022 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Nov 11  2022 koi-utf
-rw-r--r-- 1 root root 2223 Nov 11  2022 koi-win
-rw-r--r-- 1 root root 5231 Nov 11  2022 mime.types
-rw-r--r-- 1 root root 5231 Nov 11  2022 mime.types.default
-rw-r--r-- 1 root root 2336 Nov 11  2022 nginx.conf
-rw-r--r-- 1 root root 2656 Nov 11  2022 nginx.conf.default
-rw-r--r-- 1 root root  636 Nov 11  2022 scgi_params
-rw-r--r-- 1 root root  636 Nov 11  2022 scgi_params.default
-rw-r--r-- 1 root root  664 Nov 11  2022 uwsgi_params
-rw-r--r-- 1 root root  664 Nov 11  2022 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Nov 11  2022 win-utf

yum更新

# 查看当前系统中,有哪些软件是可以更新的
[root@web nginx]# yum check-update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com

NetworkManager.x86_64                     1:1.18.8-2.el7_9        updates
NetworkManager-libnm.x86_64               1:1.18.8-2.el7_9        updates
NetworkManager-team.x86_64                1:1.18.8-2.el7_9        updates
......

# 更新指定的软件包
[root@web nginx]# yum update -y zlib.x86_64 0:1.2.7-21.el7_9

# 更新所有可更新的软件包
yum update -y
- 连系统本身都会升级(不建议)

yum缓存命令

## 清除所有的缓存
yum clean all

## 加载缓存
yum makecache
[root@web nginx]# yum makecache
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                              | 3.6 kB  00:00:00     
epel                                                              | 4.7 kB  00:00:00     
extras                                                            | 2.9 kB  00:00:00     
updates                                                           | 2.9 kB  00:00:00     
(1/5): epel/x86_64/prestodelta                                    | 1.1 kB  00:00:00     
(2/5): extras/7/x86_64/other_db                                   | 150 kB  00:00:00     
(3/5): updates/7/x86_64/other_db                                  | 1.4 MB  00:00:01     
(4/5): base/7/x86_64/other_db                                     | 2.6 MB  00:00:01     
(5/5): epel/x86_64/other_db                                       | 3.4 MB  00:00:01     
Metadata Cache Created

## 默认情况下,yum是不会下载rpm的只会安装
## 除非开启,下载的配置
vim /etc/yum.conf
将配置中的  keepcache=0  改为=1  linux中一般 0代表关闭 1代表开启

## yum下载的默认路径
ll /var/cache/yum/x86_64/7/

## 只下载,不安装,并指定目录
--downloadonly:仅下载,不安装
--downloaddir:指定下载的目录
例:
只下载nginx,不安装,并指定目录为/root
yum install nginx -y --downloadonly --downloaddir=/root

## yum clean packages :清除所有下载的包 但只会清除yum默认路径下rpm包 在其他目录的包不会被一起删除
[root@web ~]#  find  /var/cache/yum/ -type f -name '*.rpm'
/var/cache/yum/x86_64/7/base/packages/PyYAML-3.10-11.el7.x86_64.rpm
/var/cache/yum/x86_64/7/base/packages/libyaml-0.1.4-11.el7_0.x86_64.rpm
/var/cache/yum/x86_64/7/base/packages/python-babel-0.9.6-8.el7.noarch.rpm
......

[root@web ~]# yum clean packages
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates
22 package files removed

yum包组管理命令

# 查看有哪些包组可以安装
yum group list

# 安装包组
yum groups install 包组名字

# 卸载包组
yum groups remove 包组名字

yum历史命令

[root@web ~]# yum history
Loaded plugins: fastestmirror
命令的ID号  执行的命令                   执行的时间           动作           操作几个包
ID     | Login user               | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
    20 | root <root>              | 2023-07-08 19:50 | Install        |   22   
    19 | root <root>              | 2023-07-08 19:41 | Update         |    1   
    18 | root <root>              | 2023-07-08 19:36 | Reinstall      |    1   
    17 | root <root>              | 2023-07-08 19:35 | Install        |    1   
    16 | root <root>              | 2023-07-08 19:35 | Erase          |    1   
    15 | root <root>              | 2023-07-08 19:34 | Install        |    1   
    14 | root <root>              | 2023-07-08 19:34 | Reinstall      |    1   
    13 | root <root>              | 2023-07-08 19:32 | Reinstall      |    1   
    12 | root <root>              | 2023-07-08 19:20 | Install        |   11  <
    11 | root <root>              | 2023-07-06 16:21 | Erase          |    1 > 
    10 | root <root>              | 2023-07-06 16:10 | Install        |    5   
     9 | root <root>              | 2023-07-06 16:02 | Erase          |    1  <
     8 | root <root>              | 2023-07-06 16:00 | Erase          |    1 ><
     7 | root <root>              | 2023-07-06 15:58 | Erase          |    1 ><
     6 | root <root>              | 2023-07-06 15:58 | Erase          |    1 ><
     5 | root <root>              | 2023-07-06 15:57 | Erase          |    1 > 
     4 | root <root>              | 2023-07-06 14:39 | Install        |    1   
     3 | root <root>              | 2023-07-03 19:22 | Update         |    1   
     2 | root <root>              | 2023-04-11 17:09 | I, U           |   64   
     1 | System <unset>           | 2023-04-11 16:18 | Install        |  304   
history list

# 查看某个历史操作的详细信息
命令: yum history info ID号
[root@web ~]# yum history info 20
Loaded plugins: fastestmirror
Transaction ID : 20
Begin time     : Sat Jul  8 19:50:47 2023
Begin rpmdb    : 383:f87044a78f30db1c932054203bce614033b638f6
End time       :            19:50:55 2023 (8 seconds)
End rpmdb      : 405:faea1d0e20970e9103eeba24fb6f7cf20a969dc4
User           : root <root>
Return-Code    : Success
Command Line   : install -y ansible
Transaction performed with:
    Installed     rpm-4.11.3-45.el7.x86_64                        @anaconda
    Installed     yum-3.4.3-168.el7.centos.noarch                 @anaconda
    Installed     yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch @anaconda
Packages Altered:
    Dep-Install PyYAML-3.10-11.el7.x86_64                                @base
    Install     ansible-2.9.27-1.el7.noarch                              @epel
    Dep-Install libyaml-0.1.4-11.el7_0.x86_64                            @base
    Dep-Install python-babel-0.9.6-8.el7.noarch                          @base
    Dep-Install python-backports-1.0-8.el7.x86_64                        @base
    Dep-Install python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch @base
    Dep-Install python-cffi-1.6.0-5.el7.x86_64                           @base
    Dep-Install python-enum34-1.0.4-1.el7.noarch                         @base
    Dep-Install python-idna-2.4-1.el7.noarch                             @base
    Dep-Install python-ipaddress-1.0.16-2.el7.noarch                     @base
    Dep-Install python-jinja2-2.7.2-4.el7.noarch                         @base
    Dep-Install python-markupsafe-0.11-10.el7.x86_64                     @base
    Dep-Install python-paramiko-2.1.1-9.el7.noarch                       @base
    Dep-Install python-ply-3.4-11.el7.noarch                             @base
    Dep-Install python-pycparser-2.14-1.el7.noarch                       @base
    Dep-Install python-setuptools-0.9.8-7.el7.noarch                     @base
    Dep-Install python-six-1.9.0-2.el7.noarch                            @base
    Dep-Install python2-cryptography-1.7.2-2.el7.x86_64                  @base
    Dep-Install python2-httplib2-0.18.1-3.el7.noarch                     @epel
    Dep-Install python2-jmespath-0.9.4-2.el7.noarch                      @epel
    Dep-Install python2-pyasn1-0.1.9-7.el7.noarch                        @base
    Dep-Install sshpass-1.06-2.el7.x86_64                                @extras
history info

# 撤销历史操作:一般撤销更新操作
命令: yum history undo ID号
[root@web ~]# yum history undo 20

yum仓库命令

## 查看所有源中可用的yum仓库
[root@web ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
# 仓库名                 仓库描述                                       仓库状态:多少个包
repo id                 repo name                                            status
base/7/x86_64           CentOS-7 - Base - mirrors.aliyun.com                 10,072
epel/x86_64             Extra Packages for Enterprise Linux 7 - x86_64       13,753
extras/7/x86_64         CentOS-7 - Extras - mirrors.aliyun.com               518
updates/7/x86_64        CentOS-7 - Updates - mirrors.aliyun.com              5,061
repolist: 29,404 # 一共多少包

## 使用yum-config-manager
# 1.没有命令,要安装命令
[root@web ~]# yum install -y yum-config-manager
No package yum-config-manager available.
Error: Nothing to do # 报错 找不到包 

# 2.查询该命令属于哪个包
[root@web ~]# yum provides */yum-config-manager
yum-utils-1.1.31-54.el7_8.noarch : Utilities based around the yum package manager
#对应包名↑
Repo        : base
Matched from:
Filename    : /usr/bin/yum-config-manager

# 3.安装对应的rpm包
[root@web ~]# yum install -y yum-utils

## all查看所有的库↓
[root@web ~]# yum repolist all
                                                  # enabled:开启状态并显示这个库有多少包
base/7/x86_64         CentOS-7 - Base - mirrors.aliyun.com                  enabled: 10,072
                                                                # disabled :关闭状态
centosplus/7/x86_64   CentOS-7 - Plus - mirrors.aliyun.com                  disabled
contrib/7/x86_64      CentOS-7 - Contrib - mirrors.aliyun.com               disabled
epel/x86_64           Extra Packages for Enterprise Linux 7 - x86_64        enabled: 13,751
epel-debuginfo/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 - Debu disabled
epel-source           Extra Packages for Enterprise Linux 7 - x86_64 - Sour disabled
extras/7/x86_64       CentOS-7 - Extras - mirrors.aliyun.com                enabled:    509
updates/7/x86_64      CentOS-7 - Updates - mirrors.aliyun.com               enabled:  3,728
repolist: 28,060

## 修改yum源配置文件,开启或关闭仓库命令↓
# 1.开启命令
例↓
[root@web ~]# yum-config-manager --enable centosplus
Loaded plugins: fastestmirror #加载
[root@web ~]# yum repolist all
base/7/x86_64         CentOS-7 - Base - mirrors.aliyun.com                  enabled: 10,072
centosplus/7/x86_64   CentOS-7 - Plus - mirrors.aliyun.com                  enabled:    215
contrib/7/x86_64      CentOS-7 - Contrib - mirrors.aliyun.com               disabled
epel/x86_64           Extra Packages for Enterprise Linux 7 - x86_64        enabled: 13,751
epel-debuginfo/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 - Debu disabled
epel-source           Extra Packages for Enterprise Linux 7 - x86_64 - Sour disabled
extras/7/x86_64       CentOS-7 - Extras - mirrors.aliyun.com                enabled:    509
updates/7/x86_64      CentOS-7 - Updates - mirrors.aliyun.com               enabled:  3,728
repolist: 28,275 # 开启以后 重新加载这个库 状态变为开启 显示库里包的数量 总数也会增加

# 2.关闭命令
[root@web ~]# yum-config-manager --disable centosplus
Loaded plugins: fastestmirror 

YUM仓库配置文件

# 仓库名字
[base]
# 仓库的描述
name=CentOS-$releasever - Base - mirrors.aliyun.com
# 仓库的地址
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
# 仓库签名检查机制
gpgcheck=1
# 仓库开启/关闭 (1:开启,0:关闭,默认1)
enabled=1
# 公钥的地址
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

公钥:锁
私钥:钥匙