[命令] CentOS Linux & RHEL 命令 yum (管理软件和软件源)

内容一: 帮助文档相关

1.1 显示 yum 帮助信息

# yum help

1.2 显示 yum 子选项的帮助信息

# yum help <option>

或者:

# yum <option> help

内容二:软件包相关

2.1 安装软件包

2.1.1 交互式安装软件包
# yum install <software>
2.1.2 非交互式安装软件包
# yum -y install <software>

或者:

# yum install <software> -y

2.2 删除软件软件包

2.2.1 交互式删除软件包
# yum remove <software>
2.2.2 非交互式删除软件包
# yum -y remove <software>

或者:

# yum remove <software> -y

2.3 升级软件包

2.3.1 升级软件包
2.3.1.1 交互式升级软件包
# yum update <software>
2.3.1.2 非交互式升级软件包
# yum -y update <software>

或者:

# yum update <software> -y
2.3.2 升级所有软件包
2.3.2.1 升级所有软件包
2.3.2.1.1 交互式升级所有软件包
# yum update
2.3.2.1.2 非交互式升级所有软件包
# yum -y update

或者:

# yum update -y
2.3.2.2 升级所有软件包时允许删除冲突软件
2.3.2.2.1 交互式升级所有软件包时允许删除冲突软件
# yum update --best --allowerasing
2.3.2.2.2 非交互式升级所有软件包时允许删除冲突软件
# yum -y update --best --allowerasing

或者:

# yum update --best --allowerasing -y
2.3.2.3 升级除某软件包外的所有软件包
2.3.2.3.1 交互式升级除某软件包外的所有软件包
# yum update --exclude=<software>
2.3.2.3.2 非交互式升级除某软件包外的所有软件包
# yum -y update --exclude=<software>

或者:

# yum update --exclude=<software> -y
2.3.2.4 使用某个配置文件升级所有软件包
2.3.2.4.1 交互式使用某个配置文件升级所有软件包
# yum ./yum.conf update

(补充:这里以使用 ./yum.conf 配置文件升级所有软件包为例)

2.3.2.4.2 非交互式使用某个配置文件升级所有软件包
# yum -y -c ./yum.conf update

(补充:这里以使用 ./yum.conf 配置文件升级所有软件包为例)

2.3.2.5 显示所有可以升级的软件包
# yum check-update
2.3.2.6 显示所有升级后需要重启才生效的软件包
# dnf needs-restarting --reboothint

2.4 显示软件包

2.4.1 显示软件包
2.4.1.1 显示软件包
# yum list <software>

或者:

# yum list all | grep <software>
2.4.1.2 显示软件包详细信息
# yum info <software>
2.4.1.3 显示软件包的所有可安装版本
# yum list <software> --showduplicate  | sort -r
2.4.1.4 显示软件包来自哪个模块
2.4.1.4.1 显示软件包来自哪个模块的格式
# yum module provides <software>
2.4.1.4.2 显示软件包来自哪个模块的案例
# yum module provides ipa-client
Updating Subscription Management repositories.
Last metadata expiration check: 1:38:12 ago on Fri 26 Jul 2024 08:00:39 AM CST.
ipa-client-4.7.1-11.module+el8+2842+7481110c.x86_64
Module   : idm:DL1:820190227212412:5986f621:x86_64
Profiles : client common
Repo     : rhel-8-for-x86_64-appstream-rpms
Summary  : The Red Hat Enterprise Linux Identity Management system module

......

(补充:从这里的输出结果可以看出 ipa-client 软件包来自 idm 模块,版本是 DL1)

2.4.2 显示所有软件包
# yum list all

内容三:软件组相关

3.1 安装软件组

3.1.1 交互式安装软件组
# yum groupinstall <group>
3.1.2 非交互式安装软件组
# yum -y groupinstall <group>

或者:

# yum groupinstall <group> -y

3.2 删除软件组

3.2.1 交互式删除软件组
# yum groupremove <group>
3.2.2 非交互式删除软件组
# yum -y groupremove <group>

或者:

# yum groupremove <group> -y

3.3 显示所有可用软件组

# yum group list

内容四:安全相关

4.1 Errata

4.1.1 Errata
4.1.1.1 安装 Errata
4.1.1.1.1 交互式安装 Errata
# yum update --advisory=<errata>
4.1.1.1.2 非交互式安装 Errata
# yum -y update --advisory=<errata>

或者:

# yum update --advisory=<errata> -y
4.1.1.2 显示 Errata 的详细信息
# yum updateinfo <errata>
4.1.2 所有 Errata
4.1.2.1 安装所有 Errata
4.1.2.1.1 交互式安装所有 Errata
# yum update-minimal --security
4.1.2.1.2 非交互式安装所有 Errata
# yum -y update-minimal --security

或者:

# yum update-minimal --security -y
4.1.2.2 显示所有 Errata 和相关软件版本
# yum updateinfo list
4.1.2.3 显示所有 Errata 和相关的 CVE
# yum updateinfo info security

4.2 CVE

4.2.1 CVE
4.2.1.1 安装 CVE
4.2.1.1.1 交互式安装 CVE
# yum update --cve <CVE>
4.2.1.1.2 非交互式安装 CVE
# yum -y update --cve <CVE>

或者:

# yum update --cve <CVE> -y
4.2.2 显示 CVE
4.2.2.1 显示所有 CVE 和相关软件版本
# yum updateinfo list cves
4.2.2.2 显示所有 CVE 和相关的 Errata
# yum updateinfo info security

4.2 安全补丁

4.2.1 安全补丁
4.2.1.1 安装安全补丁
4.2.1.1.1 交互式安装安全补丁
# yum update <security patch>
4.2.1.1.2 非交互式安装安全补丁
# yum -y update <security patch>

或者:

# yum update <security patch> -y
4.2.2 所有安全补丁
4.2.2.1 安装所有安全补丁
4.2.2.1.1 交互式安装所有安全补丁
# yum update --security
4.2.2.1.2 非交互式安装所有安全补丁
# yum -y update --security

或者:

# yum update --security -y
4.2.2.2 显示所有安全补丁
# yum check-update --security

内容五:软件源相关

5.1 显示所有软件源

# yum repolist 

5.2 缓存软件源列表

5.2.1 缓存所有已启用软件源列表
# yum repolist 
5.2.2 清除所有软件源列表缓存
# yum clean all
5.2.3 保存所有软件源列表缓存
# yum makecache

内容六:软件模块相关 (切换软件可用的版本)

6.1 软件模块相关的格式

6.1.1 显示软件模块
6.1.1.1 显示某个软件包来自某个软件模块
# yum module provides <software>
6.1.1.2 显示所有软件模块
# yum module list
6.1.1.3 显示某个软件模块
# yum module list | grep <software>
6.1.1.4 显示某个软件模块的详细信息
6.1.1.4.1 显示某个软件模块的详细信息
# yum module info <module>
6.1.1.4.2 显示某个软件模块使用的某个子软件模块是什么版本
# yum module info <module> | grep <submodule>
6.1.1.5 显示某个软件模块的某个模块流的详细信息
# dnf module info <module>:<stream>
6.1.2 重置软件模块
# yum module reset <module>
6.1.3 设置软件模块
# yum module enable <module>:<version>

6.2 切换软件模块的案例

6.2.1 检测当前软件兼容情况
# yum distro-sync

(注意:需要确保这条命令执行有以后没有报错)

6.2.2 显示要切换软件模块的软件包属于哪个软件模块
# yum module provides podman

(补充:这里以显示 podman 软件包属于哪个软件模块为例)

6.2.3 显示所有软件模块
# yum module list | grep podman
container-tools      rhel8 [d]       common [d]                               Most recent (rolling) versions of podman, buildah, skopeo, runc, conmon, runc, conmon, CRIU, Udica, etc as well as dependencies such as container-selinux built and tested together, and updated as frequently as every 12 weeks.
container-tools      1.0             common [d]                               Stable versions of podman 1.0, buildah 1.5, skopeo 0.1, runc, conmon, CRIU, Udica, etc as well as dependencies such as container-selinux built and tested together, and supported for 24 months.                                 
container-tools      2.0             common [d]                               Stable versions of podman 1.6, buildah 1.11, skopeo 0.1, runc, conmon, etc as well as dependencies such as container-selinux built and tested together, and supported as documented on the Application Stream lifecycle page.    
container-tools      3.0             common [d]                               Stable versions of podman 3.0, buildah 1.19, skopeo 1.2, runc, conmon, etc as well as dependencies such as container-selinux built and tested together, and supported as documented on the Application Stream lifecycle page.    
container-tools      4.0             common [d]                               Stable versions of podman 4.0, buildah 1.24, skopeo 1.6, runc, conmon, etc as well as dependencies such as container-selinux built and tested together, and supported as documented on the Application Stream lifecycle page.  

(补充:这里以显示所有和 podman 软件相关的软件模块为例)

6.2.4 设置软件模块
6.2.4.1 重置软件模块
# yum module reset container-tools

(补充:这里以重置 container-tools 软件模块为例)

6.2.4.2 设置软件模块
# yum module enable container-tools:3.0

(补充:这里以设置 container-tools 软件模块版本号为 3.0 为例)

或者:

# yum module enable container-tools:rhel8


补充:
1) 这里以设置最新的 container-tools 软件模块版本号为例
2) 当站主在测试这条命令时,最新的 container-tools 软件模块版本号刚好是 3.0

(注意:有些时候只能设置成最新的软件模块版本号后才能升级到最新的软件)

6.2.4.3 显示设置 container-tools 软件模块版本号为 3.0 时 podman 软件的可用版本
# yum list all | egrep ^podman\.x         
podman.x86_64                                           3.0.1-9.module+el8.6.0+14874+64436299                       rhel-8-for-x86_64-appstream-rpms    

(补充:可以看到此时 podman 软件的可用版本是 3.0.1-9)

6.2.5 重置软件模块
6.2.5.1 重置软件模块
# yum module reset container-tools

(补充:这里以重置 container-tools 软件模块为例)

6.2.5.2 显示 container-tools 软件模块没有设置时 podman 软件的可用版本
# yum list all | egrep ^podman\.x
podman.x86_64                                           2:4.0.2-6.module+el8.6.0+14877+f643d2d6                     rhel-8-for-x86_64-appstream-rpms

(补充:可以看到此时 podman 软件的可用版本是 2:4.0.2-6)

内容七:管理相关

7.1 显示 YUM 历史命令

# yum history

7.2 显示 YUM 安装记录

7.2.1 显示所有软件的 YUM 安装记录
# yum history info
7.2.2 显示某个软件的 YUM 安装记录
7.2.2.1 显示某个软件的 YUM 安装记录的格式
# yum history info <software>
7.2.2.2 显示某个软件的 YUM 安装记录的案例
# yum history info httpd

(补充:这里以查看 httpd 软件的安装记录为例)

7.3 操作回滚

# yum history
# yum history undo <ID>

(补充:上面第 1 条命令会实现操作的 ID 号,在第 2 条命令中写入那个 ID 号就可以取消那次操作)

[步骤] SFTP 日志的开启

# vim /etc/sshd/sshd_config

如果是 CentOS Linux & RHEL,将以下内容:

......
Subsystem       sftp    /usr/libexec/openssh/sftp-server
......

修改为:

......
Subsystem       sftp    /usr/libexec/openssh/sftp-server -l INFO
......

如果是 openSUSE & SLES, 将以下内容:

......
Subsystem       sftp    /usr/lib/ssh/sftp-server
......

修改为:

......
Subsystem       sftp    /usr/lib/ssh/sftp-server -l INFO
......

(补充:此时当通过 SFTP 登录系统时,系统日志记录文件 /var/log/messages 里登录记录后面会紧跟一行带 sftp-server 的记录)

[工具] Shell 批量比较服务器所有正在运行进程的变化

介绍

基本信息

作者:朱明宇
名称:批量比较服务器所有正在运行进程的变化
作用:批量比较服务器所有正在运行进程的变化

使用方法

1. 服务器清单 $server_list 每台服务器占用 1 行
2. 在此脚本的分割线内写入相应的内容,并和此脚本放在同一目录下
3. 给此脚本添加执行权限
4. 执行此脚本
5. 此脚本执行完成后,会将运行结果写入当前目录下的 $compare_file 里

脚本分割线里的变量

server_list=server_list.txt #服务器清单
first_time=first_time #存储第一次检结果的目录
second_time=second_time #存储第二次检查结果的目录
compare_file=comparison_results.txt #存储比较结果的文件

注意

此脚本执行前必须要先保证执行本脚本的用户能无密码 ssh 远程这些远程服务器

脚本

#!/bin/bash

####################### Separator ########################

server_list=server_list.txt
first_time=first_time
second_time=second_time
compare_file=comparison_results.txt

####################### Separator ########################

mkdir $first_time &> /dev/null
mkdir $second_time &> /dev/null
echo > $compare_file

read -p "Please input first second or compare now: " choice

check(){
        for server_name in `cat $1`
        do
                ssh -t $server_name "ps -A" | awk '{print $4}' > $2/$server_name
        done
}

compare(){
        for server_name in `cat $1`
        do
                echo $server_name >> $4
                for process in `cat $2/$server_name`
                        do
                        grep $process $3/$server_name &> /dev/null
                        if [ $? -ne 0 ];then
                                echo $process >> $4
                        fi
                done
                echo >> $4
        done
}

if [ $choice == first ];then
        check $server_list $first_time
fi

if [ $choice == second ];then
        check $server_list $second_time
fi

if [ $choice == compare ];then
        compare $server_list $first_time $second_time $compare_file
fi

盾牌和铠甲

在用经验拼凑的盾牌上
扎满了纷乱错综的谎言
在用梦想打造的铠甲上
布满了恶毒欺诈的伤痕

在战火之中的坚持
在孤独尽头的珍视
该相信什么是唯一的希望
该决定什么是生命绝对不应该如此

梦想之上的梦想
逆风而上的飞翔
判断之后的判断
逆光而向的洞察

过去无意间犯下的错误
就算会辗转反侧悔恨终身
也绝不会为了内心安稳而放弃的那份善念
将会成为人们在这颗星球上最后的盾牌

理想之上的理想
不会后悔的牺牲
选择之后的选择
不会后怕的勇敢

如今必须要去面对的那些邪恶
就算会因毫无安全感而变得焦躁不安
也绝不会为了内心安稳而抛弃的那份公正
将会成为人们在这颗星球上最后的铠甲

稍纵即逝的人生
生命的价值从来都不是曾存在于世
共同理想的创造
人类会因为拥有彼此而变得无所畏惧