zoukankan      html  css  js  c++  java
  • Linux之软件管理(rpm)

    一、RPM介绍

    rpm全称RedHat Package Manager 缩写,由红帽开发用于软件包的安装升级卸载与查询

    rpm包名组成如下:版本号中(4.2.46)4是大版本,2是小版本,46是修订次数

    注意:linux除了rpm安装软件,还有其他安装软件方式

     二、rpm使用

    1、rpm包安装

    '''
    命令:rmp options rpm包
    -----------options参数------------
    -i   安装rpm
    -v   显示安装详细信息
    -h   显示安装rpm进度
    -U   升级rpm包
    --force 强制重新安装
    --nodeps 忽略依赖关系
    '''
    # 1、安装tree,并显示安装详细信息和安装进度
    [root@192 Packages]# rpm -ivh tree-1.6.0-10.el7.x86_64.rpm 
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:tree-1.6.0-10.el7                ################################# [100%]
    # 2、强制重新安装tree
    [root@192 Packages]# rpm -ivh --force tree-1.6.0-10.el7.x86_64.rpm 
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:tree-1.6.0-10.el7                ################################# [100%]
    # 3、忽略samba的依赖
    [root@192 Packages]# rpm -ivh --nodeps samba-4.10.16-5.el7.x86_64.rpm 
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:samba-0:4.10.16-5.el7            ################################# [100%]
    warning: group printadmin does not exist - using root
    # 4、可以使用rpm包连接地址来安装
    [root@192 Packages]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.9-1.el7.x86_64.rpm
    # 5、对rpm软件包进行升级
    [root@192 Packages]# rpm -Uvh tree-1.7.0-10.el7.x86_64.rpm 

    3、rpm查询命令

    '''
    rpm -q 软件名/包          #查看指定软件包是否安装(重点)
    rpm -qa                  #查看系统中已安装的所有rpm软件包列表
    rpm -qi 指定软件名/包     #查看指定软件的详细信息
    rpm -ql 指定软件名/包     #查询指定软件包所安装的目录、文件列表(重点)
    rpm -qc 指定软件名/包     #查询指定软件包的配置文件(只有配置文件,etc下的)
    rpm -qf 文件/目录名       #查看文件或者目录属于哪个rpm软件
    rpm -qip 未安装软件包     #查看未安装软件包的详细信息
    rpm -qlp 未安装软件包     #查看未安装软件包会产生哪些文件
    '''
    # 实例
    # 1、查看vsftpd是否安装
    [root@192 Packages]# rpm -q vsftpd
    vsftpd-3.0.2-28.el7.x86_64
    # 2、查看已安装带有ftp的rpm包
    [root@192 Packages]# rpm -qa | grep ftp
    vsftpd-3.0.2-28.el7.x86_64
    # 3、查看安装好的tree软件的详细信息
    [root@192 Packages]# rpm -qi tree
    Name        : tree
    Version     : 1.6.0
    Release     : 10.el7
    Architecture: x86_64
    Install Date: Tue 12 Jan 2021 09:39:06 AM CST
    Group       : Applications/File
    Size        : 89505
    License     : GPLv2+
    Signature   : RSA/SHA256, Fri 04 Jul 2014 01:36:46 PM CST, Key ID 24c6a8a7f4a80eb5
    Source RPM  : tree-1.6.0-10.el7.src.rpm
    Build Date  : Tue 10 Jun 2014 03:28:53 AM CST
    Build Host  : worker1.bsys.centos.org
    Relocations : (not relocatable)
    Packager    : CentOS BuildSystem <http://bugs.centos.org>
    Vendor      : CentOS
    URL         : http://mama.indstate.edu/users/ice/tree/
    Summary     : File system tree viewer
    Description :
    The tree utility recursively displays the contents of directories in a
    tree-like format.  Tree is basically a UNIX port of the DOS tree
    utility.
    # 4、查看tree软件包所安装的目录和文件列表
    [root@192 Packages]# rpm -ql tree
    /usr/bin/tree
    /usr/share/doc/tree-1.6.0
    /usr/share/doc/tree-1.6.0/LICENSE
    /usr/share/doc/tree-1.6.0/README
    /usr/share/man/man1/tree.1.gz
    # 5、查看vsftpd的配置文件
    [root@192 Packages]# rpm -qc vsftpd
    /etc/logrotate.d/vsftpd
    /etc/pam.d/vsftpd
    /etc/vsftpd/ftpusers
    /etc/vsftpd/user_list
    /etc/vsftpd/vsftpd.conf
    # 6、查看ftp目录是属于那个rpm软件
    [root@192 Packages]# rpm -qf /var/ftp
    vsftpd-3.0.2-28.el7.x86_64
    # 7、查看未安装samba软件包会产生哪些文件
    [root@192 Packages]# rpm -qlp samba-4.10.16-5.el7.x86_64.rpm 
    error: open of samrpm failed: No such file or directory
    /etc/openldap/schema
    /etc/openldap/schema/samba.schema
    /etc/pam.d/samba
    /usr/bin/smbstatus
    /usr/lib/systemd/system/nmb.service
    ....
    # 8、查看未安装samba软件包的详细信息
    [root@192 Packages]# rpm -qip samba-4.10.16-5.el7.x86_64.rpm 
    Name        : samba
    Epoch       : 0
    Version     : 4.10.16
    Release     : 5.el7
    Architecture: x86_64
    Install Date: (not installed)
    Group       : Unspecified
    Size        : 2262422
    License     : GPLv3+ and LGPLv3+
    Signature   : RSA/SHA256, Thu 15 Oct 2020 03:00:37 AM CST, Key ID 24c6a8a7f4a80eb5
    Source RPM  : samba-4.10.16-5.el7.src.rpm
    Build Date  : Wed 14 Oct 2020 12:11:01 AM CST
    Build Host  : x86-01.bsys.centos.org
    Relocations : (not relocatable)
    Packager    : CentOS BuildSystem <http://bugs.centos.org>
    Vendor      : CentOS
    URL         : http://www.samba.org/
    Summary     : Server and Client software to interoperate with Windows machines
    Description :
    Samba is the standard Windows interoperability suite of programs for Linux and
    Unix.

    4、rpm包卸载

    注意:rpm包卸载的时候,需要先把依赖包卸载,才可以卸载,如果依赖包是系统必须的,则不可以卸载,否则会造成系统奔溃

    5、rpm包校验

    '''
    rpm校验命令;rpm -v 包
    -----------返回结果参数-----------
    S    # 代表文件的容量大小发生改变
    M    # 代表文件的类型或者文件的属性(也就是权限)发送改变
    5    # 代表MD5指纹加密发生改变
    L    # 代表路径发生变化
    U    # 文件拥有者的发生改变
    G    # 文件的所属组发生改变
    T    # 文件创建的时间被改变了
    '''
    # 案例
    [root@192 Packages]# rpm -V vsftpd
    S.5....T.  c /etc/pam.d/vsftpd
    .......T.  c /etc/vsftpd/ftpusers
    S.5....T.  c /etc/vsftpd/vsftpd.conf
    .M.......    /var/ftp/pub

    三、yum介绍

    1、定义

    yum是RedHat以及CentOS中的软件包管理器。能够通过互联网下载 .rpm 包并且安装,并可以自动处理依赖性关系,无须繁琐地一次次下载、安装

    2、yum源定义

    yum源实质:就是一个包含各种rpm软件包的软件仓库,有本地源(光盘)和网络源(如阿里源、腾讯源)

    3、更换yum源

    linux系统默认的是国外源,速度比较慢,需要替换成国内源

    [root@192 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo 
    http://mirrors.aliyun.com/repo/Centos-7.repo

    扩展源

    [root@192 ~]#wget -O /etc/yum.repos.d/epel.repo 
    http://mirrors.aliyun.com/repo/epel-7.repo

    四、yum的使用

    1、基本命令

    #-----------查询---------------
    [root@192 Packages]# yum list    #显示所有已安装和未安装的程序包
    '''
    yum list updates  #列出所有可更新软件包
    yum list installed #列出所有已安装软件包
    '''
    [root@192 Packages]# yum list | grep ftp #显示带有ftp的程序包
    [root@192 Packages]# yum info ftp  # 显示ftp软件消息
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.163.com
     * extras: ftp.sjtu.edu.cn
     * updates: mirrors.163.com
    '''
    yum info updates   #列出所有可更新的软件包信息
    yum info installed #列出所有已安裝的软件包信息
    '''
    
    #-----------安装------------
    # 1 安装
    [root@192 Packages]# yum install tree
    # 2 非交互式安装
    [root@192 ~]# yum install tree -y  #不会询问你是否要安装,也就是非交互式安装
    # 3 安装本地rpm包,依赖关系也是自动从软件仓库中下载
    [root@192 Packages]# yum localinstall bind-9.11.4-26.P2.el7.x86_64.rpm 
    # 4 从网上的rpm包安装
    [root@192 ~]# yum install http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm
    # 5 重新安装:  reinstall
    [root@192 Packages]# yum reinstall tree
    
    #--------------更新-------------
    # 检查已安装软件和yum仓库软件哪些需要升级
    [root@192 Packages]# yum check-update
    # 更新acl软件 
    [root@192 Packages]# yum update acl -y #非交互式
    # 更新系统所有软件和内核,不推荐使用,有风险
    [root@192 Packages]# yum update -y
    
    #--------------删除-------------
    # 移除一个或多个软件包
    [root@192 Packages]# yum erase vsftpd
    # 卸载软件
    [root@192 Packages]# yum remove vsftpd

    2、扩展命令

     (1)yum仓库相关命令

    # 列出yum所有可用的仓库
    [root@192 Packages]# yum repolist
    # 列出yum全部仓库(包括不可用仓库)
    [root@192 Packages]# yum repolist all
    # 查看命令或者文件是属于那个软件包的(比较常用)
    [root@192 Packages]# yum provides /etc/my.cnf
    [root@192 Packages]# yum provides cd

    (2)yum缓存相关命令

    #1.缓存rpm包方式一、修改yum全局配置文件
    [root@192 ~]# vim /etc/yum.conf
    [main]
    cachedir=/var/cache/yum/$basearch/$releasever
    keepcache=1     #启动缓存
    
    #2.缓存rpm包方式二,只下载不安装
    [root@192 ~]# yum install -y yum-plugin-downloadonly #插件
    [root@192 ~]# yum install httpd -y --downloadonly --downloaddir=/tmp
    
    #3.清除所有yum缓存的软件包以及元数据
    [root@192 ~]# yum clean all
    
    #4.只清除缓存的软件包
    [root@192 ~]# yum clean packages

    (3)组包相关指令

    [root@192 ~]# yum groups list
    #安装一整个组的软件
    [root@192~]# yum groups install Development tools 
    Compatibility libraries 
    Base Debugging Tools
    
    #yum删除包组
    [root@192 ~]# yum groups remove  -y Base

    (4)历史记录指令

    # 1、查看历史执行yum命令
    [root@192 Packages]# yum history
    Loaded plugins: fastestmirror
    ID     | Login user               | Date and time    | Action(s)      | Altered
    -------------------------------------------------------------------------------
         7 | chsh root <root>         | 2021-01-12 14:49 | Erase          |    1   
         6 | chsh root <root>         | 2021-01-12 14:48 | Erase          |    1 P<
         5 | chsh root <root>         | 2021-01-08 22:31 | Install        |    1 > 
         4 | chsh root <root>         | 2021-01-02 21:24 | Install        |    1   
         3 | chsh root <root>         | 2020-12-30 22:48 | Install        |    1   
         2 | chsh root <root>         | 2020-12-30 22:39 | Install        |    1   
         1 | System <unset>           | 2020-12-30 20:18 | Install        |  304
    
    # 2、查询历史命令的详细信息:yum history info ID
    [root@192 Packages]# yum history info 2
    Loaded plugins: fastestmirror
    Transaction ID : 2
    Begin time     : Wed Dec 30 22:39:35 2020
    Begin rpmdb    : 304:a8ba2104174eb7275b1079d758330953198ec7e5
    End time       :                           (0 seconds)
    End rpmdb      : 305:1525f6814302444cd03b93a05dbf98cb0a752e10
    User           : chsh root <root>
    Return-Code    : Success
    Command Line   : install net-tools -y
    Transaction performed with:
        Installed     rpm-4.11.3-45.el7.x86_64                        @anaconda
        Installed     yum-3.4.3-168.el7.centos.noarch                 @anaconda
        Installed     yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch @anaconda
    Packages Altered:
        Install net-tools-2.0-0.25.20131004git.el7.x86_64 @base
    history info
    
    # 3、撤销历史执行过的命令:yum history undo ID
    [root@192 Packages]# yum history undo 7
    Loaded plugins: fastestmirror
    Undoing transaction 7, from Tue Jan 12 14:49:10 2021
    Loading mirror speeds from cached hostfile
     * base: mirrors.163.com
     * extras: ftp.sjtu.edu.cn
     * updates: mirrors.163.com
        Erase vsftpd-3.0.2-28.el7.x86_64 @?base
    Resolving Dependencies
    --> Running transaction check
    ---> Package vsftpd.x86_64 0:3.0.2-28.el7 will be installed
    --> Finished Dependency Resolution

    3、搭建本地仓库

    (1)制作yum源

    #1.挂载镜像
    [root@192 ~]# mount /dev/cdrom /mnt   
    
    #2.备份原有仓库
    [root@192 ~]# gzip /etc/yum.repos.d/
    
    #3.使用yum-config-manager命令添加本地仓库
    [root@192 ~]# yum-config-manager --add-repo="file:///mnt"
    
    #4.或者使用手动添加repo文件
    [root@192~]# vim /etc/yum.repos.d/cdrom.repo  
    [cdrom]      
    name=This is local cdrom
    baseurl=file:///mnt
    enabled=1
    gpgcheck=0
    
    []          #仓库名称
    name        3仓库描述信息
    baseurl     #YUM源url地址 ,可以是file:// ftp:// http://
    enabled     #是否使用该YUM源(0代表禁用, 1代表激活)
    gpgcheck    #是否验证软件签名(0代表禁用, 1代表激活)
    
    #5.生成缓存
    [root@192 ~]# yum makecache

    (2)服务端-客服端搭建

    #1.关闭防火墙、与selinux
    [root@yum_server ~]# systemctl stop firewalld
    [root@yum_server ~]# setenforce 0
    
    #2.安装ftp服务,启动并加入开机启动
    [root@yum_server ~]# yum -y install vsftpd 
    [root@yum_server ~]# systemctl start vsftpd 
    [root@yum_server ~]# systemctl enable vsftpd
    
    #3.开启yum缓存功能
    [root@yum_server ~]# vim /etc/yum.conf
    [main] cachedir=/var/cache/yum/$basearch/$releasever 
    keepcache=1
    [root@yum_server ~]# yum clean all
    
    #4.提供基础base软件包
    [root@yum_server ~]# mkdir /var/ftp/centos7
    [root@yum_server ~]# mount /dev/cdrom /mnt
    [root@yum_server ~]# cp -rp  /mnt/Packages/.rpm /var/ftp/centos7/
    
    #5.提供第三方源
    [root@yum_server ~]# mkdir /var/ftp/ops
    [root@yum_server ~]# yum -y install nginx docker
    
    #6.复制已缓存的 Nginx docker 及依赖包 到自定义 YUM 仓库目录中
    [root@yum_server_69_112 ~]# find /var/cache/yum/x86_64/7/ 
    -iname ".rpm" -exec cp -rf {} /var/ftp/ops ;
    
    #7.安装createrepo并创建 reopdata仓库
    [root@yum_server_ ~]# yum -y install createrepo
    [root@yum_server_ ~]# createrepo /var/ftp/ops
    
    #PS: 如果此仓库每次新增软件则需要重新生成一次
    # 客户端配置yum源指向服务端
    #1.客户端配置并使用 base 基础源
    [root@yum_client ~]# gzip /etc/yum.repos.d/
    [root@yum_client ~]# vim /etc/yum.repos.d/centos7.repo 
    [centos7]
    name=centos7_base
    baseurl=ftp://10.0.0.99/centos7
    gpgcheck=0
    
    #2.客户端配置并使用 ops 源
    [root@yum_client ~]# vim /etc/yum.repos.d/ops.repo 
    [ops]
    name=local ftpserver
    baseurl=ftp://10.0.0.99/ops
    gpgcheck=0

    五、源码包

    1、介绍

    源码包:指的是开发好的程序源代码,需要自行编译,有时候,要使用软件新版本的特性,在没有rpm包(软件新版本制作成rpm包有时候会等待一些时间)的时候,就可以自己执行源码包,这样就可以使用它的新特性

    它的优点:可以自行修改源代码,可以定制需要的相关功能,新版软件优先更新源码

    缺点:安装过程相对复杂。

    常见的软件源码包都可以在其官网中获取

    2、源码包使用

    安装过程大致如下:具体可以根据相关的README帮助文档

     例子:编译nginx的源码包

    #1.基础环境准备
    [root@node1 ~]# yum install -y gcc make wget 
    
    #2.下载源码包
    [root@node1 ~]# wget http://nginx.org/download/nginx-1.15.12.tar.gz
    
    #3.解压源码包, 并进入相应目录
    [root@node1 ~]# tar xf nginx-1.15.12.tar.gz
    [root@node1 ~]# cd nginx-1.15.12
    
    #4.配置相关的选项,并生成Makefile
    [root@node1 nginx-1.15.12]# ./configure --prefix=/usr/local/nginx    #指定安装路径
    [root@node1 nginx-1.15.12]# echo $? # 检查上一个命令是否执行成功
    
    #5.将Makefile文件编译可执行二进制程序,此时,/usr/local/nginx可执行文件还不在,需要执行make install 才能copy过去
    [root@node1 nginx-1.15.12]# make
    
    #6.将二进制文件拷贝至对应的目录中
    [root@node1 nginx-1.15.12]# make install
    
    #7 建立软连接(以后方便升级)
    [root@node1 nginx-1.15.12]# ln -s nginx-1.18.0 nginx
    
    #8 目录介绍
    conf:配置文件
    html:网站文件存放
    logs:日志
    sbin:可执行文件
    
    # 9 命令
    nginx  # 启动
    nginx -s reload # 重新加载
    nginx -s stop  # 重启
    nginx -s stop # 停止

    3、常见源码编译错误解决方法

    # 1、
    checking for C compiler ... not found ./configure: error: C compiler cc is not found 
    
    # 解决办法:yum -y install gcc gcc-c++ make
    
    # 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.
    
    # 解决办法:yum install -y pcre-devel
    
    # 3、
    ./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. 
    
    # 解决办法:yum -y install zlib-devel
    
    # 4、
    ./configure: error: SSL modules require the OpenSSL library.
    You can either do not enable the modules, or install the OpenSSL 
    library into the system, or build the OpenSSL library statically
    from the source with nginx by using --with-openssl=<path> option.
    
    # 解决办法:yum -y install openssl-devel
  • 相关阅读:
    Linux下定时删除指定目下n天前的文件
    日期时间格式化
    sed与awk
    Linux守护进程(init.d和xinetd)
    python-Json模块
    python3 urllib模块
    linux 命令 rsync
    Linux下scp的用法
    代码块重定向
    使用exec
  • 原文地址:https://www.cnblogs.com/nq31/p/14264838.html
Copyright © 2011-2022 走看看