首页 Ubuntu 16.04 查看软件包的命令技巧
文章
取消

Ubuntu 16.04 查看软件包的命令技巧

Ubuntu作为一款常用的Linux系统,有着Linux下常用的使用命令行操作模式。下面为大家介绍一些管理软件包的常用命令。下面的命令需要在终端下输入,任何实用‘sudo’作为前缀的命令都需要拥有root访问权限,所以会被提示输入密码。

Ubuntu中软件包的查询方法:

  • Dpkg 使用文本文件来作为数据库.通称在 /var/lib/dpkg 目录下. 通称在 status 文件中存储软件状态,和控制信息. 在 info/ 目录下备份控制文件, 并在其下的 .list 文件中记录安装文件清单, 其下的 .mdasums 保存文件的 MD5 编码.
1. 查看已经安装了哪些包
  • dpkg -l
  • 如果系统使用时间长,安装了许多包查看不便时,可以使用翻页查看
  • dpkg -l | less
1
2
3
4
5
6
7
8
9
10
11
yexiang@ubuntu:<etc>$ dpkg -l | less
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.40-2ubuntu11.3                         amd64        query and manipulate user account information
ii  adduser                             3.113+nmu3ubuntu4                          all          add and remove users and groups
ii  amd64-microcode                     3.20180524.1~ubuntu0.16.04.1               amd64        Processor microcode firmware for AMD CPUs
ii  apparmor                            2.10.95-0ubuntu2.10                        amd64        user-space parser utility for AppArmor
ii  apt                                 1.2.15ubuntu0.2                            amd64        commandline package manager

每条记录对应一个软件包, 注意每条记录的第一, 二, 三个字符. 这就是软件包的状态标识, 后边依此是软件包名称, 版本号, 和简单描述

  • 第一字符为期望值,它包括:
    • u 状态未知,这意味着软件包未安装,并且用户也未发出安装请求
    • i 用户请求安装软件包
    • r 用户请求卸载软件包
    • p 用户请求清除软件包
    • h 用户请求保持软件包版本锁定
  • 第二列,是软件包的当前状态.此列包括软件包的六种状态
    • n 软件包未安装
    • i 软件包安装并完成配置
    • c 软件包以前安装过,现在删除了,但是它的配置文件还留在系统中
    • u 软件包被解包,但还未配置
    • f 试图配置软件包,但是失败了
    • h 软件包安装,但是但是没有成功
  • 第三列标识错误状态,可以总结为四种状态. 第一种状态标识没有问题,为空. 其它三种符号则标识相应问题
    • h 软件包被强制保持,因为有其它软件包依赖需求,无法升级
    • r 软件包被破坏,可能需要重新安装才能正常使用(包括删除)
    • x 软包件被破坏,并且被强制保持

也可以以统配符模式进行模糊查询, 比如我要查找以nano字符开始的所有软件包: dpkg -l nano*

2. 查看软件xxx安装内容
  • dpkg -L xxx
  • 可以查看该软件安装的目录和文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
yexiang@ubuntu:<etc>$ dpkg -L lirc 
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/lirc
/usr/share/doc/lirc/html
...
/usr/share/lirc
/usr/share/lirc/extras
/usr/share/lirc/extras/transmitters
/usr/share/lirc/extras/transmitters/sky
/usr/share/lirc/extras/transmitters/sky/general.conf
...
/usr/bin
/usr/bin/irpty
/usr/bin/irw
/usr/bin/irsend
...
3. 清除所有已删除包的残余配置文件
1
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
  • 可以清除一些残留无用的配置
1
2
3
4
5
6
7
yexiang@ubuntu:<etc>$ dpkg -l |grep ^rc
rc  tftpd                               0.17-18ubuntu2                             amd64        Trivial file transfer protocol server
rc  xinetd                              1:2.3.15-6                                 amd64        replacement for inetd with many enhancements
 
yexiang@ubuntu:<etc>$ dpkg -l |grep ^rc|awk '{print $2}' 
tftpd
xinetd
4. 显示系统安装包的统计信息
  • apt-cache stats
  • 可以统计已经安装包的数量,大小,占用空间等
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
yexiang@ubuntu:<etc>$ sudo apt-cache stats 
[sudo] password for yexiang: 
Total package names: 74107 (1,482 k)
Total package structures: 114321 (5,030 k)
  Normal packages: 85242
  Pure virtual packages: 2283
  Single virtual packages: 8846
  Mixed virtual packages: 2916
  Missing: 15034
Total distinct versions: 101333 (8,107 k)
Total distinct descriptions: 160450 (3,851 k)
Total dependencies: 748407/230454 (18.7 M)
Total ver/file relations: 12358 (297 k)
Total Desc/File relations: 29378 (705 k)
Total Provides mappings: 30028 (721 k)
Total globbed strings: 164906 (3,720 k)
Total slack space: 28.8 k
Total space accounted for: 43.0 M
Total buckets in PkgHashTable: 50503
  Unused: 11618
  Used: 38885
  Utilization: 76.9954%
  Average entries: 2.93998
  Longest: 15
  Shortest: 1
Total buckets in GrpHashTable: 50503
  Unused: 11618
  Used: 38885
  Utilization: 76.9954%
  Average entries: 1.9058
  Longest: 8
  Shortest: 1
5. 显示xxx包的信息
  • apt-cache show xxx
  • 可以看到某个包的源、版本等信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
yexiang@ubuntu:<etc>$ sudo apt-cache show lirc
Package: lirc
Priority: extra
Section: universe/utils
Installed-Size: 2131
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: lirc Maintainer Team <pkg-lirc-maint@lists.alioth.debian.org>
Architecture: amd64
Version: 0.9.0-0ubuntu6
Replaces: lirc-modules-source
Depends: libasound2 (>= 1.0.16), libc6 (>= 2.15), libftdi1, liblircclient0 (>= 0.6.4), libportaudio2 (>= 19+svn20101113), libusb-0.1-4 (>= 2:0.1.12), debconf (>= 0.5) | debconf-2.0, lsb-base, setserial
Recommends: udev
Suggests: lirc-x
Conflicts: lirc-modules-source, makedev (<< 2.3.1-88)
Filename: pool/universe/l/lirc/lirc_0.9.0-0ubuntu6_amd64.deb
Size: 376216
MD5sum: a8d129090da1aa2bf7bf86d17b70f9b2
SHA1: 2eef35b0a9c1ac446b0d21e857ce4f7e43f704e0
SHA256: aa0e6dc9eca6530684b13ff40d07042a6521be388f22be9236dc048c90a95e70
Description-en: infra-red remote control support
 LIRC stands for 'Linux Infra-red Remote Control'.
 .
 This package provides the daemons and some utilities to support infra-red
 remote controls under Linux.
Description-md5: 13afcdab0a98aaea919ab1053fe3d187
Homepage: http://lirc.org
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Supported: 3y
Task: mythbuntu-frontend, mythbuntu-desktop, mythbuntu-backend-slave, mythbuntu-backend-master
6. 查找文件属于哪个包
  • dpkg -S filename
  • 在当前安装的包里查找文件
1
2
3
4
5
6
7
8
9
10
11
yexiang@ubuntu:<etc>$ dpkg -S bt829.h
linux-headers-4.4.0-59-generic: /usr/src/linux-headers-4.4.0-59-generic/include/config/lirc/bt829.h
linux-headers-4.4.0-81-generic: /usr/src/linux-headers-4.4.0-81-generic/include/config/lirc/bt829.h
linux-headers-4.4.0-134-generic: /usr/src/linux-headers-4.4.0-134-generic/include/config/lirc/bt829.h
linux-headers-4.4.0-128-generic: /usr/src/linux-headers-4.4.0-128-generic/include/config/lirc/bt829.h
linux-headers-4.4.0-112-generic: /usr/src/linux-headers-4.4.0-112-generic/include/config/lirc/bt829.h
linux-headers-4.4.0-21-generic: /usr/src/linux-headers-4.4.0-21-generic/include/config/lirc/bt829.h
linux-headers-4.4.0-141-generic: /usr/src/linux-headers-4.4.0-141-generic/include/config/lirc/bt829.h
linux-headers-4.4.0-130-generic: /usr/src/linux-headers-4.4.0-130-generic/include/config/lirc/bt829.h
linux-headers-4.4.0-57-generic: /usr/src/linux-headers-4.4.0-57-generic/include/config/lirc/bt829.h
linux-headers-4.4.0-104-generic: /usr/src/linux-headers-4.4.0-104-generic/include/config/lirc/bt829.h
7. 查询软件xxx依赖哪些包
  • apt-cache depends xxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
yexiang@ubuntu:<etc>$ apt-cache depends lirc
lirc
  Depends: libasound2
  Depends: libc6
  Depends: libftdi1
  Depends: liblircclient0
  Depends: libportaudio2
  Depends: libusb-0.1-4
 |Depends: debconf
  Depends: <debconf-2.0>
    cdebconf
    debconf
  Depends: lsb-base
  Depends: setserial
  Conflicts: <lirc-modules-source>
  Conflicts: makedev
  Recommends: udev
    udev:i386
  Suggests: lirc-x
  Replaces: <lirc-modules-source>
8. 查询软件xxx被哪些包依赖
  • apt-cache rdepends xxx
1
2
3
4
5
6
7
8
9
10
11
12
13
yexiang@ubuntu:<etc>$ apt-cache rdepends lirc
lirc
Reverse Depends:
  lirc-x
  vdr
  mythbuntu-live-autostart
  mythbuntu-lirc-generator
  lirc-x
  liblircclient0
  kremotecontrol
  inputlirc
  freevo-lirc
  banshee-extension-lirc
9. 在所有源包里查找文件 (包含未安装的包)
  • apt-cache search filename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
yexiang@ubuntu:<etc>$ sudo apt-cache search skype
libopus-dev - Opus codec library development files
libopus0 - Opus codec runtime library
libopus-ocaml - OCaml interface to the opus library -- runtime files
libopus-ocaml-dev - OCaml interface to the opus library -- developpement files
libsipwitch-dev - secure peer-to-peer SIP VoIP server - development files
libsipwitch1 - secure peer-to-peer SIP VoIP server - shared libraries
libsipwitch1-dbg - secure peer-to-peer SIP VoIP server - debug symbols
postgresql-9.5-pgq3 - Skype tools for PostgreSQL replication, londiste and PGQ
python-pgq3 - python framework for Skype tools for PostgreSQL replication
python-skytools3 - python framework for Skype tools for PostgreSQL replication
sipwitch - secure peer-to-peer VoIP server for the SIP protocol
sipwitch-cgi - secure peer-to-peer SIP VoIP server - CGI XML-RPC interface
skytools3 - Skype tools for PostgreSQL replication, londiste and PGQ
skytools3-ticker - Skype tools for PostgreSQL replication, londiste and PGQ
skytools3-walmgr - Skype tools for PostgreSQL replication and failover
empathy-skype - Skype plugin for libpurple messengers (Empathy-specific files)
pidgin-skype - Skype plugin for libpurple messengers (Pidgin-specific files)
pidgin-skype-common - Skype plugin for libpurple messengers (common files)
pidgin-skype-dbg - Skype plugin for libpurple messengers (debug symbols)
python-skype4py - Skype API wrapper for Python
总结:
  • apt-cache search # ——(package 搜索包)
  • apt-cache show #——(package 获取包的相关信息,如说明、大小、版本等)
  • apt-get install # ——(package 安装包)
  • apt-get install # —–(package –reinstall 重新安装包)
  • apt-get -f install # —–(强制安装, “-f = –fix-missing”当是修复安装吧…)
  • apt-get remove #—–(package 删除包)
  • apt-get remove --purge # ——(package 删除包,包括删除配置文件等)
  • apt-get autoremove --purge # —-(package 删除包及其依赖的软件包+配置文件等(只对6.10有效,强烈推荐))
  • apt-get update #——更新源
  • apt-get upgrade #——更新已安装的包
  • apt-get dist-upgrade # ———升级系统
  • apt-get dselect-upgrade #——使用 dselect 升级
  • apt-cache depends #——-(package 了解使用依赖)
  • apt-cache rdepends # ——(package 了解某个具体的依赖,当是查看该包被哪些包依赖吧…)
  • apt-get build-dep # ——(package 安装相关的编译环境)
  • apt-get source #——(package 下载该包的源代码)
  • apt-get clean && apt-get autoclean # ——–清理下载文件的存档 && 只清理过时的包
  • apt-get check #——-检查是否有损坏的依赖
  • dpkg -S filename # —–查找filename属于哪个软件包
  • apt-file search filename # —–查找filename属于哪个软件包
  • apt-file list packagename # —–列出软件包的内容
  • apt-file update # –更新apt-file的数据库
dpkg包管理工具
  • dpkg -r 卸载软件包.不是完全的卸载,它的配置文件还存在.
  • dpkg --info "软件包名" 列出软件包解包后的包名称.
  • dpkg -l 列出当前系统中所有的包.可以和参数less一起使用在分屏查看.
  • dpkg -l|grep -i “软件包名” –查看系统中与”软件包名”相关联的包.
  • dpkg -s 查询已安装的包的详细信息. dpkg -L 查询系统中已安装的软件包所安装的位置.
  • dpkg -S 查询系统中某个文件属于哪个软件包.
  • dpkg -I 查询deb包的详细信息,在一个软件包下载到本地之后看看用不用安装(看一下呗).
  • dpkg -i 手动安装软件包(这个命令并不能解决软件包之前的依赖性问题),如果在安装某一个软件包的时候遇到了软件依赖的问题,可以用apt-get -f install在解决信赖性这个问题.
  • dpkg -reconfigure 重新配置
  • dpkg -P 全部卸载(但是还是不能解决软件包的依赖性的问题)
APT管理工具常用命令
  • apt-cache 加上不同的子命令和参数的使用可以实现查找,显示软件,包信息及包信赖关系等功能.
  • apt-cache stats 显示当前系统所有使用的Debain数据源的统计信息.
  • apt-cache search + "包名",可以查找相关的软件包.
  • apt-cache show + "包名",可以显示指定软件包的详细信息.
  • apt-cache depends +"包名",可以查找软件包的依赖关系.
  • apt-get upgrade 更新系统中所有的包到最新版
  • apt-get install 安装软件包
  • apt-get --reindtall install 重新安装软件包
  • apt-get remove 卸载软件包
  • apt-get --purge remove 完全卸载软件包
  • apt-get clean 清除无用的软件包
  • 在用命令apt-get install之前,是先将软件包下载到/var/cache/apt/archives中,之后再进行安装的.所以我们可以用apt-get clean清除/var/cache/apt/archives目录中的软件包.
Debian的软件包管理工具命令不完全列表
  • Debian系统中所有的包信息都在/var/lib/dpkg下.其中/var/lib/dpkg/info目录中保存了各个软件包的信息及管理文件.每个文件的作用如下:

    • 以 “.conffiles” 结尾的文件记录软件包的配置列表.
    • 以 “.list” 结尾的文件记录了软件包的文件列表,用户可在文件当中找到软件包文件的具体安装位置.
    • 以 “.md5sums” 结尾的文件记录了md5信息,用来进行包的验证的.
    • 以 “.config” 结尾的文件是软件包的安装配置角本.
    • 以 “.postinst” 角本是完成Debian包解开之后的配置工作,通常用来执行所安装软件包相关的命令和服务的重新启动.
    • 以 “.preinst” 角本在Debain解包之前运行,主要作用是是停止作用于即将升级的软件包服务直到软件包安装或和升级完成.
    • 以 “.prerm” 脚本负责停止与软件包关联的daemon服务,在删除软件包关联文件之前执行.
    • 以 “.postrm” 脚本负责修改软件包链接或文件关联,或删除由它创建的文件.
  • /var/lib/dpkg/available 是软件包的描述信息.
  • 包括当前系统中所有使用的Debian安装源中所有的软件包,还包括当前系统中已经安装和未安装的软件包.
本文由作者按照 CC BY 4.0 进行授权