CentOS配置本地yum源/阿里雲yum源/163yuan源,並配置yum源的優先級

 

CentOS配置本地yum源/阿里雲yum源/163yuan源,並配置yum源的優先級

2017年06月19日 22:21:25html

閱讀數:37188vim

1、用Centos鏡像搭建本地yum源

因爲安裝centos後的默認yum源爲centos的官方地址,因此在國內使用很慢甚至沒法訪問,因此通常的作法都是把默認的yum源替換成aliyun的yum源或者163等國內的yum源(下文介紹如何配置)。 
可是以上的方法都是須要網絡的,當沒有網絡的時候就沒法使用了,因此還有一個經常使用的方法就是用Centos的iso鏡像搭建本地yum源,這樣安裝軟件的速度就會飛快,缺點是可能有些包沒有。centos

1.安裝Centos後默認的yum源以下

[root@kangvcar ~]# ll /etc/yum.repos.d/
total 32
-rw-r--r--. 1 root root 1664 Dec  9  2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Dec  9  2015 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Dec  9  2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  290 Dec  9  2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Dec  9  2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Dec  9  2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 Dec  9  2015 CentOS-Vault.repo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.把默認yum源備份(可選)

[root@kangvcar ~]# mkdir /opt/centos-yum.bak
[root@kangvcar ~]# mv /etc/yum.repos.d/* /opt/centos-yum.bak/
  • 1
  • 2

3.在虛擬機上掛載CentOS鏡像文件

這裏寫圖片描述

[root@kangvcar ~]# mount -t iso9660 /dev/sr0 /opt/centos
mount: /dev/sr0 is write-protected, mounting read-only
  • 1
  • 2

4.編寫repo文件並指向鏡像的掛載目錄

[root@kangvcar ~]# vi /etc/yum.repos.d/local.repo  
[local]
name=local
baseurl=file:///opt/centos
enabled=1
gpgcheck=0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

5.清除緩存

[root@kangvcar ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: local
Cleaning up everything
Cleaning up list of fastest mirrors
[root@kangvcar ~]# yum makecache        //把yum源緩存到本地,加快軟件的搜索好安裝速度
[root@kangvcar ~]# yum list        //列出了3780個包
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2、把默認的CentOS yum源修改爲國內的aliyun yum源

阿里雲官方教程:http://mirrors.aliyun.com/help/centos緩存

1.安裝Centos後默認的yum源以下

[root@kangvcar ~]# ll /etc/yum.repos.d/
total 32
-rw-r--r--. 1 root root 1664 Dec  9  2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Dec  9  2015 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Dec  9  2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  290 Dec  9  2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Dec  9  2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Dec  9  2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 Dec  9  2015 CentOS-Vault.repo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.把默認yum源備份(可選)

[root@kangvcar ~]# mkdir /opt/centos-yum.bak
[root@kangvcar ~]# mv /etc/yum.repos.d/* /opt/centos-yum.bak/
  • 1
  • 2

3.下載aliyun yum源repo文件(對應本身的系統版本下載便可)

#各系統版本repo文件對應的下載操做
CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
[root@kangvcar ~]# cat /etc/redhat-release        //查看系統的版本
CentOS Linux release 7.2.1511 (Core)
[root@kangvcar ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2017-06-20 06:43:08--  http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 112.124.140.210, 115.28.122.210
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|112.124.140.210|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2573 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’
100%[=======================================================================================================>] 2,573       --.-K/s   in 0s      
2017-06-20 06:43:08 (118 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2573/2573]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4.清除緩存

[root@kangvcar ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@kangvcar ~]# yum makecache        //把yum源緩存到本地,加快軟件的搜索好安裝速度
[root@kangvcar ~]# yum list        //總共列出了9954個包
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3、把默認的CentOS yum源修改爲國內的163源

163官方教程:http://mirrors.163.com/.help/centos.html網絡

1.安裝Centos後默認的yum源以下

[root@kangvcar ~]# ll /etc/yum.repos.d/
total 32
-rw-r--r--. 1 root root 1664 Dec  9  2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Dec  9  2015 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Dec  9  2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  290 Dec  9  2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Dec  9  2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Dec  9  2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 Dec  9  2015 CentOS-Vault.repo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.把默認yum源備份(可選)

[root@kangvcar ~]# mkdir /opt/centos-yum.bak
[root@kangvcar ~]# mv /etc/yum.repos.d/* /opt/centos-yum.bak/
  • 1
  • 2

3.下載163 yum源repo文件

#各系統版本repo文件對應的下載操做
CentOS 5
wget -O /etc/yum.repos.d/CentOS5-Base-163.repo http://mirrors.163.com/.help/CentOS5-Base-163.repo
CentOS 6
wget -O /etc/yum.repos.d/CentOS6-Base-163.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo
CentOS 7
wget -O /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
[root@kangvcar ~]# cat /etc/redhat-release        //查看系統的版本
CentOS Linux release 7.2.1511 (Core)
[root@kangvcar ~]# wget -O /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
--2017-06-20 06:29:47--  http://mirrors.163.com/.help/CentOS7-Base-163.repo
Resolving mirrors.163.com (mirrors.163.com)... 123.58.173.185, 123.58.173.186
Connecting to mirrors.163.com (mirrors.163.com)|123.58.173.185|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1572 (1.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS7-Base-163.repo’
100%[=======================================================================================================>] 1,572       --.-K/s   in 0s      
2017-06-20 06:29:47 (293 MB/s) - ‘/etc/yum.repos.d/CentOS7-Base-163.repo’ saved [1572/1572]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4.清除緩存

[root@kangvcar ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@kangvcar ~]# yum makecache        //把yum源緩存到本地,加快軟件的搜索好安裝速度
[root@kangvcar ~]# yum list        //總共列出了9951個包
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4、修改yum源的優先級

ps:當既有本地yum源又有163源的時候,咱們在裝軟件包的時候固然但願先用本地的yum源去安裝,本地找不到可用的包時再使用163源去安裝軟件,這裏就涉及到了優先級的問題,yum提供的插件yum-plugin-priorities.noarch能夠解決這個問題app

1.查看系統是否安裝了優先級的插件

[root@kangvcar ~]# rpm -qa | grep yum-plugin-
yum-plugin-fastestmirror-1.1.31-34.el7.noarch        
//這裏看到沒有安裝yum-plugin-priorities.noarch這個插件
[root@kangvcar ~]# yum search yum-plugin-priorities        
//用search查看是否有此插件可用
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
====================================================== N/S matched: yum-plugin-priorities =======================================================
yum-plugin-priorities.noarch : plugin to give priorities to packages from different repos
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.安裝yum-plugin-priorities.noarch插件

[root@kangvcar ~]# yum -y install yum-plugin-priorities.noarch
  • 1

3.查看插件是否啓用

[root@kangvcar ~]# cat /etc/yum/pluginconf.d/priorities.conf
[main]
enabled = 1
//1爲啓用;0爲禁用
  • 1
  • 2
  • 3
  • 4

4.修改本地yum源優先使用

[root@kangvcar ~]# ll /etc/yum.repos.d/
total 8
-rw-r--r--. 1 root root 2573 May 15  2015 CentOS-Base.repo
-rw-r--r--. 1 root root   67 Jun 20 06:04 local.repo
//有兩個repo文件
[root@kangvcar ~]# vi /etc/yum.repos.d/local.repo
[local]
name=local
baseurl=file:///opt/centos
enabled=1
gpgcheck=0
priority=1
//在原基礎上加入priority=1 ;數字越小優先級越高
//能夠繼續修改其餘源的priority值,經測試僅配置本地源的優先級爲priority=1就會優先使用本地源了
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

5.測試

配置優先級前:(使用阿里雲yum源)
[root@kangvcar ~]# yum -y install vim
Dependencies Resolved
=================================================================================================================================================
Package                            Arch                         Version                                     Repository                     Size
=================================================================================================================================================
Installing:
vim-enhanced                       x86_64                       2:7.4.160-1.el7_3.1                         updates                       1.0 M
Updating for dependencies:
vim-common                         x86_64                       2:7.4.160-1.el7_3.1                         updates                       5.9 M
省略···
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
配置優先級後:(使用本地yum源)
[root@kangvcar ~]# yum -y install vim
Dependencies Resolved
=================================================================================================================================================
Package                                     Arch                        Version                                Repository                  Size
=================================================================================================================================================
Installing:
vim-enhanced                                x86_64                      2:7.4.160-1.el7                        local                      1.0 M
Installing for dependencies:
gpm-libs                                    x86_64                      1.20.7-5.el7                           local                       32 k
perl                                        x86_64                      4:5.16.3-286.el7                       local                      8.0 M
perl-Carp                                   noarch                      1.26-244.el7                           local                       19 k
perl-Encode                                 x86_64                      2.51-7.el7                             local                      1.5 M
perl-Exporter                               noarch                      5.68-3.el7                             local                       28 k
perl-File-Path                              noarch                      2.09-2.el7                             local                       26 k
perl-File-Temp                              noarch                      0.23.01-3.el7                          local                       56 k
省略···
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

版權聲明:本文爲博主原創文章,轉載註明出處。歡迎指點評論,技術交流,我的郵箱kangvcar@126.com 測試

相關文章
相關標籤/搜索