# YUM 在线升级机制

yum 是通过分析 RPM 的标头资料后,根据各软件的相关性制作出属性相依时的解决方案,然后可以自动处理软件的相依属性问题,以解决软件安装或移除或升级的问题

由于 distribution 必须要先释出软件,然后将软件放置于 yum 服务器上,提供客户端的使用。因此我们想要使用 yum 的功能时,必须要先找到适合的 yum server 才行,而每个 yum server 可能都会提供许多不同的软件功能,这就是「软件库」。因此,你必须要前往 yum server 查询到相关的软件库网址后,再继续后续的设置

事实上,CentOS 在释出软件时已经制作出多个映像站(mirror site)提供全世界的软件更新使用。所以,理论上不需要处理任何设置,只要能够连上 internet ,就可以使用 yum

# 使用 yum 进行查询、安装、升级、与移除

# 查询功能:yum [list|info|search|provides|whatprovides] 参数

yum [option] [查询工作项目] [相关参数]

选项与参数:
[option]:主要选项有:
	-y:当 yum 要等待用户输入时,自动提供 yes 的响应
	--installroot=/some/path:将该软件安装在 /some/path 路径下,而不是使用默认的路径
[查询工作项目] [相关参数]:
	search:搜索某个软件名称或则是描述(description)的重要关键字
	list:列出目前 yum 所管理的所有的软件名称与版本,类似 rpm -qa
	info:同上,不过类似 rpm -qai 的执行结果
	provides:从文件去搜索软件,类似 rpm -qf 的功能
1
2
3
4
5
6
7
8
9
10
11
# 范例 1:搜索磁盘阵列 raid 相关软件有哪些?
[root@study ~]# yum search raid
Loaded plugins: fastestmirror, langpacks		# yum 系统自己找出最近的 yum server
Loading mirror speeds from cached hostfile	# 找出速度最快的哪一部 yum server
 * base: mirrors.huaweicloud.com						# 下面的三个软件库,且来源为该服务器
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
========================================= N/S matched: raid ==========================================
dmraid.i686 : dmraid (Device-mapper RAID tool and library)
dmraid.x86_64 : dmraid (Device-mapper RAID tool and library)
dmraid-devel.x86_64 : Development libraries and headers for dmraid.
dmraid-events-logwatch.x86_64 : dmraid logwatch-based email reporting
libblockdev-mdraid.i686 : The MD RAID plugin for the libblockdev library
libblockdev-mdraid.x86_64 : The MD RAID plugin for the libblockdev library
libblockdev-mdraid-devel.i686 : Development files for the libblockdev-mdraid plugin/library
libblockdev-mdraid-devel.x86_64 : Development files for the libblockdev-mdraid plugin/library
libstoragemgmt-megaraid-plugin.noarch : Files for LSI MegaRAID support for libstoragemgmt
dmraid-events.x86_64 : dmevent_tool (Device-mapper event tool) and DSO
iprutils.x86_64 : Utilities for the IBM Power Linux RAID adapters
mdadm.x86_64 : The mdadm program controls Linux md devices (software RAID arrays)

  Name and summary matches only, use "search all" for everything.
# 上述找到了好多软件,冒号「:」左边的是软件名称,右边的则是 RPM 内的 name 设置(软件名)

# 范例 2:找出 mdadm 软件的功能
[root@study ~]# yum info mdadm
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Installed Packages			# 这说明该软件已经安装到系统上了
Name        : mdadm			# 软件名
Arch        : x86_64		# 编译架构
Version     : 4.1				# 软件版本
Release     : 1.el7			# 释出的版本
Size        : 1.0 M			# 此软件的文件总大小
Repo        : installed	# 软件库回应说已安装的
From repo   : anaconda
Summary     : The mdadm program controls Linux md devices (software RAID arrays)
URL         : http://www.kernel.org/pub/linux/utils/raid/mdadm/
License     : GPLv2+
Description : The mdadm program is used to create, manage, and monitor Linux MD (software
            : RAID) devices.  As such, it provides similar functionality to the raidtools
            : package.  However, mdadm is a single program, and it can perform
            : almost all functions without a configuration file, though a configuration
            : file can be used to help with some common tasks.
# 其他的信息则自己翻译下吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 范例 3:列出 yum 服务器上提供的所有软件名称
[root@study ~]# yum list
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
Installed Packages			# 已安装的软件
GConf2.x86_64                                           3.2.6-8.el7                          @anaconda
GeoIP.x86_64                                            1.5.0-14.el7                         @anaconda
LibRaw.x86_64                                           0.19.2-1.el7                         @anaconda
...
Available Packages		# 还可以安装的其他软件
...

# 软件列表数据含义: 软件名称   版本  在哪个软件库



# 范例 4:列出目前服务器上可供本机进行升级的软件有哪些
[root@study ~]# yum list updates
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
base                                                                                                                   | 3.6 kB  00:00:00     
extras                                                                                                                 | 2.9 kB  00:00:00     
updates                                                                                                                | 2.9 kB  00:00:00     
Updated Packages
NetworkManager.x86_64                                                         1:1.18.0-5.el7_7.2                                       updates
NetworkManager-adsl.x86_64                                                    1:1.18.0-5.el7_7.2                                       updates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 范例 5:列出提供 passwd 这个文件的软件有哪些
[root@study ~]# yum provides passwd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
passwd-0.79-5.el7.x86_64 : An utility for setting or changing passwords using PAM
Repo        : base



passwd-0.79-5.el7.x86_64 : An utility for setting or changing passwords using PAM
Repo        : @anaconda
# 就是上面这个软件提供了 passwd 这个程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

通过上面的演示,大概知道 yum 如何用在查询上了,下面实际来应用一下

例题:利用 yum 的功能,找出以 pam 开头的软件名称有哪些,而其中尚未安装的有哪些

[root@study ~]# yum list pam*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Installed Packages		# 已安装的
pam.x86_64                                                             1.1.8-22.el7                                                  @anaconda
Available Packages		# 下面是可升级或未安装的,显示 updates 的话则是可升级的
pam.i686                                                               1.1.8-22.el7                                                  base     
pam-devel.i686                                                         1.1.8-22.el7                                                  base     
pam-devel.x86_64                                                       1.1.8-22.el7                                                  base     
pam_krb5.i686                                                          2.4.8-6.el7                                                   base     
pam_krb5.x86_64                                                        2.4.8-6.el7                                                   base     
pam_pkcs11.i686                                                        0.6.2-30.el7                                                  base     
pam_pkcs11.x86_64                                                      0.6.2-30.el7                                                  base     
pam_snapper.i686                                                       0.2.8-4.el7                                                   base     
pam_snapper.x86_64                                                     0.2.8-4.el7                                                   base     
pam_ssh_agent_auth.i686                                                0.10.3-2.21.el7                                               base     
pam_ssh_agent_auth.x86_64                                              0.10.3-2.21.el7                                               base
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 安装/升级功能:yum [install|update] 软件

yum [option] [安装与升级的工作项目] [相关参数]

install:接要安装的软件
update:接要升级的软件,若要整个系统都升级,直接 update
1
2
3
4
# 范例 1:将前一个练习找到的未安装的 pam-delvel 安装
[root@study ~]# yum install pam-devel
Loaded plugins: fastestmirror, langpacks  # 前 5 行找出最快的 yum server
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Resolving Dependencies		# 处理依赖软件
--> Running transaction check
---> Package pam-devel.x86_64 0:1.1.8-22.el7 will be installed
--> Finished Dependency Resolution
# 下表是一个总结性的显示
Dependencies Resolved

==============================================================================================================================
 Package                        Arch                        Version                           Repository                 Size
==============================================================================================================================
Installing:
 pam-devel                      x86_64                      1.1.8-22.el7                      base                      184 k

Transaction Summary
==============================================================================================================================
Install  1 Package		# 需要安装一个软件

Total download size: 184 k
Installed size: 528 k
Is this ok [y/d/N]: y
Downloading packages:
pam-devel-1.1.8-22.el7.x86_64.rpm                                                                      | 184 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : pam-devel-1.1.8-22.el7.x86_64                                                                              1/1 
  Verifying  : pam-devel-1.1.8-22.el7.x86_64                                                                              1/1 

Installed:
  pam-devel.x86_64 0:1.1.8-22.el7                                                                                             

Complete!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# 移除软件 remove

# 移除刚刚安装的软件
[root@study ~]# yum remove pam-devel
Loaded plugins: fastestmirror, langpacks
Resolving Dependencies
--> Running transaction check
---> Package pam-devel.x86_64 0:1.1.8-22.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================
 Package                       Arch                       Version                             Repository                 Size
==============================================================================================================================
Removing:
 pam-devel                     x86_64                     1.1.8-22.el7                        @base                     528 k

Transaction Summary
==============================================================================================================================
Remove  1 Package

Installed size: 528 k
Is this ok [y/N]: 
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : pam-devel-1.1.8-22.el7.x86_64                                                                              1/1 
  Verifying  : pam-devel-1.1.8-22.el7.x86_64                                                                              1/1 

Removed:
  pam-devel.x86_64 0:1.1.8-22.el7                                                                                             

Complete!
# 由于没有依赖软件,只移除一个软件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# yum 的配置文件

yum 由于有映像站软件库的 yum server,有可能会选择错误,所以可以手动修改 yum 的配置文件。

比如下面就有两个网址:

  • http://ftp.twaren.net/Linux/CentOS/7/
  • http://ftp.ksu.edu.tw/FTP/CentOS/7/

打开之后,有一堆的链接,这些链接就是这个 yum 服务器所提供的软件库了。

由于我们的测试机是 x86_64 的,点击一个 os 的软件库,再选择 x86_64 ,这个页面地址就是提供安装的网址: http://ftp.ksu.edu.tw/FTP/CentOS/7/os/x86_64/。 最重要的是 repodata http://ftp.ksu.edu.tw/FTP/CentOS/7/os/x86_64/repodata/ 这个目录,该目录是分析 RPM 软件后产生的软件属性相依属性,因此,当你找软件库所在网址时,最重要的是确定该网址下一定要有一个名为 repodata 的目录存在

[root@study ~]# 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
...
1
2
3
4
5
6
7
8

如上所述,有一个 base 软件库,个字段含义如下:

  • [bash]:达标软件库的名字,里面的名称可以随意取。但是需要名称唯一
  • name:只是说明这个软件库的意义
  • mirrorlist:该软件库可以使用的站点,如果不想用,可以注释掉
  • baseurl:最重要的配置,因为后面是软件库的实际地址。 mirrorlist 是由 yum 程序自动选择的站点,而 baseurl 则是指定固定的一个软件库网址,可以将刚刚找到的网址替换掉这里
  • enable=1:软件库被启用。设置为 0 则不启用
  • gpgcheck=1:RPM 的数字签名,是否需要查询 RPM 文件内的数字签名
  • gpgkey:数字签名的公钥文件所在位置,使用默认值即可

在这里我想起来了,笔者在安装某些软件的时候,需要特别安装他们的公钥文件,就是因为这里不同软件库释放出来的软件,公钥不一致的问题

下面修改为我们选择的高速软件仓库

[root@study ~]# vim /etc/yum.repos.d/CentOS-Base.repo
[base]
name=CentOS-$releasever - Base
baseurl=http://ftp.ksu.edu.tw/FTP/CentOS/7/os/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates 
[updates]
name=CentOS-$releasever - Updates
baseurl=http://ftp.ksu.edu.tw/FTP/CentOS/7/updates/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=http://ftp.ksu.edu.tw/FTP/CentOS/7/extras/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

# 默认情况下,只有这三个仓库被启用,这里只修改这三个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

下面来测试

# 范例 1: 列出目前 yum server 所使用的软件库有哪些
[root@study yum.repos.d]# yum repolist all
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
repo id                                                repo name                                                status
C7.0.1406-base/x86_64                                  CentOS-7.0.1406 - Base                                   disabled
C7.0.1406-centosplus/x86_64                            CentOS-7.0.1406 - CentOSPlus                             disabled
...
base                                                   CentOS-7 - Base                                          enabled: 10097
base-debuginfo/x86_64                                  CentOS-7 - Debuginfo                                     disabled
base-source/7                                          CentOS-7 - Base Sources                                  disabled
c7-media                                               CentOS-7 - Media                                         disabled
centosplus/7/x86_64                                    CentOS-7 - Plus                                          disabled
centosplus-source/7                                    CentOS-7 - Plus Sources                                  disabled
cr/7/x86_64                                            CentOS-7 - cr                                            disabled
extras                                                 CentOS-7 - Extras                                        enabled:   341
extras-source/7                                        CentOS-7 - Extras Sources                                disabled
fasttrack/7/x86_64                                     CentOS-7 - fasttrack                                     disabled
updates                                                CentOS-7 - Updates                                       enabled:  1787
updates-source/7                                       CentOS-7 - Updates Sources                               disabled
...

# 右边写着 enabled 的才是有激活的。
# 会发现有其他的软件库,是因为由多个配置文件的原因

# 可以看到,再次安装的时候,则会走固定的软件库了
[root@study yum.repos.d]# yum install pam-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
base                                                                                                   | 3.6 kB  00:00:00     
extras                                                                                                 | 2.9 kB  00:00:00     
updates                                                                                                | 2.9 kB  00:00:00 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 修改软件库产生的问题与解决之道

由于我们是修改的系统默认的配置文件,事实上,我们应该在 /etc/yum.repos.d/ 下新建一个扩展名为 .repo 的文件。

由于我们指定了特定的软件库,而不是其他软件开发商提供的软件库,因此才修改系统默认配置文件,但是可能由于使用的软件库版本有新旧之分,而 yum 会先下载软件库的清单到本机的 /var/cache/yum 中,我们只修改了网址,却没有修改软件库名称(中括号里面的名称),就有可能造成本机的列表与 yum 服务器的列表不同步,此时就会出现无法更新的问题了

出现这样的情况,只要清空本机缓存即可,可以手工,也可以通过 yum 的 clean 指令来

yum clean [packages|headers|all]

packages:将已下载的软件文件删除
headers:将下载的软件文件头删除
all:将所有软件库数据都删除
1
2
3
4
5
# 这里删除已下载过的所有软件库的相关数据(含软件本身与列表)
[root@study yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors

1
2
3
4
5
6

# 国内镜像站(仓库源)

一般使用国内的镜像站,可以选择上面两种方式中的其中一种,根据你自己的喜好选择

  • 网易镜像站:http://mirrors.163.com/.help/centos.html
  • 清华大学镜像站:https://mirrors.tuna.tsinghua.edu.cn/help/centos/

# yum 的软件群组功能

通过 yum 在线安装一个软件是很简单的,如果需要安装一个大型项目,比如测试机安装系统的时候,只选择了 GNOME 这个窗口管理员,此时想安装 KDE 呢,不需要重新安装,可以通过 yum 的软件群组功能

yum [群组功能] [软件群组]

选项与参数:
	grouplist:列出所有可使用的  软件群组组,例如 Development Tools 之类的
	groupinfo:后面接 group_name,则可了解该 group 内含的所有软件名
	groupinstall:可以安装一整组的软件群组,非常好用
	groupremove:移除某个软件群组
1
2
3
4
5
6
7
# 范例 1:查询目前软件库与本机上课用于安装过的软件群组有哪些?
[root@study yum.repos.d]# yum grouplist
Loaded plugins: fastestmirror, langpacks
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
# 如果有已安装过系统环境群组
# Installed environment groups:
#	xxxxx
Available Environment Groups:		# 还可以安装的系统环境软件群组
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
# 如果有已安装过的群组。则由下面这一项
# Installed groups:
#  xxx
Available Groups:			# 还可以安装的软件群组
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

上面的 Available Groups 中有一个 Scientific Support,想知道这个是什么,可以查看他的信息

[root@study yum.repos.d]# yum groupinfo "Scientific Support"
....
Group: Scientific Support
 Group-Id: scientific
 Description: Tools for mathematical and scientific computations, and parallel computing.
 Optional Packages:
   atlas
   fftw
   fftw-devel
....
1
2
3
4
5
6
7
8
9
10

描述中说了,提供 用于数学和科学计算的工具,以及并行计算的工具,下面则是有许多的软件,在安装该群组时,会被一起安装

# 安装这个群组
[root@study yum.repos.d]# yum groupinstall "Scientific Support"
Loaded plugins: fastestmirror, langpacks
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Warning: Group scientific does not have any packages to install.
Maybe run: yum groups mark install (see man yum)
No packages in any requested group available to install or update
1
2
3
4
5
6
7
8
9
10
11
12

正常情况下,会安装好各项软件,但是这个群组里面的软件都是可选择的 「Optional Packages:」(在查看 groupinfo 里面有这个项目),而不是主要的「mandatory」,所以默认情况下,该群组软件不会安装任何软件,你可以使用 yum install atlas fftw ... 一个一个写进去安装,如果想要 groupinstall 默认安装好所有的 optional 软件,就需要修改配置文件

[root@study ~]# vim /etc/yum.conf 
# 增加下面这一行的设置
group_package_types=default,mandatory,optional
1
2
3

# EPEL/ELRepo 外挂软件以及自定义配置文件

有很多时候,可能我们要使用的软件没有纳入 CentOS 的软件库内,就无法使用 yum 来进行安装,但是我们可以使用 Tarball 去编译,当然这个又变麻烦了,好在有网友提供了预先编译版本了

在Fedora 基金会里面发展了一个外加软件计划(Extra Packages for Enterprise Linux,EPEL),该计划主要针对 Red Hat Enterprise Linux 的版本来开发的,刚好 CentOS 也是针对 RHEL 的版本处理的,所以也能支持该软件库的相关软件相依环境了,主页地址为:https://fedoraproject.org/wiki/EPEL,CentOS 7 主要可以使用的软件仓库为 :https://dl.fedoraproject.org/pub/epel/7/x86_64/

除了上述的 Fedora 计划所提供的额外软件仓库外,社群里面也针对 CentOS 与 EPEL 的不足而提供的许多软件仓库,当时作者需要处理 PCI passthrough 虚拟化而用到的 ELRepo 软件仓库,下面是提供给 CentOS 7.x 的网址:

  • http://elrepo.org/tiki/tiki-index.php
  • http://elrepo.org/linux/elrepo/el7/x86_64
  • http://elrepo.org/linux/kernel/el7/x86_64

该软件库与其他软件库比较不同的地方在于该软件库提供的数据大多是核心、核心模块与虚拟化相关软件有关。例如 NVidia 的驱动程序也在里面,尤其提供了最小的核心(取名为 kernel-ml,就是 Linux 的核心)。

当然还有其他机构提供的软件仓库,这个就需要我们自己去寻找了。

那么我们怎么通过 EPEL 计划来安装 NetCDF 以及 R 软件呢?

  1. 先针对 epel 进行 yum 配置文件处理

    # 我们新建一个文件
    [root@study ~]# vim /etc/yum.repos.d/epel.repo
    [epel]
    name=epel packages
    baseurl=https://dl.fedoraproject.org/pub/epel/7/x86_64/
    gpgcheck=0
    enabled=0
    
    1
    2
    3
    4
    5
    6
    7

    这里默认是不启用该仓库的,只是未来有需要的时候才进行安装。

  2. 使用该仓库来进行安装 netcdf 和 R 软件

    [root@study ~]# yum --enablerepo=epel install netcdf R
    
    # 未来当你不加上 --enablerepo=epel 时,这个 EPEL 的软件并不会更新
    
    1
    2
    3

# 使用本机的原版光盘

比如你的主机没有网络,但是却有很多软件要安装,你的系统也都还没有任何升级的动作,这个时候,就可以使用本机的光盘作为软件来源。

首先,将你的光盘挂载到某个目录。这里假设挂载到 /mnt 了。如下编写配置文件

[root@study ~]# vim /etc/yum.repos.d/cdrom.repo
[mycdrom]
name=mycdrom
baseurl=file:///mnt
gpgcheck=0
enabled=0

[root@study ~]# yum --enablerepo=mycdrom install software_name
1
2
3
4
5
6
7
8

# 全系统自动升级

我们在安装软件的时候,都需要我们手动选择 yes/no 来决定是否升级,其实也可以使用 -y 参数自动应答 yum -y update,再配合 crontab 来达到系统自动升级软件的需求

[root@study ~]# echo '10 1 * * * root /usr/bin/yum -y --enablerepo=epel update' > /etc/cron.d/yumupdate
# 每天 1 点 10 分开始升级软件
1
2

# 管理的选择:RPM 还是 Tarball

这一直是一个有趣的问题:如果我要升级,或则是全新安装一个新的软件,选择 RPM 还是 Tarball 来安装?

这个问题考虑的因素有很多,作者通常是这样建议:

  1. 优先选择原厂的 RPM 功能

    由于原厂释出的软件通常具有一段时间的维护期。

    比如:RHEL 与 CetnOS 每一个版本至少提供 5 年以上的更新期限,这对于我们的系统安全性来说,是在是非常棒。加上 yum 自动升级,易于安装、移除、升级等特点

  2. 选择软件官网释出的 RPM 或则是提供的软件库

    软件太多,原厂并不会都包含,比如 CentOS 就没有提供 NTFS 的相关模块,此时你可以到官网去查询,看看有没有提供 RPM 文件,如果提供了软件库网址,就更方便了

  3. 利用 Tarball 安装特殊软件

    某些特殊用途的软件并不会特别帮你制作 RPM 文件的,此时也不建议你自行制作 SRPM 来转成 RPM,因为太耗时。如果你的确有很多主机需要安装,那么将源码制作成 RPM 就很有价值了。

  4. 用 Tarball 测试新版软件

    你可能想尝鲜或则使用新软件,但是原本厂商并没有提供新版的 RPM 包,此时你就可以使用 Tarball 安装到 /usr/local 下,而且大多数软件安装不同版本在系统上也不会有干扰。

# 基础服务管理:以 Apache 为例

在第 17 章谈到 systemd 的服务管理,那时仅使用 vsftpd 这个比较简单的服务来做示例。如今网络已经配置好,且有 yum 了,这里使用 www 服务的 httpd 来实践下

在默认的情况下,你无需修改服务的配置文件,通过默认值来处理你的服务即可,那么可以通过以下的流程来安装服务软件:

  1. 安装:yum install 你的软件
  2. 启动:systemctl start 你的软件
  3. 开机启动:systemctl enable 你的软件
  4. 防火墙 firewall-cmd --add-service="你的服务"; firewall-cmd --permanent --add-service="你的服务"
  5. 测试:用软件去查阅你的服务是否正常

下面进行实践,一般来说 www 网站服务器需要有 www 服务器软件 + 网页程序语言 + 数据库系统 + 程序语言与数据库的链接软件等。在 CentOS 上,我们需要的软件就有 httpd + php + mariadb-server + php-mysql

# 0. 先检查软件是否有安装
[root@study ~]# rpm -q httpd php mariadb-server php-mysql
package httpd is not installed
package php is not installed
mariadb-server-5.5.64-1.el7.x86_64  # 这个安装好了,其他三个没有安装
package php-mysql is not installed

# 1. 安装所需要的软件
[root@study ~]# yum install httpd php mariadb-server php-mysql 

# 2.3 启动与开机启动
[root@study ~]# systemctl daemon-reload 
[root@study ~]# systemctl start httpd         
[root@study ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@study ~]# systemctl status httpd
* httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-04-09 23:03:04 CST; 21s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 16641 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           |-16641 /usr/sbin/httpd -DFOREGROUND
           |-19479 /usr/sbin/httpd -DFOREGROUND
           |-19480 /usr/sbin/httpd -DFOREGROUND
           |-19481 /usr/sbin/httpd -DFOREGROUND
           |-19482 /usr/sbin/httpd -DFOREGROUND
           `-19483 /usr/sbin/httpd -DFOREGROUND

Apr 09 23:02:20 study.centos.mrcode systemd[1]: Starting The Apache HTTP Server...
Apr 09 23:03:04 study.centos.mrcode systemd[1]: Started The Apache HTTP Server.

# 4. 防火墙
[root@study ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3 enp0s8
  sources: 
  services: dhcpv6-client ssh			# 这里还没有 http 的服务
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
# 下面开始配置  
[root@study ~]# firewall-cmd --add-service="http"; 
success
[root@study ~]# firewall-cmd --permanent --add-service="http"
success
[root@study ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3 enp0s8
  sources: 
  services: dhcpv6-client http ssh		# 就出现在这里了
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

这个时候,在你的图形界面中浏览器中访问:http://localhost 就能看到一个网页了,也可以通过在宿主机中的浏览器访问虚拟机的 IP 地址,因为防火墙已经放行该服务了,通过 IP 也能访问到