# Linux Yum 命令

#### 博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击[联系博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

yum（ Yellow dog Updater, Modified）是一个在 Fedora 和 RedHat 以及 SUSE 中的 Shell 前端软件包管理器。

基于 RPM 包管理，能够从指定的服务器自动下载 RPM 包并且安装，可以自动处理依赖性关系，并且一次安装所有依赖的软体包，无须繁琐地一次次下载、安装。

yum 提供了查找、安装、删除某一个、一组甚至全部软件包的命令，而且命令简洁而又好记。

## 一、Yum 语法

```
yum [options] [command] [package ...]
```

* **options：**&#x53EF;选，选项包括-h（帮助），-y（当安装过程提示选择全部为 "yes"），-q（不显示安装的过程）等等。
* **command：**&#x8981;进行的操作。
* **package：**&#x5B89;装的包名。

## 二、Yum常用命令

* 1\. 列出所有可更新的软件清单命令：`yum check-update`
* 2\. 更新所有软件命令：`yum update`
* 3\. 仅安装指定的软件命令：`yum install <package_name>`
* 4\. 仅更新指定的软件命令：`yum update <package_name>`
* 5\. 列出所有可安裝的软件清单命令：`yum list`
* 6\. 删除软件包命令：`yum remove <package_name>`
* 7\. 查找软件包命令：`yum search <keyword>`
* 8\. 清除缓存命令:
  * `yum clean packages`: 清除缓存目录下的软件包
  * `yum clean headers`: 清除缓存目录下的 headers
  * `yum clean oldheaders`: 清除缓存目录下旧的 headers
  * `yum clean, yum clean all` (= yum clean packages; yum clean oldheaders) :清除缓存目录下的软件包及旧的 headers

### 案例 1

安装 pam-devel

```
[root@www ~]# yum install pam-devel
Setting up Install Process
Parsing package install arguments
Resolving Dependencies  <==先检查软件的属性相依问题
--> Running transaction check
---> Package pam-devel.i386 0:0.99.6.2-4.el5 set to be updated
--> Processing Dependency: pam = 0.99.6.2-4.el5 for package: pam-devel
--> Running transaction check
---> Package pam.i386 0:0.99.6.2-4.el5 set to be updated
filelists.xml.gz          100% |=========================| 1.6 MB    00:05
filelists.xml.gz          100% |=========================| 138 kB    00:00
-> Finished Dependency Resolution
……(省略)
```

### 案例 2

移除 pam-devel

```
[root@www ~]# yum remove pam-devel
Setting up Remove Process
Resolving Dependencies  <==同样的，先解决属性相依的问题
--> Running transaction check
---> Package pam-devel.i386 0:0.99.6.2-4.el5 set to be erased
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================
 Package                 Arch       Version          Repository        Size
=============================================================================
Removing:
 pam-devel               i386       0.99.6.2-4.el5   installed         495 k

Transaction Summary
=============================================================================
Install      0 Package(s)
Update       0 Package(s)
Remove       1 Package(s)  <==还好，并没有属性相依的问题，单纯移除一个软件

Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Erasing   : pam-devel                    ######################### [1/1]

Removed: pam-devel.i386 0:0.99.6.2-4.el5
Complete!
```

### 案例 3

利用 yum 的功能，找出以 pam 为开头的软件名称有哪些？

```
[root@www ~]# yum list pam*
Installed Packages
pam.i386                  0.99.6.2-3.27.el5      installed
pam_ccreds.i386           3-5                    installed
pam_krb5.i386             2.2.14-1               installed
pam_passwdqc.i386         1.0.2-1.2.2            installed
pam_pkcs11.i386           0.5.3-23               installed
pam_smb.i386              1.1.7-7.2.1            installed
Available Packages <==底下则是『可升级』的或『未安装』的
pam.i386                  0.99.6.2-4.el5         base
pam-devel.i386            0.99.6.2-4.el5         base
pam_krb5.i386             2.2.14-10              base
```

## 三、国内 Yum 源

网易（163）yum源是国内最好的yum源之一 ，无论是速度还是软件版本，都非常的不错。

将yum源设置为163 yum，可以提升软件包安装和更新的速度，同时避免一些常见软件版本无法找到。

#### 安装步骤

首先备份/etc/yum.repos.d/CentOS-Base.repo

```
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
```

下载对应版本 repo 文件, 放入 /etc/yum.repos.d/ (操作前请做好相应备份)

* [CentOS5](http://mirrors.163.com/.help/CentOS5-Base-163.repo) ：<http://mirrors.163.com/.help/CentOS5-Base-163.repo>
* [CentOS6](http://mirrors.163.com/.help/CentOS6-Base-163.repo) ：<http://mirrors.163.com/.help/CentOS6-Base-163.repo>
* [CentOS7](http://mirrors.163.com/.help/CentOS7-Base-163.repo) ：<http://mirrors.163.com/.help/CentOS7-Base-163.repo>
* [CentOS8](http://mirrors.163.com/.help/CentOS8-Base-163.repo) ：<http://mirrors.163.com/.help/CentOS7-Base-163.repo>

```
wget http://mirrors.163.com/.help/CentOS8-Base-163.repo
mv CentOS8-Base-163.repo CentOS-Base.repo
```

运行以下命令生成缓存

```
yum clean all
yum makecache
```

* **中国开源镜像**
  * [阿里云开源镜像站](https://developer.aliyun.com/special/mirrors/notice)
  * [网易开源镜像站](https://mirrors.163.com/)
  * [搜狐开源镜像站](https://mirrors.sohu.com/)
  * [北京交通大学](https://mirror.bjtu.edu.cn/)
  * [兰州大学](https://mirror.lzu.edu.cn/)
  * [厦门大学](http://mirrors.xmu.edu.cn/)
  * [上海交通大学](https://ftp.sjtu.edu.cn/)
  * [清华大学](https://mirrors.tuna.tsinghua.edu.cn/)
  * [中国科学技术大学](https://mirrors.ustc.edu.cn/)
  * [东北大学](http://mirror.neu.edu.cn/)
  * [浙江大学](https://mirrors.zju.edu.cn/)

## **四、配置本地Yum仓库**

实现此案例需要按照如下步骤进行。

步骤一：搭建一个本地Yum，将RHEL6光盘手动挂载到/media

命令操作如下所示：

```
[root@hezhiqiang ~]# mount /dev/cdrom /media/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@hezhiqiang ~]# mount | tail -1
/dev/sr0 on /media type iso9660 (ro)
```

步骤二：将本地设置为客户端，进行Yum验证

Yum客户端需编辑配置文件，命令操作如下所示：

```
[root@hezhiqiang ~]# cd /etc/yum.repos.d/     # 必须在这个路径下
[root@hezhiqiang yum.repos.d]# ls             # 此路径下事先有配置文件的模板
rhel-source.repo

[root@hezhiqiang yum.repos.d]# cp rhel-source.repo rhel8.repo //配置文件必须以.repo结尾
[root@hezhiqiang yum.repos.d]# vim rhel8.repo
[rhel-8]                                  # 中括号里内容要求唯一，但不要出现特殊字符
name=Red Hat Enterprise Linux 6           # 此为描述信息，可以看情况填写
baseurl=file:///media/                    # 此项为yum软件仓库位置，指向光盘挂载点
enabled=1                                 # 此项为是否开启，1为开启, 0为不开启
gpgcheck=1                                # 此项为是否检查签名，1为检测, 0为不检测
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release  # 签名认证信息的路径

[root@hezhiqiang /]# yum repolist
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel-8                                            | 3.9 kB     00:00 ... 
rhel-8/primary_db                                  | 3.1 MB     00:00 ... 
repo id             repo name                                     status
rhel-8              Red Hat Enterprise Linux 8                    3,690
repolist: 3,690
```

对于 Linux 软件安装时提示缺失库的，可以使用 yum 的 provides 参数查看 libstdc++.so.6 的库文件包含在那个安装包中只需要执行：

```
yum provides libstdc++.so.6
```

然后按查询到安装包包名，使用 **yum install** 安装即可。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/about-the-author/linux-xi-tong-yun-wei-xue-xi-bi-ji/linux-yum-ming-ling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
