對於企而言,假設有一批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