在 CentOS 7 上搭建 PXE(Preboot eXecution Environment)服务器可以让客户端通过网络启动和安装操作系统,以下是详细步骤:
安装必要软件包
yum install -y dhcp tftp-server syslinux xinetd vsftpd
配置 DHCP 服务器
编辑 /etc/dhcp/dhcpd.conf:

vim /etc/dhcp/dhcpd.conf
根据网络环境调整):
option domain-name "example.com";
option domain-name-servers 8.8.8.8;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200;
option routers 192.168.1.1;
option subnet-mask 255.255.255.0;
next-server 192.168.1.10; # PXE服务器IP地址
filename "pxelinux.0"; # TFTP引导文件
} 配置 TFTP 服务器
编辑 /etc/xinetd.d/tftp:
vim /etc/xinetd.d/tftp
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -v -s /var/lib/tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
} 准备 TFTP 目录结构
# 创建目录 mkdir -p /var/lib/tftpboot/pxelinux.cfg mkdir -p /var/lib/tftpboot/images # 复制引导文件 cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/ cp /usr/share/syslinux/menu.c32 /var/lib/tftpboot/ cp /usr/share/syslinux/memdisk /var/lib/tftpboot/ cp /usr/share/syslinux/mboot.c32 /var/lib/tftpboot/ cp /usr/share/syslinux/chain.c32 /var/lib/tftpboot/
配置默认启动菜单
创建 /var/lib/tftpboot/pxelinux.cfg/default:
vim /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu.c32
PROMPT 0
TIMEOUT 600
ONTIMEOUT local
PXE Boot Menu
LABEL local
MENU LABEL Boot from local drive
LOCALBOOT 0
LABEL centos7
MENU LABEL Install CentOS 7
KERNEL images/centos7/vmlinuz
APPEND initrd=images/centos7/initrd.img ks=ftp://192.168.1.10/pub/ks.cfg
LABEL rescue
MENU LABEL CentOS 7 Rescue
KERNEL images/centos7/vmlinuz
APPEND initrd=images/centos7/initrd.img rescue 准备 CentOS 7 安装文件
# 挂载 CentOS 7 ISO mkdir /mnt/centos7 mount -o loop CentOS-7-x86_64-DVD-2009.iso /mnt/centos7 # 复制内核和initrd文件 cp /mnt/centos7/images/pxeboot/vmlinuz /var/lib/tftpboot/images/centos7/ cp /mnt/centos7/images/pxeboot/initrd.img /var/lib/tftpboot/images/centos7/ # 设置FTP目录(也可以使用HTTP或NFS) mkdir -p /var/ftp/pub cp -r /mnt/centos7/* /var/ftp/pub/ # 设置权限 chmod -R 755 /var/ftp/pub chown -R ftp:ftp /var/ftp/pub
创建 Kickstart 文件(可选自动化安装)
创建 /var/ftp/pub/ks.cfg:

vim /var/ftp/pub/ks.cfg
基本示例:
#version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media url --url="ftp://192.168.1.10/pub" # Use graphical install graphical # Run the Setup Agent on first boot firstboot --enable ignoredisk --only-use=sda # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 # Network information network --bootproto=dhcp --device=eth0 --onboot=on --ipv6=auto --no-activate network --hostname=localhost.localdomain # Root password rootpw --iscrypted $6$yourpasswordhashhere # System services services --enabled="chronyd" # System timezone timezone Asia/Shanghai --isUtc # System bootloader configuration bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda # Partition clearing information clearpart --all --initlabel # Disk partitioning information part /boot --fstype="xfs" --ondisk=sda --size=1024 part pv.01 --fstype="lvmpv" --ondisk=sda --size=8192 volgroup centos --pesize=4096 pv.01 logvol / --fstype="xfs" --grow --maxsize=51200 --size=1024 --name=root --vgname=centos logvol swap --fstype="swap" --size=2048 --name=swap --vgname=centos %packages @^minimal @core chrony %end %addon com_redhat_kdump --enable --reserve-mb='auto' %end
启动并设置服务自启
# 启动服务 systemctl start dhcpd systemctl start xinetd systemctl start vsftpd # 设置开机自启 systemctl enable dhcpd systemctl enable xinetd systemctl enable vsftpd # 防火墙配置(如果有防火墙) firewall-cmd --permanent --add-service=dhcp firewall-cmd --permanent --add-service=tftp firewall-cmd --permanent --add-service=ftp firewall-cmd --reload
验证配置
检查TFTP目录结构:
tree /var/lib/tftpboot/
测试TFTP服务:
tftp 127.0.0.1 -c get pxelinux.0
故障排除
DHCP服务启动失败

journalctl -xe systemctl status dhcpd
客户端无法获取IP
- 确认DHCP服务器IP地址配置正确
- 确保DHCP服务在正确的网络接口上监听
TFTP传输失败
systemctl status xinetd chmod 644 /var/lib/tftpboot/pxelinux.0
注意事项
- 根据实际网络环境修改IP地址、子网掩码等参数
- 确保PXE服务器和客户端在同一网络段
- 如果需要多系统引导,可以在menu中添加更多选项
- 大型环境建议使用HTTP代替FTP提供安装文件

