对于企而言,假设有一批CentOS 5.7服务器须要升级到CentOS 5.8,每一台机都使用yum update命令从外网下载升级包的话,既费时间,也占流量,实在不划算。而对于虚拟机玩家而言,可能须要升级的系统很少,也就三四台,但假若都从外网下载升级包,也是件很费劲的的事情,并且,有时候会遇到物理机不方便联网的状况,那就加无奈。html
因此,在局域网内搭建yum服务器,应该是每一个Linux从业者必须懂的基本功。SQ不才,直到如今才弄懂,实在惭愧,在此分享一下经验。其实操做很简单,注意一些细节就好了。sql
本文做业环境:centos
注:如下的操做是在VirtualBox虚拟机环境下进行的。服务器
提供Yum源的服务器,如下简称为Yum服务器:less
CentOS 5.8,主机名为luffy,IP为192.168.56.123ide
(注:Yum服务器也能够为CentOS 5.7系统)post
须要升级的服务器,如下简称为客户端:网站
CentOS 5.7,主机名为zoro,IP为192.168.56.124ui
1、this
到官网查找并下载最新的CentOS 5.8镜像(国内也有放多镜像源,好比http://mirrors.163.com/centos/ ,更新得也蛮快的),下载CentOS-5.8-i386-bin-DVD-1of2就能够了,DVD-2of2用不到。
若是Yum服务器是物理机,需将DVD-1of2镜像刻录到DVD光碟,而后将放进Yum服务器光驱;
若是是虚拟机,则在虚拟机光驱设置中,选择物理机上存放的DVD-1of2镜像便可。
Yum服务器加载光驱
[root@luffy ~]# mount /dev/cdrom /media
mount: block device /dev/sr0 is write-protected, mounting read-only
2、
为使局域网内其余CentOS服务器能够访问到Yum 服务器上的资源,须要在Yum服务器上架设http服务器或者ftp服务器。鉴于http服务器设置简易、流行,本文使用http服务器。
若是Yum服务器是联网的,可以使用yum 命令直接安装Appache服务器,即软件包httpd
[root@luffy ~]#yum install httpd
这里因为虚拟机不方便链接外网(采用了Host-only Adapter模式,而且内网IP地址是固定,懒得改了,虚拟机玩家都有遇到过这种情形吧~),须要手动安装rpm包(直接在系统镜像里找,也能够到http://rpm.pbone.net/ 等网站找,而后再从物理机上传到虚拟机里),因为rpm命令没法解决包的依赖性问题,故须要安装若干个包,包括httpd-2.2.三、apr-1.2.七、apr-util-1.2.7,postgresql-libs-8.1.23。
具体过程以下(为说清楚包的依赖性,我把报错也列出来了):
[root@luffy ~]# rpm -ivh /media/CentOS/httpd-2.2.3-63.el5.centos.i386.rpm
error: Failed dependencies:
libapr-1.so.0 is needed by httpd-2.2.3-63.el5.centos.i386
libaprutil-1.so.0 is needed by httpd-2.2.3-63.el5.centos.i386
[root@luffy ~]# rpm -ivh /media/CentOS/apr-1.2.7-11.el5_6.5.i386.rpm
Preparing... ########################################### [100%]
1:apr ########################################### [100%]
[root@luffy ~]# rpm -ivh /media/CentOS/apr-util-1.2.7-11.el5_5.2.i386.rpm
error: Failed dependencies:
libpq.so.4 is needed by apr-util-1.2.7-11.el5_5.2.i386
[root@luffy ~]# rpm -ivh /media/CentOS/postgresql-libs-8.1.23-1.el5_7.3.i386.rpm
Preparing... ########################################### [100%]
1:postgresql-libs ########################################### [100%]
[root@luffy ~]# rpm -ivh /media/CentOS/apr-util-1.2.7-11.el5_5.2.i386.rpm
Preparing... ########################################### [100%]
1:apr-util ########################################### [100%]
[root@luffy ~]# rpm -ivh /media/CentOS/httpd-2.2.3-63.el5.centos.i386.rpm
Preparing... ########################################### [100%]
1:httpd ########################################### [100%]
启动httpd服务,并设置为开机时启动
[root@luffy ~]# service httpd start
[root@luffy ~]# chkconfig httpd on
3、
在Appache根目录下创建文件夹yum及其子文件夹CentOS,将光盘内CentOS文件夹下的全部内容拷贝到/var/www/html/yum/CentOS目录下(光碟内其余的数据不须要)。
[root@luffy ~]# mkdir -p /var/www/html/yum
[root@luffy ~]# mkdir -p /var/www/html/yum/CentOS
[root@luffy ~]# cp -prf /media/CentOS/* /var/www/html/yum/CentOS/
能够弹出光碟了
[root@luffy~]# umount /dev/cdrom
[root@luffy~]# eject
4、Yum服务器安装createrepo并建立仓库
createrepo是Linux下的建立仓库的软件包,这个软件包在系统镜像CentOS-5.8-i386-bin-DVD-1of2里是有的。这里已经拷贝到/var/www/html/yum/CentOS目录下了。
[root@luffy ~]# rpm -ivh /var/www/html/yum/CentOS/createrepo-0.4.11-3.el5.noarch.rpm
Preparing... ########################################### [100%]
1:createrepo ########################################### [100%]
(注:在CentOS 5.7下安装会有以下警告,但不影响后续操做:
warning: /var/www/html/yum/CentOS/createrepo-0.4.11-3.el5.noarch.rpm: Header V3 DSA signature: NOKEY, key ID e8562897)
安装完成以后,在/usr/bin/目录下会有一个createrepo命令。使用createrepo 生成XML metadata,在/var/www/html/yum/CentOS目录下生成repodata 文件夹(具体有何做用,还没弄明白)
[root@luffy ~]# createrepo /var/www/html/yum/CentOS/
2669/2669 - compat-gcc-34-g77-3.4.6-4.1.i386.rpm
Saving Primary metadata
Saving file lists metadata
Saving other metadata
5、
客户端修改Centos-Base.repo,建议先备份一个副本,之后若要从外网更新的话,能够再使用。
[root@zoro ~]# cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
[root@zoro ~]# vi /etc/yum.repos.d/CentOS-Base.repo
##############Centos-Base.repo内容修改后,如下#######################
# 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-$releasever - Base
baseurl=http://192.168.56.123/yum/CentOS
enabled=1
gpgcheck=0
#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=http://192.168.56.123/yum/CentOS
gpgcheck=1
enabled=1
gpgcheck=0
#packages used/produced in the build but not released
[addons]
name=CentOS-$releasever - Addons
baseurl=http://192.168.56.123/yum/CentOS
enabled=1
gpgcheck=0
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=http://192.168.56.123/yum/CentOS
enabled=1
gpgcheck=0
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
#baseurl=http://mirror.centos.org/centos/$releasever/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-$releasever - Contrib
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
##############Centos-Base.repo内容修改后,以上#######################
注:[centosplus]和[contrib]的内容不须要修改,它们的enabled值默认值是0,即模块不启用。
操做基本完成了,运气好的话,能够直接从Yum服务器拿软件、升级包。
不过,并不排除意外发生。好比有报错,提示找不着路由。
以安装mc为例:
[root@zoro ~]# yum install mc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
http://192.168.56.123/yum/CentOS/repodata/repomd.xml: [Errno 4] IOError: <urlopen error (113, 'No route to host')>
Trying other mirror.
http://192.168.56.123/yum/CentOS/repodata/repomd.xml: [Errno 4] IOError: <urlopen error (113, 'No route to host')>
Trying other mirror.
http://192.168.56.123/yum/CentOS/repodata/repomd.xml: [Errno 4] IOError: <urlopen error (113, 'No route to host')>
Trying other mirror.
http://192.168.56.123/yum/CentOS/repodata/repomd.xml: [Errno 4] IOError: <urlopen error (113, 'No route to host')>
Trying other mirror.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mc.i386 1:4.6.1a-35.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mc i386 1:4.6.1a-35.el5 addons 2.1 M
Transaction Summary
================================================================================
Install 1 Package(s)
Upgrade 0 Package(s)
Total download size: 2.1 M
Is this ok [y/N]: y
Downloading Packages:
http://192.168.56.123/yum/CentOS/mc-4.6.1a-35.el5.i386.rpm: [Errno 4] IOError: <urlopen error (113, 'No route to host')>
Trying other mirror.
Error Downloading Packages:
1:mc-4.6.1a-35.el5.i386: failure: mc-4.6.1a-35.el5.i386.rpm from addons: [Errno 256] No more mirrors to try.
6、
Yum服务器上,关闭防火墙及SELinux。
(通常状况下,企业里,都是关的,假若不关,只能说是运气很差,以置出了上述报错,此所谓运气很差也~)
7、
再到客户端进行操做,一切顺利。
客户端能够经过yum命令从服务器端下载并安装软件了
[root@zoro ~]# yum install mc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mc.i386 1:4.6.1a-35.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mc i386 1:4.6.1a-35.el5 addons 2.1 M
Transaction Summary
================================================================================
Install 1 Package(s)
Upgrade 0 Package(s)
Total download size: 2.1 M
Is this ok [y/N]: y
Downloading Packages:
mc-4.6.1a-35.el5.i386.rpm | 2.1 MB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : mc 1/1
Installed:
mc.i386 1:4.6.1a-35.el5
Complete!
运行yum udpate命令,便可将客户端升级为CentOS 5.8
[root@zoro~]#yum update
注:客户端CentOS 5.7升级完成后,内核由kernel-2.6.18-274升级到了kernel-2.6.18-308,客户端须要重启才能使用新的内核,重启后可以使用yum remove kernel*274*命令将原内核删除。
查看系统安装的内核
# rpm -qa | grep kernel
查看系统当前用到的内核的版本号
# more /proc/version