CentOS yum源设置priority优先查找本地源安装

CentOS的yum在安装时默认是将/etc/yum.repos.d/目录下.repo后缀文件都查找,然后用找到的第一个源。

如果安装有网络的源和光盘cdrom的源,在列表中CentOS-Base.repo被先找到,这里是网络源。那么光盘就不会在被安装。

可以设置文件优先级来让yum先搜索优先级高的,我们设置CentOS-Media.repo文件高,CentOS-Base.repo次之。

安装yum-priorities 来管理优先级

[root@Cent0S7 ]yum -y install yum-priorities 

 文件优先级设置,priority=N # N值为1-99,数字越大,优先级越低.(如果repo文件项里不写priority那默认是99,最低。)

 注意:priority=1这个要放到repo文件的项里,和enabled一样;而不是一个repo文件里放一个priority=1,另一个文件放priority=2就是priority=1文件优先级高了.

以 163.repo和aliyun.repo文件做例子,在[base_aliyun]项下和[base_163]分别放了priority代表,当使用到base里的链接时,优先使用[base_aliyun]项下地址。

其他项也是一样,当然如果有些链接报错感觉没用,直接在项下加入 enabled=0 关闭此项即可。

[root@Cent0S7 ~]# vi /etc/yum.repos.d/aliyun.repo
[base_aliyun]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
priority=1

[updates_aliyun]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful

  

[root@Cent0S7 ~]# vi /etc/yum.repos.d/163.repo

 

[base_163]
name=CentOS-$releasever - Base - 163.com
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
priority=2

[updates_163]
name=CentOS-$releasever - Updates - 163.com
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo
=updates
baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

启用CentOS-Media 这个光盘源

[root@Cent0S7 ~]# sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/CentOS-Media.repo 

安装httpd来测试:

网络和光盘Iso都设置好,没有设置优先级:

可以看到 Repository里是base也就是网络的源。

[root@Cent0S7 yum.repos.d]# yum install httpd                      
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * c7-media: 
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check

----   中间略  ---- Dependencies Resolved ===================================================================================================================== Package Arch Version Repository Size ===================================================================================================================== Installing: httpd x86_64 2.4.6-97.el7.centos updates 2.7 M Installing for dependencies: apr x86_64 1.4.8-7.el7 base 104 k apr-util x86_64 1.5.2-6.el7 base 92 k httpd-tools x86_64 2.4.6-97.el7.centos updates 93 k mailcap noarch 2.1.41-2.el7 base 31 k Transaction Summary ===================================================================================================================== Install 1 Package (+4 Dependent packages) Total download size: 3.0 M Installed size: 10 M Is this ok [y/d/N]:

  

设置优先级后:

可以看到先搜索c7-media 也就是光盘里 c7-media是repo文件里设置的name可以随意改。

[root@Cent0S7 yum.repos.d]# yum install httpd                        
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * c7-media: 
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
10867 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check

---- 中间略 ---- Dependencies Resolved ===================================================================================================================== Package Arch Version Repository Size ===================================================================================================================== Installing: httpd x86_64 2.4.6-93.el7.centos c7-media 2.7 M Installing for dependencies: apr x86_64 1.4.8-5.el7 c7-media 103 k apr-util x86_64 1.5.2-6.el7 c7-media 92 k httpd-tools x86_64 2.4.6-93.el7.centos c7-media 92 k mailcap noarch 2.1.41-2.el7 c7-media 31 k Transaction Summary ===================================================================================================================== Install 1 Package (+4 Dependent packages) Total download size: 3.0 M Installed size: 10 M Is this ok [y/d/N]:

 查看列表

[root@Cent0S7 ]# yum repolist 

 统计安装包个数

[root@Cent0S7 ]# yum list | wc -l
10498

  

 脚本安装:

[root@Cent0S7 ]# cat <<EOF | sudo tee  yum-localrepo.sh
#! /bin/bash

mkdir -p /media/cdrom/
mount /dev/cdrom /media/cdrom/

echo "/dev/cdrom    /mnt/cdrom     iso9660     defaults    0   0" >> /etc/fstab
sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/CentOS-Media.repo
echo "priority=1" >> /etc/yum.repos.d/CentOS-Media.repo
echo "priority=2" >> /etc/yum.repos.d/CentOS-Base.repo

yum -y install yum-priorities
yum makecache
EOF

  

执行脚本:

[root@CentOs7]# sh yum-localrepo.sh 

  

 

 

参考:

http://cnblogs.com/mountain2011/p/10224350.html

 

aliyun
posted @ 2020-12-01 15:10  悟透  阅读(1689)  评论(0编辑  收藏  举报