CentOS的软件安装方式(一)
1 CentOS安装软件概述
CentOS安装软件的方式主要有源码安装、二进制安装和在线安装,在CentOS中,二进制安装为RPM安装,在线安装为yum安装。
这三者的特点如下:
(一)源码安装
源码包是指C等语言所开发的源代码文件的一个压缩包,通常压缩为.tar.gz或.tar.bz2。源码通常需要编译器将其编译成二进制文件,如gcc编译器。
源码安装的优点:
(1)开源,有能力者可以修改源代码;特别是要对某个软件,从头开发时,就特别需要对源码进行解读分析。
(2)可自由选择所需功能;
(3)软件通过编译安装,更加稳定、效率更高;
(4)卸载方便,直接删除目录即可。不了软件所依赖的环境及关系,也容易引发混乱,产生大量分散的垃圾。
(5)对于学习者而言,源码安装更有利于学习者发现软件环境和工具之间所依赖的关系。
源码安装的确定:
(1)安装过程步骤多,尤其是大型集合软件安装时,有着较多步骤,又多出现在集合软件构造不太成熟时,容易发生。需要自行解决工具间的依赖关系。
(2)某些软件编译过程耗费的时间较长,笔者曾发生过编译两三天的情况。尤其是在不合适的硬件上,安装大型软件时,最容易折磨人,甚至要考虑系统与硬件的兼容性、配置等问题,系统本身有缺陷,那就更要命了,有时需先修补系统bug【主要是功能不齐全,残缺情况居多】。
(3)大型软件编译,对新手来说较为麻烦,所报错误往往超过所能解决的范围,排错过程有可能会把系统给搞崩溃,笔者也干过这种傻事。新手多受几次挫之后,就得心应手了。
(4)某些软件有时只提供了源码安装方式,该种方式就成了必须要掌握的方式。
(5)源码安装时,有时需要区分源码未编译和源码已编译情况。
(二)rpm安装
RPM包:由Red Hat公司开发的包管理系统,软件的安装、升级、卸载都非常简单和方便。目前很多Linux发行版都在使用这种包管理机制,包括CentOS、Redhat、Fedora等。软件包文件后缀是".rpm"。
DPKG包:由Debian Linux开发出来的包管理机制,主要应用在Debian和Unbuntu系统中。软件包文件后缀是".deb"。
rpm安装,最要的优势是,已经解决了工具间的依赖关系。并不完全如此,有时候也需要自行配置系统某些环境,否则无法安装。
rpm安装的优点:
(1)包安装与管理简单,往往仅通过几个命令即可实现包的安装、升级、查询和卸载。
(2)通常安装速度要比源码安装快很多;
rpm安装的确定:
(1)经过编译,不再看到源码,修改和分析源码已太不可能【二进制还可反向破解,难度较大】。
(2)模块功能已经被定制,用户功能选择远不如源码灵活;
(3)有时也需要解决依耐性的问题,此时解决依耐性有一定的困难。
(三)yum安装
yum软件仓库是为了进一步简化RPM管理软件的难度以及自动分析所需软件包及其依赖关系的技术。yum的关键之处是要有可靠的respository,它能够从指定服务器自动下载rpm包并安装。yum能够自动解决软件依赖性问题,一次性安装所有依赖的软件包。
(四)参考资料
2 yum安装
2.1 yum源存储和命令规则
从yum源下载软件配置文件,存放在/etc/yum.repos.d/目录下,文件扩展名为".repo"。可进入该目录查看某个软件包的yum源配置文件。
yum源配置文件格式:
[base]:容器名称,一定要放在[]中
name:容器说明,随便写
mirrorlist:镜像站点,可以注释掉
baseurl:yum源服务器的地址(默认是CentOS官方的yum源服务器,可以更改)
enabled:此容器是否生效(enabled=1生效,enabled=0不生效)
gpgcheck:rpm数字证书是否生效(gpgcheck=1生效,gpgcheck=0不生效)
gpgkey:数字证书的公钥文件保存位置
2.2搭建本地光盘yum源
步骤1:将本地光盘挂载到指定位置
Step1【创建挂载点】:mkdir /mnt/cdrom
Step2【本地光盘挂载】:mount /dev/cdrom /mnt/cdrom
步骤2:将其他yum源文件失效【注:可以将yum源文件删除,但不推荐,通常做法先备份】
Step3【创建挂载点】:mkdir yum_bkup
Step4【本地光盘挂载】:mount *.repo yum_bkup
步骤3:创建本地yum源配置文件
Step5【创建本地yum源配置文件】:vim centos7.repo
[centos7]
name=centos7
baseurl=file:///mnt/cdrom
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
步骤4:测试
Step6【测试本地yum源有效性】:yum list available
2.3 yum命令
命令格式:yum -[y] COMMAND 软件包全名
选项:
-y:自动回答yes,不再询问。如果不加该选项,需手动加yes或no。
-q:静默模式,不显示安装过程。
-C:完全从缓存中运行,而不去下载或更新任何头文件。
COMMAND【所要进行的操作】:
install:执行安装操作
update:执行升级操作
remove:执行卸载操作
list:显示可用软件包列表
search:根据关键字匹配软件包
info:显示软件包信息
全部选项: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 Acts on the filesystem data of the host, mainly for removing docs/lanuages for minimal hosts.
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
langavailable Check available languages
langinfo List languages information
langinstall Install appropriate language packs for a language
langlist List installed languages
langremove Remove installed language packs for a language
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
2.3.1 yum安装命令
命令格式:yum -y install SoftwareName
2.3.2 yum升级命令
命令格式:yum -y install SoftwareName
升级所有软件:yum -y install
【在升级时,yum源服务器中软件包的版本通常要比本机安装的版本高】
2.3.3 yum卸载命令
命令格式:yum remove SoftwareName
2.3.4 yum查询命令
(一)方法一:yum list
方式1【查询某个软件包】:yum list SoftwareName
方式2【查询所有软件包】:yum list 或者 yum list | more
(二)方法二:yum search Keyword
查询yum源服务器上所有和关键词相关的软件包。
方式1【查询某个软件包】:yum search SoftwareName
(三)方法三:yum info Keyword
查询指定软件包的信息
方式1【查询某个软件包】:yum list SoftwareName
2.3.5 yum缓存命令
默认情况下,通过yum成功安装软件后,会把下载的软件包删除。
(1)如果要保留下载的软件包,可以编辑/etc/yum.conf,把里面的参数keepcache改为1(默认keepcache=0)。这样每次通过yum安装软件时,会把下载的软件包缓存在/var/cache/yum/目录下,下次安装相同软件时,就可以从缓存中直接获取软件包,在没有网络的情况下也可以正常安装。
(2)通常没有必要保留下载的软件包,若保留下载,则容易导致Linux系统臃肿,可考虑设置定期或额定空间大小清理【需写】。
清除缓存:yum clean packages|headers|oldheaders|all
packages:清除缓存目录下的软件包
headers:清除缓存目录下的headers
oldheaders:清除缓存目录下旧的headers
all:清除全部
重新建立缓存:yum makecache
只从缓存中查找软件包并安装:yum -C install 包名
2.3.6 yum软件组管理命令
软件组指的是一组相关的软件包(并不是指依赖关系),通过yum命令可对组内软件包批量管理。
yum grouplist:列出所有可用的软件组列表
yum groupinfo 软件组名:列出软件组包含的软件
yum groupinstall 软件组名:安装软件组
yum groupremove 软件组名:卸载软件组
3 rpm安装
3.1 rpm包命名规则
如mysql-community-server-8.0.31-1.el7.x86_64.rpm,分别是软件名、软件版本、软件发布次数、软件发行商、软件适用硬件平台、rpm包扩展名。
3.2 rpm包默认安装路径
rpm包默认安装路径:
/etc/:配置文件安装目录
/usr/bin/:可执行命令安装目录
/usr/lib/:程序所使用的函数库所存放的位置
/usr/share/doc/:软件适用说明或手册存放的位置
/usr/share/man/:帮助文件所存放的位置。
3.3 rpm安装命令
rpm操作命令参数如下:
| Query/Verify package selection options: | |
| -a, --all | query/verify all packages |
| -f, --file | query/verify package(s) owning file |
| -g, --group | query/verify package(s) in group |
| -p, --package | query/verify a package file |
| --pkgid | query/verify package(s) with package identifier |
| --hdrid | query/verify package(s) with header identifier |
| --triggeredby | query the package(s) triggered by the package |
| --whatrequires | query/verify the package(s) which require a dependency |
| --whatprovides | query/verify the package(s) which provide a dependency |
| --nomanifest | do not process non-package files as manifests |
| Query options (with -q or --query): | |
| -c, --configfiles | list all configuration files |
| -d, --docfiles | list all documentation files |
| -L, --licensefiles | list all license files |
| --dump | dump basic file information |
| -l, --list | list files in package |
| --queryformat=QUERYFORMAT | use the following query format |
| -s, --state | display the states of the listed files |
| Verify options (with -V or --verify): | |
| --nofiledigest | don't verify digest of files |
| --nofiles | don't verify files in package |
| --nodeps | don't verify package dependencies |
| --noscript | don't execute verify script(s) |
| Install/Upgrade/Erase options: | |
| --allfiles | install all files, even configurations which might otherwise be skipped |
| --allmatches | remove all packages which match <package> (normally an error is generated if <package> specified multiple packages) |
| --badreloc | relocate files in non-relocatable package |
| -e, --erase=<package>+ | erase (uninstall) package【卸载】 |
| --excludedocs | do not install documentation |
| --excludepath=<path> | skip files with leading component <path> |
| --force | short hand for --replacepkgs --replacefiles |
| -F, --freshen=<packagefile>+ | upgrade package(s) if already installed |
| -h, --hash | print hash marks as package installs (good with -v)【显示安装进度】 |
| --ignorearch | don't verify package architecture |
| --ignoreos | don't verify package operating system |
| --ignoresize | don't check disk space before installing |
| -i, --install | install package(s)【安装】 |
| --justdb | update the database, but do not modify the filesystem |
| --nodeps | do not verify package dependencies |
| --nofiledigest | don't verify digest of files |
| --nocontexts | don't install file security contexts |
| --noorder | do not reorder package installation to satisfy dependencies |
| --noscripts | do not execute package scriptlet(s) |
| --notriggers | do not execute any scriptlet(s) triggered by this package |
| --nocollections | do not perform any collection actions |
| --oldpackage | upgrade to an old version of the package (--force on upgrades does this automatically) |
| --percent | print percentages as package installs |
| --prefix=<dir> | relocate the package to <dir>, if relocatable |
| --relocate=<old>=<new> | relocate files from path <old> to <new> |
| --replacefiles | ignore file conflicts between packages【若安装路径存在某个同名文件,需要进行覆盖安装,谨慎操作】 |
| --replacepkgs | reinstall if the package is already present【重装软件中某个工具包】 |
| --test | don't install, but tell if it would work or not |
| -U, --upgrade=<packagefile>+ | upgrade package(s) |
| --reinstall=<packagefile>+ | reinstall package(s) |
| Common options for all rpm modes and executables: | |
| -D, --define='MACRO EXPR' | define MACRO with value EXPR |
| --undefine=MACRO | undefine MACRO |
| -E, --eval='EXPR' | print macro expansion of EXPR |
| --macros=<FILE:...> | read <FILE:...> instead of default file(s) |
| --noplugins | don't enable any plugins |
| --nodigest | don't verify package digest(s) |
| --nosignature | don't verify package signature(s) |
| --rcfile=<FILE:...> | read <FILE:...> instead of default file(s) |
| -r, --root=ROOT | use ROOT as top level directory (default: "/") |
| --dbpath=DIRECTORY | use database in DIRECTORY |
| --querytags | display known query tags |
| --showrc | display final rpmrc and macro configuration |
| --quiet | provide less detailed output |
| -v, --verbose | provide more detailed output【安装过程显示详细的信息】 |
| --version | print the version of rpm being used |
| Options implemented via popt alias/exec: | |
| --scripts | list install/erase scriptlets from package(s) |
| --setperms | set permissions of files in a package |
| --setugids | set user/group ownership of files in a package |
| --setcaps | set capabilities of files in a package |
| --restore | restore file/directory permissions |
| --conflicts | list capabilities this package conflicts with |
| --obsoletes | list other packages removed by installing this package |
| --provides | list capabilities that this package provides |
| --requires | list capabilities required by package(s) |
| --info | list descriptive information from package(s) |
| --changelog | list change logs for this package |
| --xml | list metadata in xml |
| --triggers | list trigger scriptlets from package(s) |
| --last | list package(s) by install time, most recent first |
| --dupes | list duplicated packages |
| --filesbypkg | list all files from each package |
| --fileclass | list file names with classes |
| --filecolor | list file names with colors |
| --fscontext | list file names with security context from file system |
| --fileprovide | list file names with provides |
| --filerequire | list file names with requires |
| --filecaps | list file names with POSIX1.e capabilities |
| Help options: | |
| -?, --help | Show this help message |
| --usage | Display brief usage message |
3.3.1 rpm包安装命令
命令格式:rpm -ivh 软件包全名
选项:
-i:安装【install】
-v:显示更详细的信息【verbose】
-h:打印并显示安装进度【hash】
-nodeps:不检测依耐性安装。默认会检查依耐性,若所依赖的工具还未安装,则会报错。若加上该选项,则不检测依耐性并强制安装。不推荐此种方式,大多软件无法使用。
-replacefiles:替换文件安装。若安装软件时,包中部分文件已经存在,正常安装时会报错,导致无法安装,使用该选项可忽视报错并覆盖文件。
-force:强制安装【-replacefiles和-replacepkgs的组合】
-test:测试安装【检查依耐性,不会实际安装】
-prefix:指定路径安装。不使用默认安装路径【指定安装路径,会使得系统找不到这些安装的软件,需要手工配置被系统识别】
3.3.2 rpm包升级命令
命令格式:rpm -Uvh 软件包全名
选项:
-U:升级安装【若没有安装,则直接安装;若安装版本过时,则升级到新版本】
3.3.3 rpm包卸载命令
命令格式:rpm -e 软件包全名
选项:
-e:卸载软件,可使用通配符。
-nodeps:卸载时不检查依耐性【若卸载的软件被其他软件所依赖,卸载失败,加上此选项则可以强制卸载,但可能会导致其他软件无法使用,谨慎操作】
3.3.4 rpm包查询命令
命令格式:rpm -q 软件包全名
(1)查询软件包是否安装:rpm -q SoftwareName
(2)查询系统所有安装的软件包:rpm -qa
常用命令:rpm -qa | grep SoftwareName
(3)查询软件包的详细信息:rpm -qi SoftwareName 【选项:-i 即查询软件信息】
(4)查询软件包的文件列表:
方式一【查询已安装软件的文件列表信息及安装目录】:rpm -ql SoftwareName 【选项:-i 即查询已安装软件信息】
方式二【查询未安装软件的文件列表信息及所要安装的位置】:rpm -qlp SoftwareName 【选项:-p 即查询未安装软件信息】
(5)查询系统文件属于哪个软件包:rpm -qf SystemFileName 【选项:-f即查询系统文件属于哪个软件包】
(6)查询软件包所依赖的软件包:
方式一【查询有依赖关系的软件包】:rpm -qR SoftwareName 【选项:-R即查询系统中和已经安装的软件包有依赖关系的软件包】
方式二【查询未安装的软件包的依赖性】:rpm -qRp SoftwareName 【选项:-p即查询软件未安装的软件包的依耐性,软件包为全名】
3.3.5 rpm包验证命令
命令格式:rpm -V 软件包全名
(1)校验本机已经安装的所有软件包:rpm -Va
(2)校验本已安装的指定软件包:rpm -V SoftwareName
(3)校验系统某个文件是否被修改:rpm -Vf SystemFileName
执行命令后,无任何提示,表明没有做任何修改;若执行命令后有内容输出,则软件的文件被修改过。
输入内容格式:验证内容 文件类型 文件路径
校验内容
-
- S:文件大小是否改变
- M:文件类型或文件权限(rwx)是否改变
- 5:文件MD5校验是否改变(文件内容是否改变)
- D:设备主从代码是否改变
- L:文件路径是否改变
- U:文件所有者是否改变
- G:文件所有组是否改变
- T:文件修改时间是否改变
文件类型
-
- c:配置文件(config file)
- d:普通文件(documentation)
- g:鬼文件(ghost file)
- l:授权文件(license file)
- r:描述文件(read me)
示例:
S.5...... /usr/lib64/libboost_filesystem-mt.so.1.53.0
S.5...... /usr/lib64/libboost_filesystem.so.1.53.0
....L.... /usr/lib64/libpytalloc-util.so.2
S.5...... /usr/lib64/python2.7/site-packages/talloc.so
S.5...... c /etc/aliases
S.5...... c /etc/csh.login
S.5...... c /etc/profile
..?...... c /etc/securetty
missing c /etc/libvirt/libvirtd.conf (Permission denied)
3.3.6 数字证书
(一)数字证书验证
rpm -V的校验方式只能对已经安装的RPM包中的文件进行校验。要在安装之前就对RPM进行校验需要使用数据证书验证:
(1)首先找到原厂的公钥文件,然后进行安装;
(2)安装RPM包时,去提取RPM包中的证书信息,然后和本机安装的原厂证书进行验证;
(3)如果验证通过,则允许安装;如果验证不通过,则不允许安装并警告。
(二)数字证书位置
(1)光盘查找:ll /mnt/RPM-GPG-KEY-CentOS-
(2)系统默认位置:ll /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-
(三)数字证书导入
使用命令:rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
4 源码安装
4.1 操作步骤
4.1.1 源码包下载和解压
4.1.2 编译准备:./configure
(1)安装前,检查系统环境是否符合安装要求;
(2)定义所需的功能选项。
(3)创建Makfile文件,并把系统环境的检测结果和定义好的功能选项写入,后续的编译和安装需要依赖这个文件的内容。
选项命令:
./configure --prefix=InstallPath 【指定所安装的路径】
./configure --help 【查询所支持的功能】
4.1.3 编译:make
make会调用gcc编译器,并读取Makefile文件中的信息进行系统软件编译。
编译的目的就是把源码程序转变为能被Linux识别的可执行文件,这些可执行文件保存在当前目录下,编译过程较为耗时。
4.1.4 编译清理:make clean
若./configure或make 编译报错,再重新编译前,一定先执行make clean命令,清空Makefile文件或编译产生的".o头文件"。
4.1.5 编译安装:make install
开始安装,安装前最好是指定安装路径。
4.1.6 软件卸载
(1)通过源码包安装时如果指定了安装目录,如 “./configure --prefix=安装路径”,卸载时只需删除整个安装目录就可以了 “rm -rf 安装路径”。
(2)如果源码包安装时没有指定安装目录,那么安装软件生成的文件会分别存储在 /usr/local/ 下的 bin、lib、etc等目录下,删除起来就比较麻烦一点。故建议源码包安装一定要指定安装目录,方便卸载。如果忘记指定安装目录,则可以把make install这个命令的执行过程保存下来,以备将来删除使用。
4.1.7软件环境配置
通常安装的软件,还需要手动配置软件在Linux环境,需将可执行程序的路径添加到:~/.bash_profile 或 ~/.bashrc。
4.1.8 注意事项
(1)执行 ./configure 命令时可能会提示缺少 gcc 等依赖程序,所以需使用 yum 等方式先安装依赖程序即可。
(2)不是所有的源码包安装一定是执行 “./configure”、“make”、“make install” 这三个命令,只有极少数源码包安装命令可能有些不同,比如 mysql 版本 5.5 以后的源码包安装时,使用 “cmake” 命令替代了 “./configure”。一般源码包解压后都有安装的说明文档,安装之前可先阅读以下说明文档。
(3)下载源码包时,通常会提供已经编译好的二进制包,这种二进制包里面包括了经过编译可马上运行的程序,所以解压后直接进入 “xxx/bin/” 目录找到相关命令直接运行即可。
浙公网安备 33010602011771号