1 安装前准备

关闭PXE服务端的防火墙 和 selinux,设置静态获取地址,关闭Vmware软件中的DHCP服务,基于NAT模式.

服务地址
PXE192.168.10.102
DHCP192.168.10.102
TFTP192.168.10.102
HTTP192.168.10.102

2 安装所需软件并启动服务

[root@centos7 ~]# yum install -y tree dhcp tftp-server httpd syslinux

[root@centos7 ~]#  systemctl enable --now httpd tftp.socket
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

3 配置DHCP服务

[root@centos7 ~]#  vim /etc/dhcp/dhcpd.conf
option domain-name "baidu.com";
option domain-name-servers 233.5.5.5,114.114.114.114;
default-lease-time 86400;
max-lease-time 106400;
log-facility local7;
subnet 192.168.10.0 netmask 255.255.255.0 {
  range 192.168.10.200 192.168.10.200;
  option routers 192.168.10.2;
  next-server 192.168.10.102;
  filename "pxelinux.0";
}

[root@Rocky8 ~]# systemctl enable --now dhcpd
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpd.service → /usr/lib/systemd/system/dhcpd.service.

4 准备安装系统yum仓库

[root@centos7 ~]#  mkdir -pv /var/www/html/centos{6,7,8}
mkdir: 已创建目录 '/var/www/html/centos6'
mkdir: 已创建目录 '/var/www/html/centos7'
mkdir: 已创建目录 '/var/www/html/centos8'

[root@Rocky8 ~]# mkdir /var/www/html/ks
[root@Rocky8 ~]# init 0

[root@centos7 ~]#  echo "- - -">>/sys/class/scsi_host/host0/scan
[root@centos7 ~]# echo "- - -">>/sys/class/scsi_host/host1/scan
[root@centos7 ~]#  echo "- - -">>/sys/class/scsi_host/host2/scan
[root@centos7 ~]#  mount /dev/sr0 /var/www/html/centos8/
[root@centos7 ~]#  mount /dev/sr2 /var/www/html/centos7
[root@centos7 ~]# mount /dev/sr1 /var/www/html/centos6
[root@centos7 ~]#  df -h | grep ^/dev/sr*
/dev/sr0              11G   11G     0  100% /var/www/html/centos8
/dev/sr2             4.4G  4.4G     0  100% /var/www/html/centos7
/dev/sr1             3.8G  3.8G     0  100% /var/www/html/centos6

5 准备kickstart应答文件

[root@centos7 ~]#  cat /var/www/html/ks/centos6-ks.cfg
install
text
reboot
lang en_US.UTF-8
keyboard us
rootpw --plaintext 123456
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"

network  --bootproto=static --device=eth0 --gateway=192.168.10.2 --ip=192.168.10.106 --nameserver=233.5.5.5 --netmask=255.255.255.0 --ipv6=auto --activate
network  --hostname=centos6

url --url=http://192.168.10.102/centos6/

zerombr
clearpart --all --initlabel
part /boot --fstype=ext4 --size=1024
part / --fstype=ext4 --size=20000
part swap --size=2048

%packages
@core
@server-policy
@workstation-policy
autofs
vim-enhanced
%end

%post
useradd wu
echo 123456 | passwd --stdin wu &> /dev/null
%end

[root@centos7 ~]#  cat /var/www/html/ks/centos7-ks.cfg
install
keyboard 'us'
rootpw --plaintext 123456
lang en_US
auth  --useshadow  --passalgo=sha512
text
firstboot --disable
selinux --disabled
firewall --disabled
reboot

network  --bootproto=static --device=eth0 --gateway=192.168.10.2 --ip=192.168.10.107 --nameserver=223.5.5.5 --netmask=255.255.255.0
network  --hostname=centos7
timezone Asia/Shanghai --isUt

url --url="http://192.168.10.102/centos7"

bootloader --location=mbr
zerombr
clearpart --all --initlabel
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=1024
part / --fstype="xfs" --size=20000

%post --interpreter=bash
useradd wu
echo 123456|passwd --stdin wu
%end

%packages
@^minimal
@core
%end
[root@centos7 ~]#  cat /var/www/html/ks/centos8-ks.cfg
ignoredisk --only-use=sda
zerombr
text
reboot
selinux --disabled
firewall --disabled
keyboard --vckeymap=us --xlayouts='us'
lang en_US.UTF-8
rootpw --plaintext 123456
firstboot --enable
skipx
timezone Asia/Shanghai --isUtc --nontp

network  --bootproto=static --device=eth0 --gateway=192.168.10.2 --ip=192.168.10.108 --nameserver=233.5.5.5 --netmask=255.255.255.0 --ipv6=auto --activate
network  --hostname=centos8

url --url=http://192.168.10.102/centos8/

clearpart --all --initlabel
part / --fstype="xfs" --ondisk=sda --size=20000
part swap --fstype="swap" --ondisk=sda --size=2048
part /boot --fstype="ext4" --ondisk=sda --size=1024

services --disabled="chronyd"
%packages
@^minimal-environment
kexec-tools
%end
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

%post
useradd wu
echo 123456 | passwd --stdin wu &> /dev/null
%end

6 准备PXE启动文件

#拷贝centos6,7,8系统内核文件到tftp数据目录
[root@centos7 ~]#  mkdir -pv /var/lib/tftpboot/centos{6..8}
[root@centos7 ~]#  cp /var/www/html/centos6/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6/
[root@centos7 ~]#  cp /var/www/html/centos7/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7/
[root@centos7 ~]#  cp /var/www/html/centos8/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos8/

[root@centos7 ~]#  ll /var/lib/tftpboot/
总用量 0
drwxr-xr-x 2 root root 39 8月  28 06:13 centos6
drwxr-xr-x 2 root root 39 8月  28 06:13 centos7
drwxr-xr-x 2 root root 39 8月  28 06:13 centos8

#拷贝 syslinux软件包的pxelinux.0 启动文件和 menu.c32 pxe菜单文件
[root@Rocky8 ~]# cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/

#以下三个文件是CentOS8安装所必须文件,CentOS6,7则不需要
[root@centos7 ~]#  cp /var/www/html/centos8/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32} /var/lib/tftpboot/

#生成内核启动菜单文件
[root@centos7 ~]#  mkdir /var/lib/tftpboot/pxelinux.cfg
[root@centos7 ~]#  cp /var/www/html/centos8/isolinux/isolinux.cfg  /var/lib/tftpboot/pxelinux.cfg/default

[root@Rocky8 ~]# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── centos6
│   ├── initrd.img
│   └── vmlinuz
├── centos7
│   ├── initrd.img
│   └── vmlinuz
├── centos8
│   ├── initrd.img
│   └── vmlinuz
├── ldlinux.c32
├── libcom32.c32
├── libutil.c32
├── menu.c32
├── pxelinux.0
└── pxelinux.cfg
    └── default

4 directories, 12 files

7 定制内核启动菜单文件

[root@centos7 ~]#  vim /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32
timeout 600

menu title PXE Install CentOS Linux

label linux8
  menu label Auto Install CentOS ^8
  kernel centos8/vmlinuz
  append initrd=centos8/initrd.img quiet inst.ks=http://192.168.10.102/ks/centos8-ks.cfg net.ifnames=0 biosdevname=0

label linux7
  menu label Auto Install CentOS ^7
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img quiet ks=http://192.168.10.102/ks/centos7-ks.cfg net.ifnames=0 biosdevname=0

label linux6
  menu label Auto Install CentOS ^6
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img quiet ks=http://192.168.10.102/ks/centos6-ks.cfg net.ifnames=0 biosdevname=0

label rescue
  menu label ^Rescue a CentOS system
  kernel centos8/vmlinuz
  append initrd=centos8/initrd.img quiet ks=http://192.168.10.102/ks/centos8-ks.cfg

label local
  menu default
  menu label Boot from ^local drive
  localboot 0xffff

8 测试客户端基于PXE实现自动安装

新准备一台主机,设置网卡引导,可看到看启动菜单,并实现自动安装

注意:VMware workstation 对于不同的CentOS 版本,生成的虚拟机的硬件并不兼容




[root@Rocky8 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
#增添以下内容
label manual
 menu label ^Manual Install CentOS Linux 8.0 
 kernel centos8/vmlinuz
 append initrd=centos8/initrd.img inst.repo=http://10.0.0.100/centos8