随堂练习 软件包管理

软件包的介绍

软件包中的文件分类

1)二进制文件

2)库文件

3)配置文件

4)帮助文件

范例

1)利用cpio 工具预览包内文件

rpm2cpio 包文件 | cpio  -itv

2)释放包内文件

rpm2cpio 包文件 | cpio -id "*.conf"

 

程序包管理

1)Redhat:rpm 文件,rpm 包管理器

2)debian:deb 文件,dpkg 包管理器

源码包打包文件命名

name-VERSION.tar.gz | bz2 | xz

 

rpm 包命名方式

name-VERSION-release.arch.rpm

常见的 arch

1)x86:i386,i486,i586,i686

2)x86_64:x64,x86_64,amd64

3)powerpc:ppc

4)跟平台无关:noarch

范例

bash-3.2-32.el5_9.1.i386.rpm
bash-4.2.46-19.el7.x86_64.rpm
bash-4.4.19-7.el8.x86_64.rpm
bash-4.4.19-7.el8.aarch64.rpm
bash-4.4.19-7.el8.ppc64le.rpm
bc_1.07.1-2_amd64.deb
bc_1.07.1-2_s390x.deb

范例

统计rpm 的架构类型及相应的包数量

[08:34:05 root@sz-kx-centos8 ~]# cd /misc/cd/BaseOS/Packages/
[08:53:34 root@sz-kx-centos8 /misc/cd/BaseOS/Packages]# ls *.rpm |grep -Eo '[^.]+\.rpm$' | grep -Eo '^[^.]+' | sort | uniq -c
389 i686
211 noarch
1061 x86_64

[08:55:03 root@sz-kx-centos8 /misc/cd/BaseOS/Packages]# ls *.rpm | rev | cut -d. -f2 | rev |sort | uniq -c
389 i686
211 noarch
1061 x86_64

[08:56:17 root@sz-kx-centos8 /misc/cd/BaseOS/Packages]# ls *.rpm | grep -Eo '[^.]+\.rpm$' | cut -d. -f1 | sort | uniq -c
389 i686
211 noarch
1061 x86_64

 

包的分类

1)Application-VERSION-ARCH.rpm: 主包
2)Application-devel-VERSION-ARCH.rpm 开发子包
3)Application-utils-VERSION-ARHC.rpm 其它子包
4)Application-libs-VERSION-ARHC.rpm 其它子包

 

包的依赖

软件包之间可能存在依赖关系,甚至循环依赖,即:A 包依赖B 包,B 包依赖 C 包,C 包依赖 A 包,安装软件包时,会因为缺少依赖的包,而导致安装包失败

解决依赖包管理工具:

1)yum:rpm 包管理的前端工具

2)dnf:Fedora 18+ rpm 包管理前端管理工具,CentOS8 版代替 yum

3)zypper:suse 上的 rpm 前端管理工具

程序包管理器相关文件

1. 包文件组成 (每个包独有)
  1)包内的文件
  2)元数据,如:包的名称,版本,依赖性,描述等
  3)可能会有包安装或卸载时运行的脚本
2. 数据库(公共):/var/lib/rpm
  1)程序包名称及版本
  2)依赖关系
  3)功能说明
  4)包安装后生成的各文件路径及校验码信息

 

获取程序包的途径

软件包需要事先将源码进行编译后打包形成,获取包的途径如下:
1,系统发版的光盘或官方网站

CentOS镜像:
https://www.centos.org/download/

http://mirrors.aliyun.com

https://mirrors.huaweicloud.com/

https://mirror.tuna.tsinghua.edu.cn/

http://mirrors.sohu.com

http://mirrors.163.com

Ubuntu 镜像:

http://cdimage.ubuntu.com/releases/

http://releases.ubuntu.com

2, 第三方组织提供
Fedora-EPEL:Extra Packages for Enterprise Linux

https://fedoraproject.org/wiki/EPEL

https://mirrors.aliyun.com/epel/?spm=a2c6h.13651104.0.0.3bc47dfaZpesAr

Rpmforge:官网:http://repoforge.org/, RHEL推荐,包很全,即将关闭

Community Enterprise Linux Repository:http://www.elrepo.org,支持最新的内核和硬件相关

3, 软件项目官方站点
http://yum.mariadb.org/10.4/centos8-amd64/rpms/

http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/

4,搜索引擎

http://pkgs.org

http://rpmfind.net

http://rpm.pbone.net

https://sourceforge.net/

注意:第三方包建议要检查其合法性,来源合法性,程序包的完整性

 

包管理器 rpm

CentOS 系统上使用 rpm 命令管理程序包

功能:

安装,卸载,升级,查询,校验,数据库维护

1)安装

格式:

rpm { -i | --install } [install-options] PACKAGE_FILE...

选项

-v  verbose

-vv  

-h  以 # 号显示程序包管理执行进度

常用组合

rpm -ivh PACKAGE_FILE...

--test: 测试安装,但不真正执行安装,即dry run模式

--nodeps:忽略依赖关系

--replacepkgs | replacefiles

--nosignature: 不检查来源合法性

--nodigest:不检查包完整性

--noscripts:不执行程序包脚本

  %pre: 安装前脚本 --nopre

  %post: 安装后脚本 --nopost

  %preun: 卸载前脚本 --nopreun

  %postun: 卸载后脚本 --nopostun

2)升级

rpm { -U | --upgrade } [install-options] PACKAGE_FILE...

rpm { -F | --freshen } [install-options] PACKAGE_FILE...

对应选项

upgrade  安装有旧版程序包,则“升级”,如果不存在旧版程序包,则“安装”

freshen    安装有旧版程序包,则“升级”,如果不存在旧版程序包,则不执行升级操作

--oldpackage  降级

--force  强制安装

常用组合

rpm -Uvh PACKAGE_FILE...

rpm -Fvh PACKAGE_FILE...

升级注意项:
(1) 不要对内核做升级操作;Linux支持多内核版本并存,因此直接安装新版本内核
(2) 如果原程序包的配置文件安装后曾被修改,升级时,新版本提供的同一个配置文件不会直接覆盖老
版本的配置文件,而把新版本文件重命名(FILENAME.rpmnew)后保留

3)包查询

rpm { -q | --query } [select-options] [query-options]

[select-options]

-a  所有包

-f  查看指定的文件由哪个程序包安装生成

-p rpmfile  针对尚未安装的程序包文件做查询操作

[query-options]

--changelog  查询 rpm 包的changelog

-c  查询程序的配置文件

-d  查询程序的文档

-i  information

-l  查看指定的程序包安装后生成的所有文件

--scripts  程序包自带的脚本

#和CAPABILITY相关

--whatprovides CAPABILITY:查询指定的CAPABILITY由哪个包所提供

--whatrequires CAPABILITY:查询指定的CAPABILITY被哪个包所依赖

--provides:列出指定程序包所提供的CAPABILITY

-R:查询指定的程序包所依赖的CAPABILITY

常用查询用法

-qa

-q  PACKAGE

-qi PACKAGE

-qc PACKAGE

-ql PACKAGE

-qd PACKAGE

-q --scripts PACKAGE

-qf FILE

-qpi PACKAGE_FILE

-qpl PACKAGE_FILE,...

4)包卸载

格式

rpm {-e|--erase} [--allmatches] [--nodeps] [--noscripts] [--notriggers] [--test]
PACKAGE_NAME ...

注意:当包卸载时,对应的配置文件不会删除, 以FILENAME.rpmsave形式保留
范例:强行删除 rpm 包,并恢复

[root@centos7 ~]# rpm -e rpm --nodeps

# 恢复过程

# 重启进入rescue模式

# mkdir /mnt/cdrom

# mount /dev/sr0 /mnt/cdrom

# rpm -ivh /mnt/cdrom/Packages/rpm-4.11.3-40.el7.x86_64.rpm --root=/mnt/sysimage

# reboot

5)包校验

在安装包时,系统也会检查包的来源是否是合法的
检查包的完整性和签名

rpm -K  | --checksig  rpmfile

在检查包的来源和完整性前,必须导入所需要公钥

范例:

rpm --import  /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

rpm -qa "gpg-pubkey"

范例 CentOS 8

[root@centos8 ~]# rpm -K /misc/cd/AppStream/Packages/httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64.rpm

/misc/cd/AppStream/Packages/httpd-2.4.37-
16.module_el8.1.0+256+ae790463.x86_64.rpm: digests SIGNATURES NOT OK

[root@centos8 ~]# cat /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2.0.22 (GNU/Linux)

...省略

[root@centos8 ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[root@centos8 rpm-gpg]# rpm -K /misc/cd/AppStream/Packages/httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64.rpm

/misc/cd/AppStream/Packages/httpd-2.4.37-
16.module_el8.1.0+256+ae790463.x86_64.rpm: digests signatures OK
[root@centos8 ~]# rpm -qa "gpg-pubkey*"
gpg-pubkey-8483c65d-5ccc5b19

[root@centos8 ~]# rpm -qi gpg-pubkey-8483c65d-5ccc5b19
Name : gpg-pubkey
Version : 8483c65d
Release : 5ccc5b19

...省略

范例:校验包文件

[root@centos8 ~]# rpm -K /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm
/misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm: digests signatures OK

[root@centos8 ~]# cp /misc/cd/BaseOS/Packages/tree-1.7.0-15.el8.x86_64.rpm /data

[root@centos8 ~]# cd /data

[root@centos8 data]# ll
total 60
-r--r--r-- 1 root root 60780 Apr 8 10:11 tree-1.7.0-15.el8.x86_64.rpm

[root@centos8 data]# echo >>tree-1.7.0-15.el8.x86_64.rpm

[root@centos8 data]# ll tree-1.7.0-15.el8.x86_64.rpm

-r--r--r-- 1 root root 60781 Apr 8 10:11 tree-1.7.0-15.el8.x86_64.rpm

[root@centos8 data]# cd

[root@centos8 ~]# rpm -K /data/tree-1.7.0-15.el8.x86_64.rpm

/data/tree-1.7.0-15.el8.x86_64.rpm: DIGESTS SIGNATURES NOT OK

软件在安装时,会将包里的每个文件的元数据,如:大小,权限,所有者,时间等记录至rpm相关的数据库中,可以用来检查包中的文件是否和当初安装时有所变化

rpm {-V|--verify} [select-options] [verify-options]
#示例:
[root@centos8 ~]# rpm -V centos-release

S.5....T. c /etc/issue
S   file Size differs
M   Mode differs (includes permissions and file type)
5    digest (formerly MD5 sum) differs
D   Device major/minor number mismatch
L    readLink(2) path mismatch
U   User ownership differs
G   Group ownership differs
T   mTime differs
P   capabilities differ
. (点)   表示当前位置代表的字符含义一致
c   所在的位置表示文件类型
c   配置文件
d   文件数据文件

g   该文件不属于某个文件(少数情况)
  许可证文件(license file)
  自述文件(READ ME)

范例

[root@centos8 ~]# rpm -qf /etc/issue
centos-release-8.1-1.1911.0.8.el8.x86_64
[root@centos8 ~]# vim /etc/issue
welcome to magedu
\S
Kernel \r on an \m
[root@centos8 ~]# rpm -V centos-release
S.5....T. c /etc/issue
[root@centos8 ~]# vim /etc/issue
[root@centos8 ~]# cat /etc/issue
\S
Kernel \r on an \m
[root@centos8 ~]#
[root@centos8 ~]# rpm -V centos-release
.......T. c /etc/issue
[root@centos8 ~]# rpm -ql centos-release

/etc/centos-release
/etc/centos-release-upstream
/etc/issue
/etc/issue.net
/etc/os-release
/etc/redhat-release

...省略

[root@centos8 ~]# rpm -Va
.M....... g /run/dbus
.......T. /usr/bin/tree
.M....... c /etc/machine-id
missing c /etc/systemd/system/dbus-org.freedesktop.resolve1.service
.M....... g /var/cache/private
.M....... g /var/lib/private

...省略

6)数据库

rpm 包安装时生成的信息,都放在 rpm 数据库中

/var/lib/rpm

可以重建数据库

rpm {--initdb|--rebuilddb}

initdb:      初始化,如果事先不存在数据库,则新建之,否则,不执行任何操作

rebuilddb  重建已安装的包头的数据库索引目录

 

yum

CentOS 使用 yum, dnf 解决rpm的包依赖关系
YUM: Yellowdog Update Modifier,rpm的前端程序,可解决软件包相关依赖性,可在多个库之间定位软件包,up2date的替代工具,CentOS 8 用dnf 代替了yum ,不过保留了和 yum的兼容性,配置也是通用的

yum/dnf 工作原理

yum/dnf 是基于 C/S 模式

  1)yum 服务器存放 rpm 包和相关包的元数据库

  2)yum 客户端访问 yum 服务器进行安装或查询等

yum 实现过程
先在yum服务器上创建 yum repository(仓库),在仓库中事先存储了众多rpm包,以及包的相关的元数据文件(放置于特定目录repodata下),当yum客户端利用yum/dnf工具进行安装时包时,会自动下载repodata中的元数据,查询远数据是否存在相关的包及依赖关系,自动从仓库中找到相关包下载并安装。

yum服务器的仓库可以多种形式存在:
  1)file:// 本地路径
  2)http://
  3)https://
  4)ftp://

注意:yum仓库指向的路径一定必须是repodata目录所在目录

yum 客户端配置文件

#为所有仓库提供公共配置

/etc/yum.conf 

#为每个仓库的提供配置文件

/etc/yum.repos.d/*.repo

帮助参考:man 5 yum.conf

相关变量

yum的repo配置文件中可用的变量:

$releasever  当前OS的发行版的主版本号,如:8,7,6

$arch   CPU架构,如:aarch64, i586, i686,x86_64等

$basearch  系统基础平台;i386, x86_64

$contentdir  表示目录,比如:centos-8,centos-7

$YUM0-$YUM9  自定义变量

范例

http://server/centos/$releasever/$basearch/

http://server/centos/7/x86_64

http://server/centos/6/i386

范例:CentOS 8 配置文件

[root@centos8 ~]# ll /etc/yum.conf

lrwxrwxrwx. 1 root root 12 May 14 2019 /etc/yum.conf -> dnf/dnf.conf

[root@centos8 ~]# cat /etc/yum.conf

[main]

gpgcheck=1   # 安装包前要做包的合法和完整性校验

installonly_limit=3   # 同时可以安装3个包,最小值为2,如设为0或1,为不限制

clean_requirements_on_remove=True   # 删除包时,是否将不再使用的包删除

best=True   # 升级时,自动选择安装最新版,即使缺少包的依赖

范例:CentOS 7 配置文件

[root@centos7 ~]# ll /etc/yum.conf

-rw-r--r--. 1 root root 970 Aug 8 19:57 /etc/yum.conf

[root@centos7 ~]# cat /etc/yum.conf

[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?
project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

repo 仓库配置文件指向的定义

[repositoryID]
name=Some name for this repository
baseurl=url://path/to/repository/
enabled={1|0}
gpgcheck={1|0}
gpgkey=URL
enablegroups={1|0}
failovermethod={roundrobin|priority}
roundrobin  意为随机挑选,默认值
priority  按顺序访问
cost=   默认为1000

baseurl指向的路径

阿里云提供了写好的CentOS和ubuntu的仓库文件下载链接

http://mirrors.aliyun.com/repo/

CentOS 系统的 yum 源

#阿里云
https://mirrors.aliyun.com/centos/$releasever/os/x86_64/

#华为云
https://mirrors.huaweicloud.com/

#清华大学
https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/x86_64/

EPEL 的yum 源

#阿里云
https://mirrors.aliyun.com/epel/$releasever/x86_64

范例:

1)为 CentOS 7 用系统安装光盘作的本地 yum 仓库

# 挂载光盘至某个目录,如 /mnt/cdrom

mount /dev/cdrom /mnt/cdrom

# 创建配置文件

[root@centos7 ~]# vim /etc/yum.repos.d/centos7.repo

[CentOS7]

name=CentOS 7

baseurl=file:///mnt/cdrom

gpgcheck=0

enabled=1

2)为 CentOS 8 配置 yum 的系统和 EPEL 源仓库

[root@centos8 ~]# cat /etc/yum.repos.d/centos8.repo

[BaseOS]

name=BaseOS

baseurl=file:///misc/cd/BaseOS

gpgcheck=1

gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

 

[AppStream]

name=AppStream

baseurl=file:///misc/cd/AppStream

gpgcheck=0

 

[epel]

name=EPEL

baseurl=http://mirrors.aliyun.com/epel/$releasever/Everything/$basearch

gpgcheck=0

enabled=1

 

[extras]

name=extras

baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os

gpgcheck=0

注意:与之前的版本不同,CentOS 8 系统有两个yum 源:BaseOS和AppStream ,需要分别设置两个仓库

3)用脚本实现创建 yum 仓库配置文件

[root@centos7 ~]# cat yum.sh

#!/bin/bash

mkdir /etc/yum.repos.d/backup

mv /etc/yum.repos.d/*.repo  /etc/yum.repos.d/backup

cat > /etc/yum.repos.d/base.repo <<EOF

[base]

name=base

baseurl=https://mirrors.aliyun.com/centos/\$releasever/os/\$basearch

gpgcheck=0

EOF

yum-config-manager命令
可以生成yum仓库的配置文件及启用或禁用仓库,来自于yum-utils包

格式

# 增加仓库

yum-config-manager --add-repo URL 或 file

# 禁用仓库

yum-config-manager  --disable "仓库名"

# 启用仓库

yum-config-manager  --enable "仓库名"

范例

1)创建仓库配置

[root@centos8 ~]# rpm -qf `which yum-config-manager `
dnf-utils-4.0.2.2-3.el8.noarch

[root@centos8 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/dockerce.repo

[root@centos8 ~]# ls /etc/yum.repos.d/
backup base.repo docker-ce.repo

2)创建仓库配置

#生成172.16.0.1_cobbler_ks_mirror_8_.repo
[root@centos8 ~]# yum-config-manager --addrepo=http://172.16.0.1/cobbler/ks_mirror/8/
Adding repo from: http://172.16.0.1/cobbler/ks_mirror/8/

[root@centos8 ~]# cat /etc/yum.repos.d/172.16.0.1_cobbler_ks_mirror_8_.repo
[172.16.0.1_cobbler_ks_mirror_8_]
name=created by dnf config-manager from http://172.16.0.1/cobbler/ks_mirror/8/
baseurl=http://172.16.0.1/cobbler/ks_mirror/8/
enabled=1

3)创建仓库配置

[root@centos8 ~]# ls /etc/yum.repos.d/
backup base.repo

[root@centos8 ~]# yum-config-manager --add-repo /data/docker-ce.repo
Adding repo from: file:///data/docker-ce.repo

[root@centos8 ~]# ls /etc/yum.repos.d/
backup base.repo docker-ce.repo

4)启用和禁用仓库

[root@centos8 ~]# yum repolist

[root@centos8 ~]# yum-config-manager --disable epel

[root@centos8 ~]# cat /etc/yum.repos.d/base.repo
[BaseOS]
name=BaseOS
baseurl=file:///misc/cd/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=file:///misc/cd/AppStream
gpgcheck=0
[epel]
name=EPEL
baseurl=http://mirrors.aliyun.com/epel/$releasever/Everything/$basearch
              http://mirrors.huaweicloud.com/epel/$releasever/Everything/$basearch
gpgcheck=0
enabled=0
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os
              http://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/os
gpgcheck=0
enabled=1

[root@centos8 ~]# yum repolist

...省略

[root@centos8 ~]# yum-config-manager --disable extras
[root@centos8 ~]# yum repolist
BaseOS      3.8 MB/s | 3.9 kB    00:00
AppStream    4.2 MB/s | 4.3 kB    00:00
repo id      repo name          status
AppStream    AppStream          4,755
BaseOS      BaseOS            1,659
[root@centos8 ~]# yum-config-manager --enable extras
[root@centos8 ~]# yum repolist
BaseOS      3.8 MB/s | 3.9 kB   00:00
AppStream      4.2 MB/s | 4.3 kB    00:00
extras         12 kB/s | 1.5 kB      00:00
repo id               repo name            status
AppStream        AppStream            4,755
BaseOS             BaseOS              1,659
extras      extras                                              12

 

yum 命令

用法

yum [options] [command] [package ...]

yum 命令选项

-y   # 自动回答为“yes”
-q   # 静默模式
--nogpgcheck   # 禁止进行gpg check
--enablerepo=repoidglob   # 临时启用此处指定的repo,支持通配符,如:”*“
--disablerepo=repoidglob   # 临时禁用此处指定的repo,和上面语句同时使用,放在后面的生效

显示仓库列表

yum repolist [all | enabled | disabled]

范例

[root@centos8 ~]# yum repolist
[root@centos8 ~]# yum repolist all

[root@centos8 ~]# yum --enablerepo=ep* --disablerepo=A* repolist
Last metadata expiration check: 0:01:18 ago on Sun 29 Dec 2019 12:13:27 AM CST.
repo id     repo name    status
BaseOS     BaseOS        1,657
epel       EPEL       3,733

显示程序包

yum list
yum list [all | glob_exp1] [glob_exp2] [...]
yum list {available|installed|updates} [glob_exp1] [...]

范例

[root@centos8 data]# dnf list mariadb-server

[root@centos8 data]# dnf list mariadb-server --showduplicates

[root@centos8 data]# dnf list mariadb-server --disablerepo=AppStream

[root@centos8 data]# dnf list mariadb-server --showduplicates --disablerepo=AppStream

[root@centos8 ~]# yum --disablerepo="A*" --disablerepo="B*" --disablerepo="e*" list available

安装程序包

yum install package1 [package2] [...]

#重新安装
yum reinstall package1 [package2] [...] 

 

安装epel 源包

范例

[root@centos7 ~]# yum -y install epel-release

[root@centos7 ~]# yum -y install sl

[root@centos7 ~]# rpm -ql sl
/usr/bin/sl
/usr/share/doc/sl-5.02

 

升级最新内核

范例:利用 elrepo 源在 CentOS 7 安装新版内核

[root@centos7 ~]# yum install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm

[root@centos7 ~]# rpm -ql elrepo-release-7.0-4.el7.elrepo
/etc/pki/elrepo
/etc/pki/elrepo/SECURE-BOOT-KEY-elrepo.org.der
/etc/pki/rpm-gpg
/etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
/etc/yum.repos.d
/etc/yum.repos.d/elrepo.repo

[root@centos7 ~]# yum repolist
yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* elrepo: mirrors.tuna.tsinghua.edu.cn
repo id repo name
status
base CentOS
10,019
elrepo ELRepo.org Community Enterprise Linux Repository
- el7 139
epel/7/x86_64 EPEL
13,501
extras/7/x86_64 extras
307
repolist: 23,966

[root@centos7 ~]# cat /etc/yum.repos.d/elrepo.repo
### Name: ELRepo.org Community Enterprise Linux Repository for el7
### URL: http://elrepo.org/
[elrepo]
name=ELRepo.org Community Enterprise Linux Repository - el7
baseurl=http://elrepo.org/linux/elrepo/el7/$basearch/
    http://mirrors.coreix.net/elrepo/elrepo/el7/$basearch/
    http://mirror.rackspace.com/elrepo/elrepo/el7/$basearch/
    http://repos.lax-noc.com/elrepo/elrepo/el7/$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo.el7
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0
[elrepo-testing]
name=ELRepo.org Community Enterprise Linux Testing Repository - el7
baseurl=http://elrepo.org/linux/testing/el7/$basearch/
    http://mirrors.coreix.net/elrepo/testing/el7/$basearch/
    http://mirror.rackspace.com/elrepo/testing/el7/$basearch/
    http://repos.lax-noc.com/elrepo/testing/el7/$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo-testing.el7
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0
[elrepo-kernel]
name=ELRepo.org Community Enterprise Linux Kernel Repository - el7
baseurl=http://elrepo.org/linux/kernel/el7/$basearch/
    http://mirrors.coreix.net/elrepo/kernel/el7/$basearch/
    http://mirror.rackspace.com/elrepo/kernel/el7/$basearch/
    http://repos.lax-noc.com/elrepo/kernel/el7/$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo-kernel.el7
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0
[elrepo-extras]
name=ELRepo.org Community Enterprise Linux Extras Repository - el7
baseurl=http://elrepo.org/linux/extras/el7/$basearch/
    http://mirrors.coreix.net/elrepo/extras/el7/$basearch/
    http://mirror.rackspace.com/elrepo/extras/el7/$basearch/
    http://repos.lax-noc.com/elrepo/extras/el7/$basearch/
mirrorlist=http://mirrors.elrepo.org/mirrors-elrepo-extras.el7
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0

[root@centos7 ~]# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* elrepo-kernel: mirrors.tuna.tsinghua.edu.cn
Available Packages
kernel-lt.x86_64 4.4.207-
...省略

[root@magedu ~]# yum -y --enablerepo="elrepo-kernel" install kernel-ml

[root@centos7 ~]# ls /boot
config-3.10.0-957.el7.x86_64 symvers-3.10.0-
957.el7.x86_64.gz
config-5.4.6-1.el7.elrepo.x86_64 symvers-5.4.6-
1.el7.elrepo.x86_64.gz
efi System.map-3.10.0-
957.el7.x86_64
grub System.map-5.4.6-
1.el7.elrepo.x86_64
grub2 vmlinuz-0-rescue-
907ef8572fc24762bcbaac846dd46fcc
initramfs-0-rescue-907ef8572fc24762bcbaac846dd46fcc.img vmlinuz-3.10.0-
957.el7.x86_64
initramfs-3.10.0-957.el7.x86_64.img vmlinuz-5.4.6-
1.el7.elrepo.x86_64
initramfs-5.4.6-1.el7.elrepo.x86_64.img

[root@centos7 ~]# ls /lib/modules
3.10.0-957.el7.x86_64 5.4.6-1.el7.elrepo.x86_64

[root@centos7 ~]# reboot

[root@centos7 ~]# uname -r
5.4.6-1.el7.elrepo.x86_64

卸载程序包

yum remove | erase package1 [package2] [...]

升级

yum update [package1] [package2] [...]

降级
yum downgrade package1 [package2] [...] 

范例

[root@centos7 ~]# cat /etc/yum.repos.d/base.repo
[base]
name=aliyum base
baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/$releasever/os/x86_64/RPM-GPG-KEYCentOS-$
releasever
enabled=1
[update]
name=aliyun update
baseurl=https://mirrors.aliyun.com/centos/7/updates/x86_64/
gpgcheck=0
[root@centos7 ~]# yum --disablerepo=* --enablerep=updates list available
[root@centos7 ~]# yum info samba

...省略

[root@centos7 ~]# yum info samba --showduplicates

...省略

[root@centos7 ~]# yum install samba --disablerepo=updates

[root@centos7 ~]# yum update samba

[root@centos7 ~]# yum update

检查可用升级

yum check-update

查询

yum info [...]

查看指定的特性(可以是某文件)是由哪个程序包所提供:

yum provides | whatprovides feature1 [feature2] [...]

注意:文件要写全路径,而不只是文件名,否则可能无法查询到

范例

[root@centos8 ~]# ll /etc/vsftpd/vsftpd.conf
ls: cannot access '/etc/vsftpd/vsftpd.conf': No such file or directory

#注意要写文件全路径才能查询到

[root@centos8 ~]# yum provides vsftpd.conf

[root@centos8 ~]# yum provides /etc/vsftpd/vsftpd.conf

# 使用通配符

[root@centos8 ~]# yum provides */vsftpd.conf

[root@centos8 ~]# yum provides  */updatedb*

以指定的关键字搜索程序包名及summary信息

yum search string1 [string2] [...]

查看指定包所依赖的capabilities:

yum deplist package1 [package2] [...]

范例:

[root@centos8 ~]# dnf info bash

...省略

[root@centos8 ~]# dnf list bash*

...省略

[root@centos8 ~]# dnf provides /bin/ls

...省略

[root@centos8 ~]# rpm -ivh /misc/cd/AppStream/Packages/httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64.rpm

...省略

[root@centos8 ~]#  yum provides /etc/mime.types

...省略

[root@centos8 ~]# yum deplist httpd

Last metadata expiration check: 0:41:15 ago on Wed 08 Apr 2020 03:15:02 PM CST.
package: httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
dependency: /bin/sh
provider: bash-4.4.19-10.el8.x86_64
dependency: /etc/mime.types
provider: mailcap-2.1.48-3.el8.noarch
dependency: httpd-filesystem
provider: httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch
dependency: httpd-filesystem = 2.4.37-16.module_el8.1.0+256+ae790463
provider: httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch

...省略

仓库缓存

清除目录/var/cache/yum/缓存

yum clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]

构建缓存

yum makecache

范例 管理 yum 缓存

[root@centos7 ~]# du -sh /var/cache/yum
93M /var/cache/yum

[root@centos7 ~]# ls /var/cache/yum/x86_64/7/
base epel extras timedhosts timedhosts.txt

[root@magedu ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base epel extras
Cleaning up list of fastest mirrors

[root@centos7 ~]# du -sh /var/cache/yum
4.0K /var/cache/yum

[root@centos7 ~]# yum makecache
......
Metadata Cache Created
[root@centos7 ~]# du -sh /var/cache/yum
276M /var/cache/yum

查看 yum 事务历史

yum 执行安装卸载命令会记录到相关日志中
日志 文件:

# CentOS 7以前版本日志
/var/log/yum.log

# CentOS 8 版本日志
/var/log/dnf.rpm.log
/var/log/dnf.log

日志命令

yum history [info|list|packages-list|packages-info|summary|addoninfo|redo|undo|rollback|new|sync|stats]

范例

[root@centos8 ~]# dnf history

...省略

[root@centos8 ~]# dnf history info 22
Transaction ID : 22
Begin time : Sun 22 Dec 2019 01:44:08 PM CST
Begin rpmdb : 607:35cd823ff347e56ceb688a9f72715eabb3c53d41
End time : Sun 22 Dec 2019 01:44:08 PM CST (0 seconds)
End rpmdb : 608:24139ec38fc131c182b75fdaad0626692045da94
User : root <root>
Return-Code : Success

...省略 

[root@centos8 ~]# dnf history undo 22 -y
Removed:
  dnf-utils-4.0.2.2-3.el8.noarch
Complete!

[root@centos8 ~]# dnf history redo 22 -y

安装及升级本地程序包

yum localinstall|install rpmfile1 [rpmfile2] [...]
yum localupdate|update rpmfile1 [rpmfile2] [...]

包组管理的相关命令

yum grouplist [hidden] [groupwildcard] [...]
yum groupinstall group1 [group2] [...]
yum groupupdate group1 [group2] [...]
yum groupremove group1 [group2] [...]
yum groupinfo group1 [...]

范例:最小化安装的系统安装图形环境

[root@centos8 ~]# yum grouplist
Last metadata expiration check: 0:21:21 ago on Sun 22 Dec 2019 01:56:36 PM CST.
Available Environment Groups:
  Server with GUI
  Server
  Workstation
  KDE Plasma Workspaces

...省略

[root@centos8 ~]# yum groupinfo "Server with GUI"
Last metadata expiration check: 0:32:00 ago on Wed 08 Apr 2020 04:35:02 PM CST.
Environment Group: Server with GUI
  Description: An integrated, easy-to-manage server with a graphical interface.
no group 'dns-server' from environment 'graphical-server-environment'
  Mandatory Groups:
  Common NetworkManager submodules

...省略

[root@centos8 ~]# dnf groupinstall GNOME -y

[root@centos8 ~]# init 5

实现私用 yum 仓库

下载所有yum仓库的相关包和meta 数据

# CentOS 8 dnf 工具集成
dnf reposync --help #查看帮助

# 默认只下载rpm包,不下载meta数据,需要指定--download-metadata 才能下载meta
dnf reposync --repoid=REPOID --download-metadata -p /path

# CentOS 7 以前版本,reposync工具来自于yum-utils包
reposync --repoid=REPOID --download-metadata -p /path

创建私有 yum 仓库

createrepo [options] <directory>

范例:创建局域网的基于Base的私有 yum 源

# 仓库服务器配置
[root@repo-server ~]# yum -y install httpd

[root@repo-server ~]# systemctl enable --now httpd

[root@repo-server ~]# mkdir /var/www/html/centos/8 -pv

[root@repo-server ~]# mount /dev/sr0 /mnt/

[root@repo-server ~]# cp -a /mnt/* /var/www/html/centos/8

#yum客户端配置
[root@repo-client ~]# cat /etc/yum.repos.d/test.repo

[BaseOS]
name=BaseOS
baseurl=http://10.0.0.8/centos/8/BaseOS
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[AppStream]
name=Appstream
baseurl=http://10.0.0.8/centos/8/AppStream/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

范例:下载阿里云的extras源,制作私有yum源

[root@repo-server ~]# yum repolist
Last metadata expiration check: 0:10:14 ago on Wed 08 Apr 2020 05:42:07 PM CST.
repo id     repo name       status
AppStream   AppStream       4,755
BaseOS     BaseOS         1,659
epel      EPEL              5,206

extras

[root@repo-server ~]# dnf reposync --repoid=extras --download-metadata -p /var/www/html/centos

[root@repo-server ~]# ls /var/www/html/centos/
8 extras

[root@repo-server ~]# ls /var/www/html/centos/extras/

Packages repodata
[root@repo-client ~]# yum repolist
Last metadata expiration check: 0:02:31 ago on Wed 08 Apr 2020 06:01:20 PM CST.
repo id       repo name      status
AppStream     Appstream      4,755
BaseOS      BaseOS        1,659
extras       extras           12

[root@repo-client ~]# cat /etc/yum.repos.d/test.repo
[BaseOS]
name=BaseOS
baseurl=http://10.0.0.8/centos/8/BaseOS
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[AppStream]
name=Appstream
baseurl=http://10.0.0.8/centos/8/AppStream/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[extras]
name=extras
baseurl=http://10.0.0.8/centos/extras/

[root@repo-client ~]# yum --disablerepo=* --enablerepo=extras list available

[root@repo-client ~]# yum -y install epel-release

范例:下载阿里云的EPEL源,制作私有yum源

[root@centos8 ~]# cat /etc/yum.repos.d/base.repo
[epel]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
gpgcheck=0

[root@centos8 ~]# dnf repolist
Last metadata expiration check: 0:07:40 ago on Sun 22 Dec 2019 03:14:16 PM CST.
repo id      repo name    status
AppStream    AppStream    4,681
BaseOS     BaseOS       1,655
epel      EPEL      3,707

#下载相关仓库包和元数据
[root@centos8 ~]# dnf reposync --repoid=epel --download-metadata -p /var/www/html

#--download-metadata 加此选项可以下载元数据

#下载相关的key文件
[root@repo-server ~]# wget -P /var/www/html/epel/ https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8

#下面两个步骤只有没meta数据才需要执行
#[root@centos8 ~]# dnf -y install createrepo httpd

#[root@centos8 ~]# createrepo /var/www/html/epel/

[root@centos8 ~]# ls /var/www/html/epel/
Packages repodata

[root@centos8 ~]# systemctl start httpd

[root@repo-client ~]# cat /etc/yum.repos.d/test.repo
[BaseOS]
name=BaseOS
baseurl=http://10.0.0.8/centos/8/BaseOS
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[AppStream]
name=Appstream
baseurl=http://10.0.0.8/centos/8/AppStream/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[extras]
name=extras
baseurl=http://10.0.0.8/centos/extras/
[epel]
name=epel
baseurl=http://10.0.0.8/epel/
gpgkey=http://10.0.0.8/epel/RPM-GPG-KEY-EPEL-8

[root@repo-client ~]# yum repolist
extras       1.6 MB/s | 4.9 kB      00:00
epel           88 MB/s | 6.2 MB  00:00
repo id        repo name           status
AppStream      Appstream            4,755
BaseOS      BaseOS             1,659
epel        epel              5,206
extras          extras              12

[root@repo-client ~]# dnf install openvpn

dnf 介绍

DNF,即DaNdiFied,是新一代的RPM软件包管理器。DNF 发行日期是2015年5月11日,DNF 包管理器采用Python 编写,发行许可为GPL v2,首先出现在Fedora 18 发行版中。在 RHEL 8.0 版本正式取代了 YUM,DNF包管理器克服了YUM包管理器的一些瓶颈,提升了包括用户体验,内存占用,依赖分析,运行速度等

配置文件:

/etc/dnf/dnf.conf

仓库文件

/etc/yum.repos.d/*.repo

日志

/var/log/dnf.rpm.log
/var/log/dnf.log

DNF 使用帮助:man dnf
dnf 用法与yum一致

dnf [options] <command> [<arguments>...]
dnf --version
dnf repolist
dnf reposync
dnf install httpd
dnf remove httpd
dnf clean all
dnf makecache
dnf list installed
dnf list available
dnf search nano
dnf history undo 1

CentOS 7 使用 dnf ,下载并安装下面包

wget http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/dnf-conf-0.6.4-2.sdl7.noarch.rpm

wget http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/dnf-0.6.4-2.sdl7.noarch.rpm

wget http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/python-dnf-0.6.4-2.sdl7.noarch.rpm

wget https://mirrors.aliyun.com/centos/7/extras/x86_64/Packages/python2-libcomps-0.1.8-12.el7.x86_64.rpm

wget https://mirrors.aliyun.com/centos/7/extras/x86_64/Packages/libcomps-0.1.8-12.el7.x86_64.rpm

yum 和 dnf 失败最主要原因:
1)yum的配置文件格式或路径错误
  解决方法:检查/etc/yum.repos.d/*.repo文件格式
2)yum cache
  解决方法:yum clean all
3)网络不通:
  解决方法:网卡配置

 

程序包编译

程序包编译安装:
源代码-->预处理-->编译-->汇编-->链接-->执行

多文件:文件中的代码之间,很可能存在跨文件依赖关系

开源程序源代码的获取

项目官方自建站点:
  apache.org (ASF:Apache Software Foundation)
  mariadb.org
  ...
代码托管:
  Github.com
  gitee.com
  SourceForge.net
  code.google.com

编译源码的项目工具

1)C、C++的源码编译:使用 make 项目管理器
  configure脚本 --> Makefile.in --> Makefile
  相关开发工具:
  autoconf: 生成configure脚本
  automake:生成Makefile.in

2)java的源码编译: 使用 maven

C 语言源代码编译安装过程

利用编译工具,通常只需要三个大的步骤
1)./configure
  (1) 通过选项传递参数,指定安装路径、启用特性等;执行时会参考用户的指定以及Makefile.in文件生成Makefile
  (2) 检查依赖到的外部环境,如依赖的软件包

2)make 根据Makefile文件,会检测依赖的环境,进行构建应用程序,

3)make install 复制文件到相应路径

注意:安装前可以通过查看README,INSTALL获取帮助

编译安装准备

准备:安装相关的依赖包
1)开发工具:make, gcc (c/c++编译器GNU C Complier)

2)开发环境:开发库(glibc:标准库),头文件,可安装开发包组 Development Tools

3)软件相关依赖包

生产实践:基于最小化安装的系统建议安装下面相关包

yum install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages

编译安装

第一步:运行 configure 脚本,生成Makefile 文件
其选项主要功能:
  可以指定安装位置
  指定启用的特性
获取其支持使用的选项

./configure --help

选项分类:
 1)安装路径设定:
  --prefix=/PATH:指定默认安装位置,默认为/usr/local/
  --sysconfdir=/PATH:配置文件安装位置
  System types:支持交叉编译

 2)软件特性和相关指定:
  Optional Features: 可选特性
    --disable-FEATURE
    --enable-FEATURE[=ARG]

  Optional Packages: 可选包
    --with-PACKAGE[=ARG] 依赖包
    --without-PACKAGE 禁用依赖关系

注意:通常被编译操作依赖的程序包,需要安装此程序包的“开发”组件,其包名一般类似于namedevel-VERSION

第二步:make

第三步:make install

安装后的配置

1. 二进制程序目录导入至PATH环境变量中
  编辑文件/etc/profile.d/NAME.sh

export PATH=/PATH/TO/BIN:$PATH

2. 相关用户及文件
  有些开源软件编译完成后,还需要创建相关的用户及文件
3. 导入帮助手册
  编辑/etc/man.config|man_db.conf文件,添加一个MANPATH

编译安装实战案例

范例:CentOS 7 编译安装 tree1.8

# 1 安装相关的依赖包
[root@centos7 ~]# yum install gcc make

# 2 下载源码并解压
[root@centos7 ~]# tar xvf tree-1.8.0.tgz

# 3 进入解压缩的目录,README和INSTALL

[root@centos7 ~]# cd tree-1.8.0/

[root@centos7 tree-1.8.0]# cat README

[root@centos7 tree-1.8.0]# cat INSTALL

# 4 修改源码的版本号
[root@centos7 tree-1.8.0]# sed -i 's#v1\.8\.0#v.8.8.8#' tree.c

# 5 编译准备
[root@centos7 tree-1.8.0]# vim Makefile
prefix = /apps/tree

# 6 编译
[root@centos7 tree-1.8.0]# make

# 7 安装
[root@centos7 tree-1.8.0]# make install

# 8 修改PATH变量
# 默认无法直接运行tree
[root@centos7 ~]# tree
-bash: tree: command not found

[root@centos7 ~]# echo 'PATH=/apps/tree/bin:$PATH' > /etc/profile.d/tree.sh

[root@centos7 ~]# . /etc/profile.d/tree.sh

# 或者利用软链接实现
[root@centos7 ~]# ln -s /apps/tree/bin/tree /usr/local/bin

# 9 验证结果
[root@centos7 ~]# tree --version
tree v8.8.8 (c) 1996 - 2018 by Steve Baker, Thomas Moore, Francesc Rocher,
Florian Sesser, Kyosuke Tokoro

# 10 添加man帮助
# 默认无法查看man
[root@centos7 ~]# man tree
No manual entry for tree

[root@centos7 ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /apps/tree/man

[root@centos7 ~]# man tree
# 11 运行tree查看生成的文件列表

[root@centos7 ~]# tree /apps/tree
/apps/tree
├── bin
│   └── tree
└── man
        └── man1
     └── tree.1
3 directories, 2 files

范例:CentOS 8 编译安装 cmatrix

# 1 安装相关包
[root@centos8 ~]# dnf install gcc make autoconf ncurses-devel

# 2 下载并解压缩包
[root@centos8 ~]# cd /usr/local/src
[root@centos8 ~]# wget https://github.com/abishekvashok/cmatrix/releases/download/v2.0/cmatrix-v2.0-Butterscotch.tar
[root@centos8 ~]# tar xvf cmatrix-v2.0-Butterscotch.tar

# 3 配置
[root@centos8 ~]# cd cmatrix
[root@centos8 cmatrix]# ./configure --prefix=/apps/cmatrix

# 4 编译并安装
[root@centos8 cmatrix]# make && make install

# 5 配置环境
[root@centos8 ~]# echo 'PATH=/apps/cmatrix/bin:$PATH' > /etc/profile.d/cmatrix.sh
[root@centos8 ~]# . /etc/profile.d/cmatrix.sh

# 或者用软链接实现
[root@centos8 ~]# ln -sv /apps/cmatrix/bin/cmatrix /usr/local/bin/
# 6 运行
[root@centos8 ~]# cmatrix -a -b -C yellow

# 7 帮助
[root@centos8 ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /apps/cmatrix/share/man
[root@centos8 ~]# man cmatrix

范例:centos8 编译安装 httpd-2.4.43

#安装前准备:关闭防火墙和SELinux
# 1 安装包
[root@centos8 ~]# dnf install gcc make autoconf apr-devel apr-util-devel pcredevel openssl-devel redhat-rpm-config

# 2 下载并解压缩包
[root@centos8 ~]# tar xvf httpd-2.4.43.tar.bz2 -C /usr/local/src

# 3 配置
[root@centos8 ~]# cd /usr/local/src/httpd-2.4.43/
[root@centos8 httpd-2.4.43]#./configure --prefix=/apps/httpd --sysconfdir=/etc/httpd --enable-ssl

# 4 编译并安装
[root@centos8 httpd-2.4.43]# make -j 4 && make install

# 5 配置环境
[root@centos8 ~]# echo 'PATH=/apps/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
[root@centos8 ~]#. /etc/profile.d/httpd.sh

# 6运行
[root@centos8 ~]# apachectl start

# 7 指定用apache用户运行
[root@centos8 ~]# useradd -r -s /sbin/nologin -d /var/www -c Apache -u 48 apache
[root@centos8 ~]# vim /etc/httpd/httpd.conf
user apache
group apache

# 8生效和验证
[root@centos8 ~]# apachectl restart

# 9查看进程
[root@centos8 ~]# ps aux

# 10用浏览器打开以下地址,可以看下面页面

 

Ubuntu 软件管理

Debian软件包通常为预编译的二进制格式的扩展名“.deb”,类似rpm文件,因此安装快速,无需编译软件。包文件包括特定功能或软件所必需的文件、元数据和指令

1)dpkg:package manager for Debian,类似于rpm, dpkg是基于Debian的系统的包管理器。可以安装,删除和构建软件包,但无法自动下载和安装软件包或其依赖项

2)apt:Advanced Packaging Tool,功能强大的软件管理工具,甚至可升级整个Ubuntu的系统,基于客户/服务器架构,类似于yum

API 工作原理

在服务器上先复制所有DEB包,然后用APT的分析工具genbasedir根据每个DEB 包的包头(Header)信息对所有的DEB包进行分析,并将该分析结果记录在文件夹base内的一个DEB 索引清单文件中,一旦APT 服务器内的DEB有所变动,要使用genbasedir产生新的DEB索引清单。客户端在进行安装或升级时先要查询DEB索引清单,从而获知所有具有依赖关系的软件包,并一同下载到客户端以便安装。当客户端需要安装、升级或删除某个软件包时,客户端计算机取得DEB索引清单压缩文件后,会将其解压置放于 /var/cache/apt/,而客户端使用apt-get install或apt-get upgrade命令的时候,就会将这个文件夹内的数据和客户端计算机内的DEB数据库比对,知道哪些DEB已安装、未安装或是可以升级的

dpkg 包管理器

帮助参看:man dpkg

dpkg 常见用法

# 安装包
dpkg -i package.deb

# 删除包,不建议,不自动卸载依赖于它的包
dpkg -r package

# 删除包(包括配置文件)
dpkg -P package

# 列出当前已安装的包,类似rpm -qa
dpkg -l

# 显示该包的简要说明,类似rpm –qi
dpkg -l package

# 列出该包的状态,包括详细信息,类似rpm –qi
dpkg -s package

# 列出该包中所包含的文件,类似rpm –ql
dpkg -L package

# 搜索包含pattern的包,类似rpm –qf
dpkg -S <pattern>

# 配置包,-a 使用,配置所有没有配置的软件包
dpkg --configure package

# 列出 deb 包的内容,类似rpm –qpl
dpkg -c package.deb

# 解开 deb 包的内容
dpkg --unpack package.deb

范例

# 列出系统上安装的所有软件包
dpkg -l

# 列出软件包安装的文件
dpkg -L bash

# 查看/bin/bash来自于哪个软件包
dpkg -S /bin/bash

# 安装本地的 .deb 文件
dpkg -i /mnt/cdrom/pool/main/z/zip/zip_3.0-11build1_amd64.deb

# 卸载软件包

dpkg -r zip

注意:一般建议不要使用dpkg卸载软件包。因为删除包时,其它依赖它的包不会卸载,并且可能无法再正常运行

apt

Debian 使用apt 工具集来管理包系统,apt-get 是其中一个常用的命令行工具,另外一款较为流行的命令行与 GUI 兼顾的工具是 aptitude ,之前最常用的 Linux 包管理命令都被分散在了 apt-get、aptcache和 apt-config 这三条命令中在 2014 年apt 命令发布第一个稳定版,Ubuntu 16.04 引入新特性之一便是 apt 命令,apt 命令解决了命令过于分散的问题,它包括 apt-get 命令出现以来使用最广泛的功能选项,以及 apt-cache 和 aptconfig命令中很少用到的功能。在使用 apt 命令时,用户不必再由 apt-get 转到 apt-cache 或 aptconfig,提供管理软件包所需的必要选项apt 相当于 apt-get、apt-cache 和 apt-config 中最常用命令选项的集合apt 具有更精减但足够的命令选项,而且参数选项的组织方式更为有效。此外,启用的几个特性也非常有帮助。例如:可以在使用 apt 命令安装或删除程序时看到进度条,apt 还会在更新存储库数据库时提示用户可升级的软件包个数apt 与 apt-get 有一些类似的命令选项,但它并不能完全向下兼容 apt-get 命令,也即可用 apt 替换部分apt-get 系列命令,但不是全部

apt 命令用法

查看帮助:apt help

apt与apt-get命令对比

apt 命令       被取代的命令            命令的功能
apt install        apt-get install           安装软件包
apt remove      apt-get remove        移除软件包
apt purge      apt-get purge           移除软件包及配置文件
apt update       apt-get update        刷新存储库索引
apt upgrade     apt-get upgrade           升级所有可升级的软件包
apt autoremove   apt-get autoremove     自动删除不需要的包
apt full-upgrade     apt-get dist-upgrade       在升级软件包时自动处理依赖关系
apt search        apt-cache search         搜索应用程序
apt show      apt-cache show           显示安装细节

apt 特有的命令

列出包含条件的包(已安装,可升级等)

apt list 

编辑源列表
apt edit-sources 

APT 包索引配置文件

/etc/apt/sources.list
/etc/apt/sources.list.d

可以修改上面文件为国内的安装源,提高速度
参考链接:https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.53322f70fghx56

apt命令操作(如安装和删除软件包)日志文件

/var/log/dpkg.log

范例

# 安装包:
apt install tree zip

# 安装图形桌面
apt install ubuntu-desktop

# 删除包:
apt remove tree zip

# 说明:apt remove中添加--purge选项会删除包配置文件,谨慎使用
# 更新包索引,相当于yum clean all;yum makecache
apt update

# 升级包:要升级系统,请首先更新软件包索引,再升级
apt upgrade

# apt列出仓库软件包,等于yum list
apt list

# 搜索安装包
apt search nginx

# 查看某个安装包的详细信息
apt show apache2

# 在线安装软件包
apt install apache2

# 卸载单个软件包但是保留配置⽂件
apt remove apache2

# 删除安装包并解决依赖关系
apt autoremove apache2

# 更新本地软件包列表索引,修改了apt仓库后必须执⾏
apt update

# 卸载单个软件包删除配置⽂件
apt purge apache2

# 升级所有已安装且可升级到新版本的软件包
apt upgrade

# 升级整个系统,必要时可以移除旧软件包。
apt full-upgrade

# 编辑source源⽂件
apt edit-sources

# 查看仓库中软件包有哪些版本可以安装
apt-cache madison nginx

# 安装软件包的时候指定安装具体的版本
apt install nginx=1.14.0-0ubuntu1.6

软件管理案例

#显示系统安装包的统计信息,可以统计已经安装包的数量,大小,占用空间等
# apt-cache stats
[root@ubuntu1804 ~]# apt-cache stats
Total package names: 84873 (1,697 k)
Total package structures: 126998 (5,588 k)
Normal packages: 91623
Pure virtual packages: 2648
Single virtual packages: 10275

...省略

#显示xxx包的信息,可以看到某个包的源、版本等信息
# apt-cache show xxx #更详细
# apt show xxx
[root@ubuntu1804 ~]# apt show keepalived
Package: keepalived
Version: 1:1.3.9-1ubuntu0.18.04.2
Priority: optional
Section: admin
Origin: Ubuntu
Maintainer: Ubuntu Developers...省略

[root@ubuntu1804 ~]# apt-cache show keepalived
Package: keepalived
Architecture: amd64
Version: 1:1.3.9-1ubuntu0.18.04.2
Priority: optional
Section: admin
Origin: Ubuntu
Maintainer: Ubuntu Developers...省略

# 查找文件属于哪个包
# dpkg -S filename :在当前安装的包里查找文件
[root@ubuntu1804 ~]# dpkg -S /bin/ls
coreutils: /bin/ls
# 在所有源包里查找文件。(包含未安装的包)
# apt-file search filename

[root@ubuntu1804 ~]# apt search /bin/tree
Sorting... Done
Full Text Search... Done
[root@ubuntu1804 ~]# apt -y install apt-file
[root@ubuntu1804 ~]# apt-flle upate
[root@ubuntu1804 ~]# apt-file search /bin/tree
arb: /usr/lib/arb/bin/treedist
asp.net-examples: /usr/share/asp.net-demos/bin/treeview.dll
beast-mcmc: /usr/bin/treeannotator
beast-mcmc: /usr/bin/treestat
beast2-mcmc:...省略

# 查询软件xxx依赖哪些包
apt depends xxx
# apt-cache depends xxx
[root@ubuntu1804 ~]# apt depends keepalived
keepalived
Depends: iproute2
iproute2:i386
Depends: libc6 (>= 2.27)
Depends: libglib2.0-0 (>= 2.26.0)

...省略

#查询软件xxx被哪些包依赖
# apt rdepends xxx

# apt-cache rdepends xxx
[root@ubuntu1804 ~]# apt rdepends bash
bash
Reverse Depends:
Depends: bash-builtins (= 4.4.18-2ubuntu1.2)
bash:i386
Recommends: plasma-sdk (>= 4.3)...省略

Ubuntu 建议安装的常用包

[root@ubuntu1804 ~]# apt purge ufw lxd lxd-client lxcfs liblxc-common

[root@ubuntu1804 ~]# apt install iproute2 ntpdate tcpdump telnet traceroute nfskernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-devzlib1g-dev gcc openssh-server iotop unzip zip

posted @ 2020-07-02 12:47  空白的旋律  阅读(604)  评论(0编辑  收藏  举报