Linux deb 软件包管理

点击访问 Linux 命令大全 >>

deb 格式是 Debian 系统(包含 Debian 和 Ubuntu)专属安装包格式,配合 apt 软件管理系统,成为了当前在 Linux 下非常流行的一种安装包。

deb 将包文件信息以及包内容,经过 gzip 和 tar 打包而成。处理这些包的经典程序是 dpkg,经常是通过 Debian 的 apt-get 来运作。通过 Alien 工具,可以将 deb 包转换成 rpm、tar.gz 格式。

deb 包在 Linux 操作系统中类似于 windows 中的软件包(exe),几乎不需要什么复杂的编译即可通过鼠标点击安装使用。

deb包格式

deb 是 Unix 系统(其实主要是 Linux)下的安装包,基于 tar 包,因此本身会记录文件的权限(读/写/可执行)以及所有者/用户组。由于 Unix 类系统对权限、所有者、组的严格要求,而 deb 格式安装包又经常会涉及到系统比较底层的操作,所以权限等的设置尤其重要。

deb 包本身有三部分组成:

(1)数据包,包含实际安装的程序数据,文件名为 data.tar.XXX;data.tar.XXX 包含的是实际安装的程序数据,而在安装过程中,该包里的数据会被直接解压到根目录(即 / ),因此在打包之前需要根据文件所在位置设置好相应的文件/目录树。

(2)安装信息及控制脚本包,包含 deb 的安装说明,标识,脚本等,文件名为 control.tar.gz;而 control.tar.gz 则包含了一个 deb 安装的时候所需要的控制信息。一般有 5 个文件:

  • control 用了记录软件标识,版本号,平台,依赖信息等数据;

  • preinst 在解包data.tar.gz 前运行的脚本;

  • postinst 在解包数据后运行的脚本;

  • prerm 卸载时,在删除文件之前运行的脚本;

  • postrm 在删除文件之后运行的脚本;

(3)deb 文件的一些二进制数据,包括文件头等信息,一般看不到,在某些软件中打开可以看到;

例如:下载 tree 软件包,下载地址为 https://www.rpmseek.com/rpm-dl/tree_1.6.0-1_amd64.html?hl=com&cs=tree:PN:0:0:0:0:0:13629564 ,下载后使用压缩工具打开,内容如下图:

【注意】

deb 本身可以使用不同的压缩方式。tar 格式并不是一种压缩格式,而是直接把分散的文件和目录集合在一起,并记录其权限等数据信息。之前提到过的 data.tar.XXX,这里 XXX 就是经过压缩后的后缀名。deb 默认使用的压缩格式为 gzip 格式,所以最常见的就是 data.tar.gz。常有的压缩格式还有 bzip2 和 lzma,其中 lzma 压缩率最高,但压缩需要的 CPU 资源和时间都比较长。

deb包安装(dpkg命令)

dpkg 命令是“Debian Packager ”的简写。为 Debian 专门开发的套件管理系统,方便软件的安装、更新及移除。所有源自 Debian 的 Linux  发行版都使用 dpkg,例如:Ubuntu、Knoppix 等。

dpkg 是 Debian 软件包管理器的基础,它被伊恩·默多克创建于1993年。dpkg 与 RPM 十分相似,同样被用于安装、卸载和供给 .deb 软件包相关的信息。

dpkg 本身是一个底层的工具。上层的工具,如 apt 被用于从远程获取软件包以及处理复杂的软件包关系。

注意:下面的命令均在 Ubuntu 20 LTS 下面进行执行。

列出已安装的软件

可以使用命令列出当前系统中已经安装的软件以及软件包的状态。如:

snow@ubuntu:~$ dpkg -l
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                       Version                             Architecture Description
+++-==========================================-===================================-============-==========================================>
ii  accountsservice                            0.6.55-0ubuntu12~20.04.1            amd64        query and manipulate user account informat>
ii  acl                                        2.2.53-6                            amd64        access control list - utilities
ii  acpi-support                               0.143                               amd64        scripts for handling many ACPI events
ii  acpid                                      1:2.0.32-1ubuntu1                   amd64        Advanced Configuration and Power Interface>
ii  adduser                                    3.118ubuntu2                        all          add and remove users and groups
ii  adwaita-icon-theme                         3.36.1-2ubuntu0.20.04.2             all          default icon theme of GNOME (small subset)
ii  aisleriot                                  1:3.22.9-1                          amd64        GNOME solitaire card game collection
ii  alsa-base                                  1.0.25+dfsg-0ubuntu5                all          ALSA driver configuration files
ii  alsa-topology-conf                         1.2.2-1                             all          ALSA topology configuration files
ii  alsa-ucm-conf                              1.2.2-1ubuntu0.1                    all          ALSA Use Case Manager configuration files
ii  alsa-utils                                 1.2.2-1ubuntu1                      amd64        Utilities for configuring and using ALSA
ii  amd64-microcode                            3.20191218.1ubuntu1                 amd64        Processor microcode firmware for AMD CPUs
ii  anacron                                    2.3-29                              amd64        cron-like program that doesn't go by tim
......

该命令每行输出中的第一列 ii 表示软件包的安装和配置状态,其格式如下:期望状态|当前状态|错误,其中:

期望状态有以下几种

  • u:即 unknown,软件包未安装且用户未请求安装

  • i:即 install,用户请求安装该软件包

  • r:即 remove,用户请求卸载该软件包

  • p:即 purge,用户请求卸载该软件包并清理配置文件

  • h:即 hold,用户请求保持续当前软件包版本

当前状态有以下几种:

  • n:即 not-installed,软件包未安装

  • i:即 installed,软件包已安装并完成配置

  • c:即 config-files,软件包已经被卸载,但是其配置文件未清理

  • u:即 unpacked,软件包已经被解压缩,但还未配置

  • f:即 half-configured,配置软件包时出现错误

  • w:即 triggers-awaited,触发器等待

  • t:即 triggers-pending,触发器未决

错误状态有以下几种:

  • h:软件包被强制保持

  • r:即 reinstall-required,需要卸载并重新安装

  • x:软件包被破坏

因此 ii 表示该软件需要安装且已经安装,没有出现错误;iu 表示已经安装该软件,但未正确配置;rc 表示该软件已经被删除,但配置文件未清理。

查看处于 rc 状态的软件包:

snow@ubuntu:~$ dpkg -l | grep ^rc
rc  linux-image-5.4.0-26-generic               5.4.0-26.30                         amd64        Signed kernel image generic
rc  linux-modules-5.4.0-26-generic             5.4.0-26.30                         amd64        Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP
rc  linux-modules-extra-5.4.0-26-generic       5.4.0-26.30                         amd64        Linux kernel extra modules for version 5.4.0 on 64 bit x86 SMP

此外,还可以使用 dpkg -l <package_name_pattern> 命令筛选出名称中包含指定模式的软件包。如下:

snow@ubuntu:~$ dpkg -l vim
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version             Architecture Description
+++-==============-===================-============-=================================
ii  vim            2:8.1.2269-1ubuntu5 amd64        Vi IMproved - enhanced vi editor

安装软件

dpkg 命令可以使用 -i 选项用来安装 deb 安装包。例如:

snow@ubuntu:~$ sudo dpkg -i tree_1.6.0-1_amd64.deb
Selecting previously unselected package tree.
(Reading database ... 189833 files and directories currently installed.)
Preparing to unpack tree_1.6.0-1_amd64.deb ...
Unpacking tree (1.6.0-1) ...
Setting up tree (1.6.0-1) ...
Processing triggers for man-db (2.9.1-1) ...

上面实例中,我们成功安装了 tree 命令。使用 tree 命令查看当前目录的树形结构,如下:

snow@ubuntu:~$ tree
.
├── deb
│   ├── net-tools_1.60-25ubuntu2_amd64.deb
│   ├── tree_1.6.0-1_amd64.deb
│   └── vim_7.4.052-1ubuntu3_amd64.deb
├── Desktop
├── Documents
├── Downloads
├── Music
├── Pictures
├── Public
├── snap
│   └── snap-store
│       ├── 433
│       ├── 467
│       ├── common
│       └── current -> 433
├── Templates
└── Videos

15 directories, 3 files

卸载软件

dpkg 命令的 -r 选项可以用来卸载已安装的软件包,此时只需要指定软件的名称即可。例如:

snow@ubuntu:~$ sudo dpkg -r tree
[sudo] password for snow:
(Reading database ... 189840 files and directories currently installed.)
Removing tree (1.8.0-1) ...
Processing triggers for man-db (2.9.1-1) ...
snow@ubuntu:~$ tree
-bash: /usr/bin/tree: No such file or directory

注意:-r 选项只会移除指定的软件包而不对其配置文件产生影响,可以使用 -P 选项在删除软件包的同时清理配置文件。例如:

snow@ubuntu:~$ sudo dpkg -P tree
(Reading database ... 189840 files and directories currently installed.)
Removing tree (1.8.0-1) ...
Processing triggers for man-db (2.9.1-1) ...

查看软件包的内容

dpkg 命令可以使用 -c 选项查看 deb 包中的内容,如下:

snow@ubuntu:~$ dpkg -c tree_1.6.0-1_amd64.deb
drwxr-xr-x root/root         0 2012-05-01 09:43 ./
drwxr-xr-x root/root         0 2012-05-01 09:43 ./usr/
drwxr-xr-x root/root         0 2012-05-01 09:43 ./usr/bin/
-rwxr-xr-x root/root     57176 2012-05-01 09:43 ./usr/bin/tree
drwxr-xr-x root/root         0 2012-05-01 09:43 ./usr/share/
drwxr-xr-x root/root         0 2012-05-01 09:43 ./usr/share/man/
drwxr-xr-x root/root         0 2012-05-01 09:43 ./usr/share/man/man1/
-rw-r--r-- root/root      4100 2012-05-01 09:43 ./usr/share/man/man1/tree.1.gz
drwxr-xr-x root/root         0 2012-05-01 09:43 ./usr/share/doc/
drwxr-xr-x root/root         0 2012-05-01 09:43 ./usr/share/doc/tree/
-rw-r--r-- root/root      2210 2011-06-24 07:34 ./usr/share/doc/tree/TODO
-rw-r--r-- root/root      2425 2012-02-04 05:32 ./usr/share/doc/tree/copyright
-rw-r--r-- root/root      2254 2011-06-24 07:34 ./usr/share/doc/tree/README.gz
-rw-r--r-- root/root      1552 2012-05-01 09:43 ./usr/share/doc/tree/changelog.Debian.gz

上面实例将输出 tree_1.6.0-1_amd64.deb 软件包的内容。

查看软件包(已安装)的详细信息

使用 dpkg -s <package> 命令可以查看指定软件包的详细信息。例如:

snow@ubuntu:~$ dpkg -s tree
Package: tree
Status: install ok installed
Priority: optional
Section: utils
Installed-Size: 106
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Version: 1.6.0-1
Depends: libc6 (>= 2.4)
Description: displays directory tree, in color
 Displays an indented directory tree, using the same color assignments as
 ls, via the LS_COLORS environment variable.
Original-Maintainer: Florian Ernst <florian@debian.org>
Homepage: https://mama.indstate.edu/users/ice/tree/

或者使用 dpkg --status <package> 命令查看,如下:

snow@ubuntu:~$ dpkg --status tree
Package: tree
Status: install ok installed
Priority: optional
Section: utils
Installed-Size: 106
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Version: 1.6.0-1
Depends: libc6 (>= 2.4)
Description: displays directory tree, in color
 Displays an indented directory tree, using the same color assignments as
 ls, via the LS_COLORS environment variable.
Original-Maintainer: Florian Ernst <florian@debian.org>
Homepage: https://mama.indstate.edu/users/ice/tree/

查看软件包的安装位置

使用 dpkg -L <package> 或 dpkg --list-files <package> 命令查看安装包的安装位置。例如:

snow@ubuntu:~$ dpkg -L tree
/.
/usr
/usr/bin
/usr/bin/tree
/usr/share
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/tree.1.gz
/usr/share/doc
/usr/share/doc/tree
/usr/share/doc/tree/TODO
/usr/share/doc/tree/copyright
/usr/share/doc/tree/README.gz
/usr/share/doc/tree/changelog.Debian.gz

上面实例查看了 tree 命令的安装位置信息。

显示dpkg的版本号

dpkg 使用 --version 选项获取 dpkg 命令的版本信息。例如:

now@ubuntu:~/Desktop$ dpkg --version
Debian 'dpkg' package management program version 1.19.7 (amd64).
This is free software; see the GNU General Public License version 2 or
later for copying conditions. There is NO warranty.

搜索指定包里面的文件(模糊查询)

dpkg 使用 -S 选项模糊查询包里面包含指定名称的文件。例如:

snow@ubuntu:~$ dpkg -S presubj
libuno-salhelpergcc3-3: /usr/share/bug/libuno-salhelpergcc3-3/presubj
fonts-sil-abyssinica: /usr/share/bug/fonts-sil-abyssinica/presubj
fonts-liberation2: /usr/share/bug/fonts-liberation2/presubj
libreoffice-style-elementary: /usr/share/bug/libreoffice-style-elementary/presubj
fonts-opensymbol: /usr/share/bug/fonts-opensymbol/presubj
libreoffice-writer: /usr/share/bug/libreoffice-writer/presubj
bash-completion: /usr/share/bug/bash-completion/presubj
hplip: /usr/share/bug/hplip/presubj
......

上面实例中,查询所有包中包含 presubj 文件的安装包。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号