centos中Yum常用命令

<code>本文由“開發者手冊”原創,如需轉載,拿走不謝!/<code>

前提概要

Yum是一個rpm包管理器,能夠從指定的服務器上下載RPM包並安裝,並且可以自動處理包之間的依賴性關係。

相關配置文件說明

1、全局配置文件yum.conf

<code>全局配置文件位於:/etc/yum.conf

命令:cat /etc/yum.conf

輸出:
[main]
# 包緩存目錄
cachedir=/var/cache/yum/$basearch/$releasever
# 緩存是否保存,1保存,0不保存
keepcache=0
# 調試級別
debuglevel=2
# 日誌文件路徑
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
# gpg校驗,1-開啟,0-禁用
gpgcheck=1
# 是否允許使用插件,1-允許,0-不允許
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/<code>

2、倉庫源配置文件*.repo

<code>倉庫源配置文件位於目錄:/etc/yum.repos.d下。 


命令:ls /etc/yum.repos.d

輸出說明:
# 網絡源配置
CentOS-Base.repo
CentOS-CR.repo
CentOS-Debuginfo.repo
CentOS-fasttrack.repo
# 掛載源配置
CentOS-Media.repo
CentOS-Sources.repo
CentOS-Vault.repo/<code>
<code>CentOS-Base.repo文件的具體說明:

命令:cat /etc/yum.repos.d/CentOS-Base.repo

輸出說明:
# 倉庫標識,不可重複
[base]
# 倉庫名稱
name=CentOS-$releasever - Base
# 鏡像服務器地址列表
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
# baseurl指定源鏡像服務器地址,已被註釋不使用
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
# 標識這個源是否啟用,1-啟動,0-禁用
enabled=1
# gpg校驗
gpgcheck=1
# gpg校驗key
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7/<code>

配置基礎網絡源為阿里Yum源

1、備份原CentOS-Base.repo

<code>先備份原CentOS-Base.repo為CentOS-Base.repo.bak 


命令:mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak/<code>

2、下載阿里雲Yum源

<code>命令:wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

輸出:
--2020-03-23 15:43:12-- http://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主機 mirrors.aliyun.com (mirrors.aliyun.com)... 153.99.248.243, 218.98.31.248, 116.153.35.246, ...
正在連接 mirrors.aliyun.com (mirrors.aliyun.com)|153.99.248.243|:80... 已連接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”
100%[===============>] 2,523 --.-K/s 用時 0s
2020-03-23 15:43:12 (245 MB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])/<code>

3、清理並重建緩存

<code>命令:yum clean all && yum makecache

輸出:
已加載插件:fastestmirror
正在清理軟件源: base extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
已加載插件:fastestmirror
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/10): base/7/x86_64/group_gz | 165 kB 00:00:00
(2/10): base/7/x86_64/filelists_db | 7.3 MB 00:00:02
(3/10): extras/7/x86_64/filelists_db | 216 kB 00:00:00
(4/10): base/7/x86_64/other_db | 2.6 MB 00:00:01
(5/10): extras/7/x86_64/other_db | 106 kB 00:00:00
(6/10): extras/7/x86_64/primary_db | 164 kB 00:00:01

(7/10): updates/7/x86_64/primary_db | 7.5 MB 00:00:01
(8/10): updates/7/x86_64/other_db | 562 kB 00:00:00
(9/10): base/7/x86_64/primary_db | 6.0 MB 00:00:22
(10/10): updates/7/x86_64/filelists_db | 4.4 MB 00:00:19
元數據緩存已建立/<code>

4、驗證是否配置成功(yum repolist):

<code>命令:yum repolist

輸出:
源標識 源名稱 狀態
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,097
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 335
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 1,774/<code>

常用命令

1、顯示已經安裝過的軟件包列表(yum list)

<code>命令:yum list installed

輸出:
會列出當前已經通過Yum安裝過的軟件列表/<code>

2、顯示某個已安裝過的軟件(yum list)

<code>1) 如果已安裝過,會顯示詳情。如果沒有安裝過,顯示空。

本地已經安裝過git:
命令:yum list installed | grep git
輸出:
git.x86_64 1.8.3.1-20.el7 @updates
本地沒有安裝過apache:
命令:yum list installed | grep git
輸出:空/<code>
<code>2)不管有沒有安裝過,會同時顯示已安裝的包和可安裝的包列表

本地已經安裝過git:
命令:yum list git
輸出:
已安裝的軟件包
git.x86_64 1.8.3.1-20.el7 @updates
可安裝的軟件包
git.x86_64 1.8.3.1-21.el7_7 updates

本地沒有安裝過apache:
命令:yum list httpd
可安裝的軟件包
httpd.x86_64 2.4.6-90.el7.centos base/<code>

3、升級(yum update)

<code>上圖中git顯示當前版本為:1.8.3.1-20.el7,可安裝新版本為:1.8.3.1-21.el7_7。做升級試試。

命令:yum -y update git

輸出:
更新完畢: git.x86_64 0:1.8.3.1-21.el7_7
作為依賴被升級: perl-Git.noarch 0:1.8.3.1-21.el7_7
完畢!/<code>

4、安裝(yum install)

<code>演示安裝apache

命令:yum -y install httpd

輸出:
Running transaction
正在安裝 : apr-1.4.8-5.el7.x86_64 1/5
正在安裝 : apr-util-1.5.2-6.el7.x86_64 2/5

正在安裝 : httpd-tools-2.4.6-90.el7.centos.x86_64 3/5
正在安裝 : mailcap-2.1.41-2.el7.noarch 4/5
正在安裝 : httpd-2.4.6-90.el7.centos.x86_64 5/5
驗證中 : apr-1.4.8-5.el7.x86_64 1/5
驗證中 : mailcap-2.1.41-2.el7.noarch 2/5
驗證中 : httpd-tools-2.4.6-90.el7.centos.x86_64 3/5
驗證中 : apr-util-1.5.2-6.el7.x86_64 4/5
驗證中 : httpd-2.4.6-90.el7.centos.x86_64 5/5
已安裝:
httpd.x86_64 0:2.4.6-90.el7.centos
作為依賴被安裝:
apr.x86_64 0:1.4.8-5.el7
apr-util.x86_64 0:1.5.2-6.el7
httpd-tools.x86_64 0:2.4.6-90.el7.centos mailcap.noarch 0:2.1.41-2.el7
完畢!/<code>

5、卸載(yum remove)

<code>演示卸載apache

命令:yum -y remove httpd
輸出:
刪除: httpd.x86_64 0:2.4.6-90.el7.centos
完畢!/<code>

6、查看包依賴(yum deplist)

<code>命令:yum deplist git/<code>

結束語

希望我的文章能給您帶來幫助,如果您有什麼疑問,可以給我留言,我會在第一時間給你提供解答。如果您有好的建議或想法,也歡迎給我留言。

centos中Yum常用命令


分享到:


相關文章: