安装软件包的三种方法
linux 中有三种安装软件的方法 rpm 工具, yum 工具, 源码包其中最常用的就是 yum 工具
rpm 工具使用
如果在虚拟机上, 先将光盘连接, 然后挂载到系统中, 一般我们都挂载到 mnt 下
首先, 先查看一下 mnt 是否有挂载其他的设备
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用 % 挂载点
- /dev/sda3 28G 926M 27G 4% /
- devtmpfs 485M 0 485M 0% /dev
- tmpfs 495M 0 495M 0% /dev/shm
- tmpfs 495M 6.8M 488M 2% /run
- tmpfs 495M 0 495M 0% /sys/fs/cgroup
- /dev/sda1 197M 97M 100M 50% /boot
- tmpfs 99M 0 99M 0% /run/user/0
查看后发现没有挂载其他的设备, 然后我们开始挂载, 挂载的命令是 mount
[root@localhost ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护, 将以只读方式挂载
挂载完后他会有一个提示写保护, 因为我们的光盘只能读取, 不能写入
然后我们到 mnt 目录下
- [root@localhost ~]# cd /mnt
- [root@localhost mnt]# ll
总用量 1586
-rw-rw-r--. 3 root root 14 9 月 5 21:25 CentOS_BuildTag
drwxr-xr-x. 3 root root 2048 9 月 5 21:36 EFI
-rw-rw-r--. 3 root root 227 8 月 30 22:33 EULA
-rw-rw-r--. 3 root root 18009 12 月 10 2015 GPL
drwxr-xr-x. 3 root root 2048 9 月 6 18:52 images
drwxr-xr-x. 2 root root 2048 9 月 5 21:36 isolinux
drwxr-xr-x. 2 root root 2048 9 月 5 21:36 LiveOS
drwxrwxr-x. 2 root root 1585152 9 月 6 05:48 Packages
drwxrwxr-x. 2 root root 4096 9 月 6 05:57 repodata
-rw-rw-r--. 3 root root 1690 12 月 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r--. 3 root root 1690 12 月 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root 2883 9 月 6 18:53 TRANS.TBL
然后发现里面有很多的目录和文件, 我们安装系统的时候就是用的这些文件其中有个目录是 Packages
- [root@localhost Packages]# ls | head -5
- 389-ds-base-1.3.6.1-16.el7.x86_64.rpm
- 389-ds-base-devel-1.3.6.1-16.el7.x86_64.rpm
- 389-ds-base-libs-1.3.6.1-16.el7.x86_64.rpm
- 389-ds-base-snmp-1.3.6.1-16.el7.x86_64.rpm
进入后发现这里面有很多的 rpm 文件, 这就是 rpm 包
rpm 包文件名有如下的特点, 我们拿 zziplib-0.13.62-5.el7.x86_64.rpm 这个文件做例子, 这个包名它是由符号 -, 符号., 数字, 字符串组成
首先, 最前面的字符串叫包名, 包名后面的符号 - 和数字组成包名的版本号, 版本号中也包括主版本号, 次版本号, 修正版本号和发布版本号后面的由 x86 和数字组成的是运行平台其中 64 位可以安装 32 位的包, 但是 32 位不能安装 64 位的包, 最后是后缀名
rpm 包的使用方法
比如我们要安装 zsh-5.0.2-28.el7.x86_64.rpm 这个包
[root@localhost Packages]# rpm -ivh zsh-5.0.2-28.el7.x86_64.rpm
准备中... ################################# [100%]
正在升级 / 安装...
1:zsh-5.0.2-28.el7 ################################# [100%]
其中, i 表示 install,v 表示可视化, h 表示更人性的可视化
如果我们发现更高版本的话就要升级这个包, rpm -Uvh zsh-5.0.2-28.el7.x86_64.rpm, 其中 U 是大写
如果要卸载一个包, 那么在 rpm 后面加 -e, 后面直接加包名即可, 不用输入包的文件名
[root@localhost Packages]# rpm -e zsh-5.0.2-28.el7.x86_64.rpm
错误: 未安装软件包 zsh-5.0.2-28.el7.x86_64.rpm
如果卸载错误, 它就会出现提示
[root@localhost Packages]# rpm -e zsh
正常卸载则不会出现提示
查询安装的包
如果我们想查询当前系统系下安装的包, 就输入命令 rpm -qa
- [root@localhost Packages]# rpm -qa | tail -5
- alsa-tools-firmware-1.1.0-1.el7.x86_64
- dmidecode-3.0-5.el7.x86_64
- kbd-misc-1.15.5-13.el7.noarch
- kbd-legacy-1.15.5-13.el7.noarch
- kernel-3.10.0-693.el7.x86_64
所列出的就都是现在系统下所有的安装的包
然后我们就可以对某个包进行操作
这里需要我们注意的是如果要卸载一个包, 他会出现提示, 某个包会被其他包所需要, 也就是说你想卸载某个包, 你就要卸载其他所需要的包所以说 rpm 这个工具不是很方便
我们也可以查指定的包是否安装, 命令是 rpm -q 加包名
- [root@localhost Packages]# rpm -q kbd
- kbd-1.15.5-13.el7.x86_64
如果安装了, 它就会显示这个包
[root@localhost Packages]# rpm -q ppp
未安装软件包 ppp
如果没有安装, 它就会提示为安装
我们也可以查询指定包的信息, 命令是 rpm -qi, 然后就会出现这个包的详细信息
- [root@localhost Packages]# rpm -qi kbd
- Name : kbd
- Version : 1.15.5
- Release : 13.el7
- Architecture: x86_64
Install Date: 2017 年 12 月 14 日 星期四 21 时 59 分 09 秒
- Group : System Environment/Base
- Size : 1380661
- License : GPLv2+
Signature : RSA/SHA256, 2017 年 08 月 11 日 星期五 00 时 56 分 40 秒, Key ID 24c6a8a7f4a80eb5
Source RPM : kbd-1.15.5-13.el7.src.rpm
Build Date : 2017 年 08 月 02 日 星期三 21 时 09 分 13 秒
- Build Host : c1bm.rdu2.centos.org
- Relocations : (not relocatable)
- Packager : CentOS BuildSystem <http://bugs.centos.org>;
- Vendor : CentOS
- URL : http://ftp.altlinux.org/pub/people/legion/kbd
- Summary : Tools for configuring the console (keyboard, virtual terminals, etc.)
- Description :
- The kbd package contains tools for managing a Linux
- systems consoles behavior, including the keyboard, the screen
- fonts, the virtual terminals and font files.
我们也可以查询这个包安装的文件, 比如我们安装了一个 rpm 包, 那这个包会帮助我们安装那些文件呢? 就通过命令 rpm -ql 加包名来查询
- [root@localhost Packages]# rpm -ql vim-enhanced
- /etc/profile.d/vim.csh
- /etc/profile.d/vim.sh
- /usr/bin/rvim
- /usr/bin/vim
- /usr/bin/vimdiff
- /usr/bin/vimtutor
通过查询, 我们可以发现 vim-enhanced 这个包安装了这些文件
也可以通过文件的绝对路径去查询这时由哪个包安装的, 命令是 rpm -qf
比如 vim 这个名命令, 它的路径是 / usr/bin/vim, 然后我们反向查询
- [root@localhost Packages]# rpm -qf /usr/bin/vim
- vim-enhanced-7.4.160-2.el7.x86_64
然后我们就可以发现, vim 命令是由 vim-enhanced-7.4.160-2.el7.x86_64 来安装的
这里有一个很实用命令 "
" 反引号, 如果我们不知道这个命令的路径, 那么我们可以通过
反引号来查询
- [root@localhost Packages]# rpm -qf which cd
- bash-4.2.46-28.el7.x86_64
查询结果是 cd 这个命令是有 bash 这个包安装的, 但是 which 查询命令输出的结果必须是一行, 如果两行则查不出结果, 不如 ls 这个命令, 它 which 的结果是 [root@localhost Packages]# which ls
- alias ls=ls --color=auto
- /usr/bin/ls
我们不能以两行作为参数, 那么 rpm -qf 就不能显示出结果
[root@localhost Packages]# rpm -qf which ls
rpm: --color=auto: 未知的选项
如果出现这种情况, 我们只能先查询路径, 然后再查询
yum 工具
rpm 工具在安装包时会出现一种情况, 你安装一个包时他会提示安装替他依赖的包, 比较麻烦但是 yum 工具不会, 他会自动的去安装你需要的包和其他依赖的包, 这就是 yum 的优势
如果我们想查看那些 rpm 包可以安装, 输入命令 yum list 可以列出可用的 rpm 包
- [root@localhost Packages]# yum list
- zlib.i686 1.2.7-17.el7 base
- zlib-devel.i686 1.2.7-17.el7 base
- zlib-devel.x86_64 1.2.7-17.el7 base
- zlib-static.i686 1.2.7-17.el7 base
- zlib-static.x86_64 1.2.7-17.el7 base
- zsh.x86_64 5.0.2-28.el7 base
- zsh-html.x86_64 5.0.2-28.el7 base
- zziplib.i686 0.13.62-5.el7 base
- zziplib.x86_64 0.13.62-5.el7 base
- zziplib-devel.i686 0.13.62-5.el7 base
- zziplib-devel.x86_64 0.13.62-5.el7 base
- zziplib-utils.x86_64 0.13.62-5.el7 base
最左侧是包名加相关信息, 中间这一列是版本号和平台版本号, 最后这一列是仓库名字
仓库名是哪里来的呢? 我们可以看一下 yum 的配置文件,/etc/yum.repos.d/
[root@localhost Packages]# ll /etc/yum.repos.d/
总用量 28
-rw-r--r--. 1 root root 1664 8 月 30 23:53 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 8 月 30 23:53 CentOS-CR.repo
-rw-r--r--. 1 root root 649 8 月 30 23:53 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 8 月 30 23:53 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 8 月 30 23:53 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 8 月 30 23:53 CentOS-Sources.repo
-rw-r--r--. 1 root root 3830 8 月 30 23:53 CentOS-Vault.repo
其中最核心的配置文件是第一个 - rw-r--r--. 1 root root 1664 8 月 30 23:53 CentOS-Base.repo
我们查看一下这个文件
- [root@localhost Packages]# cat /etc/yum.repos.d/CentOS-Base.repo
- CentOS-Base.repo
- #
- The mirror system uses the connecting IP address of the client and the
- update status of each mirror to pick mirrors that are updated to and
- geographically close to the client. You should use this for CentOS updates
- unless you are manually picking other mirrors.
- #
- If the mirrorlist= does not work for you, as a fall back you can try the
- remarked out baseurl= line instead.
- #
- #
[base](rpm 包的仓库名)
- name=CentOS-$releasever - Base
- mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
- #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
- gpgcheck=1
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
- #released updates
- [updates]
- name=CentOS-$releasever - Updates
- mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
- #baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
- gpgcheck=1
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
- #additional packages that may be useful
- [extras]
- name=CentOS-$releasever - Extras
- mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
- #baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
- gpgcheck=1
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
- #additional packages that extend functionality of existing packages
- [centosplus]
- name=CentOS-$releasever - Plus
- mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
- #baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
- gpgcheck=1
- enabled=0
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[base](rpm 包的仓库名)
- name=CentOS-$releasever - Base
- mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
- #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
我们可以通过这个网址, 找到所需要的 rpm 包
我们也可以搜索一个包, 命令是 yum search, 比如我们要搜索 vim 这个包
[root@localhost Packages]# yum search vim
已加载插件: fastestmirror
- Loading mirror speeds from cached hostfile
- base: mirror.bit.edu.cn
- extras: mirror.bit.edu.cn
- updates: mirror.bit.edu.cn
- ================================================================== N/S matched: vim ==================================================================
- protobuf-vim.x86_64 : Vim syntax highlighting for Google Protocol Buffers descriptions
- vim-X11.x86_64 : The VIM version of the vi editor for the X Window System
- vim-common.x86_64 : The common files needed by any version of the VIM editor
- vim-enhanced.x86_64 : A version of the VIM editor which includes recent enhancements
- vim-filesystem.x86_64 : VIM filesystem layout
- vim-minimal.x86_64 : A minimal version of the VIM editor
名称和简介匹配 only, 使用 search all 试试
他可以搜索出很多和 vim 有关的包但是这种搜不是很精确, 如我们想精确的搜索, 可以通过 grep 来过滤
- [root@localhost Packages]# yum list |grep vim
- vim-common.x86_64 2:7.4.160-2.el7 @base
- vim-enhanced.x86_64 2:7.4.160-2.el7 @base
- vim-filesystem.x86_64 2:7.4.160-2.el7 @base
- vim-minimal.x86_64 2:7.4.160-2.el7 @anaconda
- protobuf-vim.x86_64 2.5.0-8.el7 base
- vim-X11.x86_64 2:7.4.160-2.el7 base
这样就可以精确的搜索我们想要的了
安装 yum 包, 命令是 yum install -y(加 - y 可以不用询问), 比如我们要安装 zip 这个命令
[root@localhost Packages]# yum install -y zip
已加载插件: fastestmirror
- Loading mirror speeds from cached hostfile
- base: mirror.bit.edu.cn
- extras: mirror.bit.edu.cn
- updates: mirror.bit.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 zip.x86_64.0.3.0-11.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
======================================================================================================================================================
Package 架构 版本 源 大小
正在安装:
zip x86_64 3.0-11.el7 base 260 k
事务概要
安装 1 软件包
总下载量: 260 k
安装大小: 796 k
- Downloading packages:
- zip-3.0-11.el7.x86_64.rpm | 260 kB 00:00:00
- Running transaction check
- Running transaction test
- Transaction test succeeded
- Running transaction
正在安装 : zip-3.0-11.el7.x86_64 1/1
验证中 : zip-3.0-11.el7.x86_64 1/1
已安装:
zip.x86_64 0:3.0-11.el7
完毕
这样就安装完成了
我们也可以查询可用的组, 命令是 yum grouplist
[root@localhost Packages]# yum grouplist
已加载插件: fastestmirror
没有安装组信息文件
- Maybe run: yum groups mark convert (see man yum)
- Loading mirror speeds from cached hostfile
- base: mirror.bit.edu.cn
- extras: mirror.bit.edu.cn
- updates: mirror.bit.edu.cn
可用的环境分组:
最小安装
基础设施服务器
计算节点
文件及打印服务器
基本网页服务器
虚拟化主机
带 GUI 的服务器
GNOME 桌面
KDE Plasma Workspaces
开发及生成工作站
可用组:
传统 UNIX 兼容性
兼容性程序库
图形管理工具
安全性工具
开发工具
控制台互联网工具
智能卡支持
科学记数法支持
系统管理
系统管理工具
完成
这样我们就可以查看可用的组了, 如果选择这样的组, 那么它就会安装这个组里面的 rpm 包, 虽然我们在安装系统时没有选择某些组, 但是, 我们还是可以通过 yum 去安装其他的可用组
比如我们想要安装一个包组, Virtualization Host
[root@localhost Packages]# yum groupinstall Virtualization Host
然后会出现很多我们需要安装的包, 最后会出现这样的提示
- Install 69 Packages (+189 Dependent packages)
- Upgrade ( 6 Dependent packages)
- Is this ok [y/d/N]:
它的意思是 69 个包需要安装, 还有 189 个依赖包需要安装, 6 个需要升级, 由于我们没有加 - y, 所以他会提示我们是否安装
能安装, 就能卸载 yum 照比 rpm 的优势就是可以帮你安装和卸载依赖包, 那么 yum 的卸载命令就是就是 yum remove, 这是一个很危险的操作, 因为你卸载一个包, 那么其他的依赖包 yum 也一并给你卸载了, 会影响我们的使用所以我们要谨慎使用
- [root@localhost Packages]# yum remove vim
- Failed to set locale, defaulting to C
- Loaded plugins: fastestmirror
- Resolving Dependencies
- --> Running transaction check
- ---> Package vim-enhanced.x86_64 2:7.4.160-2.el7 will be erased
- --> Finished Dependency Resolution
- Dependencies Resolved
- ======================================================================================================================================================
- Package Arch Version Repository Size
- Removing:
- vim-enhanced x86_64 2:7.4.160-2.el7 @base 2.2 M
- Transaction Summary
- Remove 1 Package
- Installed size: 2.2 M
- Is this ok [y/N]:
yum 也可以去升级一个包, 命令是 yum update, 如果我们不加包名, 他会默认把所有的包都升级, 也会将你的系统升级到最新的版本不仅仅是系统, 还有内核也会升级这个操作我们会在刚安装完系统时去操作, 如果系统已经运行了一些服务, 安装了一些环境, 就不要去操作了, 会出现问题
我们也可以通过去搜有一个命令, 然后决定用哪一个包, 命令是 yum provides /*/ 命令, 比如我们要搜 vim 这个命令
yum 搭建本机仓库
有时候我们在安装好 centos 后没有联网, 所以就不能用在线的 yum 工具, 所以这时候我们要建立一个本地的 yum 仓库来供我们使用使用这个的前提是你要有一个光驱, 一个光盘, 一个镜像文件, 将这个光驱挂在
首先先挂载镜像在 mnt 下, 然后将 repos 中的 rpm 包作为本地的仓库首先我们先备份一些文件
[root@localhost ~]# cp -r /etc/yum.repos.d /etc/yum.repos.d.bak
然后进入到 yum.repos.d 目录
[root@localhost ~]# cd /etc/yum.repos.d
备份后要将 yum.repos.d 中的文件删除
[root@localhost yum.repos.d]# ll
总用量 28
-rw-r--r--. 1 root root 1664 8 月 30 23:53 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 8 月 30 23:53 CentOS-CR.repo
-rw-r--r--. 1 root root 649 8 月 30 23:53 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 8 月 30 23:53 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 8 月 30 23:53 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 8 月 30 23:53 CentOS-Sources.repo
-rw-r--r--. 1 root root 3830 8 月 30 23:53 CentOS-Vault.repo
[root@localhost yum.repos.d]# \rm ./*
然后创建一个 dvd.repo, 里面输入一下内容
[root@localhost yum.repos.d]# vi ded.repo
[dvd] (仓库名)
name=install dvd (自定义描述, 基本没啥用)
baseurl=file:///mnt (rpm 包的地址)
enable=1 (是否可用, 1 为可用, 0 为不可用)
gpcheck=0 (是否检测, 1 为检测, 0 为不检测)
然后保存退出
然后进行一个缓存清除操作
[root@localhost yum.repos.d]# yum clean all
然后我们再打开 yum 的列表查看
[root@localhost yum.repos.d]# yum list(由于篇幅问题这里只显示部分)
已加载插件: fastestmirror
Loading mirror speeds from cached hostfile
已安装的软件包
- GeoIP.x86_64 1.5.0-11.el7 @anaconda
- NetworkManager.x86_64 1:1.8.0-9.el7 @anaconda
- NetworkManager-libnm.x86_64 1:1.8.0-9.el7 @anaconda
- NetworkManager-team.x86_64 1:1.8.0-9.el7 @anaconda
- NetworkManager-tui.x86_64 1:1.8.0-9.el7 @anaconda
- NetworkManager-wifi.x86_64 1:1.8.0-9.el7 @anaconda
- acl.x86_64 2.2.51-12.el7 @anaconda
- zziplib.i686 0.13.62-5.el7 dvd
- zziplib.x86_64 0.13.62-5.el7 dvd
- zziplib-devel.i686 0.13.62-5.el7 dvd
- zziplib-devel.x86_64 0.13.62-5.el7 dvd
- zziplib-utils.x86_64 0.13.62-5.el7 dvd
其中 @anaconda 是已安装的程序, 而 dvd 是可以安装的程序
然后我们来安装一个软件
[root@localhost yum.repos.d]# yum install GConf2
已加载插件: fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 GConf2.x86_64.0.3.2.6-8.el7 将被 安装
--> 正在处理依赖关系 /usr/bin/killall, 它被软件包 GConf2-3.2.6-8.el7.x86_64 需要
--> 正在检查事务
---> 软件包 psmisc.x86_64.0.22.20-15.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
======================================================================================================================================================
Package 架构 版本 源 大小
正在安装:
GConf2 x86_64 3.2.6-8.el7 dvd 1.0 M
为依赖而安装:
psmisc x86_64 22.20-15.el7 dvd 141 k
事务概要
安装 1 软件包 (+1 依赖软件包)
总下载量: 1.1 M
安装大小: 6.8 M
- Is this ok [y/d/N]: y
- Downloading packages:
总计 16 MB/s | 1.1 MB 00:00:00
- Running transaction check
- Running transaction test
- Transaction test succeeded
- Running transaction
正在安装 : psmisc-22.20-15.el7.x86_64 1/2
正在安装 : GConf2-3.2.6-8.el7.x86_64 2/2
验证中 : psmisc-22.20-15.el7.x86_64 1/2
验证中 : GConf2-3.2.6-8.el7.x86_64 2/2
已安装:
GConf2.x86_64 0:3.2.6-8.el7
作为依赖被安装:
psmisc.x86_64 0:22.20-15.el7
完毕!
这样就安装好了, 这样方便我们用本地的光盘去作为他的仓库, 很方便的去安装它依赖的包
来源: http://www.bubuko.com/infodetail-2516003.html