Linux运维教程-Linux软件包管理_linu

 

 

Linux软件包管理

 

  • rpm软件包管理

  • Yum软件包管理

  • Dnf软件包管理

  • 源码编译安装

 

准备工作

挂载iso镜像

Linux运维教程-Linux软件包管理_linu _02

Linux运维教程-Linux软件包管理_linu _03

 

 

将光驱挂载到系统中

[root@myserver dev]# mount /dev/sr0 /mnt/
mount: /mnt: WARNING: device write-protected, mounted read-only.
[root@myserver dev]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
devtmpfs                      884M     0  884M   0% /dev
tmpfs                         901M     0  901M   0% /dev/shm
tmpfs                         901M  9.8M  891M   2% /run
tmpfs                         901M     0  901M   0% /sys/fs/cgroup
/dev/mapper/cl_myserver-root   17G  5.2G   12G  31% /
/dev/sdb1                     5.0G   68M  5.0G   2% /data/sdb1
/dev/sda1                     976M  194M  716M  22% /boot
tmpfs                         181M   16K  181M   1% /run/user/0
tmpfs                         181M  1.2M  179M   1% /run/user/42
/dev/sr0                      6.7G  6.7G     0 100% /mnt

 


Rpm软件包管理

windows :exe  ;mac OS :dmg ;Linux:rpm

当我们的应用可以直接安装的时候建议使用rpm直接安装。但是当我们的应用需要依赖于其他的包的时候rpm就会变得很难受。

rpm 命令使用方法

rpm -qa | grep xz :查看所有已经安装的包

rpm -ivh xxx.rpm  : 安装xxx.rpm

rpm -e xxx : 卸载 xxx

rpm -Uvh xxx.rpm  :  更新xxx.rpm

 

cd /mnt/BaseOS/Packages

## 查找包是否已经安装
[root@myserver Packages]# rpm -qa  | grep xz
xz-libs-5.2.4-3.el8.x86_64
xz-5.2.4-3.el8.x86_64


## 获取相关的RPM包
[root@myserver Packages]# ls | grep xz
xz-5.2.4-3.el8.x86_64.rpm
xz-devel-5.2.4-3.el8.i686.rpm
xz-devel-5.2.4-3.el8.x86_64.rpm
xz-libs-5.2.4-3.el8.i686.rpm
xz-libs-5.2.4-3.el8.x86_64.rpm

## 开始安装
[root@myserver Packages]# rpm -ivh xz-devel-5.2.4-3.el8.x86_64.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:xz-devel-5.2.4-3.el8             ################################# [100%]

## 验证是否安装成功
[root@myserver Packages]# rpm -qa  | grep xz
xz-libs-5.2.4-3.el8.x86_64
xz-5.2.4-3.el8.x86_64
xz-devel-5.2.4-3.el8.x86_64

## 卸载
[root@myserver Packages]# rpm -e xz-devel-5.2.4-3.el8.x86_64
[root@myserver Packages]# rpm -qa  | grep xz
xz-libs-5.2.4-3.el8.x86_64
xz-5.2.4-3.el8.x86_64

 


 

Yum软件包管理工具

yum仓库概念=== yum源

源配置需要的是一个xxx.repo文件, /etc/yum.repos.d/

yum remove xz-devel: 卸载xz-devel

yum -y install xz-devel : 安装xz-devel  -y不用交互方式。

yum update xz-devel:更新xz-devel

yum clean all :清理缓存

 

操作前准备: 备份初始源

[root@myserver yum.repos.d]# mkdir bak
[root@myserver yum.repos.d]# ls
bak                     CentOS-Debuginfo.repo  CentOS-Media.repo
CentOS-AppStream.repo   CentOS-Devel.repo      CentOS-PowerTools.repo
CentOS-Base.repo        CentOS-Extras.repo     CentOS-Sources.repo
CentOS-centosplus.repo  CentOS-fasttrack.repo  CentOS-Vault.repo
CentOS-CR.repo          CentOS-HA.repo
[root@myserver yum.repos.d]# mv *.repo bak/
[root@myserver yum.repos.d]# ls
bak

 

yum源文件格式

[BaseOS]    ## 仓库名称
name=CentOS-$releasever - Base   ## 仓库描述
baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/  ## 源地址 【在线HTTP|FILE本地文件】
gpgcheck=1   ## 证书验证
enabled=1   ## 是否启动
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

 

配置本地YUM
[myrepo]
name=My first repo test.
baseurl=file:///mnt/BaseOS/
gpgcheck=0
enabled=1
[root@myserver ~]# cd /etc/yum.repos.d/
[root@myserver yum.repos.d]# ls
bak
[root@myserver yum.repos.d]# vim myrepo.repo
[root@myserver yum.repos.d]# ls
bak  myrepo.repo

 

[root@myserver ~]# yum repolist
repo id                             repo name
myrepo                              My first repo test.

 

[root@myserver ~]# rpm -qa  | grep xz
xz-libs-5.2.4-3.el8.x86_64
xz-5.2.4-3.el8.x86_64
[root@myserver ~]# yum install xz*
My first repo test.                                 24 MB/s | 2.2 MB     00:00    
Last metadata expiration check: 0:00:01 ago on Sun 21 Jun 2020 10:00:03 PM EDT.
Package xz-5.2.4-3.el8.x86_64 is already installed.
Package xz-libs-5.2.4-3.el8.x86_64 is already installed.
Dependencies resolved.
===================================================================================
 Package            Architecture     Version                Repository        Size
===================================================================================
Installing:
 xz-devel           x86_64           5.2.4-3.el8            myrepo            62 k

Transaction Summary
===================================================================================
Install  1 Package

Total size: 62 k
Installed size: 202 k
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                        1/1
  Installing       : xz-devel-5.2.4-3.el8.x86_64                                            1/1
  Running scriptlet: xz-devel-5.2.4-3.el8.x86_64                                            1/1
  Verifying        : xz-devel-5.2.4-3.el8.x86_64                                            1/1
Installed products updated.

Installed:
  xz-devel-5.2.4-3.el8.x86_64                                                                  

Complete!
[root@myserver ~]# rpm -qa  | grep xz
xz-devel-5.2.4-3.el8.x86_64
xz-libs-5.2.4-3.el8.x86_64
xz-5.2.4-3.el8.x86_64

 

[root@myserver ~]# yum remove xz-devel
Dependencies resolved.
================================================================================================
 Package               Architecture        Version                   Repository            Size
================================================================================================
Removing:
 xz-devel              x86_64              5.2.4-3.el8               @myrepo              202 k

Transaction Summary
================================================================================================
Remove  1 Package

Freed space: 202 k
Is this ok [y/N]: y
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                        1/1
  Erasing          : xz-devel-5.2.4-3.el8.x86_64                                            1/1
  Running scriptlet: xz-devel-5.2.4-3.el8.x86_64                                            1/1
  Verifying        : xz-devel-5.2.4-3.el8.x86_64                                            1/1
Installed products updated.

Removed:
  xz-devel-5.2.4-3.el8.x86_64

Complete!

 

 

在线YUM源

备份本地yum,创建新的在线yum源

[root@myserver ~]# cd /etc/yum.repos.d/
[root@myserver yum.repos.d]# ls
bak  myrepo.repo
[root@myserver yum.repos.d]# mv myrepo.repo myrepo.repo~
[root@myserver yum.repos.d]# ls
bak  myrepo.repo~
[root@myserver yum.repos.d]# vim myrepo.repo

 

[myrepo]
name=My first repo test.
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/8/BaseOS/x86_64/os/
gpgcheck=0
enabled=1

 

[root@myserver yum.repos.d]# yum -y install xz-devel
My first repo test.                                             537 kB/s | 2.2 MB     00:04
Last metadata expiration check: 0:00:01 ago on Sun 21 Jun 2020 10:09:55 PM EDT.
Dependencies resolved.
================================================================================================
 Package               Architecture        Version                    Repository           Size
================================================================================================
Installing:
 xz-devel              x86_64              5.2.4-3.el8                myrepo               62 k

Transaction Summary
================================================================================================
Install  1 Package

Total download size: 62 k
Installed size: 202 k
Downloading Packages:
xz-devel-5.2.4-3.el8.x86_64.rpm                                 105 kB/s |  62 kB     00:00
------------------------------------------------------------------------------------------------
Total                                                           104 kB/s |  62 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                        1/1
  Installing       : xz-devel-5.2.4-3.el8.x86_64                                            1/1
  Running scriptlet: xz-devel-5.2.4-3.el8.x86_64                                            1/1
  Verifying        : xz-devel-5.2.4-3.el8.x86_64                                            1/1
Installed products updated.

Installed:
  xz-devel-5.2.4-3.el8.x86_64

Complete!

 

 

FAQ

[root@myserver yum.repos.d]# yum -y install xz-devel
My first repo test.                                             0.0  B/s |   0  B     00:00
Errors during downloading metadata for repository 'myrepo':
  - Curl error (6): Couldn't resolve host name for https://mirrors.tuna.tsinghua.edu.cn/centos/8/BaseOS/x86_64/os/repodata/repomd.xml [Could not resolve host: mirrors.tuna.tsinghua.edu.cn]
Error: Failed to download metadata for repo 'myrepo': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried
[root@myserver yum.repos.d]# ping www.baidu.com
ping: www.baidu.com: Name or service not known

## 重新获取地址信息
[root@myserver yum.repos.d]# dhclient
[root@myserver yum.repos.d]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=56 time=4.68 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=56 time=4.92 ms
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 4.675/4.795/4.916/0.138 ms

 

 


Dnf软件包管理工具

DNF是Dandified yum的缩写。DNF也是基于RPM的包管理工具,其首先在Fedora系统中出现,已经成为Fedora系统的默认的包管理工具。

比Yum要简单、消耗更少内容、依赖包解析速度更快

 

xz-devel

dnf list installed:列出所有已安装的包

dnf search :查找包

dnf install:安装命令

dnf reinstall :重新安装

dnf download :下载包

dnf check-update:检查更新

dnf update :更新

dnf repolist all :查看所有可用的源

dnf remove :卸载软件

dnf clean all :清除缓存

 


源码编译安装

 

 

 

创建归档

tar cf 归档名称  文件

tar xf 归档名称 -C 指定解包路径

[root@myserver ~]# tar cf mytestfile.tar a.txt b.txt c.txt
[root@myserver ~]# ls
a                b.txt    Documents             Music       Pictures   test
anaconda-ks.cfg  c.txt    Downloads             mytestfile  Public     Videos
a.txt            Desktop  initial-setup-ks.cfg  mytest.txt  Templates
[root@myserver ~]# file mytestfile.tar
mytestfile: POSIX tar archive (GNU)
[root@myserver ~]# tar xf mytestfile.tar -C /opt/
[root@myserver ~]# ls /opt/
a.txt  b.txt  c.txt

压缩包 tar.gz

[root@myserver ~]# tar zcf mytestfile.tar.gz a.txt b.txt c.txt
[root@myserver ~]# ls
a                c.txt      initial-setup-ks.cfg  mytest.txt  test
anaconda-ks.cfg  Desktop    Music                 Pictures    Videos
a.txt            Documents  mytestfile            Public
b.txt            Downloads  mytestfile.tar.gz     Templates
[root@myserver ~]# file mytestfile.tar.gz
mytestfile.tar.gz: gzip compressed data, last modified: Mon Jun 22 12:38:35 2020, from Unix, original size 10240
[root@myserver ~]# tar zxf mytestfile.tar.gz -C /mnt/
[root@myserver ~]# ls /mnt/
a.txt  b.txt  c.txt  hgfs

 


源码编译安装过程

  • 下载应用的源码包

    • wget 下载源码包

  • 解压源码包

    • 默认解压路径 : /usr/local/

    • tar zxf xxxx -C /usr/local

  • 执行配置文件

  • 执行编译

  • 执行编译安装

 

[root@myserver ~]# wget http://zlib.net/zlib-1.2.11.tar.gz
--2020-06-22 08:44:02--  http://zlib.net/zlib-1.2.11.tar.gz
Resolving zlib.net (zlib.net)... 68.66.224.49
Connecting to zlib.net (zlib.net)|68.66.224.49|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 607698 (593K) [application/x-gzip]
Saving to: \u2018zlib-1.2.11.tar.gz\u2019

zlib-1.2.11.tar.gz    100%[=======================>] 593.46K  11.4KB/s    in 49s

2020-06-22 08:44:47 (12.2 KB/s) - \u2018zlib-1.2.11.tar.gz\u2019 saved [607698/607698]

 

[root@myserver ~]# ls
a                c.txt      initial-setup-ks.cfg  mytest.txt  test
anaconda-ks.cfg  Desktop    Music                 Pictures    Videos
a.txt            Documents  mytestfile            Public      zlib-1.2.11.tar.gz
b.txt            Downloads  mytestfile.tar.gz     Templates
[root@myserver ~]# tar zxf zlib-1.2.11.tar.gz  -C /usr/local/
[root@myserver ~]# ls /usr/local/
bin  etc  games  include  lib  lib64  libexec  sbin  share  src  zlib-1.2.11
[root@myserver ~]# cd /usr/local/zlib-1.2.11/
[root@myserver zlib-1.2.11]# ls
adler32.c       examples    inflate.c     README           zlib.3
amiga           FAQ         inflate.h     test             zlib.3.pdf
ChangeLog       gzclose.c   inftrees.c    treebuild.xml    zlib.h
CMakeLists.txt  gzguts.h    inftrees.h    trees.c          zlib.map
compress.c      gzlib.c     Makefile      trees.h          zlib.pc.cmakein
configure       gzread.c    Makefile.in   uncompr.c        zlib.pc.in
contrib         gzwrite.c   make_vms.com  watcom           zutil.c
crc32.c         INDEX       msdos         win32            zutil.h
crc32.h         infback.c   nintendods    zconf.h
deflate.c       inffast.c   old           zconf.h.cmakein
deflate.h       inffast.h   os400         zconf.h.in
doc             inffixed.h  qnx           zlib2ansi
[root@myserver zlib-1.2.11]#

 

开始编译  https://www.nginx.cn/install

编译之前首先需要安装gcc gcc-c++ 编译器

 

yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++`

 

[root@myserver zlib-1.2.11]# ./configure
Checking for gcc...
Checking for shared library support...
Building shared library libz.so.1.2.11 with gcc.
Checking for size_t... Yes.
Checking for off64_t... Yes.
Checking for fseeko... Yes.
Checking for strerror... Yes.
Checking for unistd.h... Yes.
Checking for stdarg.h... Yes.
Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf().
Checking for vsnprintf() in stdio.h... Yes.
Checking for return value of vsnprintf()... Yes.
Checking for attribute(visibility) support... Yes.

 

[root@myserver zlib-1.2.11]# make
[root@myserver zlib-1.2.11]# make install
rm -f /usr/local/lib/libz.a
cp libz.a /usr/local/lib
chmod 644 /usr/local/lib/libz.a
cp libz.so.1.2.11 /usr/local/lib
chmod 755 /usr/local/lib/libz.so.1.2.11
rm -f /usr/local/share/man/man3/zlib.3
cp zlib.3 /usr/local/share/man/man3
chmod 644 /usr/local/share/man/man3/zlib.3
rm -f /usr/local/lib/pkgconfig/zlib.pc
cp zlib.pc /usr/local/lib/pkgconfig
chmod 644 /usr/local/lib/pkgconfig/zlib.pc
rm -f /usr/local/include/zlib.h /usr/local/include/zconf.h
cp zlib.h zconf.h /usr/local/include
chmod 644 /usr/local/include/zlib.h /usr/local/include/zconf.h

 


 


Linux运维教程-Linux软件包管理_linu _04