linux命令之yum

yum是用于Redhat/Centos/Fedora等操作系统的安装软件包的程序。
在介绍yum之前,先来了解一下rpm:rpm是上述Linux操作系统的软件打包管理程序(类似windows平台的“添加/删除程序”),全称是Redhat Package Manager。主要用途就是在linux平台上安装软件或者管理软件(查看删除等)。

rpm常见的用法有:

  • 安装软件:
// 安装某个软件包
rpm -ivh nginx-1.10.0-1.el6.ngx.x86_64.rpm
// 安装或升级某个软件包
rpm -Uvh nginx-1.10.0-1.el6.ngx.x86_64.rpm

-i:--install,安装命令
-U: --upgrade, 如果软件不存在,就安装软件;如果存在,就升级。
-v: 打印log
-h: --hash,打印软件hash值, 要和-v一起使用

  • 查看软件包的依赖包
// 查看未安装软件包的
rpm -qRp nginx-1.10.0-1.el6.ngx.x86_64.rpm
// 查看已安装软件包的
rpm -qp nginx-1.10.0-1.el6.ngx.x86_64.rpm

-q:--query,查看命令
-R:--requires,列出软件包所有依赖
-p:--package, 查看尚未在本地安装的软件包时,使用此命令

  • 查看某个软件的信息
// 查询rpm名
rpm -q nginx
// 列出相信信息
rpm -qi nginx
  • 查看所有安装的软件
rpm -qa
// 包含`ngi`字符的软件
rpm -qa | grep ngi

-a:表示所有软件

  • 卸载软件
rpm -e nginx-1.10.0-1.el6.ngx.x86_64.rpm

rpm软件包虽然能方便的在linux系统上安装管理软件,但是无法解决软件的依赖问题,安装一个rpm包后有时候会提示缺少某个依赖的软件,仍然需要手动安装,比较麻烦。这时候就有了yum,yum是建立在rpm基础之上的包管理工具,能够一键式安装软件及其依赖软件,很好的解决了上述rpm遇到的问题。

yum是典型的c/s架构,server端将所有rpm包保存在一个仓库中(repository),client端通过yum命令与server端建立连接来安装或更新软件。下面看一下client端的配置。

yum的配置文件/etc/yum.conf,定义了yum的缓存文件、日志文件,log级别,是否检查密钥(gpg)等。

[$] cat /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
......
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
timeout=3

/etc/yum.repo.d/目录下保存了所有repo(repository)的配置。其中CentOS-Base.repo文件一般作为repo配置的最基础的文件。看一下它里面的内容

[$] cat CentOS-Base.repo
[base]
name=CentOS-$releasever - Base
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
......

可以看到访问的repository的地址是阿里云上:http://mirrors.aliyun.com/centos/...
如果想修改repository的地址,只需要修改baseurl和gpgkey(对应的公钥)的值即可。

下面演示如何增加repo源:

  • 增加epel的repo。epel是官方以外的扩展软件包,nginx,nodejs等软件都可以再epel仓库中找到。
// 使用阿里云的镜像
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

// 查看epel文件,可以看到仓库的地址和公钥都已经配置好了
[$] cat /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
        http://mirrors.aliyuncs.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  • 增加jenkins的repo。
// 下载repo到指定目录
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
// 将公钥添加到rpm数据库
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key

// 查看jenkins.repo文件,会发现只配置了仓库的地址,没有配置公钥,所以上面的添加公钥的步骤需要手动执行。
[$] cat /etc/yum.repos.d/jenkins.repo
[jenkins]
name=Jenkins
baseurl=http://pkg.jenkins-ci.org/redhat
gpgcheck=1

配置好yum的repo之后,就可以通过yum命令进行安装删除等操作了。

  • 安装软件:
// -y 表示直接安装,不再询问“是否安装”
yum install -y nginx
// 使用`enablerepo`指定repo源
yum install nodejs --enablerepo=epel
  • 搜索软件
yum search nginx
  • 更新软件
yum update nginx
  • 删除软件
yum remove nginx
  • 查看本地安装的repo源
[$] yum repolist
源标识                                                                             源名称                                                                                                           状态
!base/7/x86_64                                                                     CentOS-7 - Base                                                                                                   9,007
!docker-main-repo                                                                  Docker main Repository                                                                                               65
!epel/x86_64                                                                       Extra Packages for Enterprise Linux 7 - x86_64                                                                   10,865
!extras/7/x86_64                                                                   CentOS-7 - Extras                                                                                                   393
!jenkins                                                                           Jenkins                                                                                                             366
!updates/7/x86_64                                                                  CentOS-7 - Updates                                                                                                2,560
repolist: 23,256
  • 查看某个软件的信息
[$] yum info nginx
已安装的软件包
名称    :nginx
架构    :x86_64
时期       :1
版本    :1.6.3
发布    :6.el7
大小    :1.4 M
源    :installed
来自源:epel
简介    : A high performance web server and reverse proxy server
网址    :http://nginx.org/
协议    : BSD
描述    : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
         : IMAP protocols, with a strong focus on high concurrency, performance and low
         : memory usage.
可安装的软件包
名称    :nginx
架构    :x86_64
时期       :1
版本    :1.10.2
发布    :1.el7
大小    :505 k
源    :epel/x86_64
简介    : A high performance web server and reverse proxy server
网址    :http://nginx.org/
协议    : BSD
描述    : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
         : IMAP protocols, with a strong focus on high concurrency, performance and low
         : memory usage.
  • 将server上的repo源缓存到本地,已加快访问速度
yum makecache
  • 清除已下载的本地缓存数据,这个命令会清除缓存目录/var/cache/yum下的文件:
yum clean all

你可能感兴趣的:(linux命令之yum)