一.适用环境:
1.需要大规模批量部署linux操作系统
2.客户端无光驱或光驱损坏,需要网络安装linux系统
3.需要隔三差五重复安装linux系统,为了减少麻烦,节约时间,提高效率。
二、原理
在服务器端搭建DHCPtftpnfs服务并用kickstart配置自动安装脚本,DHCP服务用于向客户端分配IP,tftp服务用于传输bootloader文件pxelinux.0nfs服务负责传输安装镜像文件,用kickstart配置的ks.cfg实现无人值守全自动安装。
前提:客户端网卡内置PXE芯片,现在一般网卡都支持pxe协议。
三.具体操作过程(基于RHEL)
1.Server IP为:192.168.0.20 ,安装镜像挂载到/mnt下。

2. 安装tftp 服务 、dhcp服务和nfs服务(一般默认已安装).
安装前先查看这些服务是否已安装
# rpm -qa |grep tftp 
# rpm -qa |grep dhcp
# rpm -qa |grep nfs
# rpm -q portmap  
未安装的进行安装
#cd /mnt/RedHat/RPMS/    RHEL5 中是cd /mnt/Server
#rpm –ivh dhcp-*
#rpm –ivh tftp-* 
#rpm –ivh nfs-utils portmap
 
3.配置启动DHCP服务
#cp cp /usr/share/doc/dhcp-3.0.1/dhcpd.conf.sample /etc/dhcpd.conf
#vi /etc/dhcpd.conf   
添加filename "pxelinux.0";      //指定bootloader文件
     next-server 192.168.0.20;  //指定索取pxelinux.0tftp服务器IP
添加的这两行可在大括号外面,也可在里面,next-server选项可不写,但建议最好写上
配置举例:
ddns-update-style interim;
ignore client-updates;
 
subnet 192.168.0.0 netmask 255.255.255.0 {
 
        option routers                  192.168.0.1;
        option subnet-mask              255.255.255.0;
option time-offset              -18000; # Eastern Standard Time
 
range dynamic-bootp 192.168.0.128 192.168.0.254;
        default-lease-time 21600;
        max-lease-time 43200;
        filename "pxelinux.0";
        next-server 192.168.0.20;       
}
#service dhcpd start     //启动服务
4.复制内核相关文件到/tftpboot
  #cd /tftpboot
  #cp  /mnt/isolinux/*  ./      (
实际需要的是vmlinuzinitrd.img *.msg 这几个文件,但为了操作方便,我直接把isolinux目录下的文件全cp过来)
  #mkdir pxelinux.cfg
  #mv isolinux.cfg     pxelinux.cfg/
default       (
default配置文件的作用是告诉主机从哪里去加载操作系统内核)
  #cp /usr/lib/syslinux/pxelinux.0  ./    (
将启动加载文件拷到/tftpboot)
5. 修改tftp参数并启动tftp服务
# vi /etc/xinetd.d/tftp
……………………………………………………………………………………
          service tftp
           {
            socket_type             = dgram
            protocol                = udp
            wait                    = yes
            user                    = root
            server                  = /usr/sbin/in.tftpd
            server_args             = -s /tftpboot
            disable                 = no
            per_source              = 11
            cps                     = 100 2
            flags                   = IPv4
           }
…………………………………………………………………………………………
 
tftpboot 这个参数主要是指定tftp client 客户端从服务器的哪个目录去加载bootloaderpxelinux.0文件。
#service xinetd restart       //启动服务
或者不改tftp参数直接启动
#chkconfig tftp on
#service xinetd restart
6.修改/tftpboot/pxelinux.cfg/default文件
#vi /tftpboot/pxelinux.cfg/default
修改第3行,第12.
1 default linux
2 prompt 1
3 timeout
10            //时间调小点
4 display boot.msg
5 F1 boot.msg
6 F2 options.msg
7 F3 general.msg
8 F4 param.msg
9 F5 rescue.msg
10 label linux
11 kernel vmlinuz
12 append
ks=nfs:192.168.0.20:/pub/ks.cfg  initrd=initrd.img
13 label text
……
7.安装kickstart配置ks.cfg
 #cd /mnt/ RedHat/RPMS/
#rpm –ivh *kickstart*
 #system-config-kickstart   (
在图形界面终端打此命令
)
 
根据需要配置ks.cfg.
/目录下创建pub目录,将ks.cfg移动到此目录下。
#
chmod 707 /pub/ks.cfg     //修改ks.cfg权限
ks.cfg文件,最终配置示例如下:
#Generated by Kickstart Configurator
#platform=x86, AMD64, or Intel EM64T
#System  language
lang en_SG
#Language modules to install
langsupport zh_CN en_US --default=en_SG
#System keyboard
keyboard us
#System mouse
mouse
#Sytem timezone
timezone Asia/Shanghai
#Root password
rootpw --iscrypted $1$2qm1.E7d$/p/NGomGdh00jvBeeIyLR.
#Reboot after installation
reboot
#Install OS instead of upgrade
install
#Use NFS installation Media
nfs --server=192.168.0.20  --dir=/mnt
#System bootloader configuration
bootloader --location=mbr
#Clear the Master Boot Record
zerombr yes
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
clearpart --all --initlabel
#Disk partitioning information
part swap --size 512
part /boot --fstype ext3 --size 100
part / --fstype ext3 --size 1 --grow
#System authorization infomation
auth  --useshadow  --enablemd5
#Network information
network --bootproto=dhcp --device=eth0
#Firewall configuration
firewall --disabled
#XWindows configuration information
xconfig --depth=8 --resolution=640x480 --defaultdesktop=GNOME
#Package install information
%packages --resolvedeps
@ base-x                  //最小化安装  (如果用kickstart不能选择安装包时需要手动添加)
如果是RHEL5需要手动添加一行
key --skip          //跳过安装序列号
8.配置nfs并启动服务
#vi /etc/exports         //添加两行记录
/mnt    *(ro)
/pub     *(ro)
#service nfs start
9.为保证实验一次成功,将所有服务都重新启动一下
#service dhcpd restart
#service xinetd restart
#service nfs restart
10.在一客户端上进行测试(server在同一局域网内).
提示:客户端在确定domain namehost name时会等几分钟时间请耐心等待
※成功标志:能顺利跳过各项设置到开始自动安装软件包代表服务器配置没问题(客户端只能有个开机动作)