说实话,平时用RHEL系统,最头疼的问题之一就是yum源——毕竟官方在线更新是收费的,不注册的话根本没法在线装软件。每次要装个东西都得先挂载本地光盘,操作繁琐不说,光盘里的软件版本往往也偏老。那有没有什么曲线救国的办法?当然有。既然CentOS和RHEL高度兼容,直接用CentOS的YUM源替换掉RHEL自带的,问题就迎刃而解了。下面把整个替换流程拆解开,一步步来说。
一、删除RHEL原有的YUM
动手之前先清理干净。RHEL自带的yum包得先卸载,不然新版装不上。命令很简单:
rpm -aq|grep yum|xargs rpm -e --nodeps
这里用--nodeps是为了跳过依赖检查,直接干掉,一步到位。
二、下载CentOS的yum安装包(163源)
既然要换,就找个靠谱的源。网易的CentOS镜像站速度不错,下面直接拉包:
wget https://mirrors.163.com/centos/5/os/x86_64/CentOS/yum-3.2.22-40.el5.centos.noarch.rpm
wget https://mirrors.163.com/centos/5/os/x86_64/CentOS/yum-fastestmirror-1.1.16-21.el5.centos.noarch.rpm
wget https://mirrors.163.com/centos/5/os/x86_64/CentOS/yum-metadata-parser-1.1.2-4.el5.x86_64.rpm
wget https://mirrors.163.com/centos/5/os/x86_64/RPM-GPG-KEY-CentOS-5
四个文件,前三者是核心安装包,最后一个是GPG密钥,用来验证包的安全性的。
三、安装CentOS的YUM安装包
这里有个细节要注意:yum和yum-fastestmirror这两个包存在依赖关系,必须一起装,分开装铁定失败。另外,在动手安装前最好先把GPG密钥导入系统,否则装包时会冒出一堆警告信息,看着心烦。
先装metadata-parser:
rpm -ivh yum-metadata-parser-1.1.2-4.el5.x86_64.rpm
导入密钥:
rpm --import RPM-GPG-KEY-CentOS-5
最后把yum和fastestmirror一起装上:
rpm -ivh yum-3.2.22-40.el5.centos.noarch.rpm yum-fastestmirror-1.1.16-21.el5.centos.noarch.rpm
到这一步,yum就已经可以在线使用了。
四、修改yum源
虽然yum装好了,但默认源可能还是国外的,速度会比较慢。国内用户嘛,当然首选国内的镜像源,比如网易源或者其他优质镜像。
五、更改yum源
直接编辑yum的仓库配置文件。先切换到对应的目录:
cd /etc/yum.repos.d/
编辑rhel-debuginfo.repo文件(如果没有就新建一个):
vi rhel-debuginfo.repo
把下面的内容写进去,这是网易CentOS 5的仓库配置:
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
[base]
name=CentOS-5 - Base - 163.com
mirrorlist=https://mirrorlist.centos.org/?release=5&arch=$basearch&repo=os
baseurl=https://mirrors.163.com/centos/5/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
#released updates
[updates]
name=CentOS-5 - Updates - 163.com
mirrorlist=https://mirrorlist.centos.org/?release=5&arch=$basearch&repo=updates
baseurl=https://mirrors.163.com/centos/5/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
#packages used/produced in the build but not released
[addons]
name=CentOS-5 - Addons - 163.com
mirrorlist=https://mirrorlist.centos.org/?release=5&arch=$basearch&repo=addons
baseurl=https://mirrors.163.com/centos/5/addons/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
#additional packages that may be useful
[extras]
name=CentOS-5 - Extras - 163.com
mirrorlist=https://mirrorlist.centos.org/?release=5&arch=$basearch&repo=extras
baseurl=https://mirrors.163.com/centos/5/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-5 - Plus - 163.com
mirrorlist=https://mirrorlist.centos.org/?release=5&arch=$basearch&repo=centosplus
baseurl=https://mirrors.163.com/centos/5/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
#contrib - packages by Centos Users
[contrib]
name=CentOS-5 - Contrib - 163.com
mirrorlist=https://mirrorlist.centos.org/?release=5&arch=$basearch&repo=contrib
baseurl=https://mirrors.163.com/centos/5/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
六、清理缓存
配置改好后,先清理一下旧的yum缓存,再重新生成新的缓存:
yum clean all
yum makecache
这一步跑通了,说明新源已经生效。
七、更新
最后一步,验证一下整个链路是否正常。直接执行更新命令:
yum update
如果一切顺利,系统会开始从网易源拉取更新。到这一步,恭喜,RHEL系统已经成功“借用”CentOS的在线yum源,以后装软件就方便多了,再也不需要挂光盘或者手动找包了。
