2023-06-01    2024-01-10    3065 字  7 分钟

当我们需要在CentOS系统上安装软件时,通常可以通过RPMyum和源码安装的几种方式安装。

RPM

RPMRedhat Package Manager 的缩写,由红帽开发用于软件包的安装升级卸载与查询的工具。 rpm常用选项及说明如下表:

选项 说明
i 安装软件包。
v 显示安装的详细信息。
h 显示安装的进度。
U 升级软件包
e 卸载软件包
q 查看版本信息
qa 查看所有已安装的软件包
ql 查看软件所有的文件所在的路径
qc 查看软件配置文件所在的路径
qf 查看系统已安装的命令属于哪个软件包(需要用文件的绝对路径)

安装

rpm安装本地的软件包。

1
2
3
4
5
6
[root@localhost ~]# mount /dev/cdrom  /mnt/
mount: /dev/sr0 is write-protected, mounting read-only 
[root@localhost ~]# rpm -ivh /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.6.0-10.el7                ################################# [100%]

安装网络的软件包。

1
2
3
4
5
[root@localhost ~]# rpm -ivh https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/Packages/vsftpd-3.0.2-25.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/Packages/vsftpd-3.0.2-25.el7.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:vsftpd-3.0.2-25.el7              ################################# [100%]

升级

rpm安装软件。

1
2
3
4
5
6
[root@localhost ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.9-1.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.9-1.el7.x86_64.rpm
warning: /var/tmp/rpm-tmp.7LX5Zg: Header V4 DSA/SHA1 Signature, key ID 79ea5ed4: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-agent-3.0.9-1.el7         ################################# [100%]

查看软件包的版本号。

1
2
[root@localhost ~]# rpm -q zabbix-agent
zabbix-agent-3.0.9-1.el7.x86_64

通过-U选项对软件包升级。

1
2
3
4
5
6
7
8
[root@localhost ~]# rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.5-1.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.5-1.el7.x86_64.rpm
warning: /var/tmp/rpm-tmp.8ZlRib: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-agent-4.2.5-1.el7         ################################# [ 50%]
Cleaning up / removing...
   2:zabbix-agent-3.0.9-1.el7         ################################# [100%]

通过软件包版本号查看是否升级成功。

1
2
[root@localhost ~]# rpm -q zabbix-agent
zabbix-agent-4.2.5-1.el7.x86_64

卸载

rpm卸载软件包。

1
2
3
4
5
# 通过-e选项卸载软件包
[root@localhost ~]# rpm -e zabbix-agent
# 查看软件包是否存在
[root@localhost ~]# rpm -q zabbix-agent
package zabbix-agent is not installed

查询

rpm查询相关。

列出系统安装的所有软件包。

1
2
3
[root@localhost ~]# rpm -qa
byacc-1.9.20130304-3.el7.x86_64
libproxy-0.4.11-11.el7.x86_64

通常会配合grep进行模糊查询。

1
2
[root@localhost ~]# rpm -qa | grep ftp
vsftpd-3.0.2-25.el7.x86_64

查看软件包所有文件。

1
2
3
4
[root@localhost ~]# rpm -ql vsftpd
/etc/logrotate.d/vsftpd
/etc/pam.d/vsftpd
/etc/vsftpd

查看软件包的配置文件的路径。

1
2
3
4
5
6
[root@localhost ~]# rpm -qc vsftpd
/etc/logrotate.d/vsftpd
/etc/pam.d/vsftpd
/etc/vsftpd/ftpusers
/etc/vsftpd/user_list
/etc/vsftpd/vsftpd.conf

查询已经安装的命令属于哪个软件包。

1
2
3
4
5
# 查询的命令必须为绝对路径
[root@localhost ~]# rpm -qf /usr/sbin/vsftpd
vsftpd-3.0.2-25.el7.x86_64
[root@localhost ~]# rpm -qf $(which vsftpd)
vsftpd-3.0.2-25.el7.x86_64

yum

使用rpm安装软件时,有些软件包会存在依赖关系,会导致软件安装失败。

1
2
3
[root@localhost ~]# rpm -ivh /mnt/Packages/samba-4.8.3-4.el7.x86_64.rpm error: Failed dependencies:
	libCHARSET3-samba4.so()(64bit) is needed by samba-0:4.8.3-4.el7.x86_64
	libCHARSET3-samba4.so(SAMBA_4.8.3)(64bit) is needed by samba-0:4.8.3-4.el7.x86_64

使用yum工具安装,会自动解决依赖关系,yum本质上安装的也是.rpm的安装包。

yum源配置

yum源通常可以通过以下三种方式进行配置。

  1. vim手动编辑repo文件。

  2. wget下载已经编辑好的repo文件。

    1
    
    [root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    
  3. 安装release软件,会释放一个repo文件至/etc/yum.repos.d/目录。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    [root@localhost ~]# rpm -Uvh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-2.el7.noarch.rpm
    Retrieving https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-2.el7.noarch.rpm
    warning: /var/tmp/rpm-tmp.P8jQMy: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:zabbix-release-4.2-2.el7         ################################# [100%]
    [root@localhost ~]# ls /etc/yum.repos.d/
    # /etc/yum.repos.d/下多了zabbix.repo
    CentOS-Base.repo      CentOS-Debuginfo.repo  CentOS-Sources.repo
    CentOS-Base.repo.bak  CentOS-fasttrack.repo  CentOS-Vault.repo
    CentOS-CR.repo        CentOS-Media.repo      zabbix.repo
    

系统默认为国外的yum源,为了提升软件安装速度。我们通常需要换成国内的yum源,且需要安装扩展源。

1
2
3
4
# 基本源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#扩展源
[root@localhost ~]#  wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

安装Nginx时,需要使用到官方的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
[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo 
baseurl=http://nginx.org/packages/centos/7/$basearch/ 
gpgcheck=0
enabled=1
# 查看可用的yum源
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
nginx                                                        | 2.9 kB  00:00:00     
nginx/x86_64/primary_db                                      |  49 kB  00:00:19     
repo id                     repo name                                         status
base/7/x86_64               CentOS-7 - Base - mirrors.aliyun.com              10,019
epel/x86_64                 Extra Packages for Enterprise Linux 7 - x86_64    13,343
extras/7/x86_64             CentOS-7 - Extras - mirrors.aliyun.com               435
nginx/x86_64                nginx repo                                           164
updates/7/x86_64            CentOS-7 - Updates - mirrors.aliyun.com            2,500
zabbix/x86_64               Zabbix Official Repository - x86_64                   93
zabbix-non-supported/x86_64 Zabbix Official Repository non-supported - x86_64      4
repolist: 26,558

在使用zabbix、Docker、Nginx、OpenStack之类的程序时,都可以通过官方网站找yum源。

安装

使用网络的yum源安装“小火车”。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# yum install sl -y
[root@localhost ~]# sl
                              (  ) (@@) ( )  (@)  ()    @@    O     @     O     @      O
                         (@@@)
                     (    )
                  (@@@@)

                (   )
               ====        ________           	___________
           _D _|  |_______/        \__I_I_____===__|_________|
            |(_)---  |   H\________/ |   |        =|___ ___|      _________________
            /     |  |   H  |  |     |   |         ||_| |_||     _|       	    \_____A
           |      |  |   H  |__--------------------| [___] |   =|                        |
           | ________|___H__/__|_____/[][]~\_______|       |   -|                        |
           |/ |   |-----------I_____I [][] []  D   |=======|____|________________________|_
         __/ =| o |=-O=====O=====O=====O \ ____Y___________|__|__________________________|_
          |/-=|___|=    ||    ||    ||    |_____/~\___/          |_D__D__D_|  |_D__D__D_|
           \_/      \__/  \__/  \__/  \__/      \_/               \_/   \_/    \_/   \_/

使用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
[root@localhost ~]# ls
samba-4.8.3-4.el7.x86_64.rpm
[root@localhost ~]# yum localinstall samba-4.8.3-4.el7.x86_64.rpm
# yum会联网找安装本地软件包所使用的依赖包
Dependencies Resolved
===========================================================================================
 Package                Arch       Version             Repository                     Size
===========================================================================================
Installing:
 samba                  x86_64     4.8.3-4.el7         /samba-4.8.3-4.el7.x86_64     1.9 M
Installing for dependencies:
 libldb                 x86_64     1.3.4-1.el7         base                          137 k
 libtalloc              x86_64     2.1.13-1.el7        base                           32 k
 libtdb                 x86_64     1.3.15-1.el7        base                           48 k
 libtevent              x86_64     0.9.36-1.el7        base                           36 k
 libwbclient            x86_64     4.8.3-4.el7         base                          109 k
 pytalloc               x86_64     2.1.13-1.el7        base                           17 k
 samba-client-libs      x86_64     4.8.3-4.el7         base                          4.8 M
 samba-common           noarch     4.8.3-4.el7         base                          206 k
 samba-common-libs      x86_64     4.8.3-4.el7         base                          164 k
 samba-common-tools     x86_64     4.8.3-4.el7         base                          448 k
 samba-libs             x86_64     4.8.3-4.el7         base                          276 k

Transaction Summary
===========================================================================================
Install  1 Package (+11 Dependent packages)

利用yum安装本地的软件包适用于安装大型软件。可提前将软件包下载至本地,通过yum可安装可以解决依赖关系,快速安装软件,节约软件安装的时间。

升级

查看系统中哪些软件包可以升级。

1
[root@localhost ~]# yum check-update

升级软件包

1
[root@localhost ~]# yum update openssl

yum update可以升级系统容内核,适用于刚装完系统的服务器。

卸载

因为多个软件可能依赖于同一个文件,当卸载软件时,会同时删除依赖文件。可能会导致其他软件不能正常使用。所以卸载软件,慎用!!!

1
[root@localhost ~]# yum remove samba -y

查询

查询所有软件包,包括已安装的和未安装的。

1
[root@localhost ~]# yum list

列出系统中已成功安装的软件包。

1
[root@localhost ~]# yum list installed

列出yum源可用的软件仓库。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                                             repo name                                                                   status
base/7/x86_64                                       CentOS-7 - Base - mirrors.aliyun.com                                        10,019
extras/7/x86_64                                     CentOS-7 - Extras - mirrors.aliyun.com                                         435
updates/7/x86_64                                    CentOS-7 - Updates - mirrors.aliyun.com                                      2,500
repolist: 12,954

查看所有的仓库,包括不可用的。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[root@localhost ~]# yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                                          repo name                                                             status
base/7/x86_64                                    CentOS-7 - Base - mirrors.aliyun.com                                  enabled: 10,019
centosplus/7/x86_64                              CentOS-7 - Plus - mirrors.aliyun.com                                  disabled
contrib/7/x86_64                                 CentOS-7 - Contrib - mirrors.aliyun.com                               disabled
extras/7/x86_64                                  CentOS-7 - Extras - mirrors.aliyun.com                                enabled:    435
updates/7/x86_64                                 CentOS-7 - Updates - mirrors.aliyun.com                               enabled:  2,500

查找命令所属的软件包。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[root@localhost ~]# yum provides wget
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
wget-1.14-18.el7.x86_64 : A utility for retrieving files using the HTTP or FTP
                        : protocols
Repo        : base
wget-1.14-18.el7_6.1.x86_64 : A utility for retrieving files using the HTTP or
                            : FTP protocols
Repo        : updates
wget-1.14-18.el7_6.1.x86_64 : A utility for retrieving files using the HTTP or
                            : FTP protocols
Repo        : @updates

yum不需要知道命令的绝对路径,只需要知道命令即可, yum会通过所有已启用的仓库进行查找那些软件包中涵盖了需要查询的命令。

其他用法

缓存rpm包

方法一:通过修改配置文件,全局缓存rpm文件。

1
2
3
4
5
6
[root@localhost ~]# vim /etc/yum.conf 
[main]
# 配置缓存的目录
cachedir=/var/cache/yum/$basearch/$releasever
# 0表示不缓存rpm文件,rpm=1表示缓存
keepcache=1

安装软件包并检查是否缓存成功。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[root@localhost ~]# yum install samba -y
[root@localhost ~]# find /var/cache/yum/x86_64/7/ -type f -name "*.rpm"
/var/cache/yum/x86_64/7/base/packages/libldb-1.3.4-1.el7.x86_64.rpm
/var/cache/yum/x86_64/7/base/packages/libtalloc-2.1.13-1.el7.x86_64.rpm
/var/cache/yum/x86_64/7/base/packages/libtdb-1.3.15-1.el7.x86_64.rpm
/var/cache/yum/x86_64/7/base/packages/libtevent-0.9.36-1.el7.x86_64.rpm
/var/cache/yum/x86_64/7/base/packages/pytalloc-2.1.13-1.el7.x86_64.rpm
/var/cache/yum/x86_64/7/updates/packages/samba-4.8.3-6.el7_6.x86_64.rpm
/var/cache/yum/x86_64/7/updates/packages/libwbclient-4.8.3-6.el7_6.x86_64.rpm
/var/cache/yum/x86_64/7/updates/packages/samba-client-libs-4.8.3-6.el7_6.x86_64.rpm
/var/cache/yum/x86_64/7/updates/packages/samba-common-4.8.3-6.el7_6.noarch.rpm
/var/cache/yum/x86_64/7/updates/packages/samba-common-libs-4.8.3-6.el7_6.x86_64.rpm
/var/cache/yum/x86_64/7/updates/packages/samba-common-tools-4.8.3-6.el7_6.x86_64.rpm
/var/cache/yum/x86_64/7/updates/packages/samba-libs-4.8.3-6.el7_6.x86_64.rpm

方法二:只下载不安装 指定要下载的软件包及下载的路径。

1
[root@localhost ~]# yum install httpd -y --downloadonly --downloaddir=/tmp

检查是否下载成功

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@localhost ~]# ll /tmp/
total 2912
-rw-r--r--. 1 root root 2844140 Jul 31 11:37 httpd-2.4.6-89.el7.centos.1.x86_64.rpm
-rw-r--r--. 1 root root   92776 Jul 31 11:37 httpd-tools-2.4.6-89.el7.centos.1.x86_64.rpm
-rw-r--r--. 1 root root   31264 Jul  4  2014 mailcap-2.1.41-2.el7.noarch.rpm
drwx------. 2 root root       6 Aug 15 05:21 vmware-root_5895-1983720749
drwx------. 2 root root       6 Aug 15 05:12 vmware-root_5911-1949641533
drwx------. 2 root root       6 Aug 15 16:18 vmware-root_6108-960608207
drwx------. 2 root root       6 Aug 15 04:49 vmware-root_6131-1958619680
-rw-------. 1 root root     244 Aug 15 04:53 yum_save_tx.2019-08-15.04-53._kaSKx.yumtx
-rw-------. 1 root root     757 Aug 15 18:48 yum_save_tx.2019-08-15.18-48.9u9Z9V.yumtx

清除缓存 清除所有的缓存。

1
2
3
4
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors

清除缓存包

1
2
3
4
[root@localhost ~]# yum clean packages
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
0 package files removed

执行了清除缓存的指令后,之前缓存的软件包会被删除

组相关指令

查看整组软件。

1
[root@localhost ~]# yum groups list

安装一个组软件包。

1
[root@localhost ~]# yum groups install "GNOME Desktop"

卸载组软件包。

1
[root@localhost ~]# yum groups remove "GNOME Desktop"

搭建YUM源服务器

源码安装

安装步骤

源码安装软件大体可分为以下几个步骤。

  1. 安装必要的编译工具。

    1
    
    yum groupinstall 'Development Tools'
    
  2. 下载源代码:从软件的官方网站或其他可靠来源下载软件的源代码包。

  3. 解压源代码:使用解压工具将源代码包解压到一个目录中。

  4. 配置软件:进入源代码目录,运行./configure命令来配置软件的编译和安装选项,该命令将检查系统环境并生成一个适合您的系统的Makefile

  5. 编译软件:运行make命令来编译软件,这将根据Makefile文件编译软件。

  6. 安装软件:运行make install将软件安装到系统当中,默认安装目录是/usr/local

安装Nginx

下载并解压源码包。

1
2
[root@node1 ~]# wget http://nginx.org/download/nginx-1.15.12.tar.gz
[root@node1 ~]# tar xf nginx-1.15.12.tar.gz

源码编译安装。

1
2
3
4
5
6
7
8
[root@node1 ~]# cd nginx-1.15.12/
# 配置相关的选项,并生成Makefile
# 指定了安装路径
[root@node1 nginx-1.15.12]# ./configure --prefix=/soft/nginx-1.12.2
# 将Makefile文件编译可执行二进制程序
[root@node1 nginx-1.15.12]# make
# 将二进制文件拷贝至对应的目录中
[root@node1 nginx-1.15.12]# make install

报错信息处理。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[root@node1 nginx-1.15.12]# ./configure --prefix=/soft/nginx-1.12.2
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
# 解决方法
[root@node1 nginx-1.15.12]# yum install pcre-devel

[root@node1 nginx-1.15.12]# ./configure --prefix=/soft/nginx-1.12.2
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
# 解决方法
[root@node1 nginx-1.15.12]# yum install zlib-devel

启动并验证。

1
2
3
4
# 做软连接,方便后期版本升级
[root@node1 ~]# ln -s /soft/nginx-1.12.2/ /soft/nginx
# 启动服务
[root@node1 ~]# /soft/nginx/sbin/nginx 

验证页面


image-20231028232834657