随着公司业务增长,服务器不断增多,软件的安装,升级等需求也随之而来,为了避免重复下载,我们肯定是需要自己搭建yum仓库的,将自己需要的包放进去。

下面的环境是:在rhel6上基于ftp服务实现yum仓库搭建
步骤如下:

1.安装ftp服务器

# 安装ftp服务器需要软件包,这里我们直接挂载光盘,使用rpm安装  
[root@rhel1 /]# mount /iso/rhel-server-6.3-x86_64-dvd.iso  /mnt/cdrom/ -o loop  
# 安装vsftpd包  
[root@rhel1 Packages]# rpm -ivh vsftpd-2.2.2-11.el6.x86_64.rpm  
warning: vsftpd-2.2.2-11.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY  
Preparing...                                            ########################################### [100%]  
   1:vsftpd                                             ########################################### [100%]  
# 启动ftp服务  
[root@rhel1 Packages]# /etc/init.d/vsftpd start  
Starting vsftpd for vsftpd:                                [  OK  ]  
# 设置ftp服务为开机启动  
[root@rhel1 Packages]# chkconfig vsftpd on

2. 在生产环境中,iptables都是开启[我们很多人做实验都喜欢关闭,不符合生产环境],所以要做策略

[root@rhel1 Packages]# vim /etc/sysconfig/iptables-config    # 添加相应模块  
IPTABLES_MODULES="ip_conntrack_ftp"     # ip_conntrack_ftp 必须小写  
[root@rhel1 rhel6]# iptables -I INPUT -p tcp --dport 21 -j ACCEPT  # 加入相应的防火墙策略  
[root@rhel1 rhel6]# service iptables save    # 保存防火墙策略 [此步不要忘记]  
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

3.创建存放软件的目录,手动创建repodata数据文件[要使用createrepo工具]

# 创建软件存放目录  
[root@rhel1 /]# mkdir /var/ftp/rhel6/other -p  
# 因为 createrepo 软件具有依赖关系,为了方面,我们先配置本地yum源,方便安装  
[root@rhel1 /]# vim /etc/yum.repos.d/test.repo  
[rhel-source]  
name=test  
baseurl=file:///mnt/cdrom  
enabled=1  
gpgcheck=0  
# 关于*.repo文件的配置详解如下:  
[ ]内的是仓库的名字  
name是仓库的描述也可以说是名字  
baseurl 仓库的位置  
enabled 是否启用这个仓库,1为起用,0为禁用  
gpgcheck 是否检查GPG签名(用来验证要安装的包是不是REDHAT官方的)  
GPGKEY 的存放地址我们需要用下面的命令导入这个签名。才能使用这项功能  
  
# 配好yum源好,执行如下操作  
[root@rhel1 /]# yum clean all  
[root@rhel1 /]# yum makecache  
  
# 安装createrepo 软件  
[root@rhel1 /]# yum -y install createrepo  
# 为软件目录创建 repodata数据文件  
[root@rhel1 Packages]# createrepo --database /var/ftp/rhel6/other  
  
# 配置 yum 仓库源  
[root@rhel1 cdrom]# vim /etc/yum.repos.d/test.repo  
[rhel-source]  
name=test  
baseurl=ftp://192.168.1.11/rhel6/other   # 此路径要写对,默认去掉/var/ftp前缀  
enabled=1  
gpgcheck=0  
# 执行如下操作  
[root@rhel1 /]# yum clean all  
[root@rhel1 /]# yum makecache  
  
# 如果没有报错,就表示成功了,现在你可以测试了  
  
# 下面附上完整的yum源配置文件  
[other]  
name=other  
baseurl=ftp://192.168.1.11/rhel6/other  
enabled=1  
gpgcheck=0  
[HighAvailability]  
name=HighAvailability  
baseurl=ftp://192.168.1.11/rhel6/HighAvailability  
enabled=1  
gpgcheck=0  
[LoadBalancer]  
name=LoadBalancer  
baseurl=ftp://192.168.1.11/rhel6/LoadBalancer  
enabled=1  
gpgcheck=0  
[Server]  
name=Server  
baseurl=ftp://192.168.1.11/rhel6/Server  
enabled=1  
gpgcheck=0  
[ResilientStorage]  
name=ResilientStorage  
baseurl=ftp://192.168.1.11/rhel6/ResilientStorage  
enabled=1  
gpgcheck=0

yum的几个命令如下:

yum -y localinstall VirtualBox-3.1-3.1.0_5467_fedora12-1.i686.rpm --nogpgcheck从本机目录安装软件包
yum -y groupinstall "Development Tools" "Development Libraries" 安装某个组件的全部软件包



在我们实际工作中,有时候服务器可能面临批量上线时,面对那么多服务器,如何实现快速安装操作系统,并及时完成任务,无人值守安装,可以很好的解决这个问题。

一、无人值守安装原理图及其简述:

pxe

安装顺序如下:

(1)、PXE Client向DHCP发送请求,请求分配IP地址 [前提:网卡支持pxe,其次,在bios设置开机网络启动]

(2)、DHCP服务器提收到客户端的请求,验证是否来至合法的PXE Client的请求,如果通过的话,为客户端分配的IP地址、pxelinux启动程序(TFTP)位置,以及配置文件所在位置

(3)、客户端收到回应后,向服务器(TFTP服务器)请求下载启动文件:pxelinux.0、pxelinux.cfg/default、vmlinuz、initrd.img等文件

(4)、当服务器(TFTP服务器)收到客户端的请求后,向客户端传送启动安装程序所必须的文件(pxelinux.0、pxelinux.cfg/default),default文件下载完成后,会根据该文件中定义的引导顺序,启动Linux安装程序的引导内核

(5)、向(FTP服务器)请求下载自动应答文件:ks.cfg

(6)、客户端根据ks.cfg的配置,安装操作系统

二、PXE + KickStart安装的条件:

(1)、一台支持PXE网卡的主机

(2)、DHCP 服务器:用来给客户机分配IP

(3)、TFTP 服务器:用来存放PXE的相关文件(pxelinux.0、pxelinux.cfg/default、vmlinuz、initrd.img)

(4)、FTP 服务器:用来存放系统安装文件

(5)、kickstart生成的ks.cfg配置文件

三、无人值守安装案例:

1.环境:rhel 6.4-x64(tftp+ftp+dhcp+pxe+kickstart)

[root@test ~]# uname -a
Linux test 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

2.步骤如下:

(1).切换到root用户,关闭防火墙,清空策略,关闭开机启动,关闭selinux

[kongzhong@rhel1 ~]$ sudo su  
[root@rhel1 kongzhong]# iptables -F  
[root@rhel1 kongzhong]# /etc/init.d/iptables stop  
[root@rhel1 kongzhong]# chkconfig iptables off  
[root@rhel1 kongzhong]# vim /etc/selinux/config  
SELINUX=permissive或者disabled

(2).安装tftp、vsftp、dhcp、xinetd、system-config-kickstart等软件

[root@rhel1 kongzhong]# yum -y install dhcp vsftpd xinetd tftp-server pykickstart system-config-kickstart  
  
# 启动ftp,并设置为开机启动。具体配置,请参看以前的一篇配置yum仓库的文章,有讲解  
[root@rhel1 /]# /etc/init.d/vsftpd start  
Starting vsftpd for vsftpd: [ OK ]  
[root@rhel1 /]# chkconfig vsftpd on  
  
# 配置dhcp服务器  
[root@rhel1 /]# vim /etc/dhcp/dhcpd.conf  
ddns-update-style interim;  
#authoritative;  
ignore client-updates;  
subnet 192.168.1.0 netmask 255.255.255.0 {  
range 192.168.1.101 192.168.1.200;  
default-lease-time 21600;  
max-lease-time 43200;  
option domain-name-servers 192.168.1.1;  
option routers 192.168.1.1;  
option subnet-mask 255.255.255.0;  
option nis-domain "kongzhong.com";  
option time-offset -18000;  
# tftp server 指定tftp-server服务器的IP地址  
next-server 192.168.1.11;  
# pxe 得到IP以后的引导文件  
filename "pxelinux.0";  
}  
  
# 启动dhcp服务器,并设置为开机启动  
[root@rhel1 /]# /etc/init.d/dhcpd start  
Starting dhcpd: [ OK ]  
[root@rhel1 /]# chkconfig dhcpd on  
  
# 编辑tftp的配置文件  
[root@rhel1 /]# vim /etc/xinetd.d/tftp  
# default: off  
# description: The tftp server serves files using the trivial file transfer \  
# protocol. The tftp protocol is often used to boot diskless \  
# workstations, download configuration files to network-aware printers, \  
# and to start the installation process for some operating systems.  
service tftp  
{  
socket_type = dgram  
protocol = udp  
wait = yes  
user = root  
server = /usr/sbin/in.tftpd  
server_args = -s /var/lib/tftpboot # 这个路径可以不修改,也可以写你自己指定的目录  
disable = no # 这里的yes改为no  
per_source = 11  
cps = 100 2  
flags = IPv4  
}  
  
# 启动超级守护进程  
[root@rhel1 /]# service xinetd start  
Starting xinetd: [ OK ]  
[root@rhel1 /]# chkconfig xinetd on

(3).复制需要用到相关文件

# 挂载镜像,复制镜像  
# 我的镜像已经复制到ftp的共享目录下[这里看不明白的,请看yum仓库搭建的文章]  
[root@rhel1 p_w_picpaths]# cd /var/ftp/rhel6/p_w_picpaths/pxeboot/  
[root@rhel1 pxeboot]# ll  
total 33632  
-r--r--r--. 1 root root 30442765 Nov 1 22:51 initrd.img  
-r--r--r--. 1 root root 441 Nov 1 22:51 TRANS.TBL  
-r-xr-xr-x. 1 root root 3986992 Nov 1 22:51 vmlinuz  
# 将上面路径中的initrd.img和vmlinuz文件 复制到/var/lib/tftpboot/目录下  
[root@rhel1 pxeboot]# cp initrd.img vmlinuz /var/lib/tftpboot/  
  
# 找到pxelinux.0驱动文件,并把它复制到/var/ftp/tftpboot/目录下  
[root@rhel1 /]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/  
  
# 显示刚才复制的情况,下面这些文件都不能少  
[root@rhel1 /]# cd /var/lib/tftpboot/  
[root@rhel1 tftpboot]# ll  
total 33656  
-r--r--r--. 1 root root 30442765 Nov 6 21:34 initrd.img  
-rw-r--r--. 1 root root 26828 Nov 6 21:39 pxelinux.0  
-r-xr-xr-x. 1 root root 3986992 Nov 6 21:34 vmlinuz  
  
# 建立 pxelinux.cfg 目录[这个目录是必须的]  
[root@rhel1 tftpboot]# mkdir pxelinux.cfg  
  
# 复制 /var/ftp/rhel6/isolinux/下 模板文件(isolinux.cfg),到 /var/lib/tftpboot/pxelinux.cfg/目录下,并改名为default  
[root@rhel1 tftpboot]# cp /var/ftp/rhel6/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default  
[root@rhel1 pxelinux.cfg]# chmod 755 default # 修改文件权限  
  
# 编辑刚才配置文件,未见内容如下:  
[root@rhel1 pxelinux.cfg]# vim default  
default rhel6 # 默认启动哪个系统[即用户输入的指令]  
prompt 1 # 是否显示 boot:[1:表示显示]  
timeout 600 # 默认等待时间  
display boot.msg  
# 标签,rhel6是指一个标签  
label rhel6  
kernel vmlinuz  
append initrd=initrd.img ks=ftp://192.168.1.11/rhel6/ks.cfg # 指定ks.cfg文件的位置  
# 标签,文本安装  
label rhel6-text  
kernel vmlinuz  
append initrd=initrd.img text  
# 标签,硬盘启动  
label local  
localboot 1

(4).生成ks.cfg文件,使用system-config-kickstart 生成图形化界面,具体根据界面选择,这里就不详述,仅给出ks.cfg文件

[root@rhel1 pxelinux.cfg]# system-config-kickstart # 在图形化界面,生成自动安装文件:ks.cfg  
# 复制ks.cfg文件到 ftp共享目录下: /var/ftp/rhel6/  
[root@rhel1 pxelinux.cfg]# cp ks.cfg /var/ftp/rhel6/  
  
# 下面看一下ks.cfg的详细配置:  
[root@rhel1 pxelinux.cfg]# vim /var/ftp/rhel6/  
#platform=x86, AMD64, or Intel EM64T  
#version=DEVEL  
# Firewall configuration  
firewall --disabled  
# Install OS instead of upgrade  
install  
# Use network installation  
url --url="ftp://192.168.1.11/rhel6"  
# Root password  
rootpw --iscrypted $1$NxE8YVhk$x9N62YgtEkdC.WpgZve8f.  
# System authorization information  
auth --useshadow --passalgo=sha512  
# Use text mode install  
text  
firstboot --disable  
# System keyboard  
keyboard us  
# System language  
lang en_US  
# SELinux configuration  
selinux --disabled  
# Installation logging level  
logging --level=info  
# Reboot after installation  
reboot  
# System timezone  
timezone Asia/Shanghai  
# Network information  
network --bootproto=dhcp --device=eth0 --onboot=on  
# System bootloader configuration  
bootloader --location=mbr  
# Clear the Master Boot Record  
zerombr  
# Partition clearing information  
clearpart --all --initlabel  
# Disk partitioning information  
part /boot --fstype="ext4" --size=200  
part swap --fstype="swap" --size=1000  
part / --fstype="ext4" --size=20700  
  
%post --interpreter=/bin/bash  
# 安装完成后,执行的脚本  
#!/bin/bash  
# 创建用户,设置密码,设置固定ip地址,设置hostname,设置开机启动方式,  
/usr/sbin/useradd kongzhong  
/bin/echo kongzhong|passwd --stdin kongzhong  
ADDRESS=`/sbin/ifconfig eth0|grep -i "inet\>" |awk '{print $2}'`  
NUM=${ADDRESS##*.}  
NETADDRESS=`/sbin/ifconfig eth0|grep -i "inet\>" |awk '{print $2}'|awk -F: '{print $2}'`  
sed -i -e "s/HOSTNAME.*/HOSTNAME=client$NUM.kongzhong.com/g" /etc/sysconfig/network  
sed -i -e "s/127.0.0.1.*/127.0.0.1 client$NUM.kongzhong.com localhost localhost.localdomain/g" /etc/hosts  
sed -i -e "s/BOOTPROTO.*/BOOTPROTO=static\nIPADDR=$NETADDRESS\nNETMASK=255.255.255.0\nGATEWAY=192.168.1.1\nDNS1=192.168.1.1\n/g" /etc/sysconfig/network-scripts/ifcfg-eth0  
sed -i -e "s/id:3:initdefault/id:5:initdefault/g" /etc/inittab  
# 临时获取root权限,配置yum源  
/bin/chmod 755 /etc/sudoers  
cat >> /etc/sudoers << EOF   
kongzhong ALL=(ALL) NOPASSWD:ALL   
EOF   
/bin/chmod 440 /etc/sudoers   
# 配置yum源  
rm -rf /etc/yum.repos.d/*   
cat >> /etc/yum.repos.d/test.repo << EOF  
[other]  
name=other  
baseurl=ftp://192.168.1.11/rhel6/other  
enabled=1  
gpgcheck=0  
[HighAvailability]  
name=HighAvailability  
baseurl=ftp://192.168.1.11/rhel6/HighAvailability  
enabled=1  
gpgcheck=0  
[LoadBalancer]  
name=LoadBalancer  
baseurl=ftp://192.168.1.11/rhel6/LoadBalancer  
enabled=1  
gpgcheck=0  
[Server]  
name=Server  
baseurl=ftp://192.168.1.11/rhel6/Server  
enabled=1  
gpgcheck=0  
[ResilientStorage]  
name=ResilientStorage  
baseurl=ftp://192.168.1.11/rhel6/ResilientStorage  
enabled=1  
gpgcheck=0  
EOF  
%end  
  
%packages  
@base  
@basic-desktop  
@client-mgmt-tools  
@compat-libraries  
@console-internet  
@fonts  
@general-desktop  
@graphical-admin-tools  
@input-methods  
@internet-browser  
@virtualization  
@virtualization-client  
@virtualization-platform  
@virtualization-tools  
@x11  
%end

(5).现在可以重新检查一下,tftp,ftp,dhcp,xinetd是否开启,并设置开机启动;

iptable和selinux是否关闭,这个一定要关闭,不然会有问题。