PXE,全名Pre-boot Execution Environment,预启动执行环境。

1、准备一台CentOS服务器

检查系统yum源是可用的,源配置这里就不介绍了。

[root@test yum.repos.d]# yum repolist -v
 

2、安装所需的软件

[root@test ~]# yum -y install dhcp xinet tftp-server httpd syslinux vim 
[root@test ~]# yum -y install system-config-kickstart

注意:关闭selinux和防火墙
 

3、配置DHCP

[root@test ~]# vim /etc/dhcp/dhcpd.conf

[root@test ~]# cat /etc/dhcp/dhcpd.conf
subnet 192.168.1.0 netmask 255.255.255.0 {
        range 192.168.1.100 192.168.1.200;
        option subnet-mask 255.255.255.0;
        default-lease-time 21600;
        max-lease-time 43200;
        next-server 192.168.1.21;
        filename "/pxelinux.0";
}
systemctl start dhcpd
netstat -tunlp|grep dhcp  #监听67端口

如果dhcp配置有问题可以看日志进行排查:tailf /var/log/messages

4、配置tftp

[root@test ~]# cat /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
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
systemctl start tftp
netstat -tunlp|grep 69

5、配置http

sed -i "277i ServerName 127.0.0.1:80" /etc/httpd/conf/httpd.conf
systemctl start httpd
 110  mkdir /var/www/html/CentOS
  111  mount /dev/sr0  /var/www/html/CentOS/
  112  df -h

浏览器访问IP+CentOS,检查配置是否正确

6、配置pxe

114  ls /usr/share/syslinux/pxelinux.0
  115  cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
  116  cp -a /var/www/html/CentOS/isolinux/* /var/lib/tftpboot/
  117  ls /var/lib/tftpboot/
  118  mkdir -p /var/lib/tftpboot/pxelinux.cfg
  119  cp /var/www/html/CentOS/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

配置完成后,打开服务器电源,esc使用pxe启动

选择URL方式安装

禁用IPV6

输入http地址

7、配置ks.cfg

[root@test ~]# grub-crypt
[root@test ~]# mkdir /var/www/html/ks_config
[root@test ~]#vim /var/www/html/ks_config/CentOS7-ks.cfg
# Kickstart Configurator for CentOS by yao zhang
install
url --url="http://192.168.1.21/CentOS/"
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS7
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw  --iscrypted $6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
clearpart --all --initlabel
part /boot --fstype=ext4 --asprimary --size=200
part swap --size=1024
part / --fstype=ext4 --grow --asprimary --size=200
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot
%packages
@base
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet
%post
wget -O /tmp/optimization.sh http://192.168.1.21/ks_config/optimization.sh &>/dev/null
/bin/sh /tmp/optimization.sh
%end

关键字含义install告知安装程序,这是一次全新安装,而不是升级upgradeurl --url=" "通过FTPHTTP从远程服务器上的安装树中安装。
url --url="http://10.0.0.7/CentOS-6.7/"url --url ftp://<username>:<password>@<server>/<dir>nfs从指定的NFS服务器安装。
nfs --server=nfsserver.example.com --dir=/tmp/install-treetext使用文本模式安装。lang设置在安装过程中使用的语言以及系统的缺省语言。lang en_US.UTF-8keyboard设置系统键盘类型。keyboard uszerombr清除mbr引导信息。bootloader系统引导相关配置。
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"--location=,指定引导记录被写入的位置.有效的值如下:mbr(缺省),partition(在包含内核的分区的第一个扇区安装引导装载程序)或none(不安装引导装载程序)。
--driveorder,指定在BIOS引导顺序中居首的驱动器。
--append=,指定内核参数.要指定多个参数,使用空格分隔它们。network为通过网络的kickstart安装以及所安装的系统配置联网信息。
network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS6--bootproto=[dhcp/bootp/static]中的一种,缺省值是dhcpbootpdhcp被认为是相同的。
static方法要求在kickstart文件里输入所有的网络信息。
network --bootproto=static --ip=10.0.0.100 --netmask=255.255.255.0 --gateway=10.0.0.2 --nameserver=10.0.0.2 请注意所有配置信息都必须在一行上指定,不能使用反斜线来换行。
--ip=,要安装的机器的IP地址.
--gateway=,IP地址格式的默认网关.
--netmask=,安装的系统的子网掩码.
--hostname=,安装的系统的主机名.
--onboot=,是否在引导时启用该设备.
--noipv6=,禁用此设备的IPv6.
--nameserver=,配置dns解析.timezone设置系统时区。timezone --utc Asia/Shanghaiauthconfig系统认证信息。authconfig --enableshadow --passalgo=sha512 设置密码加密方式为sha512 启用shadow文件。rootpwroot密码clearpart清空分区。clearpart --all --initlabel--all 从系统中清除所有分区,--initlable 初始化磁盘标签part磁盘分区。
part /boot --fstype=ext4 --asprimary --size=200part swap --size=1024part / --fstype=ext4 --grow --asprimary --size=200--fstype=,为分区设置文件系统类型.有效的类型为ext2,ext3,swapvfat
--asprimary,强迫把分区分配为主分区,否则提示分区失败。
--size=,以MB为单位的分区最小值.在此处指定一个整数值,如500.不要在数字后面加MB
--grow,告诉分区使用所有可用空间(若有),或使用设置的最大值。firstboot负责协助配置redhat一些重要的信息。
firstboot --disableselinux关闭selinuxselinux --disabledfirewall关闭防火墙。firewall --disabledlogging设置日志级别。logging --level=inforeboot

设定安装完成后重启,此选项必须存在,不然kickstart显示一条消息,并等待用户按任意键后才重新引导,也可以选择halt关机。

优化脚本

vim /var/www/html/ks_config/optimization.sh
#!/bin/bash
##############################################################
# File Name: /var/www/html/ks_config/optimization.sh
# Version: V1.0
# Author: yao zhang
# Organization: www.zyops.com
# Created Time : 2015-12-03 15:23:08
# Description: Linux system initialization
##############################################################
. /etc/init.d/functions
Ip=10.0.0.7
Port=80
ConfigDir=ks_config
# Judge Http server is ok?
PortNum=`nmap $Ip  -p $Port 2>/dev/null|grep open|wc -l`
[ $PortNum -lt 1 ] && {
        echo "Http server is bad!"
        exit 1
}
# Defined result function
function Msg(){
        if [ $? -eq 0 ];then
          action "$1" /bin/true
        else
          action "$1" /bin/false
        fi
}
# Defined IP function
function ConfigIP(){
        Suffix=`ifconfig eth0|awk -F "[ .]+" 'NR==2 {print $6}'`
        cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<-END
        DEVICE=eth0
        TYPE=Ethernet
        ONBOOT=yes
        NM_CONTROLLED=yes
        BOOTPROTO=none
        IPADDR=10.0.0.$Suffix
        PREFIX=24
        GATEWAY=10.0.0.2
        DNS1=10.0.0.2
        DEFROUTE=yes
        IPV4_FAILURE_FATAL=yes
        IPV6INIT=no
        NAME="System eth0"
        END
        Msg "config eth0"
}
# Defined Yum source Functions
function yum(){
        YumDir=/etc/yum.repos.d
        [ -f "$YumDir/CentOS-Base.repo" ] && cp $YumDir/CentOS-Base.repo{,.ori} 
        wget -O $YumDir/CentOS-Base.repo http://$Ip:$Port/$ConfigDir/CentOS-Base.repo &>/dev/null &&\
        wget -O $YumDir/epel.repo http://$Ip:$Port/$ConfigDir/epel.repo &>/dev/null &&\
        Msg "YUM source"
}
# Defined Hide the system version number Functions
function HideVersion(){
        [ -f "/etc/issue" ] && >/etc/issue
        Msg "Hide issue" 
        [ -f "/etc/issue.net" ] && > /etc/issue.net
        Msg "Hide issue.net"
}
# Defined OPEN FILES Functions
function openfiles(){
        [ -f "/etc/security/limits.conf" ] && {
        echo '*  -  nofile  65535' >> /etc/security/limits.conf
        Msg "open files"
        }
}
# Defined Kernel parameters Functions
function kernel(){
        KernelDir=/etc
        [ -f "$KernelDir/sysctl.conf" ] && /bin/mv $KernelDir/sysctl.conf{,.ori}
        wget -O $KernelDir/sysctl.conf http://$Ip:$Port/$ConfigDir/sysctl.conf &>/dev/null
        Msg "Kernel config"
}
# Defined System Startup Services Functions
function boot(){
        for oldboy in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|rsyslog|sshd|sysstat"` 
          do 
           chkconfig $oldboy off
        done
        Msg "BOOT config"
}
# Defined Time Synchronization Functions
function Time(){
        echo "#time sync by zhangyao at $(date +%F)" >>/var/spool/cron/root
        echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null' >>/var/spool/cron/root
        Msg "Time Synchronization"
}
# Defined main Functions
function main(){
        ConfigIP
        yum
        HideVersion
        openfiles
        kernel
        boot
        Time
}
main
# rz上传CentOS-Base.repo、epel.repo、sysctl.conf


vim /var/lib/tftpboot/pxelinux.cfg/default
default ks
prompt 0
label ks
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.1.21/ks_config/CentOS7-ks.cfg # 告诉安装程序ks.cfg文件在哪里
# append initrd=initrd.img ks=http://192.168.1.21/ks_config/CentOS7-ks.cfg ksdevice=eth0
# ksdevice=eth0代表当客户端有多块网卡的时候,要实现自动化需要设置从eth1安装