在说这个之前,我要先说下我对 yum 工具的认识,在我的认识中,yum 工具其实只是一个软件管理器,相当与 Windows 下的各种软件管家或者 Android 下的应用商店一样的东西而已,不过这个工具却不仅仅只是安装卸载应用软件这么简单,他还提供维持 linux 系统稳定的必须系统软件,包括内核也在它的管理范围之内。所以在 linux 下,这样的软件管理工具已经成为各发行版必备的东西。在 debian 阵营中他的名字叫 dpkg,在 ubuntu 中它的名字叫 apt,在 OpenSUSE 中,他的名字叫 zypper,在 Red Hat 中,它的名字叫 rpm,在 CentOS 中,它的名字就叫做 yum。
由于 CentOS 是属于 RedHat 阵营的,俗称盗版 Red Hat Enterprise(误),所以它具有 Red Hat 阵营的基本特性,也就是包括 rpm 在内的系统架构方式。因此会 CentOS 的同学,用其他 Red Hat 阵营的 linux 发行版都很容易适应。
其实 CentOS 6 到 CentOS 7 这两个版本之间产生了很多的变化,比如原来的 iptables 已不再使用,主打防火墙是 firewalld,在实际使用中还会体验到更多的明显变化。所以我们在学习 yum 之前,应该要先了解 CentOS。
查看 linux 内核的办法1
2# uname -a
Linux myServer 3.10.0-123.13.1.el7.x86_64 #1 SMP Tue Dec 9 23:06:09 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
这里边的 el7 其实就已经说明了现在使用的 rhel7 的内核了,也就是 CentOS 7,x86_64 说明是 64 位架构的系统。
如果不确定,其实还有更准确的办法。直接查看 release 文档1
2# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
或者 cat 别的都可以,不过他们本质上都是 centos-release 的映射1
2
3
4
5# ls -al /etc/*-release
-rw-r--r--. 1 root root 38 Feb 20 14:38 /etc/centos-release
-rw-r--r--. 1 root root 254 Feb 20 14:38 /etc/os-release
lrwxrwxrwx. 1 root root 14 Mar 2 05:41 /etc/redhat-release -> centos-release
lrwxrwxrwx. 1 root root 14 Mar 2 05:41 /etc/system-release -> centos-release
了解了系统的 CentOS 的版本之后,我们才保证正确的使用 yum 工具。在不知道 yum 工具怎么用的情况下,我们先看下 yum 工具的自带帮助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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90# yum --help
Loaded plugins: fastestmirror
Usage: yum [options] COMMAND
List of Commands:
check Check for problems in the rpmdb
check-update Check for available package updates
clean Remove cached data
deplist List a package's dependencies
distribution-synchronization Synchronize installed packages to the latest available versions
downgrade downgrade a package
erase Remove a package or packages from your system
fs Creates filesystem snapshots, or lists/deletes current snapshots.
fssnapshot Creates filesystem snapshots, or lists/deletes current snapshots.
groups Display, or use, the groups information
help Display a helpful usage message
history Display, or use, the transaction history
info Display details about a package or group of packages
install Install a package or packages on your system
list List a package or groups of packages
load-transaction load a saved transaction from filename
makecache Generate the metadata cache
provides Find what package provides the given value
reinstall reinstall a package
repo-pkgs Treat a repo. as a group of packages, so we can install/remove all of them
repolist Display the configured software repositories
search Search package details for the given string
shell Run an interactive yum shell
swap Simple way to swap packages, instead of using shell
update Update a package or packages on your system
update-minimal Works like upgrade, but goes to the'newest'package match which fixes a problem that affects your system
updateinfo Acts on repository update information
upgrade Update packages taking obsoletes into account
version Display a version for the machine and/or available repos.
Options:
-h, --help show this help message and exit
-t, --tolerant be tolerant of errors
-C, --cacheonly run entirely from system cache, don't update cache
-c [config file], --config=[config file]
config file location
-R [minutes], --randomwait=[minutes]
maximum command wait time
-d [debug level], --debuglevel=[debug level]
debugging output level
--showduplicates show duplicates, in repos, in list/search commands
-e [error level], --errorlevel=[error level]
error output level
--rpmverbosity=[debug level name]
debugging output level for rpm
-q, --quiet quiet operation
-v, --verbose verbose operation
-y, --assumeyes answer yes for all questions
--assumeno answer no for all questions
--version show Yum version and exit
--installroot=[path] set install root
--enablerepo=[repo] enable one or more repositories (wildcards allowed)
--disablerepo=[repo] disable one or more repositories (wildcards allowed)
-x [package], --exclude=[package]
exclude package(s) by name or glob
--disableexcludes=[repo]
disable exclude from main, for a repo or for
everything
--disableincludes=[repo]
disable includepkgs for a repo or for everything
--obsoletes enable obsoletes processing during updates
--noplugins disable Yum plugins
--nogpgcheck disable gpg signature checking
--disableplugin=[plugin]
disable plugins by name
--enableplugin=[plugin]
enable plugins by name
--skip-broken skip packages with depsolving problems
--color=COLOR control whether color is used
--releasever=RELEASEVER
set value of $releasever in yum config and repo files
--downloadonly don't update, just download
--downloaddir=DLDIR specifies an alternate directory to store packages
--setopt=SETOPTS set arbitrary config and repo options
--bugfix Include bugfix relevant packages, in updates
--security Include security relevant packages, in updates
--advisory=ADVS, --advisories=ADVS
Include packages needed to fix the given advisory, in
updates
--bzs=BZS Include packages needed to fix the given BZ, in
updates
--cves=CVES Include packages needed to fix the given CVE, in
updates
--sec-severity=SEVS, --secseverity=SEVS
Include security relevant packages matching the
severity, in updates
Plugin Options:
看的眼花缭乱的,不过这个只是一个前奏,下面来说下实际的应用吧,比如我要安装 apache2,那么可以直接1
yum install apache2
这个时候会弹出是否确认要安装的提示,如果你要安装的软件包很多的时候,所有的包都要一个一个按 y 来确认,这样会显的很麻烦,这个时候可以加个 - y 的参数,这样就可以默认情况下忽略提示直接安装了1
yum -y install apache2 libpcap libgcc python2 .....
也许有的时候我们会遇到这样一个情况,当我们需要一个工具的时候,直接打 yum install … 要弄好半天,有的时候提示找不到这个软件。这个时候会很烦,若是我想知道我要用的软件到底叫什么名字,或者想查看类似的软件列表,这个时候就要用到搜索功能了,没错就是软件搜索!
比如我要搜索 python1
yum search python
这个时候你就会很清晰的看到可以安装的软件的列表,接下来就可以根据你的需要来安装软件了!
其实,你也可以用 yum list | grep 想要的软件名,这样的形式来获取软件列表,比如1
yum list | grep python
效果都一样。
有的时候,要装一个软件,需要很多很多的包,比如桌面环境,这个时候一个一个包去敲会有点麻烦,yum 工具很人性化的提供了组安装的功能
也就是一次性的安装一组软件,你可以用这个命令查看当前可以装的软件组1
yum grouplist
这个时候你可以看到很多 yum 源里面准备好的软件组,比如我要装 GNOME 桌面环境的话,我只需要在类表里面找到 GNOME 这个组的名字。
我这的 GNOME 桌面环境组的名字直接就叫做 GNOME Desktop,于是我输入以下命令就可以直接安装这个组1
yum groupinstall GNOME Desktop
这样就完成了一次性安装整个软件组的操作了。
有的时候,可能会因为网络问题,导致有些软件安装失败,如果需要重新安装的话,建议先 clean 一次1
yum clean all
有的时候,我们想安装一些自定义的软件,由第三方提供了软件源。这个时候我们就需要修改 yum 的软件源。
yum 源的列表在 CentOS 7 中默认在这个位置1
# cd /etc/yum.repos.d/
这个目录下的文件都是名为. repo 的文件,这些文件就是源的地址列表1
2
3
4
5# ls -al
total 40
drwxr-xr-x. 2 root root 4096 Mar 31 08:39 .
drwxr-xr-x. 83 root root 4096 Mar 11 10:43 ..
-rw-r--r--. 1 root root 1528 Dec 16 21:27 CentOS-Base.repo
可以 vi 进去看看1
vi CentOS-Base.repo
一个标准的 repo 标签就是这样的1
2
3
4
5[base]
name=...
baseurl=...
gpgcheck=...
gpgkey=...
当需要修改的时候,就是改这里的文件了,改完之后需要 yum update 一下,才会把新的源的软件列表添加到本地软件列表里