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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM