Linux软件包管理
linux软件包从内容上可分为二进制包和源码包,不同类的软件包使用的管理工具也各不相同,源码包是没有经过编译的包,需要经过GCC,C++等编译器环境编译才能运行,二进制包无需编译,可以直接安装使用
通常而言,可以通过后缀区分源码包和二进制包,例如:以.tar.gz, .zip, .rar结尾的包称之为源码包,以.rpm结尾的软件包称之为二进制包
对于linux系统而言,提供的工具就叫做rpm,我们可以用rpm工具轻松管理rpm包
rpm软件包管理
参数
-a:查询所有已安装的软件包 -q:询问用户,输出信息 -l:打印软件包的列表 -f:查询包含file的软件包 -i:显示软件包信息,包括名称版本描述 -v:打印输出信息 -U:升级软件包 -h:显示安装进度条 -e:卸载软件包 --force:强制安装或者卸载 --nodeps:忽略依赖包 -l:列出软件包的文件 --provides:列出软件包提供的特性 -R:列出软件包依赖的rpm包
rpm包的安装
rpm -ivh
[root@bogon lizf]# which wget /usr/bin/which:nowgetin(/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
#我们查找wget命令是发现在主机中没有这个命令
[root@bogon ~]# ls
wget-1.14-18.el7_6.1.x86_64.rpm
#找到安装包后上传到主机
[root@bogon ~]# rpm -ivh wget-1.14-18.el7_6.1.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:wget-1.14-18.el7_6.1 ################################# [100%]
#用rpm -ivh 安装wget命令的rpm包
强制安装
1 2 3 4 5 6 7 8 9 10 11 12 | [root@bogon ~] # rpm -ivh wget-1.19.5-10.el8.x86_64.rpm 警告:wget-1.19.5-10.el8.x86_64.rpm: 头V3 RSA /SHA256 Signature, 密钥 ID 8483c65d: NOKEY 错误:依赖检测失败: libgnutls.so.30()(64bit) 被 wget-1.19.5-10.el8.x86_64 需要 libgnutls.so.30(GNUTLS_3_4)(64bit) 被 wget-1.19.5-10.el8.x86_64 需要 libgnutls.so.30(GNUTLS_3_6_3)(64bit) 被 wget-1.19.5-10.el8.x86_64 需要 libidn2.so.0()(64bit) 被 wget-1.19.5-10.el8.x86_64 需要 libidn2.so.0(IDN2_0.0.0)(64bit) 被 wget-1.19.5-10.el8.x86_64 需要 libmetalink.so.3()(64bit) 被 wget-1.19.5-10.el8.x86_64 需要 libnettle.so.6()(64bit) 被 wget-1.19.5-10.el8.x86_64 需要 libnettle.so.6(NETTLE_6)(64bit) 被 wget-1.19.5-10.el8.x86_64 需要 libpsl.so.5()(64bit) 被 wget-1.19.5-10.el8.x86_64 需要 |
1 | 当我们要安装其他版本的wget安装包时会报需要依赖,如果我们想强制安装时就需要加 --force --nodeps (不建议这么安装) |
1 2 3 4 5 | [root@bogon ~] # rpm -ivh wget-1.19.5-10.el8.x86_64.rpm --force --nodeps 警告:wget-1.19.5-10.el8.x86_64.rpm: 头V3 RSA /SHA256 Signature, 密钥 ID 8483c65d: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:wget-1.19.5-10.el8 ################################# [100%] |
rpm包的查看
rpm -qa 软件包名称
[root@bogon ~]# rpm -qa |grep wget
wget-1.14-18.el7_6.1.x86_64
#查看wget安装包版本信息
rpm -qi 软件包名称 :查看软件安装的版本信息

[root@bogon ~]# rpm -qi wget Name : wget Version : 1.14 Release : 18.el7_6.1 Architecture: x86_64 Install Date: 2021年08月18日 星期三 23时02分56秒 Group : Applications/Internet Size : 2055573 License : GPLv3+ Signature : RSA/SHA256, 2019年05月16日 星期四 23时48分07秒, Key ID 24c6a8a7f4a80eb5 Source RPM : wget-1.14-18.el7_6.1.src.rpm Build Date : 2019年05月16日 星期四 05时02分02秒 Build Host : x86-02.bsys.centos.org Relocations : (not relocatable) Packager : CentOS BuildSystem <http://bugs.centos.org> Vendor : CentOS URL : http://www.gnu.org/software/wget/ Summary : A utility for retrieving files using the HTTP or FTP protocols Description : GNU Wget is a file retrieval utility which can use either the HTTP or FTP protocols. Wget features include the ability to work in the background while you are logged out, recursive retrieval of directories, file name wildcard matching, remote file timestamp storage and comparison, use of Rest with FTP servers and Range with HTTP servers to retrieve files over slow or unstable connections, support for Proxy servers, and configurability. Name : wget Version : 1.19.5 Release : 10.el8 Architecture: x86_64 Install Date: 2021年08月18日 星期三 23时32分32秒 Group : Applications/Internet Size : 2895440 License : GPLv3+ Signature : RSA/SHA256, 2020年04月29日 星期三 00时08分57秒, Key ID 05b555b38483c65d Source RPM : wget-1.19.5-10.el8.src.rpm Build Date : 2020年04月27日 星期一 10时09分54秒 Build Host : x86-02.mbox.centos.org Relocations : (not relocatable) Packager : CentOS Buildsys <bugs@centos.org> Vendor : CentOS URL : http://www.gnu.org/software/wget/ Summary : A utility for retrieving files using the HTTP or FTP protocols Description : GNU Wget is a file retrieval utility which can use either the HTTP or FTP protocols. Wget features include the ability to work in the background while you are logged out, recursive retrieval of directories, file name wildcard matching, remote file timestamp storage and comparison, use of Rest with FTP servers and Range with HTTP servers to retrieve files over slow or unstable connections, support for Proxy servers, and configurability.
rpm -ql 软件包名称 :查看软件安装路径

[root@bogon ~]# rpm -ql wget /etc/wgetrc /usr/bin/wget /usr/share/doc/wget-1.14 /usr/share/doc/wget-1.14/AUTHORS /usr/share/doc/wget-1.14/COPYING /usr/share/doc/wget-1.14/MAILING-LIST /usr/share/doc/wget-1.14/NEWS /usr/share/doc/wget-1.14/README /usr/share/doc/wget-1.14/sample.wgetrc /usr/share/info/wget.info.gz /usr/share/locale/be/LC_MESSAGES/wget.mo /usr/share/locale/bg/LC_MESSAGES/wget.mo /usr/share/locale/ca/LC_MESSAGES/wget.mo /usr/share/locale/cs/LC_MESSAGES/wget.mo /usr/share/locale/da/LC_MESSAGES/wget.mo /usr/share/locale/de/LC_MESSAGES/wget.mo /usr/share/locale/el/LC_MESSAGES/wget.mo /usr/share/locale/en_GB/LC_MESSAGES/wget.mo /usr/share/locale/eo/LC_MESSAGES/wget.mo /usr/share/locale/es/LC_MESSAGES/wget.mo /usr/share/locale/et/LC_MESSAGES/wget.mo /usr/share/locale/eu/LC_MESSAGES/wget.mo /usr/share/locale/fi/LC_MESSAGES/wget.mo /usr/share/locale/fr/LC_MESSAGES/wget.mo /usr/share/locale/ga/LC_MESSAGES/wget.mo /usr/share/locale/gl/LC_MESSAGES/wget.mo /usr/share/locale/he/LC_MESSAGES/wget.mo /usr/share/locale/hr/LC_MESSAGES/wget.mo /usr/share/locale/hu/LC_MESSAGES/wget.mo /usr/share/locale/id/LC_MESSAGES/wget.mo /usr/share/locale/it/LC_MESSAGES/wget.mo /usr/share/locale/ja/LC_MESSAGES/wget.mo /usr/share/locale/lt/LC_MESSAGES/wget.mo /usr/share/locale/nb/LC_MESSAGES/wget.mo /usr/share/locale/nl/LC_MESSAGES/wget.mo /usr/share/locale/pl/LC_MESSAGES/wget.mo /usr/share/locale/pt/LC_MESSAGES/wget.mo /usr/share/locale/pt_BR/LC_MESSAGES/wget.mo /usr/share/locale/ro/LC_MESSAGES/wget.mo /usr/share/locale/ru/LC_MESSAGES/wget.mo /usr/share/locale/sk/LC_MESSAGES/wget.mo /usr/share/locale/sl/LC_MESSAGES/wget.mo /usr/share/locale/sr/LC_MESSAGES/wget.mo /usr/share/locale/sv/LC_MESSAGES/wget.mo /usr/share/locale/tr/LC_MESSAGES/wget.mo /usr/share/locale/uk/LC_MESSAGES/wget.mo /usr/share/locale/vi/LC_MESSAGES/wget.mo /usr/share/locale/zh_CN/LC_MESSAGES/wget.mo /usr/share/locale/zh_TW/LC_MESSAGES/wget.mo /usr/share/man/man1/wget.1.gz /etc/wgetrc /usr/bin/wget /usr/lib/.build-id /usr/lib/.build-id/8d /usr/lib/.build-id/8d/ec58c9a41fa3f0d9966de12c634bfa25b11451 /usr/share/doc/wget /usr/share/doc/wget/AUTHORS /usr/share/doc/wget/COPYING /usr/share/doc/wget/MAILING-LIST /usr/share/doc/wget/NEWS /usr/share/doc/wget/README /usr/share/doc/wget/sample.wgetrc /usr/share/info/wget.info.gz /usr/share/locale/be/LC_MESSAGES/wget.mo /usr/share/locale/bg/LC_MESSAGES/wget.mo /usr/share/locale/ca/LC_MESSAGES/wget.mo /usr/share/locale/cs/LC_MESSAGES/wget.mo /usr/share/locale/da/LC_MESSAGES/wget.mo /usr/share/locale/de/LC_MESSAGES/wget.mo /usr/share/locale/el/LC_MESSAGES/wget.mo /usr/share/locale/en_GB/LC_MESSAGES/wget.mo /usr/share/locale/eo/LC_MESSAGES/wget.mo /usr/share/locale/es/LC_MESSAGES/wget.mo /usr/share/locale/et/LC_MESSAGES/wget.mo /usr/share/locale/eu/LC_MESSAGES/wget.mo /usr/share/locale/fi/LC_MESSAGES/wget.mo /usr/share/locale/fr/LC_MESSAGES/wget.mo /usr/share/locale/ga/LC_MESSAGES/wget.mo /usr/share/locale/gl/LC_MESSAGES/wget.mo /usr/share/locale/he/LC_MESSAGES/wget.mo /usr/share/locale/hr/LC_MESSAGES/wget.mo /usr/share/locale/hu/LC_MESSAGES/wget.mo /usr/share/locale/id/LC_MESSAGES/wget.mo /usr/share/locale/it/LC_MESSAGES/wget.mo /usr/share/locale/ja/LC_MESSAGES/wget.mo /usr/share/locale/lt/LC_MESSAGES/wget.mo /usr/share/locale/nb/LC_MESSAGES/wget.mo /usr/share/locale/nl/LC_MESSAGES/wget.mo /usr/share/locale/pl/LC_MESSAGES/wget.mo /usr/share/locale/pt/LC_MESSAGES/wget.mo /usr/share/locale/pt_BR/LC_MESSAGES/wget.mo /usr/share/locale/ro/LC_MESSAGES/wget.mo /usr/share/locale/ru/LC_MESSAGES/wget.mo /usr/share/locale/sk/LC_MESSAGES/wget.mo /usr/share/locale/sl/LC_MESSAGES/wget.mo /usr/share/locale/sr/LC_MESSAGES/wget.mo /usr/share/locale/sv/LC_MESSAGES/wget.mo /usr/share/locale/tr/LC_MESSAGES/wget.mo /usr/share/locale/uk/LC_MESSAGES/wget.mo /usr/share/locale/vi/LC_MESSAGES/wget.mo /usr/share/locale/zh_CN/LC_MESSAGES/wget.mo /usr/share/locale/zh_TW/LC_MESSAGES/wget.mo /usr/share/man/man1/wget.1.gz
rpm -qf 根据命令反推出是哪个安装包提供的
1 2 | [root@bogon ~] # rpm -qf /usr/bin/ls coreutils-8.22-24.el7.x86_64 |
rpm包的升级(以openssh为例)
先查看openssh的安装包版本
1 2 3 4 | [root@bogon ~] # rpm -qa |grep openssh openssh-server-7.4p1-21.el7.x86_64 openssh-clients-7.4p1-21.el7.x86_64 openssh-7.4p1-21.el7.x86_64 |
最新安装包版本为openssh-8.6p1-1.el7.x86_64.rpm,使用rpm -Uvh升级
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@bogon ROOT] # ls openssh-8.6p1-1.el7.x86_64.rpm openssh-askpass-gnome-8.6p1-1.el7.x86_64.rpm openssh-debuginfo-8.6p1-1.el7.x86_64.rpm openssl-1.0.2k-19.el7.x86_64.rpm openssl-libs-1.0.2k-19.el7.x86_64.rpm openssh-askpass-8.6p1-1.el7.x86_64.rpm openssh-clients-8.6p1-1.el7.x86_64.rpm openssh-server-8.6p1-1.el7.x86_64.rpm openssl-devel-1.0.2k-19.el7.x86_64.rpm readme.txt [root@bogon ROOT] # rpm -Uvh openssh-8.6p1-1.el7.x86_64.rpm openssh-clients-8.6p1-1.el7.x86_64.rpm openssh-server-8.6p1-1.el7.x86_64.rpm 准备中... ################################# [100%] 正在升级/安装... 1:openssh-8.6p1-1.el7 ################################# [ 17%] 2:openssh-clients-8.6p1-1.el7 ################################# [ 33%] 3:openssh-server-8.6p1-1.el7 ################################# [ 50%] 正在清理/删除... 4:openssh-server-7.4p1-21.el7 ################################# [ 67%] 5:openssh-clients-7.4p1-21.el7 ################################# [ 83%] 6:openssh-7.4p1-21.el7 ################################# [100%] |
[root@bogon ROOT]# sshd -V #查看openssh版本信息
unknown option -- V
OpenSSH_8.6p1, OpenSSL 1.0.1e-fips 11 Feb 2013
usage: sshd [-46DdeiqTt] [-C connection_spec] [-c host_cert_file]
[-E log_file] [-f config_file] [-g login_grace_time]
[-h host_key_file] [-o option] [-p port] [-u len]
rpm -qa 查看openssh包信息
[root@bogon ROOT]# rpm -qa | grep openssh openssh-clients-8.6p1-1.el7.x86_64 openssh-8.6p1-1.el7.x86_64 openssh-server-8.6p1-1.el7.x86_64
#升级成功
rpm包的卸载
rpm -e 安装包名
1 2 3 | [root@bogon ROOT] # rpm -e lrzsz-0.12.20-43.el8.x86_64 [root@bogon ROOT] # rpm -qa | grep lrzsz [root@bogon ROOT] # |
rpm包是有依赖性的,可能你在装A包的时候需要依赖B包,装B包的时候又要依赖C包,所以rpm安装时解决依赖性是最头疼的问题,为了解决这种问题,我们有自动解决依赖的包管理工具yum.
yum软件包管理
前端软件包管理器(yellow dog updater modified,YUM)适用于CentOS,RedHat及SUSE等操作系统,主要用于管理RPM包,YUM能够从指定的服务器自动下载RPM包并且安装,还可以自动解除依赖关系
YUM工作原理
yum正常运行需要依赖两个部分:一是yum前端,二是yum客户端
YUM源通过HTTP或者FTP服务器发布,YUM安装或者搜索软件时,会查找/etc/yum.repos.d下以.repo结尾文件,CentOS默认的.repo文件名为CentOS-Base.repo,该文件配置yum源端的镜像地址,所以下载和更新,yum都会找.repo文件
如过配置了官方的repo源,一定要能够连接互联网,如果生产环境不能联网,我们也可以自己创建yum仓库,创建一个本地yum源。
yum参数
yum install httpd:安装httpd软件包 yum search:yum搜索软件包 yum list httpd:显示指定程序包安装情况 yum list:显示所有已安装和可安装的软件包 yum remove httpd:删除软件包httpd yum update:内核升级和软件更新 yum update httpd:更新httpd软件包 yum check-update:检查可更新的程序 yum info httpd:显示安装包信息httpd yum provides:列出软件包提供哪些文件 yum provides rz:列出rz命令由哪个软件包提供 yum grouplist:查询可以用groupinstall 安装的组名称 yum deplist httpd:查询程序httpd依赖情况 yum clean all:清除缓存 yum makecache:建立缓存 yum install httpd --downloadonly --downloaddir=/tmp/:仅下载httpd软件包,并且放到/tmp目录下,注意:这里是仅下载,不安装
yum repolist :查看yum仓库
基于ISO镜像构建YUM本地源
1、挂载本地镜像
1 | [root@localhost ~] # mount /dev/cdrom /mnt/ |
2、编辑yum文件
1 2 3 4 5 6 | [root@localhost myrepo] # vim /etc/yum.repos.d/my.repo [myself] name=myself baseurl= file : ///root/myrepo gpgcheck=0 enabled=1 |
3、清空yum缓存
1 2 3 4 5 | [root@localhost myrepo] # yum clean all Loaded plugins: fastestmirror, langpacks Cleaning repos: myself Cleaning up everything Cleaning up list of fastest mirrors |
4、创建yum缓存
1 2 3 4 5 6 7 8 | [root@localhost myrepo] # yum makecache Loaded plugins: fastestmirror, langpacks myself | 2.9 kB 00:00:00 (1 /3 ): myself /filelists_db | 3.0 MB 00:00:00 (2 /3 ): myself /other_db | 1.3 MB 00:00:00 (3 /3 ): myself /primary_db | 3.0 MB 00:00:00 Determining fastest mirrors Metadata Cache Created |
tar软件包管理
linux操作系统出了使用RPM管理工具对二进制软件包管理以外,还可以通过tar、zip、jar等工具对源码包软件进行管理
tar命令参数详解如下:
-c:建立新的存档 -v:详细显示处理的文件 -f:指定存档或设备,后接归档文件名称 -x:解压文件 -C:归档到指定新的目录下 -z:㓟gz属性的软件包 -j:有bz2属性的软件包 -t:列出存当中文件的目录
-g:建立新GNU格式的备份 --exclude=FILE:不把指定文件包含在内
tar企业案例演示
- 把1-7文件归档到test.tar中
[root@localhost test]# tar -cvf test.tar * 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt [root@localhost test]# ls -ltr 总用量 12 -rw-r--r-- 1 root root 0 11月 9 16:17 4.txt -rw-r--r-- 1 root root 0 11月 9 16:17 3.txt -rw-r--r-- 1 root root 0 11月 9 16:17 2.txt -rw-r--r-- 1 root root 0 11月 9 16:17 1.txt -rw-r--r-- 1 root root 0 11月 9 16:17 7.txt -rw-r--r-- 1 root root 0 11月 9 16:17 6.txt -rw-r--r-- 1 root root 0 11月 9 16:17 5.txt -rw-r--r-- 1 root root 10240 11月 9 16:25 test.tar
- 查看test.tar中的内容
[root@localhost test]# tar -tf test.tar 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt
- 将8.txt文件追加到test.tar中
[root@localhost test]# tar -rf test.tar 8.txt [root@localhost test]# tar -tf test.tar 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt
- 解压test.tar包
[root@localhost test]# tar -xvf test.tar 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt [root@localhost test]# ls 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt test.tar
- 将后缀为txt的文件归档到tar.gz中
[root@localhost test]# tar -zcvf test.tar.gz *.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt
- 将后缀为txt的文件归档到tar.bz2
[root@localhost test]# tar -cjvf test.tar.bz2 *.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt
除了4.txt和5.txt都打包到test.tar中
[root@localhost test]# tar -cvf test.tar * --exclude=4.txt --exclude=5.txt 1.txt 2.txt 3.txt 6.txt 7.txt 8.txt
zip软件包管理
zip是计算机文件的压缩的算法,主流的压缩格式包括,tar,rar,zip,war,gzip,bz2,iso等等,性能上tar,war,rar格式比zip格式压缩率高,但压缩时间远远高于zip
zip工具打包常见参数如下:
-r:递归到目录 -q:安静操作 -x:不包括一下名称 -d:从压缩文件中删除文件
- 压缩当前目录下所有文件文件
[root@localhost test]# zip -rv all.zip * adding: 10.txt (in=0) (out=0) (stored 0%) adding: 11.txt (in=0) (out=0) (stored 0%) adding: 1.txt (in=0) (out=0) (stored 0%) adding: 2.txt (in=0) (out=0) (stored 0%) adding: 3.txt (in=0) (out=0) (stored 0%) adding: 4.txt (in=0) (out=0) (stored 0%) adding: 5.txt (in=0) (out=0) (stored 0%) adding: 6.txt (in=0) (out=0) (stored 0%) adding: 7.txt (in=0) (out=0) (stored 0%) adding: 8.txt (in=0) (out=0) (stored 0%) adding: 9.txt (in=0) (out=0) (stored 0%) adding: auto_backup_system.sh (in=749) (out=396) (deflated 47%) total bytes=749, compressed=396 -> 47% savings
- 压缩除了11.txt以外所有的文件
[root@localhost test]# zip -rv all.zip * -x 11.txt adding: 10.txt (in=0) (out=0) (stored 0%) adding: 1.txt (in=0) (out=0) (stored 0%) adding: 2.txt (in=0) (out=0) (stored 0%) adding: 3.txt (in=0) (out=0) (stored 0%) adding: 4.txt (in=0) (out=0) (stored 0%) adding: 5.txt (in=0) (out=0) (stored 0%) adding: 6.txt (in=0) (out=0) (stored 0%) adding: 7.txt (in=0) (out=0) (stored 0%) adding: 8.txt (in=0) (out=0) (stored 0%) adding: 9.txt (in=0) (out=0) (stored 0%) adding: auto_backup_system.sh (in=749) (out=396) (deflated 47%) total bytes=749, compressed=396 -> 47% savings
从all.zip压缩包中删除10.txt文件
[root@localhost test]# zip all.zip -d 10.txt deleting: 10.txt
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」