---
title: 最新PXE网络安装系统教程
published: 2026-04-27
tags: [教程]
category: 教程
draft: false
---

## 简介

PXE是一套让电脑从网卡启动而无需从硬盘或U盘启动的标准协议，支持多种系统，并且非常适合批量安装系统的场景

本文中用到的 `netboot.xyz` iPXE固件是一个开源项目，是基于iPXE实现的增强版网络装系统的启动工具，内置了很多功能。

## Linux系统网络重装系统

#### 查看系统信息并备份
出意外时可用于手动配置
```
# 查看系统盘
df -h /

# 查看默认网关
ip r

# 查看默认网络接口
ip a
```
#### 下载 netboot.xyz 启动镜像（iPXE引导器）
```
wget https://boot.netboot.xyz/ipxe/netboot.xyz.img
```
#### 将镜像DD写入系统盘（记得替换你实际的系统盘，会覆盖原系统，请确保重要数据已备份）
```
dd if=netboot.xyz.img of=/dev/vda bs=4M
```
#### 强制将缓存写入磁盘，确保数据落盘完成
```
sync
```
#### 重启服务器
```
reboot
```
#### 进入启动菜单，选择网络安装
云服务器厂商可以在管理后台通过VNC界面操作
```
Linux Network Installs (64-bit)
```

> 如果重装成功，结束后会自动进入新系统，需要注意的是新系统一般没有开启root用户的ssh权限，需要用普通用户ssh连接，然后切换为root用户后再修改。`Alpine`系统默认是安装在内存中的，进入系统后需要执行`setup-alpine`命令安装到硬盘。
> 
> 如果安装失败的话可能是硬盘分区有问题或者其他设置不对，可以返回到`netboot.xyz 启动菜单`，重新选择网络安装再装一遍即可。

#### 国内镜像站

`netboot.xyz`项目下载系统镜像是在各大镜像官网下载的，国内服务器下载会很慢，可以加载自己的ipxe脚本，然后脚本里使用国内镜像源。

可以在第6步中，选择`IPXE shell`加载自己的脚本，命令示例：
```
chain https://example.com/embed.ipxe
```
---

## Windows系统重装为Linux

#### 下载 netboot.xyz 启动镜像
`C`盘里创建一个`boot`目录

下载`https://boot.netboot.xyz/ipxe/netboot.xyz.img`放到`boot`目录

#### 以管理员权限执行命令添加启动项
```
bcdedit /copy {current} /d "Linux Install"
```
执行完成后会显示一个GUID，类似于`{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}`

确认GUID后，逐条复制粘贴执行，记得替换GUID：
```
bcdedit /set {你的GUID} path \boot\netboot.xyz.img

bcdedit /set {你的GUID} loadoptions "initrd=\boot\netboot.xyz.img"

bcdedit /set {你的GUID} inirootpath \boot\netboot.xyz.img

bcdedit /timeout 60
```

#### 验证配置
```
bcdedit /enum {你的GUID}
```
确认输出中至少包含：

```
path                    \boot\netboot.xyz.img
description              Linux Install
```

---

## 手搓PXE服务

#### 安装dnsmasq

```
apt update
apt install -y dnsmasq
```

创建和编辑 `/etc/dnsmasq.d/pxe.conf` 配置ProxyDHCP和TFTP服务，要和PXE客户端（要装系统的设备）在同一局域网。

此配置使用`ProxyDHCP`模式，不会跟其他`DHCP`服务冲突，只会响应和下发PXE请求
```
# ProxyDHCP + TFTP
# 关闭 DNS
port=0

# 日志
log-dhcp
log-facility=/var/log/dnsmasq.log

# 改1：你的网段
dhcp-range=192.168.1.0,proxy

# 改2：本机 IP
dhcp-option=option:tftp-server,192.168.1.10

# ==================== TFTP （放IPXE固件）====================
enable-tftp
tftp-root=/etc/tftp

# ==================== PXE 引导 ====================
pxe-service=x86PC,       "PXE BIOS",             netboot.xyz-undionly.kpxe
pxe-service=X86-64_EFI,  "PXE UEFI x64",         netboot.xyz-snponly.efi
pxe-service=ARM64_EFI,   "PXE UEFI ARM64",       netboot.xyz-arm64-snponly.efi
```
#### 管理命令
```
# 启动服务
sudo systemctl start dnsmasq

# 设置开机自启
sudo systemctl enable dnsmasq

# 查看运行状态
sudo systemctl status dnsmasq

# 查看实时日志
sudo tail -f /var/log/dnsmasq.log

# 重启服务
sudo systemctl restart dnsmasq
```

#### netboot.xyz的IPXE固件下载地址

| 架构 | 文件名 | 下载地址 | 说明 |
| :--- | :--- | :--- | :--- |
| **Legacy (PCBIOS)** | `netboot.xyz-undionly.kpxe` | https://boot.netboot.xyz/ipxe/netboot.xyz-undionly.kpxe | 传统 BIOS DHCP 引导文件，通用网卡驱动|
| **UEFI x86_64** | `netboot.xyz-snponly.efi` | https://boot.netboot.xyz/ipxe/netboot.xyz-snponly.efi | UEFI 引导文件|
| **UEFI ARM64** | `netboot.xyz-arm64-snponly.efi` | https://boot.netboot.xyz/ipxe/netboot.xyz-arm64-snponly.efi | ARM64 UEFI 引导文件|

#### netboot.xyz本地钩子

`netboot.xyz`官方固件启动后会自动调用`local-vars.ipxe`脚本，如果存在的话。

判断脚本是否存在：
他会自动扫描你tftp目录里的`local-vars.ipxe`脚本，你需要提前创建并放到tftp目录。如果没有的话他会走自己菜单的逻辑。并且是根据`DHCP`响应里的`server`信息找到你的tftp的IP地址。

#### 编译自己的IPXE固件

当然你也可以从 [IPXE官方项目地址](https://github.com/ipxe/ipxe) 自行编译，这样你就可以将自己的ipxe启动脚本内置进去。

#### ipxe脚本示例
ipxe脚本是核心步骤，从这里定义去哪里下载镜像，其他资源，和如何启动。

可根据自己情况修改，本地镜像文件则需要你启动一个HTTP服务，然后将镜像文件放进去，供客户端下载，适合离线环境。
```
#!ipxe
# ==================== iPXE 网络安装菜单 ====================
# 包含 Debian 12 安装 + Windows PE 安装

# 镜像文件托管地址（本地和高校镜像站二选一）
set server_ip http://192.168.1.10
set debian_mirror http://mirrors.cernet.edu.cn

# ==================== 菜单界面 ====================
:start
menu iPXE Network Install Menu
item debian    Debian 12 Automated Install
item winpe     Windows PE Install (Load boot.wim)
item shell     Enter iPXE Shell
item reboot    Reboot
choose --default debian --timeout 10000 option

goto ${option}

# ==================== Debian 12 安装 ====================
:debian
echo Starting Debian 12 automated installation...

kernel ${debian_mirror}/debian/dists/bookworm/main/installer-amd64/current/images/netboot/debian-installer/amd64/linux
initrd ${debian_mirror}/debian/dists/bookworm/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz

imgargs linux initrd=initrd.gz auto=true priority=critical
boot

goto start

# ==================== Windows PE 安装 ====================
:winpe
echo Loading Windows PE...
kernel ${server_ip}/winpe/wimboot
initrd ${server_ip}/winpe/sources/boot.wim boot.wim

boot

goto start

# ==================== 进入 iPXE 命令行 ====================
:shell
echo Entering iPXE Shell...
shell

goto start

# ==================== 重启 ====================
:reboot
echo Rebooting...
reboot
```
然后在IPXE环境里下载并运行脚本
```
dhcp

chain http://example.com/embed.ipxe
```
建议使用HTTP协议，因为某些旧版本没有内置HTTPS模块，并且固件里的根证书也不齐全。

### PXE 客户端启动链路
```
开机
  │
  ▼
DHCP (路由器)
  │ 分配 IP/网关/DNS
  ▼
ProxyDHCP (dnsmasq)
  │ 附加 TFTP 地址 + 引导文件名
  ▼
TFTP (dnsmasq)
  │ 下载 iPXE 固件
  ▼
iPXE 环境启动
  │
  ▼
HTTP 加载 menu.ipxe 脚本（需要手动加载，除非自己编译固件内置进去）
  │
  ▼
menu.ipxe 菜单（定义安装选项和逻辑）
```


### 客户端机器装Windows

对于安装Windows系统，可以使用 https://github.com/ipxe/wimboot 来直接加载 Windows 安装程序。

微软官方的`Windows ISO`镜像文件中，解压后`sources`目录下有一个核心文件`boot.wim`。这个`boot.wim`本质上就是一个微型的 `Windows PE`环境，可以用来启动并运行Windows安装程序。

### 编译自己的IPXE固件

- 安装依赖
```
sudo apt update
sudo apt install -y build-essential liblzma-dev mtools mkisofs syslinux gcc-aarch64-linux-gnu wget tar git
```
- 下载源码
```
git clone https://github.com/ipxe/ipxe.git
```
- 进入`src`目录

- 编辑`config/general.h`文件（可选）
这个文件定义了固件的功能，常用的功能默认已经启用了，有些高级功能被注释了，如果需要启用就取消注释，一般默认的就够用了。

- 创建内置脚本`embed.ipxe`放到当前目录

- 编译命令

三种BIOS架构
```
# Legacy BIOS
make bin/undionly.kpxe EMBED=embed.ipxe

# UEFI x86_64
make bin-x86_64-efi/ipxe.efi EMBED=embed.ipxe

# UEFI ARM64
make CROSS_COMPILE=aarch64-linux-gnu- bin-arm64-efi/ipxe.efi EMBED=embed.ipxe
```

编译后的产物在`bin`目录
