Linux 命令行与shell编程 第8章 安装软件程序

时间:2022-06-07 15:29:53

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 <ubuntu-devel-discuss@lists.ubuntu.com>
体系: 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文件中。最后选择安全的途径并且只复制/粘贴。

8.3 基于Red Hat的系统

和基于Debian的发行版本类似,基于Red Hat的系统也有几种不同的可用前端工具。学见的有以下3种。

        yum:在Red Hat和Fedora中使用

        urpm:在Mandriva中使用

        zypper:在openSUSE中使用。

这些前端都是基于rpm命令行工具的。下一节讨论如何用这些芋rpm的各种工具来管理软件包。重点是在yum上,但同样会包含zypper和urpm的信息。

要找出系统上已安装的包,可以shell提示符输入如下命令:

[root@localhost ~]# yum list installed

输出的信息可以会在屏幕上一闪而过,所以最好是将已安装包的列表重定向到一个文件中。你可以用more或less命令(或一个GUI编辑器)来可控地查看这个列表。

要找到某个特定软件包的详细信息,yum就非常突出了。不仅它能给出关于包的非常详尽的描述,而且你可以通过一条简单命令就能查看包是否已安装:

[root@localhost ~]# yum list xterm
已加载插件:fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.tuna.tsinghua.edu.cn
可安装的软件包
xterm.x86_64                                    295-3.el7                                    base
[root@localhost ~]# yum list indtalled xterm
已加载插件:fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.tuna.tsinghua.edu.cn
可安装的软件包
xterm.x86_64                                    295-3.el7                                    base

最后,如果你需要棉花出什么软件包提供了系统上的某个特定文件,万能的yum也可以做到。只要输入命令:

yum provides file_name

这里有个查找什么软件提供了配置文件/etc/yum.conf的例子"

[root@localhost ~]# yum provides /etc/yum.conf
已加载插件:fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.tuna.tsinghua.edu.cn
yum-3.4.3-154.el7.centos.noarch : RPM package installer/updater/manager
源    :base
匹配来源:
文件名    :/etc/yum.conf



yum-3.4.3-132.el7.centos.0.1.noarch : RPM package installer/updater/manager
源    :@anaconda
匹配来源:
文件名    :/etc/yum.conf

yum 会查看两个分开的库:bash和@anaconda。从两个库中答案都是:该文件是yum软件包提供的。

8.3.2 用yum安装软件

yum install package_name

你马可以手动下载rpm安装文件并用yum安装。这称为本地安装(local installation)。基本的命令是:

yum localinstall package_name.rpm

你可以发现yum的优点之一是,它使用非常有逻辑的、用户友好的命令。

8.3.3 用 yum 更新软件

    大大多数Linux发行版本上,如果你是在GUI上工作,你会看到一些好看的小通知图标,告诉你需要更新了。在命令行下,它需要一点额外的工作。

要列出所的针对已安装包的可用更新,输入如下命令:

[root@localhost ~]# yum list updates

如果这个命令没有输出那就太好了,因为它说明你没有任何需要 更新的!但如果你发现某个特定软件包需要更新,输入如下 命令:yum update package_name

如果你想更新所有列在更新列表中的包,只要输入如下命令:

yum update

8.3.4 用yum 卸载软件

yum 工具还提供了简单地卸载在你系统上不再想要的应用。和aptitude一样,你需要决定是否保留软件包的数据和配置文件。

只删除软件包而保留配置文件和数据文件,用如下命令:

yum remove package_name

要删除软件和它所有的文件,用erase选项:

yum erase package_name

虽然有了PMS包生活变得相当简单,但并非就一直不会遇到问题。偶尔事情也会出错,幸运的是,有解决的办法。

8.3.5 处理损坏的包依赖关系

有时在安装多个软件包时,某个包的软件依赖关系可能会被另一个包的安装覆盖掉。这称为损坏的包依赖关系( broken dependency)。

如果你的系统出现了这个问题。首先试试下面的命令:

yum clean all

然后试着用yum命令的update选项。有时,只需要清理了放错位置的文件就可以了。

如果学还解决不了问题,试试下面的命令:

yum deplist package_name

这个命令显示了所有包的库依赖关系以及什么软件可以提供这些库依赖关系。一旦你知道了某个包需要的库,你就能安装它们了。下面是一个判断xterm 包依赖关系的例子:

[root@localhost ~]# yum deplist xterm
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.tuna.tsinghua.edu.cn
软件包:xterm.x86_64 295-3.el7
   依赖:/bin/sh
   provider: bash.x86_64 4.2.46-30.el7
   依赖:libICE.so.6()(64bit)
   provider: libICE.x86_64 1.0.9-9.el7
   依赖:libX11.so.6()(64bit)
   provider: libX11.x86_64 1.6.5-1.el7
   依赖:libXaw.so.7()(64bit)
   provider: libXaw.x86_64 1.0.13-4.el7
   依赖:libXft.so.2()(64bit)
   provider: libXft.x86_64 2.3.2-2.el7
   依赖:libXmu.so.6()(64bit)
   provider: libXmu.x86_64 1.1.2-2.el7
   依赖:libXpm.so.4()(64bit)
   provider: libXpm.x86_64 3.5.12-1.el7
   依赖:libXt.so.6()(64bit)
   provider: libXt.x86_64 1.1.5-3.el7
   依赖:libc.so.6(GLIBC_2.15)(64bit)
   provider: glibc.x86_64 2.17-222.el7
   依赖:libfontconfig.so.1()(64bit)
   provider: fontconfig.x86_64 2.10.95-11.el7
   依赖:libtinfo.so.5()(64bit)
   provider: ncurses-libs.x86_64 5.9-14.20130511.el7_4
   依赖:libutempter.so.0()(64bit)
   provider: libutempter.x86_64 1.1.6-4.el7
   依赖:rtld(GNU_HASH)
   provider: glibc.x86_64 2.17-222.el7
   provider: glibc.i686 2.17-222.el7

如果这样还没解决问题,你还有最后一个工具:

yum update --skip-broken

--skip-broken选项允许你忽略依赖关系损坏的那个包而更新其他软件包。这可能没法帮助损坏的包,但至少你可以更新系统上的其他包了。

8.3.6 yum 软件库

要知道你现在正从什么库中获取软件,输入如下命令:

[root@localhost ~]# yum repolist
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.tuna.tsinghua.edu.cn
源标识                                                源名称                                                   状态
base/7/x86_64                                         CentOS-7 - Base                                          9,911
dockerrepo                                            Docker Repository                                          110
extras/7/x86_64                                       CentOS-7 - Extras                                          305
updates/7/x86_64                                      CentOS-7 - Updates                                         654
repolist: 10,980
yum 库定义文件位于/etc/yum.repos.d。你需要添加正确的URL并获得必要的加密密钥。

  8.4 从源码安装

源源安装sysstat软件包。

首先你必须下载sysstat的tar包到你的Linux系统上。

解压

[root@localhost ~]# tar -zxvf sysstat-11.7.4.tar.gz 
sysstat-11.7.4/
sysstat-11.7.4/sa_conv.h
sysstat-11.7.4/cifsiostat.c
sysstat-11.7.4/ioconf.h
sysstat-11.7.4/images/
sysstat-11.7.4/images/tcgraph.png
sysstat-11.7.4/images/color_output.png
sysstat-11.7.4/images/cpugraph.jpg
sysstat-11.7.4/rd_stats.h
sysstat-11.7.4/COPYING
sysstat-11.7.4/common.h
sysstat-11.7.4/sysconfig.in
sysstat-11.7.4/.travis.yml
sysstat-11.7.4/raw_stats.c
sysstat-11.7.4/mpstat.h
sysstat-11.7.4/rndr_stats.h
sysstat-11.7.4/count.c
sysstat-11.7.4/contrib/
sysstat-11.7.4/contrib/irqstat/
sysstat-11.7.4/contrib/irqstat/irqstat
sysstat-11.7.4/contrib/irqstat/README.md
sysstat-11.7.4/contrib/irqstat/pylint.rc
sysstat-11.7.4/contrib/isag/
sysstat-11.7.4/contrib/isag/isag
sysstat-11.7.4/contrib/isag/README-isag
sysstat-11.7.4/contrib/isag/isag.1
sysstat-11.7.4/contrib/irqtop/
sysstat-11.7.4/contrib/irqtop/irqtop
sysstat-11.7.4/contrib/sargraph/
sysstat-11.7.4/contrib/sargraph/sargraph2
sysstat-11.7.4/contrib/sargraph/sargraph
sysstat-11.7.4/contrib/README-contrib
sysstat-11.7.4/README.md
sysstat-11.7.4/FAQ.md
sysstat-11.7.4/format.c
sysstat-11.7.4/sa.h
sysstat-11.7.4/sa1.in
sysstat-11.7.4/sadf.h
sysstat-11.7.4/tmp/

跑到这个新目录中,然后列出这目录的内容;

[root@localhost sysstat-11.7.4]# ls
activity.c    contrib   INSTALL       mpstat.h      rd_stats.c    sadc.c        sysstat-11.7.4.lsm    version.in
BUG_REPORT    COPYING   ioconf.c      nls           rd_stats.h    sadf.c        sysstat-11.7.4.spec   xml
build         count.c   ioconf.h      pidstat.c     README.md     sadf.h        sysstat.in            xml_stats.c
CHANGES       count.h   iostat.c      pidstat.h     rndr_stats.c  sadf_misc.c   sysstat.ioconf        xml_stats.h
cifsiostat.c  CREDITS   iostat.h      pr_stats.c    rndr_stats.h  sa.h          sysstat.service.in
cifsiostat.h  cron      json_stats.c  pr_stats.h    sa1.in        sar.c         sysstat.sysconfig.in
common.c      FAQ.md    json_stats.h  raw_stats.c   sa2.in        sa_wrap.c     tapestat.c
common.h      format.c  Makefile.in   raw_stats.h   sa_common.c   svg_stats.c   tapestat.h
configure     iconfig   man           rd_sensors.c  sa_conv.c     svg_stats.h   tests
configure.in  images    mpstat.c      rd_sensors.h  sa_conv.h     sysconfig.in  tmp

下一步是为你的系统配置sysstat.它会检查你的Linux系统来保证除了用来编译源代码的合适的编译器外,它还有正确的包含关系

[root@localhost sysstat-11.7.4]# ./configure
.
Check programs:
.
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking minix/config.h usability... no
checking minix/config.h presence... no
checking for minix/config.h... no
checking whether it is safe to define __EXTENSIONS__... yes
checking whether ln -s works... yes

下一步就是用make命令来构建各种二进制文件。

make命令会在编译和链接所有的源码文件时产生大量的输出:

[root@localhost sysstat-11.7.4]# make
gcc -o sadc.o -c -g -O2 -Wall -Wstrict-prototypes -pipe -O2  -DSA_DIR=\"/var/log/sa\" -DSADC_PATH=\"/usr/local/lib64/sa/sadc\"  -DHAVE_SYS_SYSMACROS_H -DHAVE_SYS_PARAM_H -DUSE_NLS -DPACKAGE=\"sysstat\" -DLOCALEDIR=\"/usr/local/share/locale\" sadc.c
gcc -o act_sadc.o -c -g -O2 -Wall -Wstrict-prototypes -pipe -O2 -DSOURCE_SADC  -DSA_DIR=\"/var/log/sa\" -DSADC_PATH=\"/usr/local/lib64/sa/sadc\"  -DHAVE_SYS_SYSMACROS_H -DHAVE_SYS_PARAM_H -DUSE_NLS -DPACKAGE=\"sysstat\" -DLOCALEDIR=\"/usr/local/share/locale\" activity.c
gcc -o sa_wrap.o -c -g -O2 -Wall -Wstrict-prototypes -pipe -O2 -DSOURCE_SADC  -DSA_DIR=\"/var/log/sa\" -DSADC_PATH=\"/usr/local/lib64/sa/sadc\"  -DHAVE_SYS_SYSMACROS_H -DHAVE_SYS_PARAM_H -DUSE_NLS -DPACKAGE=\"sysstat\" -DLOCALEDIR=\"/usr/local/share/locale\" sa_wrap.c
gcc -o sa_common_sadc.o -c -g -O2 -Wall -Wstrict-prototypes -pipe -O2 -DSOURCE_SADC  -DSA_DIR=\"/var/log/sa\" -DSADC_PATH=\"/usr/local/lib64/sa/sadc\"  -DHAVE_SYS_SYSMACROS_H -DHAVE_SYS_PARAM_H
.....
msgfmt -o nls/nn.gmo nls/nn.po
msgfmt -o nls/hr.gmo nls/hr.po
msgfmt -o nls/fr.gmo nls/fr.po
msgfmt -o nls/eo.gmo nls/eo.po
msgfmt -o nls/fur.gmo nls/fur.po
msgfmt -o nls/it.gmo nls/it.po
msgfmt -o nls/cs.gmo nls/cs.po
msgfmt -o nls/ko.gmo nls/ko.po
msgfmt -o nls/pt_BR.gmo nls/pt_BR.po
msgfmt -o nls/uk.gmo nls/uk.po
msgfmt -o nls/nl.gmo nls/nl.po
msgfmt -o nls/ru.gmo nls/ru.po
msgfmt -o nls/zh_CN.gmo nls/zh_CN.po
msgfmt -o nls/sk.gmo nls/sk.po
msgfmt -o nls/pl.gmo nls/pl.po
msgfmt -o nls/zh_TW.gmo nls/zh_TW.po
msgfmt -o nls/af.gmo nls/af.po
msgfmt -o nls/id.gmo nls/id.po

或make install

8.5 小结