8.1 包管理基础
基于Debian的发行版,比如Ubuntu和Linux Mint,它们PMS工具的底层用的是dpkg命令。这个命令会直接和Linux系统上的PMS交互,用来安装、管理和删除软件包。
基于Red Hat的发行版本,比如Fedora、openSUSE以及Mandriva,在它们PMS的底层的是rpm 命令。类似于dpkg命令,rpm命令能够列出已安装包、安装新包和删除已有软件。
注意, 这两个命令是它们各自PMS的核心,而不是整个PMS自身。许多使用dpkg或rpm方法的Linux发行版在这些基础命令之上构建了其他专有PMS工具来帮助生活简单点。后面几节将带你逐步了解你可能碰到的主流Linux发行版上的各种PMS工具命令。
8.2 基于Debian的系统
dpkg命令是基于Debian系PMS工具的核心。包含在这个PMD中的其他工具有:
apt-get;
apt-cache;
aptitude.
到目前为止,最常用的命令行工具是aptitude,而这是有原因的。aptitude工具本质是apt工具和dpkg的前端。dpkg就一个软件包管理系统工具,而aptitude则是一个完整的软件包管理系统。
命令行下使用aptitude命令会帮助你避免常见的软件安装问题,比如软件依赖关系缺失,系统环境不稳定以及其他一些不必要的麻烦。本节将会介绍在命令行下使用aptitude命令工具。
8.2.1 用aptitude管理软件包
Linux系统管理员面对的一个常见任务是判断系统上已经安装了什么软件包。幸运的是,aptitude有个很方便的交互式界面让这个任务变得很简单。
在shell提示符键入aptitude并按下回车键。你会被切到aptitude的全屏模式,如你在图8-1中看到的。
你可以用用方向键在菜单上移动。选择菜单选项Installed Packages来查看已安装了什么软件包。你可以看到几组软件包,比如编辑器等。每组后面的括号里有个数字,表示这个组包包含多少个软件包。
使用方向键高亮显示一个组,按回车键来查看每个软件包分组。你会看到每个单独的软件包名称以及它们的版本号。在软件包上按回车可以获得更详细的信息,比如软件包的描述、主页、大小和维护人员等。
当你看完了已安装软件包,按q键来退出显示。你台以继续用方向键和回车键来打开或关闭软件包和它们所在的分组。当你想退出了,按几次q键直到你收到弹出的屏幕“Rellay quit aptitude?”.
如果你已经知道了系统上的那些软件包,只想快速显示某个特定包的详细信息,就没有必要到aptitude的交互式界面。你可以在命令行下以单个命令的方式用aptitude:
aptitude show package_name
下面是显示包vim
jingpan@ubuntu:~$ aptitude show vim 软件包: vim 状态: 已安装 自动安装: 否 版本号: 2:7.4.1689-3ubuntu1.2 优先级: 可选 部分: editors 维护者: Ubuntu Developers <[email protected]> 体系: amd64 未压缩尺寸: 2,458 k 依赖于: vim-common (= 2:7.4.1689-3ubuntu1.2), vim-runtime (= 2:7.4.1689-3ubuntu1.2), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libgpm2 (>= 1.20.4), libpython3.5 (>= 3.5.0~b1), libselinux1 (>= 1.32), libtinfo5 (>= 6) 建议: ctags, vim-doc, vim-scripts 冲突: vim:i386 提供: editor 提供者:: vim-athena (2:7.4.1689-3ubuntu1), vim-athena (2:7.4.1689-3ubuntu1.2), vim-athena-py2 (2:7.4.1689-3ubuntu1), vim-athena-py2 (2:7.4.1689-3ubuntu1.2), vim-gnome (2:7.4.1689-3ubuntu1), vim-gnome (2:7.4.1689-3ubuntu1.2), vim-gnome-py2 (2:7.4.1689-3ubuntu1), vim-gnome-py2 (2:7.4.1689-3ubuntu1.2), vim-gtk (2:7.4.1689-3ubuntu1), vim-gtk (2:7.4.1689-3ubuntu1.2), vim-gtk-py2 (2:7.4.1689-3ubuntu1), vim-gtk-py2 (2:7.4.1689-3ubuntu1.2), vim-gtk3 (2:7.4.1689-3ubuntu1), vim-gtk3 (2:7.4.1689-3ubuntu1.2), vim-gtk3-py2 (2:7.4.1689-3ubuntu1), vim-gtk3-py2 (2:7.4.1689-3ubuntu1.2), vim-nox (2:7.4.1689-3ubuntu1), vim-nox (2:7.4.1689-3ubuntu1.2), vim-nox-py2 (2:7.4.1689-3ubuntu1), vim-nox-py2 (2:7.4.1689-3ubuntu1.2) 描述: Vi 增强版 - 增强的 vi 编辑器 Vim 是一个与 UNIX 编辑器 Vi 几乎完全兼容的版本。 Vim 在 vi 的基础上添加了许多特性:多次撤销、语法高亮、命令行 历史、 在线帮助、文件名补全、块操作、折叠、Unicode 支持等。 此软件包提供了一个带有一系列标准功能的 vim。它不提供 Vim 的 图形界面,如果您需要更多(或更少)的功能,请查看其它 vim-* 软 件包。 主页: http://www.vim.org/
说明:aptitude show命令并不表明那个软件包已经在系统上安装了。它只显示从软件库中得到的详细的软件包信息。
你无法通过aptitude看到一个细节是所有跟某个特定软件包关联的所有文件的列表。要得到这个列表,你需要用dpkg命令本身
dpkg -L package_name
这里有个用dpkg列出作为vim 软件包一部分安装所有文件的例子:
jingpan@ubuntu:~$ dpkg -L vim /. /usr /usr/share /usr/share/doc /usr/share/lintian /usr/share/lintian/overrides /usr/share/lintian/overrides/vim /usr/share/bug /usr/share/bug/vim /usr/share/bug/vim/script /usr/share/bug/vim/presubj /usr/bin /usr/bin/vim.basic /usr/share/doc/vim
你同样可以进行反向操作--查找某个特定文件属于哪个软件包:
dpkg --search absolute_file_name
注意,必须用绝对文件路径来和它一起使用:
jingpan@ubuntu:~$ dpkg --search /usr/bin/vim.basic vim: /usr/bin/vim.basic
jingpan@ubuntu:~$ dpkg -L grub-pc /. /etc /etc/kernel /etc/kernel/postrm.d /etc/kernel/postrm.d/zz-update-grub /etc/kernel/postinst.d /etc/kernel/postinst.d/zz-update-grub /usr /usr/lib /usr/lib/grub-legacy /usr/lib/grub-legacy/update-grub /usr/bin /usr/sbin /usr/sbin/upgrade-from-grub-legacy /usr/share /usr/share/bug /usr/share/bug/grub-pc /usr/share/bug/grub-pc/script /usr/share/bug/grub-pc/presubj /usr/share/doc /usr/share/man /usr/share/man/man8 /usr/share/man/man8/grub-bios-setup.8.gz /usr/bin/grub-ntldr-img /usr/sbin/grub-bios-setup /usr/share/doc/grub-pc jingpan@ubuntu:~$ dpkg --search /usr/share/man/man8/grub-bios-setup.8.gz grub-pc: /usr/share/man/man8/grub-bios-setup.8.gz输出说明grub-bios-setup.8.gz 文件是作为grub-pc包的一部分安装的。
8.2.2 用aptitude安装软件包
既然你已经了解了怎样在系统中列出软件包信息,本节带你逐步了解自怎样安装软件包。首先,你要确定准备安装的软件包名称。你怎么找一个特定的软件包呢?用aptitube合作加search选项:
aptitude search package_name
search选项之美在于,你无需在package_name边上通配符。通配符会隐式添加。下面是用aptitube来查找wine软件包的例子:
jingpan@ubuntu:~$ aptitude search wine
p gnome-wine-icon-theme - red variation of the GNOME-Colors icon theme
p libkwineffects7 - KDE window manager effects library
p libkwineffects7:i386 - KDE window manager effects library
p libwine-development - Windows API implementation - library
p libwine-development:i386 - Windows API implementation - library
p libwine-development-dev - Windows API implementation - development files
p libwine-development-dev:i386 - Windows API implementation - development files
p q4wine - Qt GUI for wine (WINE)
p q4wine:i386 - Qt GUI for wine (WINE)
p shiki-wine-theme - red variation of the Shiki-Colors theme
p twine - utility for interacting with PyPI
p wine - Microsoft Windows Compatibility Layer (meta-package)
p wine:i386 - Microsoft Windows Compatibility Layer (meta-package)
v wine-amd64 -
v wine-dev -
v wine-dev:i386 -
p wine-development - Windows API implementation - standard suite
v wine-development:i386 -
v wine-gecko -
v wine-gecko:i386 -
p wine-gecko2.21 - Microsoft Windows compatibility layer (embedded web browser)
p wine-gecko2.21:i386 - Microsoft Windows compatibility layer (embedded web browser)
v wine-i386:i386 -
v wine-mono -
p wine-mono0.0.8 - Microsoft Windows compatibility layer (.NET compatibility)
v wine-mono0.0.8:i386 -
p wine1.4 - Microsoft Windows Compatibility Layer (dummy package)
p wine1.4:i386 - Microsoft Windows Compatibility Layer (dummy package)
p wine1.4-amd64 - Microsoft Windows Compatibility Layer (dummy package)
p wine1.4-dbg - Microsoft Windows Compatibility Layer (dummy package)
p wine1.4-dbg:i386 - Microsoft Windows Compatibility Layer (dummy package)
p wine1.4-dev - Microsoft Windows Compatibility Layer (dummy package)
p wine1.4-dev:i386 - Microsoft Windows Compatibility Layer (dummy package)
p wine1.4-i386:i386 - Microsoft Windows Compatibility Layer (dummy package)
v wine1.4:any -
v wine1.5-amd64 -
v wine1.5-i386:i386 -
p wine1.6 - Microsoft Windows Compatibility Layer (Binary Emulator and Library)
p wine1.6:i386 - Microsoft Windows Compatibility Layer (Binary Emulator and Library)
p wine1.6-amd64 - Microsoft Windows Compatibility Layer (64-bit support)
p wine1.6-dbg - Microsoft Windows Compatibility Layer (debugging symbols)
p wine1.6-dbg:i386 - Microsoft Windows Compatibility Layer (debugging symbols)
p wine1.6-dev - Microsoft Windows Compatibility Layer (Development files)
p wine1.6-dev:i386 - Microsoft Windows Compatibility Layer (Development files)
v wine1.6-i386 -
p wine1.6-i386:i386 - Microsoft Windows Compatibility Layer (32-bit support)
v wine1.6:any -
v wine32-development -
p wine32-development:i386 - Windows API implementation - 32-bit binary loader
p wine32-development-preloader:i386 - Windows API implementation - prelinked 32-bit binary loader
p wine32-development-tools:i386 - Windows API implementation - 32-bit developer tools
p wine64-development - Windows API implementation - 64-bit binary loader
p wine64-development-preloader - Windows API implementation - prelinked 64-bit binary loader
p wine64-development-tools - Windows API implementation - 64-bit developer tools
v wine:any -
p winefish - LaTeX Editor based on Bluefish
p winefish:i386 - LaTeX Editor based on Bluefish
p winetricks - Microsoft Windows Compatibility Layer (winetricks)
v winetricks:i386 -
要检查安装过程是否正确地执行,只要再次使用search选项就可以了。这次你可以看到在wine软件包之前有一个i.这说明它已经安装了。
你可能还注意到这里有另外一些包前面也有一个i。 这是因为aptitude为我们自动 解析了必要的包依赖关系并安装了需要的额外库和软件包。这是许多包管理系统都有的特别棒的功能。
jingpan@ubuntu:~$ aptitude search nginx p lua-nginx-cookie - Pure Lua cookie parser for the nginx embedded Lua language p lua-nginx-dns - DNS resolver for the nginx embedded Lua language p lua-nginx-kafka - Pure Lua Kafka producer for the nginx embedded Lua language p lua-nginx-memcached - Pure Lua memcached client driver for the nginx embedded Lua language p lua-nginx-redis - Pure Lua redis client driver for the nginx embedded Lua language p lua-nginx-redis-connector - Connection utilities for lua-nginx-redis p lua-nginx-string - String utilities for the nginx embedded Lua language p lua-nginx-websocket - Lua websocket client driver for the nginx embedded Lua language i nginx - small, powerful, scalable web/proxy server v nginx:i386 - i A nginx-common - small, powerful, scalable web/proxy server - common files i A nginx-core - nginx web/proxy server (core version) p nginx-core:i386 - nginx web/proxy server (core version) p nginx-core-dbg - nginx web/proxy server (core version) - debugging symbols p nginx-core-dbg:i386 - nginx web/proxy server (core version) - debugging symbols p nginx-doc - small, powerful, scalable web/proxy server - documentation p nginx-extras - nginx web/proxy server (extended version) p nginx-extras:i386 - nginx web/proxy server (extended version) p nginx-extras-dbg - nginx web/proxy server (extended version) - debugging symbols p nginx-extras-dbg:i386 - nginx web/proxy server (extended version) - debugging symbols p nginx-full - nginx web/proxy server (standard version) p nginx-full:i386 - nginx web/proxy server (standard version) p nginx-full-dbg - nginx web/proxy server (standard version) - debugging symbols p nginx-full-dbg:i386 - nginx web/proxy server (standard version) - debugging symbols p nginx-light - nginx web/proxy server (basic version) p nginx-light:i386 - nginx web/proxy server (basic version) p nginx-light-dbg - nginx web/proxy server (basic version) - debugging symbols p nginx-light-dbg:i386 - nginx web/proxy server (basic version) - debugging symbols
8.2.3 用aptitude更新软件
aptitude safe-upgrade
注意,这个命令不需要跟一个软件包名称作为参数。这是因为safe-upgrade选项会将所有已安装的包更新到软件库中的最新版本,更有利于系统稳定。
还有一些不那么保守的可用的软件升级选项。
aptitude full-upgrade。
aptibude dist-upgrade。
这些选项执行相同的任务,将所有的软件包升级到最新版本。它们同safe-upgrade的区别在于它们不会检查包与包之间的依赖关系。整个包依赖关系问题非常麻烦。如果你不是很确定各种包的依赖关系,那还是坚持用save-upgrade选项吧。
8.4.2 用aptitude卸载软件
用aptitude来卸载软件包与安装及更新它们一样容易。你要做的唯一选择是,是否要在之后保留软件数据和配置文件。
只删除软件包但不删除数据和配置文件,可用aptitude的remove选项。要删除软件包和相关的数据和配置文件,可用 purge选项:
jingpan@ubuntu:~$ aptitude search nginx p lua-nginx-cookie - Pure Lua cookie parser for the nginx embedded Lua language p lua-nginx-dns - DNS resolver for the nginx embedded Lua language p lua-nginx-kafka - Pure Lua Kafka producer for the nginx embedded Lua language p lua-nginx-memcached - Pure Lua memcached client driver for the nginx embedded Lua language p lua-nginx-redis - Pure Lua redis client driver for the nginx embedded Lua language p lua-nginx-redis-connector - Connection utilities for lua-nginx-redis p lua-nginx-string - String utilities for the nginx embedded Lua language p lua-nginx-websocket - Lua websocket client driver for the nginx embedded Lua language p nginx - small, powerful, scalable web/proxy server v nginx:i386 - c nginx-common - small, powerful, scalable web/proxy server - common files p nginx-core - nginx web/proxy server (core version) p nginx-core:i386 - nginx web/proxy server (core version) p nginx-core-dbg - nginx web/proxy server (core version) - debugging symbols p nginx-core-dbg:i386 - nginx web/proxy server (core version) - debugging symbols p nginx-doc - small, powerful, scalable web/proxy server - documentation p nginx-extras - nginx web/proxy server (extended version) p nginx-extras:i386 - nginx web/proxy server (extended version) p nginx-extras-dbg - nginx web/proxy server (extended version) - debugging symbols p nginx-extras-dbg:i386 - nginx web/proxy server (extended version) - debugging symbols p nginx-full - nginx web/proxy server (standard version) p nginx-full:i386 - nginx web/proxy server (standard version) p nginx-full-dbg - nginx web/proxy server (standard version) - debugging symbols p nginx-full-dbg:i386 - nginx web/proxy server (standard version) - debugging symbols p nginx-light - nginx web/proxy server (basic version) p nginx-light:i386 - nginx web/proxy server (basic version) p nginx-light-dbg - nginx web/proxy server (basic version) - debugging symbols p nginx-light-dbg:i386 - nginx web/proxy server (basic version) - debugging symbols jingpan@ubuntu:~$
查看软件包是否已经被删除,你可以再用aptitude search 选项。如果你在软件包名称的前面看到一个c,那意味着软件已被删除,但配置文件尚未从系统中清除。前面是个p的话说明配置文件也被删除。
8.2.5 aptitube库
aptitude默认的软件库位置是在安装Linux发行版时设置的。库位置存储在文件/etc/apt/sources.list中。
很多情况下,你根本不需要添加或删除软件库,因此你不需要碰这个文件。但aptitude只会从这些库中下载文件,还有,在搜索或安装更新软件时,aptitude只会检查这些库。如果你需要为你的PMS添加一此额外的软件库,这里正是做这个的地方。
提示 Linux发行版开发人员在努力工作以保证添加到软件库的包版本不会互相冲突。通常通过库来升级或安装软件包是最安全的。即使在其他地方有更新的版本,你也尽可以不要安装,直到该版本在你的Linux发行版库中可用时。
jingpan@ubuntu:~$ cat /etc/apt/sources.list # # deb cdrom:[Ubuntu-Server 16.04.4 LTS _Xenial Xerus_ - Release amd64 (20180228)]/ xenial main restricted #deb cdrom:[Ubuntu-Server 16.04.4 LTS _Xenial Xerus_ - Release amd64 (20180228)]/ xenial main restricted # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to # newer versions of the distribution. deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted ## Major bug fix updates produced after the final release of the ## distribution. deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu ## team. Also, please note that software in universe WILL NOT receive any ## review or updates from the Ubuntu security team. deb http://cn.archive.ubuntu.com/ubuntu/ xenial universe # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial universe deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu ## team, and may not be under a free licence. Please satisfy yourself as to ## your rights to use the software. Also, please note that software in ## multiverse WILL NOT receive any review or updates from the Ubuntu ## security team. deb http://cn.archive.ubuntu.com/ubuntu/ xenial multiverse # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial multiverse deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates multiverse # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates multiverse ## N.B. software from this repository may not have been tested as ## extensively as that contained in the main release, although it includes ## newer versions of some applications which may provide useful features. ## Also, please note that software in backports WILL NOT receive any review ## or updates from the Ubuntu security team. deb http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse # deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse ## Uncomment the following two lines to add software from Canonical's ## 'partner' repository. ## This software is not part of Ubuntu, but is offered by Canonical and the ## respective vendors as a service to Ubuntu users. # deb http://archive.canonical.com/ubuntu xenial partner # deb-src http://archive.canonical.com/ubuntu xenial partner deb http://security.ubuntu.com/ubuntu xenial-security main restricted # deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted deb http://security.ubuntu.com/ubuntu xenial-security universe # deb-src http://security.ubuntu.com/ubuntu xenial-security universe deb http://security.ubuntu.com/ubuntu xenial-security multiverse # deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse # kubeadm及kubernetes组件安装源 deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
首先,注意文件里满是帮助性的注释和警告。指定的源用下面的结构:
deb (or deb-src) address distribution_name package_type_list
deb或deb-src的值表明了软件包的类型。deb值说明这是一个编译后程序源,而deb-src值说明这是一个源代码的源。
address条目是软件库的Web地址。distribution_name条目是这个特定软件库的发行版版本的名称。在这个例子中,发行版名称是 xenial-security。 这并不能说明你运行的发行版就是Ubuntu xenial ,它只是说明这个Linux发行版正在使用Ubuntu xenial-security软件库。举个例子,在Linux Mint的sources.list文件中,你能看到混用的Linux Mint和Ubuntu的软件库。
最后,package_type_List条目可以不止一个单词,它表明库里面有什么类型的包。举个例子,你可以看到的值有main、restricted、universe和partner。
当你需要组你的源文件添加软件库时,你可以自己发挥,但通常会带来问题。通常软件库网站或各种包开发人员网站会那么一两行文本,你可以直接从他们的网站上复制,粘贴到你的sources.list文件中。最后选择安全的途径并且只复制/粘贴。