Java教程

yum管理

本文主要是介绍yum管理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

yum管理工具

目录
  • yum管理工具
    • yum概述
    • 什么是yum源
    • 配置yum源方法
    • yum命令实践
      • yum 查询
    • yum安装
      • yum安装方式:
    • yum重装
    • yum更新
    • yum卸载
    • yum仓库指令
    • yum缓存命令
    • yum包组管理命令
    • yum历史命令
    • YUM仓库配置文件
    • 思维导图

yum概述

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


举例:
nginx 安装需要依赖 pcre-devel 、 openssl-devel

yum install -y nginx

什么是yum源

  • yum源:可以理解为手机中的应用商店

  • yum源其他名称:镜像站,yum仓库,rpm仓库


配置yum源方法

  • 以配置阿里云的yum源举例:

    http://mirrors.aliyun.com

  • 系统中需要的基础yum源

    • base源:所有系统的基础软件包,和镜像中的Linux基础rpm包差不多。

      • centos

    • epel源:一些扩展安装包

      • epel

  • yum源的配置文件存放目录

[root@localhost~]# ll /etc/yum.repos.d/
-rw-r--r-- 1 root root 1759 Jul 5 2021 CentOS-Base.repo
-rw-r--r-- 1 root root 664 Jul 5 2021 epel.repo
  • 删除所有官方yum源

    • 方式1:

      [root@localhost~]# rm -f /etc/yum.repos.d/*
      
    • 方式2:安全方式(推荐)

      打包备份官方的yum源:
      [root@localhost~]# gzip -r /etc/yum.repos.d/
      
  • 下载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

## 查看配置完的base源
[root@localhost~]# ll /etc/yum.repos.d/
total 8
-rw-r--r-- 1 root root 2523 May  2 05:08 CentOS-Base.repo

  • 下载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

## 这时候因为有base源和epel源 可以看到两个配置文件
[root@localhost~]# ll /etc/yum.repos.d/
total 8
-rw-r--r-- 1 root root 2523 May  2 05:08 CentOS-Base.repo
-rw-r--r-- 1 root root  664 Dec 26  2020 epel.repo


## 注意:在/etc/yum.repos.d/目录下所有的yum源配置,必须以.repo结尾

  • 示范安装nginx服务,使用nginx官方yum源
## 安装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
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

yum命令实践

yum 查询

  • **查看yum仓库中的所有可以安装的rpm包 **
    • yum list 
## 查看yum仓库中的所有可以安装的rpm包    yum list
[root@localhost~]# yum list

## 过滤看看yum仓库中是否有zip包
[root@localhost~]# yum list|grep '^zip'
zip.x86_64                               3.0-11.el7                    installed
####(包名)                            (版本和发布次数)             (这包在哪个仓库中)

  • 查看安装包的详细信息
    • yum info
## 查看yum仓库中,所有安装包的详细信息  yum info
[root@localhost~]# yum info 

## 查看指定包的详细信息
[root@localhost~]# yum info zip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
Name        : zip
Arch        : x86_64
Version     : 3.0
Release     : 11.el7
Size        : 796 k
Repo        : installed
Summary     : A file compression and packaging utility compatible with PKZIP
URL         : http://www.info-zip.org/Zip.html
License     : BSD
Description : The zip program is a compression and file packaging utility.  Zip is
            : analogous to a combination of the UNIX tar and compress commands and
            : is compatible with PKZIP (a compression and file packaging utility for
            : MS-DOS systems).
            : 
            : Install the zip package if you need to compress files using the zip
            : program.

  • 根据命令查找包
    • yum provides
    • 最好使用命令的绝对路径
## 根据命令查找包 yum provides 

[root@localhost~]# yum provides tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
tree-1.6.0-10.el7.x86_64 : File system tree viewer
Repo        : base



tree-1.6.0-10.el7.x86_64 : File system tree viewer
Repo        : @base

## 最好使用命令的绝对路径
[root@localhost~]# yum provides */tree
### 该命令隶属于的包名
tree-1.6.0-10.el7.x86_64 : File system tree viewer
### 该命令隶属于的仓库
Repo        : @base
### 装完后,命令会在/usr/sbin下叫tree
Matched from:
Filename    : /usr/bin/tree

yum安装

yum安装方式:

  • 本地安装

    • yum localinstall -y 包名 (前提该包已经下载在服务器上了)
  • yum源安装

    • yum install -y tree
  • 网站上安装

    • yum install -y http://test.driverzeng.com/Nginx_package/nginx-1.12.2-
      3.el7.x86_64.rpm
  • 自动解决依赖关系的前提条件:在你的所有yum源中都要有该软件的依赖包

yum重装

  • yum reinstall -y 包名
作用:误删除了该服务相关的任何一个文件,使用reinstall都可以恢复,但是恢复的是最初始的配置

reinstall的方式,必须跟最开始安装这个包的方式保持一致
即:最开始安装的时候是本地rpm包安装,那么重装的时候一定要加上本地包的绝对路径或者cd到包的位置下去重装
如果一开始是yum源安装的  那么重装的时候就加上包名就可以了

示例1:
[root@localhost~]# yum reinstall -y /mnt/Packages/zip-3.0-11.el7.x86_64.rpm 


示例2:
[root@localhost~]# yum reinstall zip -y

yum更新

  • 查看当前系统中,有哪些软件是可以更新的

yum check-update

[root@localhost~]# yum check-update
  • 更新指定的软件包

    yum update -y 包名

    [root@localhost~]# yum update -y curl
    
  • 更新所有可更新的软件包

    • 升级所有包同时也升级软件和系统内核(命令使用要谨慎)

      yum update -y

yum卸载

  • yum erase 包名 -y

  • yum remove 包名 -y

[root@localhost~]# yum remove -y tree

[root@localhost~]# yum erase -y tree

yum仓库指令

  • 查看所有源中可用的yum仓库
    • yum repolist
[root@localhost~]# 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,751
extras/7/x86_64      CentOS-7 - Extras - mirrors.aliyun.com                   509
updates/7/x86_64     CentOS-7 - Updates - mirrors.aliyun.com                 3,728
repolist: 28,060

  • 查看所有源中,所有的yum仓库

    • yum repolist all 显示所有资源库

    • yum repolist enabled # 显示所有已启动的资源库

    • yum repolist disabled # 显示所有被禁用的资源库

[root@localhost~]# yum repolist all

仓库名(包)                    仓库描述                                    仓库状态:多少个
repo id                       repo name                                     status
base/7/x86_64         CentOS-7 - Base - mirrors.aliyun.com            enabled: 10,072
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 - Debug disabled
epel-source           Extra Packages for Enterprise Linux 7 - x86_64 - Sourcedisabled
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-config-manager

    • 通过yum-config-manager命令对资源库进行管理

    • yum-config-manager命令的本质是对 /etc/yum.repos.d/(库数据的储存位置)文件夹下文件的增删查改时,推荐使用yum-config-manager命令进行改动


    • 1.没有命令,要安装命令 yum-config-manager

      [root@localhost~]# yum install -y yum-config-manager
      Loaded plugins: fastestmirror
      Loading mirror speeds from cached hostfile
       * base: mirrors.aliyun.com
       * extras: mirrors.aliyun.com
       * updates: mirrors.aliyun.com
      No package yum-config-manager available.
      Error: Nothing to do
      
      
    • 2.查询该命令属于哪个包?

      [root@localhost~]# yum provides */yum-config-manager
      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:19     
      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
      
      
    • 安装对应的rpm包

      [root@localhost~]# yum install -y yum-utils
      
  • 修改yum源配置文件,开启或关闭仓库

    • 开启

      [root@localhost~]# yum-config-manager --enable nginx-mainline
      
    • 关闭

      [root@localhost~]# yum-config-manager --disable nginx-mainline
      

yum缓存命令

  • 清除所有的缓存

    yum clean all

  • 加载缓存

    yum makecache

  • 默认情况下,yum是不会下载rpm的只会安装

  • 除非开启,下载的配置

    • vim /etc/yum.conf
    [root@localhost~]# vim /etc/yum.conf
    
      1 [main]                                                                                                                                                                                                       
      2 cachedir=/var/cache/yum/$basearch/$releasever     
      3 keepcache=0                                       # 把0改成1就是开启下载rpm
      4 debuglevel=2
      5 logfile=/var/log/yum.log
      6 exactarch=1
      7 obsoletes=1
      8 gpgcheck=1
      9 plugins=1
     10 installonly_limit=5
     11 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
     12 distroverpkg=centos-release
    
    
    • yum下载rpm包的默认路径
      • /var/cache/yum/x86_64/7
    [root@localhost~]# ll /var/cache/yum/x86_64/7
    total 16
    drwxr-xr-x. 4 root root 4096 May  3 03:37 base
    drwxr-xr-x  4 root root   33 May  3 04:53 centosplus
    drwxr-xr-x  4 root root   33 May  3 04:53 contrib
    drwxr-xr-x  4 root root 4096 May  3 05:59 epel
    drwxr-xr-x  4 root root   33 May  3 04:53 epel-debuginfo
    drwxr-xr-x  4 root root   33 May  3 04:53 epel-source
    drwxr-xr-x. 4 root root  276 May  3 03:37 extras
    -rw-r--r--  1 root root  118 May  3 06:02 timedhosts
    -rw-r--r--. 1 root root  536 Apr 30 20:40 timedhosts.txt
    drwxr-xr-x. 4 root root  276 May  3 03:37 updates
    
    
  • --downloadonly:仅下载,不安装
    --downloaddir:指定下载的目录

## 下载tree,不安装,并指定目录
[root@localhost~]# yum install tree -y --downloadonly --downloaddir=/bao

提示tree的这个包已经安装,并且是最新的版本,所以上面这个命令对已经装过的命令且最新版本是无法下载包的
Package tree-1.6.0-10.el7.x86_64 already installed and latest version
Nothing to do
 
  • 只会清除默认路径下的rpm包

    yum clean packages

[root@localhost~]# find /var/cache/yum/ -type f -name '*.rpm'
/var/cache/yum/x86_64/7/base/packages/tree-1.6.0-10.el7.x86_64.rpm

[root@localhost~]# yum clean packages
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates
1 package file removed

[root@localhost~]# ll /var/cache/yum/x86_64/7/
total 16
drwxr-xr-x. 4 root root 4096 May  3 17:53 base
drwxr-xr-x  4 root root   33 May  3 04:53 centosplus
drwxr-xr-x  4 root root   33 May  3 04:53 contrib
drwxr-xr-x  4 root root 4096 May  3 17:53 epel
drwxr-xr-x  4 root root   33 May  3 04:53 epel-debuginfo
drwxr-xr-x  4 root root   33 May  3 04:53 epel-source
drwxr-xr-x. 4 root root  276 May  3 17:53 extras
-rw-r--r--  1 root root  118 May  3 17:53 timedhosts
-rw-r--r--. 1 root root  536 Apr 30 20:40 timedhosts.txt
drwxr-xr-x. 4 root root  276 May  3 17:53 updates
[root@localhost~]# find /var/cache/yum/ -type f -name '*.rpm'

yum包组管理命令

  • 查看有哪些包组可以安装

    yum group list

[root@localhost~]# yum group list

[root@localhost~]# yum group list
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com

Available Environment Groups:        可用环境组:(代表已经安装的)
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Cinnamon Desktop
   MATE Desktop
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation

Available Groups:                   可用组    (表示还未安装的)
   Cinnamon
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Educational Software
   Electronic Lab
   Fedora Packager
   General Purpose Desktop
   Graphical Administration Tools
   Haskell
   LXQt Desktop
   Legacy UNIX Compatibility
   MATE
   Milkymist
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
   TurboGears application framework
   Xfce
Done

  • 安装包组

    • yum groups install 包组名字
    [root@localhost~]# yum groups install Security Tools
    
  • 卸载包组

    • yum groups remove
    [root@localhost~]# yum groups remove Security Tools
    

yum历史命令

  • 查看yum的历史操作

    yum history

[root@localhost~]# yum history
Loaded plugins: fastestmirror

命令的ID   执行的命令                  执行的时间           动作             操作几个包
ID     | Command line             | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
    21 | install tree -y          | 2022-05-03 17:53 | Install        |    1   
    20 | remove -y tree           | 2022-05-03 17:53 | Erase          |    1   
    19 | install -y yum-utils     | 2022-05-03 06:02 | I, U           |    5   
    18 | install tree             | 2022-05-03 04:31 | Install        |    1   
    17 | erase -y tree            | 2022-05-03 04:28 | Erase          |    1   
    16 | install -y tree          | 2022-05-03 04:26 | Install        |    1   
    15 | remove -y tree           | 2022-05-03 04:26 | Erase          |    1  
  • 查看某个历史操作的详细信息

    yum history info ID

[root@localhost~]# yum history info 21
Loaded plugins: fastestmirror
Transaction ID : 21
Begin time     : Tue May  3 17:53:47 2022
Begin rpmdb    : 362:4242d484cb81e7f6f42957fb55301773c20da71f
End time       :                          (0 seconds)
End rpmdb      : 363:51b155e4738e35ba20da6e1c6ae711d050b65444
User           : root <root>
Return-Code    : Success
Command Line   : install tree -y
Transaction performed with:
    Installed     rpm-4.11.3-35.el7.x86_64                      @anaconda
    Installed     yum-3.4.3-161.el7.centos.noarch               @anaconda
    Installed     yum-plugin-fastestmirror-1.1.31-50.el7.noarch @anaconda
Packages Altered:
    Install tree-1.6.0-10.el7.x86_64 @base
history info

  • 撤销历史操作:一般撤销更新操作

    yum history undo ID

[root@localhost~]# yum history undo 20

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



公钥:锁
私钥:钥匙

思维导图

这篇关于yum管理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!