您的位置:首页 > 运维架构 > Linux

搭建局域网CentOS Yum服务器

2012-03-31 12:30 471 查看
对于企而言,假设有一批CentOS 5.7服务器需要升级到CentOS 5.8,每一台机都使用yum update命令从外网下载升级包的话,既费时间,也占流量,实在不划算。而对于虚拟机玩家而言,可能需要升级的系统不多,也就三四台,但倘若都从外网下载升级包,也是件很费劲的的事情,而且,有时候会遇到物理机不方便联网的情况,那就加无奈。

所以,在局域网内搭建yum服务器,应该是每个Linux从业者必须懂的基本功。SQ不才,直到现在才弄懂,实在惭愧,在此分享一下经验。其实操作很简单,注意一些细节就行了。

本文作业环境:
注:以下的操作是在VirtualBox虚拟机环境下进行的。

提供Yum源的服务器,以下简称为Yum服务器:
CentOS 5.8,主机名为luffy,IP为192.168.56.123
(注:Yum服务器也可以为CentOS 5.7系统)

需要升级的服务器,以下简称为客户端:
CentOS 5.7,主机名为zoro,IP为192.168.56.124

一、
到官网查找并下载最新的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

二、
为使局域网内其他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.3、apr-1.2.7、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

三、
在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

四、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

五、
客户端修改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.

六、
Yum服务器上,关闭防火墙及SELinux。
(一般情况下,企业里,都是关的,倘若不关,只能说是运气不好,以置出了上述报错,此所谓运气不好也~)

七、
再到客户端进行操作,一切顺利。
客户端可以通过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

本文出自 “一树清劲” 博客,请务必保留此出处http://sunshyfangtian.blog.51cto.com/1405751/822167
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: