Linux软件包的分类

inghu

  • 源码包
  • 二进制包(RPM包)

源码包特点

  • 源码包缺点:安装过程麻烦,需要用户手动编译,需要手动解决软件包的依赖关系
  • 源码包优点:软件源代码开放,允许用户二次开发,安装灵活,可以自定义安装路径与安装功能,卸载方便

RPM包特点

  • RPM包缺点:所有功能用户无法自定义,安装没有源码包灵活,不可以看到软件源代码
  • RPM包优点:由于已经提前被编译过,所以安装简单,安装速度快
  • RPM包命名规则,如:vsftpd-3.0.2-25.el7.x86_64.rpm
    • vsftpd #软件包名称
    • 3.0.2 #软件包版本,主版本.次版本.修改版本
    • 25 #补丁次数
    • el7 #适合的系统(el7表示RHEL7)
    • x86_64 #适合的CPU架构
    • rpm #rpm包扩展名

RPM管理软件包

  • RPM命令管理软件包需要手动解决软件包之间依赖关系
    • 树形依赖:a—>b—>c—d
    • 环形依赖:a—>b—>c—a
    • 模块依赖:需要模块文件支持,模块查询地址:www.rpmfind.net
  • 命令格式:rpm 选项… 软件包全名
  • 常用选项:
    • -q #仅查询软件是否安装
    • -qa #列出所有已经安装在系统中的所有软件,可配合grep过滤指定的软件包
    • -qi #列出软件包详细信息,包含版本与官网地址
    • -qf #后边接文件名,查询配置文件由哪个软件包产生
    • -ql #列出与该软件包相关所有文件与目录的存放位置
    • -ivh #i安装,v显示详细信息,h显示软件安装进度
    • -Uvh #升级安装软件包
    • -e #卸载软件包
    • —import #导入红帽签名
  1. #挂载iso镜像文件
  2. [root@localhost ~]# mkdir /mnt/centos
  3. [root@localhost ~]# mount /dev/cdrom /mnt/centos/
  4. #实现永久挂载,查看iso镜像文件系统类型
  5. [root@localhost ~]# df -hT
  6. [root@localhost ~]# vim /etc/fstab
  7. ...
  8. /dev/cdrom /mnt/centos iso9660 defaults 0 0
  9. #重新加载
  10. [root@localhost ~]# mount -a
  11. #查询软件包是否安装
  12. [root@localhost centos]# rpm -q vsftpd
  13. 未安装软件包 vsftpd
  14. #安装vsftpd软件包
  15. [root@localhost centos]# rpm -i Packages/vsftpd-3.0.2-25.el7.x86_64.rpm
  16. #查询系统中以安装的所有软件
  17. [root@localhost centos]# rpm -qa
  18. [root@localhost centos]# rpm -qa | grep vsftpd
  19. vsftpd-3.0.2-25.el7.x86_64
  20. #查询软件包详细的信息
  21. [root@localhost centos]# rpm -qi vsftpd
  22. Name : vsftpd #软件包名
  23. Version : 3.0.2 #版本
  24. Release : 25.el7 #最终稳定版
  25. Architecture: x86_64 #适合安装的CPU架构
  26. Install Date: 20210504 星期二 144706 #安装时间
  27. Group : System Environment/Daemons #软件包属于哪个群组
  28. Size : 361335 #软件包大小
  29. License : GPLv2 with exceptions
  30. Signature : RSA/SHA256, 20181112 星期一 224854秒, Key ID 24c6a8a7f4a80eb5
  31. Source RPM : vsftpd-3.0.2-25.el7.src.rpm
  32. Build Date : 20181031 星期三 034510
  33. Build Host : x86-01.bsys.centos.org
  34. Relocations : (not relocatable)
  35. Packager : CentOS BuildSystem <http://bugs.centos.org>
  36. Vendor : CentOS
  37. URL : https://security.appspot.com/vsftpd.html #软件包官网地址
  38. Summary : Very Secure Ftp Daemon
  39. Description : #描述信息
  40. vsftpd is a Very Secure FTP daemon. It was written completely from
  41. scratch.
  42. [root@localhost centos]# which ls
  43. alias ls='ls --color=auto'
  44. /usr/sbin/ls
  45. #查询文件由哪个软件包产生
  46. [root@localhost centos]# rpm -qf /usr/bin/ls
  47. coreutils-8.22-23.el7.x86_64
  48. [root@localhost centos]# which vim
  49. /usr/bin/vim
  50. [root@localhost centos]# rpm -qf /usr/bin/vim
  51. vim-enhanced-7.4.160-5.el7.x86_64
  52. [root@localhost centos]# rpm -qi vim-enhanced
  53. #查询软件包自带的文件与目录安装路径
  54. [root@localhost centos]# rpm -ql vsftpd
  55. [root@localhost centos]# rpm -qf /usr/bin/vim
  56. vim-enhanced-7.4.160-5.el7.x86_64
  57. [root@localhost centos]# rpm -ql vim-enhanced
  58. /etc/profile.d/vim.csh
  59. /etc/profile.d/vim.sh
  60. /usr/bin/rvim
  61. /usr/bin/vim
  62. /usr/bin/vimdiff
  63. /usr/bin/vimtutor
  64. [root@localhost centos]# rpm -q vsftpd
  65. vsftpd-3.0.2-25.el7.x86_64
  66. #卸载软件包
  67. [root@localhost centos]# rpm -e vsftpd
  68. [root@localhost centos]# rpm -q vsftpd
  69. 未安装软件包 vsftpd
  70. #安装vsftpd软件包
  71. [root@localhost centos]# rpm -ivh Packages/vsftpd-3.0.2-25.el7.x86_64.rpm
  72. 警告:Packages/vsftpd-3.0.2-25.el7.x86_64.rpm: V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
  73. 准备中... ################################# [100%]
  74. 正在升级/安装...
  75. 1:vsftpd-3.0.2-25.el7 ################################# [100%]
  76. [root@localhost centos]# rpm -q vsftpd
  77. vsftpd-3.0.2-25.el7.x86_64
  78. #升级安装软件包
  79. [root@localhost centos]# rpm -Uvh Packages/vsftpd-3.0.2-25.el7.x86_64.rpm
  80. 警告:Packages/vsftpd-3.0.2-25.el7.x86_64.rpm: V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
  81. 准备中... ################################# [100%]
  82. 软件包 vsftpd-3.0.2-25.el7.x86_64 已经安装
  83. #导入红帽签名文件
  84. [root@localhost centos]# rpm --import RPM-GPG-KEY-CentOS-7
  85. #安装软件包,查看是否还有警告信息
  86. [root@localhost centos]# rpm -q vsftpd
  87. vsftpd-3.0.2-25.el7.x86_64
  88. [root@localhost centos]# rpm -e vsftpd
  89. [root@localhost centos]# rpm -ivh Packages/vsftpd-3.0.2-25.el7.x86_64.rpm
  90. 准备中... ################################# [100%]
  91. 正在升级/安装...
  92. 1:vsftpd-3.0.2-25.el7 ################################# [100%]
  93. #安装httpd软件包
  94. [root@localhost centos]# rpm -ivh Packages/httpd-(tab键)
  95. httpd-2.4.6-88.el7.centos.x86_64.rpm httpd-manual-2.4.6-88.el7.centos.noarch.rpm
  96. httpd-devel-2.4.6-88.el7.centos.x86_64.rpm httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
  97. [root@localhost centos]# rpm -ivh Packages/httpd-2.4.6-88.el7.centos.x86_64.rpm
  98. 错误:依赖检测失败:
  99. /etc/mime.types httpd-2.4.6-88.el7.centos.x86_64 需要
  100. httpd-tools = 2.4.6-88.el7.centos httpd-2.4.6-88.el7.centos.x86_64 需要
  101. libapr-1.so.0()(64bit) httpd-2.4.6-88.el7.centos.x86_64 需要
  102. libaprutil-1.so.0()(64bit) httpd-2.4.6-88.el7.centos.x86_64 需要
  103. [root@localhost centos]# ls /etc/mime.types
  104. ls: 无法访问/etc/mime.types: 没有那个文件或目录
  105. #解决依赖关系
  106. [root@localhost centos]# rpm -ivh Packages/mailcap-2.1.41-2.el7.noarch.rpm
  107. 准备中... ################################# [100%]
  108. 正在升级/安装...
  109. 1:mailcap-2.1.41-2.el7 ################################# [100%]
  110. [root@localhost centos]# ls /etc/mime.types
  111. /etc/mime.types
  112. #解决依赖关系
  113. [root@localhost centos]# rpm -ivh Packages/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
  114. 错误:依赖检测失败:
  115. libapr-1.so.0()(64bit) httpd-tools-2.4.6-88.el7.centos.x86_64 需要
  116. libaprutil-1.so.0()(64bit) httpd-tools-2.4.6-88.el7.centos.x86_64 需要
  117. #解决依赖关系(www.rpmfind.net官网查询提供libapr-1.so.0模块文件的软件包)
  118. [root@localhost centos]# rpm -ivh Packages/apr-(tab键)
  119. apr-1.4.8-3.el7_4.1.x86_64.rpm apr-util-1.5.2-6.el7.x86_64.rpm
  120. apr-devel-1.4.8-3.el7_4.1.x86_64.rpm apr-util-devel-1.5.2-6.el7.x86_64.rpm
  121. [root@localhost centos]# rpm -ivh Packages/apr-1.4.8-3.el7_4.1.x86_64.rpm
  122. 准备中... ################################# [100%]
  123. 正在升级/安装...
  124. 1:apr-1.4.8-3.el7_4.1 ################################# [100%]
  125. #解决依赖关系(www.rpmfind.net官网查询提供libaprutil-1.so.0模块文件的软件包)
  126. [root@localhost centos]# rpm -ivh Packages/apr-util-(tab键)
  127. apr-util-1.5.2-6.el7.x86_64.rpm apr-util-devel-1.5.2-6.el7.x86_64.rpm
  128. [root@localhost centos]# rpm -ivh Packages/apr-util-
  129. apr-util-1.5.2-6.el7.x86_64.rpm apr-util-devel-1.5.2-6.el7.x86_64.rpm
  130. [root@localhost centos]# rpm -ivh Packages/apr-util-1.5.2-6.el7.x86_64.rpm
  131. 准备中... ################################# [100%]
  132. 正在升级/安装...
  133. 1:apr-util-1.5.2-6.el7 ################################# [100%]
  134. #安装依赖关系
  135. [root@localhost centos]# rpm -ivh Packages/httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
  136. 准备中... ################################# [100%]
  137. 正在升级/安装...
  138. 1:httpd-tools-2.4.6-88.el7.centos ################################# [100%]
  139. #安装httpd主包
  140. [root@localhost centos]# rpm -ivh Packages/httpd-(tab键)
  141. httpd-2.4.6-88.el7.centos.x86_64.rpm httpd-manual-2.4.6-88.el7.centos.noarch.rpm
  142. httpd-devel-2.4.6-88.el7.centos.x86_64.rpm httpd-tools-2.4.6-88.el7.centos.x86_64.rpm
  143. [root@localhost centos]# rpm -ivh Packages/httpd-2.4.6-88.el7.centos.x86_64.rpm
  144. 准备中... ################################# [100%]
  145. 正在升级/安装...
  146. 1:httpd-2.4.6-88.el7.centos ################################# [100%]

yum软件包管理

  • yum(软件仓库):提供众多软件包的仓库,并自动解决软件包之间复杂依赖关系
  • yum常用命令:
    • yum repolist #列出仓库可用软件包
    • yum list 软件包名 #查看系统中提供的软件包(包含未安装的软件包)
    • yum list updates #查看系统中可供本机升级的软件包
    • yum install 软件包名 #安装软件包,-y自动回答yes
    • yum update 软件包名 #升级软件包版本
    • yum remove 软件包名 #卸载软件包
    • yum clean all #清除仓库缓存
    • yum provides 文件名 #查看文件由哪个软件包产生(主要用于查找程序文件)
    • yum search 命令 #查找软件包
  • 本地yum源配置(本地软件仓库)
  1. [root@localhost ~]# vim /etc/yum.repos.d/local.repo
  2. [local] #仓库名称,名称自定义,但具有唯一性
  3. name=local_centos #仓库描述,(类似于仓库解释),描述信息自定义,不具备唯一性
  4. baseurl=file:///mnt/centos #指定软件仓库地址,file://用于指定本地软件包存放位置
  5. enabled=1 #软件仓库是否启动,1启动,0不启动
  6. gpgcheck=0 #是否检测软件包签名,0不检测,1检测
  7. #检测仓库可用性
  8. [root@localhost centos]# yum repolist
  9. 已加载插件:fastestmirror, langpacks
  10. 源标识 源名称 状态
  11. local local_centos 4,021
  12. repolist: 4,021
  13. #查找指定的软件包
  14. [root@localhost centos]# yum list gcc
  15. #安装软件包
  16. [root@localhost centos]# yum install gcc
  17. ...
  18. Is this ok [y/d/N]: y (y安装/d下载到本地不安装/N不安装)
  19. [root@localhost centos]# rpm -q gcc
  20. gcc-4.8.5-36.el7.x86_64
  21. [root@localhost centos]# rpm -qi gcc
  22. [root@localhost centos]# rpm -ql gcc
  23. #安装软件包并自动回答yes
  24. [root@localhost centos]# yum -y install gcc-c++
  25. [root@localhost centos]# rpm -q gcc
  26. [root@localhost centos]# rpm -qi gcc
  27. [root@localhost centos]# rpm -ql gcc
  28. [root@localhost centos]# rpm -qf /usr/bin/ls
  29. [root@localhost centos]# yum provides /usr/bin/ls
  30. #下载挂载点
  31. [root@localhost ~]# umount /mnt/centos/
  32. [root@localhost ~]# ls /mnt/centos/
  33. #查看仓库可用性
  34. [root@localhost ~]# yum repolist
  35. 源标识 源名称 状态
  36. local local_centos 4,021
  37. repolist: 4,021
  38. [root@localhost ~]# rpm -e vsftpd
  39. root@localhost ~]# yum -y install vsftpd
  40. Error downloading packages:
  41. vsftpd-3.0.2-25.el7.x86_64: [Errno 256] No more mirrors to try.
  42. [root@localhost ~]# yum clean all
  43. [root@localhost ~]# yum repolist
  44. 源标识 源名称 状态
  45. local local_centos 0
  46. repolist: 0
  47. #挂载
  48. [root@localhost ~]# mount -a
  49. mount: /dev/sr0 写保护,将以只读方式挂载
  1. #下载wget工具
  2. [root@localhost ~]# yum -y install wget
  3. #下载阿里Base源(基本软件仓库,解决rpm软件的依赖关系)
  4. [root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
  5. #下载阿里epel源(额外软件仓库,包含许多基本软件仓库没有的软件包)
  6. [root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  7. [root@localhost ~]# vim /etc/yum.repos.d/CentOS-Base.repo
  8. [base]
  9. name=CentOS-$releasever - Base - mirrors.aliyun.com
  10. failovermethod=priority #故障转移方法,默认优先
  11. baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
  12. http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
  13. http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
  14. gpgcheck=1
  15. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  16. #配置清华大学开源镜象站
  17. [root@localhost ~]# vim /etc/yum.repos.d/local.repo
  18. [local]
  19. name=local_centos
  20. baseurl=file:///mnt/centos/
  21. enabled=1
  22. gpgcheck=0
  23. [tuna.tsinghua.edu.cn]
  24. name=tuna.tsinghua.edu.cn
  25. baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/
  26. enabled=1
  27. gpgcheck=0
  28. #生成yum仓库缓存提高软件包下载速度
  29. [root@localhost ~]# yum makecache
  30. ...
  31. 元数据缓存已建立

设置yum源优先级

  1. #设置本地yum为最高优先级
  2. [root@localhost ~]# vim /etc/yum.repos.d/local.repo
  3. [local]
  4. name=local_centos
  5. baseurl=file:///mnt/centos
  6. enabled=1
  7. gpgcheck=0
  8. priority=1 #优先级为1-99之间,数字越小越优先

源码包安装方式

问题:
image.png

  1. #wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  2. #sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
  3. #yum repolist
  1. #从官网下载源码包
  2. http://nginx.org/
  3. #安装源码包依赖包
  4. [root@localhost ~]# yum -y install gcc pcre-devel openssl-devel zlib
  5. #解压源码包并进入源码包路径
  6. [root@localhost ~]# tar -xf nginx-1.20.0.tar.gz
  7. [root@localhost ~]# cd nginx-1.20.0/
  8. [root@localhost nginx-1.20.0]# ls
  9. auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
  10. #使用configure程序检查系统环境并指定安装参数
  11. [root@localhost nginx-1.20.0]# ./configure --with-http_ssl_module --with-file-aio --with-http_realip_module
  12. nginx path prefix: "/usr/local/nginx"
  13. nginx binary file: "/usr/local/nginx/sbin/nginx"
  14. nginx modules path: "/usr/local/nginx/modules"
  15. nginx configuration prefix: "/usr/local/nginx/conf"
  16. nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  17. nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  18. nginx error log file: "/usr/local/nginx/logs/error.log"
  19. nginx http access log file: "/usr/local/nginx/logs/access.log"
  20. nginx http client request body temporary files: "client_body_temp"
  21. nginx http proxy temporary files: "proxy_temp"
  22. nginx http fastcgi temporary files: "fastcgi_temp"
  23. nginx http uwsgi temporary files: "uwsgi_temp"
  24. nginx http scgi temporary files: "scgi_temp"
  25. #make将源码包转换成二进制
  26. [root@localhost nginx-1.20.0]# make
  27. #make install安装源码包
  28. [root@localhost nginx-1.20.0]# make install
  29. [root@localhost nginx-1.20.0]# cd /usr/local/nginx/
  30. [root@localhost nginx]# ls
  31. conf html logs sbin

源码包管理方式

  1. #启动nginx服务
  2. [root@localhost nginx]# sbin/nginx
  3. #netstat|ss命令用于查看系统中启动的端口信息
  4. -a 显示所有端口信息
  5. -n 以数字格式显示端口号
  6. -t 显示TCP连接的端口
  7. -u 显示UDP连接的端口
  8. -l 显示服务正在监听的端口信息
  9. -p 显示监听端口的服务名称是什么(也就是程序名)
  10. #查看nginx服务端口信息
  11. [root@localhost ~]# ss -anptul | grep nginx
  12. tcp LISTEN 0 128 *:80 #Nginx服务默认通过TCP 80 端口监听客户端请求
  13. #查看系统所有服务占用的端口虚拟系
  14. [root@localhost nginx]# ss -ntlp
  15. [root@localhost nginx]# ss -anptul | grep sshd
  16. [root@localhost nginx]# ss -anptul | grep vsftpd

systemd管理服务

  • systemd是内核加载的第一个进程(PID=1),systemd负责整个Linux系统的运行与服务控制,systemd为用户提供systemctl命令来管理RPM包安装的服务,如:启动服务、重启服务、关闭服务、查看服务状态,服务随机自启
  • 服务的启动有两个阶段,一是系统开机时随着系统的启动而启动(随机自启),二是系统启动以后用户手动将服务启动
  • 常用命令:
    • systemctl start 程序名 #启动服务
    • systemctl restart 程序名 #重启服务
    • systemctl stop 程序名 #停止服务
    • systemctl enable 程序名 #设置服务随机自启
    • systemctl disable 程序名 #设置服务不随机自启
    • systemctl status 程序名 #查看服务状态
    • systemctl is-enabled 程序名 #查看服务是否被设置随机自启
  1. [root@localhost nginx]# yum -y install vsftpd
  2. [root@localhost nginx]# rpm -ql vsftpd
  3. ...
  4. /usr/sbin/vsftpd
  5. #启动vsftpd服务
  6. [root@localhost nginx]# systemctl start vsftpd
  7. #查看服务运行状态
  8. [root@localhost nginx]# systemctl status vsftpd
  9. vsftpd.service - Vsftpd ftp daemon
  10. Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
  11. Active: active (running) since 2021-05-04 17:58:38 CST; 1min 7s ago
  12. Process: 14028 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
  13. Main PID: 14030 (vsftpd)
  14. #查看端口信息
  15. [root@localhost nginx]# ss -anptul | grep vsftpd
  16. tcp LISTEN 0 32 :::21 :::* users:(("vsftpd",pid=14030,fd=4))
  17. #重启服务(用于对配置发生修改且立即生效时使用)
  18. [root@localhost nginx]# systemctl restart vsftpd
  19. #停止服务
  20. [root@localhost nginx]# systemctl stop vsftpd
  21. #启动服务并设置服务随机自启
  22. [root@localhost nginx]# systemctl start vsftpd
  23. [root@localhost nginx]# systemctl enable vsftpd
  24. Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
  25. #查看服务是否被设置随机自启
  26. [root@localhost nginx]# systemctl is-enabled vsftpd
  27. enabled #随机自启
  28. #设置服务不随机自启
  29. [root@localhost nginx]# systemctl disable vsftpd
  30. Removed symlink /etc/systemd/system/multi-user.target.wants/vsftpd.service.
  31. [root@localhost nginx]# systemctl is-enabled vsftpd
  32. disabled #不随机自启
  33. [root@localhost nginx]# systemctl is-enabled sshd
  34. enabled