1. 用 Centos 镜像搭建本地 yum 源
在配置网络 yum 之前我们先卸载旧的 yum, 并下载 CentOS 的对应软件包安装, 这一步配置 yum 源都要操作
- [root@localhost tools]# rpm -aq|grep yum
- yum-metadata-parser-1.1.2-16.el6.x86_64
- yum-rhn-plugin-0.9.1-58.el6.noarch
- yum-3.2.29-69.el6.noarch
- [root@localhost tools]# rpm -aq|grep yum|xargs rpm -e --nodeps
- [root@localhost tools]#
- [root@localhost tools]# rpm -aq|grep yum|xargs rpm -e --nodeps
- rpm: no packages given for erase
- wget http://mirrors.163.com/centos/6/os/x86_64/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm
- wget http://mirrors.163.com/centos/6/os/x86_64/Packages/python-urlgrabber-3.9.1-11.el6.noarch.rpm
- wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-3.2.29-81.el6.centos.noarch.rpm
- wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
- wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.30-40.el6.noarch.rpm
- [root@localhost ~]# cd /home/linzhongniao/tools/
- [root@localhost tools]# ls
- yum-3.2.29-81.el6.centos.noarch.rpm python-iniparse-0.3.1-2.1.el6.noarch.rpm yum-metadata-parser-1.1.2-16.el6.x86_64.rpmpython-urlgrabber-3.9.1-11.el6.noarch.rpm yum-plugin-fastestmirror-1.1.30-40.el6.noarch.rpm
- [root@localhost tools]# rpm -ivh *
1.1 安装完 centos 后默认的 yum 源如下
[root@localhost yum.repos.d]# ll
总用量 24
-rw-r--r--. 1 root root 1991 5 月 19 2016 CentOS-Base.repo
-rw-r--r--. 1 root root 647 5 月 19 2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 289 5 月 19 2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 5 月 19 2016 CentOS-Media.repo
-rw-r--r--. 1 root root 6259 5 月 19 2016 CentOS-Vault.repo
1.2 备份默认 yum 源
- [root@localhost ~]# mkdir -p /home/yum.repos.d
- [root@localhost ~]# mv /etc/yum.repos.d/* /home/yum.repos.d/
1.3 编写 repo 文件 并指向镜像的挂载目录
- [root@localhost ~]# mount /dev/cdrom /media/
- mount: block device /dev/sr0 is write-protected, mounting read-only
- [root@localhost ~]# vi /etc/yum.repos.d/local.repo
- [local]
- name=local
- baseurl=file:///media/
- enabled=1
- gpgcheck=0
1.4 清楚缓存
[root@localhost ~]# yum clean all
已加载插件: fastestmirror
Cleaning repos: local
清理一切
Cleaning up list of fastest mirrors
1.5 将 yum 源缓存到本地, 加快安装速度
[root@localhost ~]# yum makecache
已加载插件: fastestmirror
- Determining fastest mirrors
- local | 4.0 kB 00:00 ...
- local/group_gz | 226 kB 00:00 ...
- local/filelists_db | 6.3 MB 00:00 ...
- local/primary_db | 4.7 MB 00:00 ...
- local/other_db | 2.8 MB 00:00 ...
元数据缓存已建立
1.6 列出软件包
[root@localhost ~]# yum list
2. 将默认的 yum 源改成 aliyun yum 源
阿里云官方教程: http://mirrors.aliyun.com/help/centos
2.1 备份 yum 源
[root@localhost ~]# mv /etc/yum.repos.d/* /home/yum.repos.d/
2.2 下载 aliyun yum 源 repo 文件对应自己系统下载
各系统对应的 yum 源 repo 文件
- CentOS 5
- wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
或者
- curl -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
或者
- curl -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
或者
- curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
- [root@localhost ~]# cat /etc/redhat-release 查看系统版本
- CentOS release 6.8 (Final)
- [root@localhost ~]# wget -o /etc/yum.repos.d/centos.repo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
- [root@localhost ~]# wget -o /etc/yum.repos.d/centos.txt wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
- [root@localhost ~]# ll /etc/yum.repos.d/
总用量 8
-rw-r--r--. 1 root root 2572 7 月 2 2014 CentOS-Base.repo
-rw-r--r--. 1 root root 793 3 月 7 04:53 centos.txt
2.3 清除缓存
[root@localhost ~]# yum clean all
已加载插件: fastestmirror
Cleaning repos: local
清理一切
- Cleaning up list of fastest mirrors
- [root@localhost ~]# yum makecache 把 yum 源缓存到本地, 加快安装速度
已加载插件: fastestmirror
- Determining fastest mirrors
- local | 4.0 kB 00:00 ...
- local/group_gz | 226 kB 00:00 ...
- local/filelists_db | 6.3 MB 00:00 ...
- local/primary_db | 4.7 MB 00:00 ...
- local/other_db | 2.8 MB 00:00 ...
- [root@localhost ~]# yum list
3. 把默认 centos yum 源换成 163 源
163 官方教程: http://mirrors.163.com/.help/centos.html
3.1 Centos 默认 yum 源如下
[root@localhost yum.repos.d]# ll
总用量 24
-rw-r--r--. 1 root root 1991 5 月 19 2016 CentOS-Base.repo
-rw-r--r--. 1 root root 647 5 月 19 2016 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 289 5 月 19 2016 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 5 月 19 2016 CentOS-Media.repo
-rw-r--r--. 1 root root 6259 5 月 19 2016 CentOS-Vault.repo
3.2 备份 yum 源文件
[root@localhost ~]# mv /etc/yum.repos.d/* /home/yum.repos.d/
3.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
- [root@localhost ~]# cat /etc/redhat-release 查看系统版本
- CentOS release 6.8 (Final)
- [root@localhost ~]# wget -O /etc/yum.repos.d/CentOS6-Base-163.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo
- --2018-03-07 06:11:02-- http://mirrors.163.com/.help/CentOS6-Base-163.repo
正在解析主机 mirrors.163.com... 59.111.0.251
正在连接 mirrors.163.com|59.111.0.251|:80... 已连接
已发出 HTTP 请求, 正在等待回应... 200 OK
长度: 2006 (2.0K) [application/octet-stream]
正在保存至: /etc/yum.repos.d/CentOS6-Base-163.repo
100%[============================================================================================>] 2,006 --.-K/s in 0.001s
2018-03-07 06:11:03 (1.97 MB/s) - 已保存 /etc/yum.repos.d/CentOS6-Base-163.repo [2006/2006])
[root@localhost ~]# ll /etc/yum.repos.d/
总用量 4
-rw-r--r--. 1 root root 2006 12 月 1 2016 CentOS6-Base-163.repo
3.4 清除缓存
[root@localhost ~]# yum clean all
已加载插件: fastestmirror
Cleaning repos: local
清理一切
- Cleaning up list of fastest mirrors
- [root@localhost ~]# yum makecache 把 yum 源缓存到本地, 加快安装速度
已加载插件: fastestmirror
- Determining fastest mirrors
- local | 4.0 kB 00:00 ...
- local/group_gz | 226 kB 00:00 ...
- local/filelists_db | 6.3 MB 00:00 ...
- local/primary_db | 4.7 MB 00:00 ...
- local/other_db | 2.8 MB 00:00 ...
- [root@localhost ~]# yum list
4. 修改 yum 源的优先级
当既有本地源又有网络源的时候, 我们安装软件的时候当然希望按着本地 yum 源去安装, 当找不到安装包的时候在用网络软安装, 那么这里就涉及了一个优先级的问题
4.1 查看是否安装了优先级的插件
- [root@localhost ~]# rpm -qa | grep yum-plugin-
- yum-plugin-fastestmirror-1.1.30-37.el6.noarch
我们看没有安装 yum-plugin-priorities.noarch 这个插件
4.2 安装 yum-plugin-priorities.noarch 插件
[root@localhost ~]# yum install yum-plugin-priorities.noarch -y
4.3 查看插件是否可用
- [root@localhost ~]# cat /etc/yum/pluginconf.d/priorities.conf
- [main]
enabled = 1 1 表示可用, 0 为禁用
4.4 修改本地 yum 源优先使用
- [root@localhost ~]# cp /home/yum.repos.d/local.repo /etc/yum.repos.d/
- [root@localhost ~]# ll /etc/yum.repos.d/
总用量 8
-rw-r--r--. 1 root root 2006 12 月 1 2016 CentOS6-Base-163.repo
-rw-r--r--. 1 root root 63 3 月 7 06:34 local.repo
- [root@localhost ~]# cat /etc/yum.repos.d/local.repo
- [local]
- name=local
- baseurl=file:///media/
- enabled=1
- gpgcheck=0
- priority=1
添加 priority=1, 数字越小优先级越高, 也可以修改网络源的 priority 的值
来源: http://www.bubuko.com/infodetail-2518202.html