yum是用于Redhat/Centos/Fedora等操作系統(tǒng)的安裝軟件包的程序。
在介紹yum之前矢腻,先來了解一下rpm:rpm是上述Linux操作系統(tǒng)的軟件打包管理程序(類似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系統(tǒng)上安裝管理軟件,但是無法解決軟件的依賴問題贩耐,安裝一個rpm包后有時候會提示缺少某個依賴的軟件弧腥,仍然需要手動安裝,比較麻煩潮太。這時候就有了yum管搪,yum是建立在rpm基礎(chǔ)之上的包管理工具,能夠一鍵式安裝軟件及其依賴軟件,很好的解決了上述rpm遇到的問題抛蚤。
yum是典型的c/s架構(gòu)台谢,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配置的最基礎(chǔ)的文件蒂胞⊥寄兀看一下它里面的內(nèi)容
[$] 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(對應(yīng)的公鑰)的值即可骗随。
下面演示如何增加repo源:
- 增加epel的repo蛤织。epel是官方以外的擴(kuò)展軟件包,nginx,nodejs等軟件都可以再epel倉庫中找到鸿染。
// 使用阿里云的鏡像
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
// 查看epel文件指蚜,可以看到倉庫的地址和公鑰都已經(jīng)配置好了
[$] 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數(shù)據(jù)庫
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
// 查看jenkins.repo文件涨椒,會發(fā)現(xiàn)只配置了倉庫的地址摊鸡,沒有配置公鑰,所以上面的添加公鑰的步驟需要手動執(zhí)行蚕冬。
[$] cat /etc/yum.repos.d/jenkins.repo
[jenkins]
name=Jenkins
baseurl=http://pkg.jenkins-ci.org/redhat
gpgcheck=1
配置好yum的repo之后免猾,就可以通過yum命令進(jìn)行安裝刪除等操作了巩那。
- 安裝軟件:
// -y 表示直接安裝琅轧,不再詢問“是否安裝”
yum install -y nginx
// 使用`enablerepo`指定repo源
yum install nodejs --enablerepo=epel
- 搜索軟件
yum search nginx
- 更新軟件
yum update nginx
- 刪除軟件
yum remove nginx
- 查看本地安裝的repo源
[$] yum repolist
源標(biāo)識 源名稱 狀態(tài)
!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
架構(gòu) :x86_64
時期 :1
版本 :1.6.3
發(fā)布 :6.el7
大小 :1.4 M
源 :installed
來自源:epel
簡介 : A high performance web server and reverse proxy server
網(wǎng)址 :http://nginx.org/
協(xié)議 : 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
架構(gòu) :x86_64
時期 :1
版本 :1.10.2
發(fā)布 :1.el7
大小 :505 k
源 :epel/x86_64
簡介 : A high performance web server and reverse proxy server
網(wǎng)址 :http://nginx.org/
協(xié)議 : 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
- 清除已下載的本地緩存數(shù)據(jù)禁漓,這個命令會清除緩存目錄
/var/cache/yum
下的文件:
yum clean all