PXE 网络启动环境搭建教程

PXE(Preboot Execution Environment,预启动执行环境)是一种通过网络引导计算机并安装操作系统的技术。它允许客户端机器在没有本地存储介质的情况下,从网络上的 PXE 服务器获取启动文件和安装源。虽然搬瓦工 VPS 本身通过 KiwiVM 面板就能重装系统,但掌握 PXE 技术对于理解 Linux 自动化部署流程非常有帮助。你可以在搬瓦工 VPS 上搭建 PXE 服务器来管理内网中的物理服务器或其他虚拟机。

一、PXE 启动流程

  • 步骤 1:客户端机器通过网卡的 PXE ROM 发送 DHCP 请求。
  • 步骤 2:DHCP 服务器返回 IP 地址和 PXE 启动文件的位置(next-server 和 filename)。
  • 步骤 3:客户端通过 TFTP 下载启动引导程序(如 pxelinux.0)。
  • 步骤 4:引导程序加载菜单配置,显示安装选项。
  • 步骤 5:客户端下载内核和 initrd 文件,启动安装程序。
  • 步骤 6:安装程序通过 HTTP/NFS 获取安装源并完成系统安装。

二、安装基础服务

# 在搬瓦工 VPS 上安装所需服务
apt update
apt install dnsmasq pxelinux syslinux-common tftpd-hpa apache2 -y

三、配置 TFTP 服务

# 编辑 TFTP 配置
vi /etc/default/tftpd-hpa

TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/srv/tftp"
TFTP_ADDRESS=":69"
TFTP_OPTIONS="--secure"

# 创建 TFTP 目录结构
mkdir -p /srv/tftp/pxelinux.cfg
mkdir -p /srv/tftp/images/ubuntu2204

# 复制 PXE 引导文件
cp /usr/lib/PXELINUX/pxelinux.0 /srv/tftp/
cp /usr/lib/syslinux/modules/bios/ldlinux.c32 /srv/tftp/
cp /usr/lib/syslinux/modules/bios/menu.c32 /srv/tftp/
cp /usr/lib/syslinux/modules/bios/libutil.c32 /srv/tftp/
cp /usr/lib/syslinux/modules/bios/libcom32.c32 /srv/tftp/

# 重启 TFTP 服务
systemctl restart tftpd-hpa
systemctl enable tftpd-hpa

四、配置 DHCP(使用 dnsmasq)

# 备份原配置
cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

# 编辑 dnsmasq 配置
cat > /etc/dnsmasq.conf <<'EOF'
# DHCP 配置
interface=eth0
dhcp-range=192.168.1.100,192.168.1.200,255.255.255.0,12h

# PXE 启动配置
dhcp-boot=pxelinux.0
enable-tftp
tftp-root=/srv/tftp

# DNS 配置
server=8.8.8.8
server=8.8.4.4
EOF

# 重启 dnsmasq
systemctl restart dnsmasq
systemctl enable dnsmasq

五、准备安装源

# 下载 Ubuntu 22.04 网络安装文件
cd /srv/tftp/images/ubuntu2204
wget http://archive.ubuntu.com/ubuntu/dists/jammy/main/installer-amd64/current/legacy-images/netboot/ubuntu-installer/amd64/linux
wget http://archive.ubuntu.com/ubuntu/dists/jammy/main/installer-amd64/current/legacy-images/netboot/ubuntu-installer/amd64/initrd.gz

# 下载完整 ISO 用于 HTTP 安装源(可选)
mkdir -p /var/www/html/ubuntu2204
cd /tmp
wget https://releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso
mount -o loop ubuntu-22.04-live-server-amd64.iso /mnt
cp -a /mnt/* /var/www/html/ubuntu2204/
umount /mnt

六、创建 PXE 启动菜单

# 创建默认引导菜单
cat > /srv/tftp/pxelinux.cfg/default <<'EOF'
UI menu.c32
PROMPT 0
TIMEOUT 300
MENU TITLE PXE Boot Menu

LABEL ubuntu2204
    MENU LABEL Install Ubuntu 22.04 LTS
    KERNEL images/ubuntu2204/linux
    APPEND initrd=images/ubuntu2204/initrd.gz auto=true priority=critical url=http://PXE_SERVER_IP/preseed.cfg

LABEL ubuntu2204-manual
    MENU LABEL Install Ubuntu 22.04 LTS (Manual)
    KERNEL images/ubuntu2204/linux
    APPEND initrd=images/ubuntu2204/initrd.gz

LABEL local
    MENU LABEL Boot from local disk
    MENU DEFAULT
    LOCALBOOT 0
EOF

七、创建预配置文件(Preseed)

# Ubuntu 自动安装预配置文件
cat > /var/www/html/preseed.cfg <<'EOF'
# 区域设置
d-i debian-installer/locale string en_US.UTF-8
d-i keyboard-configuration/layoutcode string us

# 网络配置
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string server
d-i netcfg/get_domain string example.com

# 镜像源
d-i mirror/country string manual
d-i mirror/http/hostname string archive.ubuntu.com
d-i mirror/http/directory string /ubuntu

# 磁盘分区
d-i partman-auto/method string regular
d-i partman-auto/choose_recipe select atomic
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true

# 用户账户
d-i passwd/root-login boolean true
d-i passwd/root-password password changeme
d-i passwd/root-password-again password changeme

# 软件包选择
tasksel tasksel/first multiselect standard, openssh-server

# 安装完成后重启
d-i finish-install/reboot_in_progress note
EOF

八、UEFI 支持

# 为 UEFI 机器准备启动文件
apt install grub-efi-amd64-signed shim-signed -y

mkdir -p /srv/tftp/grub

cp /usr/lib/shim/shimx64.efi.signed /srv/tftp/grub/bootx64.efi
cp /usr/lib/grub/x86_64-efi-signed/grubnetx64.efi.signed /srv/tftp/grub/grubx64.efi

# 创建 GRUB 配置
cat > /srv/tftp/grub/grub.cfg <<'EOF'
set timeout=10
set default=0

menuentry "Install Ubuntu 22.04 LTS" {
    linux images/ubuntu2204/linux auto=true priority=critical url=http://PXE_SERVER_IP/preseed.cfg
    initrd images/ubuntu2204/initrd.gz
}

menuentry "Boot from local disk" {
    exit
}
EOF

# 更新 dnsmasq 支持 UEFI
cat >> /etc/dnsmasq.conf <<'EOF'
# UEFI PXE 支持
dhcp-match=set:efi-x86_64,option:client-arch,7
dhcp-boot=tag:efi-x86_64,grub/bootx64.efi
EOF

systemctl restart dnsmasq

九、测试与排查

# 检查 TFTP 服务
systemctl status tftpd-hpa
tftp localhost -c get pxelinux.0

# 检查 DHCP 服务
systemctl status dnsmasq
journalctl -u dnsmasq -f

# 检查 HTTP 服务
curl http://localhost/preseed.cfg

# 查看 DHCP 分配日志
cat /var/lib/misc/dnsmasq.leases

# 使用 tcpdump 抓包排查
tcpdump -i eth0 port 67 or port 68 or port 69

总结

PXE 网络启动是大规模服务器部署的基础技术。通过在搬瓦工 VPS 上搭建 PXE 服务器,你可以实现内网机器的自动化操作系统安装。PXE 配合 Kickstart 自动安装 技术可以实现完全无人值守的系统部署。如果你只需要管理搬瓦工 VPS 本身的系统重装,使用 KiwiVM 面板即可。选购搬瓦工 VPS 请查看全部方案,使用优惠码 NODESEEK2026 可享受 6.77% 循环折扣。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。