Linux(CentOS)軟體管理(2)- yum 線上安裝

白菜1031發表於2018-05-23

手動安裝RPM包時,解決依賴性問題是一個非常大的麻煩。如果所有RPM包都是用手工安裝,則RPM包的使用難道較大。因此,Red Hat 系列推出了 “yum 線上安裝” 方法。

yum 線上安裝優點:將所有軟體包放在官方伺服器上,當進行yum線上安裝時,可以自動解決依賴性問題。

一、yum 源配置檔案

1. 位置

/etc/yum.repos.d/CentOS-Base.repo

2. 內容例項(CentOS7)

[root/etc/yum.repos.d]$cat CentOS-Base.repo
# CentOS-Base.repo
[base]
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

3. 說明

說明
[base] 容器名稱,一定要放在[]中
name 容器說明,可以自己隨便寫
mirrorlist 映象站點
baseurl yum源伺服器地址
enabled 此容器是否生效。不寫或者enable=1生效,enable=0不生效
gpgcheck RPM數字證書是否生效。1:生效;0:不生效
gpgkey RPM數字證書公鑰檔案儲存位置。一般不要修改。

關於 baseurl

baseurl 預設是CentOS官方的yum源伺服器地址。是可以使用的,但是由於是國外網址,可能會出現訪問較慢的問題,這時可以替換成其他yum源伺服器地址。

常用yum源伺服器地址

地址
網易 http://mirrors.163.com/.help/…
搜狐 http://mirrors.sohu.com/help/…
清華大學 https://mirrors.tuna.tsinghua…
中國科學技術大學 http://centos.ustc.edu.cn/hel…

更多開源映象見【四、常用的Linux國內開源映象站整理】

4. 清除 yum 快取

yum 會下載容器的清單到本機的 /var/cache/yum 中,當我們修改了容器的網址卻沒有修改容器的名稱時,可能會造成本機快取中的列表與yum服務其中的列表不同步,此時就會出現無法更新的問題。
這種情況下,就需要清除 yum 快取。

命令


yum clean [packages|headers|all]

引數

packages:刪除已下載的軟體快取檔案
headers:刪除已下載的軟體檔案頭快取
all:刪除所有的容器快取資料

例項

[root/var/cache/yum/x86_64/7]$yum clean packages
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
0 package files removed

[root/var/cache/yum/x86_64/7]$yum clean headers
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
0 header files removed

[root/var/cache/yum/x86_64/7]$yum clean all
Loaded plugins: fastestmirror
Cleaning repos: 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

5. 列出當前 yum 伺服器的所有容器

命令

yum repolist all

例項

[vagrant~]$yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.huaweicloud.com
repo id                            repo name                         status
C7.0.1406-base/x86_64              CentOS-7.0.1406 - Base            disabled
C7.0.1406-centosplus/x86_64        CentOS-7.0.1406 - CentOSPlus      disabled
...省略...
base/7/x86_64                      CentOS-7 - Base                   enabled: 9,911
base-debuginfo/x86_64              CentOS-7 - Debuginfo              disabled
base-source/7                      CentOS-7 - Base Sources           disabled
...省略...
extras/7/x86_64                    CentOS-7 - Extras                 enabled:   291
extras-source/7                    CentOS-7 - Extras Sources         disabled
fasttrack/7/x86_64                 CentOS-7 - fasttrack              disabled
updates/7/x86_64                   CentOS-7 - Updates                enabled:   626
updates-source/7                   CentOS-7 - Updates Sources        disabled
repolist: 10,828

6. yum-fastestmirror 外掛

對centos系統管理員來說,yum絕對是個好東西,只可惜,官方yum源的速度實在讓人不敢恭維,而非官方的yum源又五花八門,讓人難以取捨。

幸運的是,yum-fastestmirror外掛彌補了這一缺陷:自動選擇最快的yum源。

安裝

yum -y install yum-fastestmirror

配置檔案

/etc/yum/pluginconf.d/fastestmirror.conf

/var/cache/yum/timedhosts.txt #yum映象的速度測試記錄檔案

二、使用光碟搭建yam源

在沒有網路的情況下,我們就無法使用 yum 線上安裝。這時,我們可以使用光碟搭建 yum 源。

1. 掛載光碟

  • 插入光碟
  • 建立掛載點
[root/dev]$mkdir /mnt/cdrom
  • 掛載光碟
[root/dev]$mount /dev/cdrom /mnt/cdrom/
mount: /dev/sr0 is write-protected, mounting read-only

2. 使網路yum源失效

  • 進入 /etc/yum.repos.d 目錄,修改網路yum原始檔字尾名,使其失效
[root~]$cd /etc/yum.repos.d/
[root/etc/yum.repos.d]$mv CentOS-Base.repo CentOS-Base.repo.bak

3. 使光碟yum源生效

[root/etc/yum.repos.d]$vim CentOS-Media.repo
# CentOS-Media.repo

[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///mnt/cdrom   # 地址為光碟掛載地址
#        file:///media/CentOS/ # 註釋掉不存在的地址
#        file:///media/cdrom/
#        file:///media/cdrecorder/
gpgcheck=1
enabled=1 # 將enabled=0改為enabled=1,讓這個yum原始檔生效
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

4. 可以使用 yum list 命令檢視光碟yum源中的可用軟體包

三、yum 常用命令

1. 查詢

yum [list|info|search|provides|whatprovides] [引數]

查詢所有可用軟體包列表

yum list

yum list installed 可以檢視所有已安裝的軟體

使用萬用字元匹配查詢可用軟體包列表

yum list [pattern]
  • 例項
[root~]$yum list g?nc-[a-e]*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.sohu.com
Available Packages
gvnc-devel.i686                      0.7.0-3.el7                   base

gvnc-devel.x86_64                    0.7.0-3.el7                   base

查詢伺服器上可供本機進行升級的軟體列表

yum list updates
  • 例項
[root/etc/yum.repos.d]$yum list updates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.huaweicloud.com
Updated Packages
NetworkManager.x86_64                       1:1.10.2-14.el7_5               updates
NetworkManager-libnm.x86_64                 1:1.10.2-14.el7_5               updates
NetworkManager-ppp.x86_64                   1:1.10.2-14.el7_5               updates
...省略...

搜尋伺服器上所有與關鍵字相關的包

yum search [關鍵字]
yum search all [關鍵字]

例項

[root~]$yum search fastestmirror
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.huaweicloud.com
==================================== N/S matched: fastestmirror ====================
yum-plugin-fastestmirror.noarch : Yum plugin which chooses fastest repository from a mirrorlist

  Name and summary matches only, use "search all" for everything.
[root~]$yum search all fastestmirror
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.huaweicloud.com
==================================== Matched: fastestmirror ========================
yum-plugin-fastestmirror.noarch : Yum plugin which chooses fastest repository from a mirrorlist

2. 安裝

語法

yum -y install [包名]

選項

-y:提示是否安裝依賴時,自動回答yes
install:安裝

例項

[root~]$yum install -y gcc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.huaweicloud.com
base                                                                | 3.6 kB  00:00:00
extras                                                              | 3.4 kB  00:00:00
...省略...
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-28.el7 will be updated
--> Processing Dependency: gcc = 4.8.5-28.el7 for package: gcc-c++-4.8.5-28.el7.x86_64
...省略...
---> Package libstdc++-devel.x86_64 0:4.8.5-28.el7_5.1 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
 Package          Arch          Version     Repository       Size
=======================================================================================
Updating:
 gcc            x86_64    4.8.5-28.el7_5.1    updates     16 M
Updating for dependencies:
 cpp            x86_64    4.8.5-28.el7_5.1    updates     5.9 M
...省略...
Transaction Summary
=========================================================================================
Upgrade  1 Package (+8 Dependent packages)

Total download size: 32 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/9): gcc-4.8.5-28.el7_5.1.x86_64.rpm                               |  16 MB  00:00:03
(2/9): libgcc-4.8.5-28.el7_5.1.i686.rpm                              | 108 kB  00:00:00
...省略...
(9/9): gcc-c++-4.8.5-28.el7_5.1.x86_64.rpm                           | 7.2 MB  00:00:06
----------------------------------------------------------------------------------------
Total                                                       4.9 MB/s |  32 MB  00:00:06
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Updating   : libgcc-4.8.5-28.el7_5.1.x86_64                         1/18
  Updating   : libstdc++-4.8.5-28.el7_5.1.x86_64                      2/18
...省略...
  Cleanup    : libgomp-4.8.5-28.el7.x86_64                            18/18
  Verifying  : libgomp-4.8.5-28.el7_5.1.x86_64                        1/18
  Verifying  : libstdc++-4.8.5-28.el7_5.1.i686                        2/18
  ...省略...
  Verifying  : libstdc++-4.8.5-28.el7.i686                            18/18

Updated:
  gcc.x86_64 0:4.8.5-28.el7_5.1

Dependency Updated:
  cpp.x86_64 0:4.8.5-28.el7_5.1         gcc-c++.x86_64 0:4.8.5-28.el7_5.1       libgcc.i686 0:4.8.5-28.el7_5.1                libgcc.x86_64 0:4.8.5-28.el7_5.1     libgomp.x86_64 0:4.8.5-28.el7_5.1
  libstdc++.i686 0:4.8.5-28.el7_5.1     libstdc++.x86_64 0:4.8.5-28.el7_5.1     libstdc++-devel.x86_64 0:4.8.5-28.el7_5.1

Complete!

3. 升級

在生產伺服器中,除非當前軟體版本有重大的BUG,否則儘量不對軟體進行升級
使用 yum -y update [包名] 命令時要極其謹慎,若未輸入 包名,命令會變成 yum -y update,該命令升級所有包同時也升級軟體和系統核心,有可能造成系統重啟甚至崩潰!

語法

yum -y update [包名]
yum -y upgrade [包名]

選項

-y:提示是否安裝依賴時,自動回答yes
update:更新,後面接要更新的軟體,否則將會更新所有包,改變軟體設定和系統設定,更新系統版本核心
upgrade:升級,後面接要升級的軟體,否則將會升級所有包。

4. 解除安裝

在生產伺服器中,伺服器應使用最小化安裝,用什麼軟體裝什麼軟體,儘量不解除安裝

語法

yum -y remove [包名]

選項

-y:提示是否安裝依賴時,自動回答yes
remove:解除安裝軟體

例項

[root~]$yum -y remove zip
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package zip.x86_64 0:3.0-11.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================
 Package        Arch        Version         Repository          Size
===========================================================================================

Removing:
 zip            x86_64      3.0-11.el7      installed           796 k

Transaction Summary
===========================================================================================

Remove  1 Package

Installed size: 796 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : zip-3.0-11.el7.x86_64                                1/1
  Verifying  : zip-3.0-11.el7.x86_64                                1/1

Removed:
  zip.x86_64 0:3.0-11.el7

5. yum 軟體組管理命令

列出所有可用軟體組列表

yum grouplist
  • 例項
[root~]$yum grouplist
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.sohu.com
Available Environment Groups:
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done

安裝指定軟體組,組名可以有grouplist查詢出來

yum groupinstall [軟體組名]

解除安裝指定軟體組

yum groupremove [軟體組名]

四、常用的Linux國內開源映象站整理

【阿里雲開源映象站】:https://opsx.alibaba.com/

【華為開源映象站】:http://mirrors.huaweicloud.com/

【網易開源映象站】:http://mirrors.163.com/

【搜狐開源映象站】:http://mirrors.sohu.com/

【清華大學開源軟體映象站】:https://mirrors.tuna.tsinghua…

【中國科學技術大學開源映象站】:http://centos.ustc.edu.cn/

【上海大學開源映象站】:http://mirrors.shu.edu.cn/

【浙江大學開源映象站】:http://mirrors.zju.edu.cn/

【上海交通大學開源映象站】:http://ftp.sjtu.edu.cn/

【重慶大學開源軟體映象站】:http://mirrors.cqu.edu.cn/

【南京大學開源映象站】:http://mirrors.nju.edu.cn/

【南京郵電大學開源軟體映象站】:http://mirrors.njupt.edu.cn/

【東軟資訊學院開源映象站】:http://mirrors.neusoft.edu.cn/

【蘭州大學開源社群映象站】:http://mirror.lzu.edu.cn/

相關文章