GRUB2 引导管理器完全教程 / 第 10 章:网络引导
第 10 章:网络引导
10.1 网络引导概述
网络引导(Network Boot)允许计算机通过网络加载操作系统,无需本地存储设备。这在大规模服务器部署、无盘工作站和集中化管理场景中非常有用。
10.1.1 网络引导协议
| 协议 | 全称 | 用途 | 端口 |
|---|---|---|---|
| PXE | Preboot Execution Environment | 初始引导 | 67/68(UDP), 69(UDP) |
| TFTP | Trivial File Transfer Protocol | 传输引导文件 | 69(UDP) |
| NFS | Network File System | 远程文件系统 | 2049(TCP/UDP) |
| HTTP | Hypertext Transfer Protocol | 传输引导文件 | 80(TCP) |
| iSCSI | Internet Small Computer System Interface | 远程块存储 | 3260(TCP) |
| DHCP | Dynamic Host Configuration Protocol | 网络配置 | 67/68(UDP) |
10.1.2 网络引导流程
┌──────────┐ DHCP 请求 ┌──────────┐
│ 客户机 │ ─────────────────→ │ DHCP 服务器│
└────┬─────┘ └──────┬────┘
│ │ 返回 IP + TFTP 地址
│ ◄──────────────────────────────┘
│
│ TFTP 请求引导文件 ┌──────────┐
│ ─────────────────────→ │ TFTP 服务器│
│ ◄───────────────────── │ │
│ 接收 GRUB/NBP └──────────┘
│
│ 加载 grub.cfg
│
│ HTTP/TFTP 请求内核 ┌──────────┐
│ ─────────────────────→ │ HTTP 服务器│
│ ◄───────────────────── │ │
│ 接收内核 + initramfs └──────────┘
│
▼
引导操作系统
10.2 PXE 引导
10.2.1 PXE 概述
PXE(Preboot Execution Environment)是由 Intel 定义的网络引导标准,集成在大多数网卡的固件中。
10.2.2 PXE 引导流程
1. 客户机发送 DHCP Discover(广播)
2. DHCP 服务器回复 DHCP Offer(含 IP + next-server + filename)
3. 客户机请求 TFTP 下载引导文件
4. TFTP 传输引导文件(pxelinux.0 / grubx64.efi / ipxe.efi)
5. 引导文件加载,执行后续引导流程
10.2.3 环境准备
# 安装必要软件(Debian/Ubuntu)
$ sudo apt install isc-dhcp-server tftpd-hpa apache2 grub-efi-amd64-signed shim-signed
# RHEL/Fedora
$ sudo dnf install dhcp-server tftp-server httpd grub2-efi-x64 shim-x64
10.3 DHCP 服务器配置
10.3.1 ISC DHCP 配置
# /etc/dhcp/dhcpd.conf
# 全局设置
option domain-name "example.com";
option domain-name-servers 8.8.8.8, 8.8.4.4;
default-lease-time 600;
max-lease-time 7200;
authoritative;
# 子网配置
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 broadcast-address 192.168.1.255;
# PXE 引导设置
# BIOS 模式
# filename "pxelinux.0";
# next-server 192.168.1.10;
# UEFI 模式
option architecture-type code 93 = unsigned integer 16;
if option architecture-type = 00:00 {
# BIOS 客户端
filename "pxelinux.0";
next-server 192.168.1.10;
} else if option architecture-type = 00:07 {
# UEFI x86_64 客户端
filename "grub/x86_64-efi/core.efi";
next-server 192.168.1.10;
} else if option architecture-type = 00:09 {
# UEFI x86_64 HTTP 客户端
filename "http://192.168.1.10/grub/x86_64-efi/core.efi";
next-server 192.168.1.10;
} else {
# 其他
filename "pxelinux.0";
next-server 192.168.1.10;
}
}
10.3.2 dnsmasq 配置(轻量级替代方案)
# /etc/dnsmasq.conf
# 监听接口
interface=eth0
bind-interfaces
# DHCP 设置
dhcp-range=192.168.1.100,192.168.1.200,255.255.255.0,12h
dhcp-option=option:router,192.168.1.1
dhcp-option=option:dns-server,8.8.8.8
# PXE 设置
# BIOS 模式
# dhcp-boot=pxelinux.0,,192.168.1.10
# UEFI 模式(自动检测)
dhcp-match=set:efi-x86_64,option:client-arch,7
dhcp-boot=tag:efi-x86_64,grub/x86_64-efi/core.efi,,192.168.1.10
# BIOS 模式
dhcp-match=set:bios,option:client-arch,0
dhcp-boot=tag:bios,pxelinux.0,,192.168.1.10
# 启用 TFTP
enable-tftp
tftp-root=/var/lib/tftpboot
10.4 TFTP 服务器配置
10.4.1 tftpd-hpa 配置
# /etc/default/tftpd-hpa
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/var/lib/tftpboot"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="--secure --create"
10.4.2 准备 TFTP 根目录
# 创建目录结构
$ sudo mkdir -p /var/lib/tftpboot/grub/x86_64-efi/
$ sudo mkdir -p /var/lib/tftpboot/grub/fonts/
$ sudo mkdir -p /var/lib/tftpboot/images/
# 复制 GRUB EFI 文件
$ sudo cp /usr/lib/grub/x86_64-efi-signed/grubnetx64.efi.signed \
/var/lib/tftpboot/grub/x86_64-efi/core.efi
# 或构建自定义 GRUB 网络镜像
$ sudo grub-mknetdir --net-directory=/var/lib/tftpboot \
--subdir=/grub
10.4.3 构建网络引导的 GRUB 镜像
# 创建包含网络模块的 GRUB 镜像
$ sudo grub-mkimage -O x86_64-efi \
-o /var/lib/tftpboot/grub/x86_64-efi/core.efi \
-p '(tftp)/grub' \
--modules=" \
efinet tftp http \
net ls normal \
boot linux chain \
part_gpt part_msdos \
ext2 fat iso9660 \
configfile search \
search_fs_uuid search_label \
test echo gzio \
"
# 参数说明:
# -O x86_64-efi: 目标平台
# -o: 输出文件
# -p: GRUB 前缀(配置文件路径前缀)
# --modules: 预加载的模块列表
10.5 GRUB 网络配置文件
10.5.1 TFTP GRUB 配置
# /var/lib/tftpboot/grub/grub.cfg
# 网络初始化
# net_bootp 或手动设置 IP
# 菜单
menuentry "Install Debian 12 (Network)" {
set base-url=http://192.168.1.10/debian-installer
linux ${base-url}/amd64/linux auto=true \
priority=critical \
url=http://192.168.1.10/preseed.cfg \
netcfg/get_hostname=debian
initrd ${base-url}/amd64/initrd.gz
}
menuentry "Install Ubuntu 24.04 (Network)" {
set base-url=http://192.168.1.10/ubuntu
linux ${base-url}/casper/vmlinuz \
boot=casper netboot=nfs \
nfsroot=192.168.1.10:/srv/nfs/ubuntu \
ip=dhcp autoinstall
initrd ${base-url}/casper/initrd
}
menuentry "Rescue System (Network)" {
set base-url=http://192.168.1.10/rescue
linux ${base-url}/vmlinuz ip=dhcp
initrd ${base-url}/initrd.img
}
10.5.2 GRUB 网络命令
# 在 GRUB Shell 中配置网络
# 方式一:使用 DHCP
grub> net_ls_cards # 列出网卡
grub> set net_default_server=192.168.1.10
grub> net_bootp # DHCP 获取 IP
# 方式二:手动设置
grub> net_add_addr eth0 efinet0 192.168.1.50/24
grub> net_add_route default 192.168.1.1
# 测试网络
grub> net_ls_addr # 显示 IP 地址
grub> net_ls_routes # 显示路由表
# 通过 HTTP 加载文件
grub> set root=(http,192.168.1.10)
grub> linux /grub/grub.cfg
10.6 HTTP 引导
10.6.1 为什么使用 HTTP 引导
| 对比 | TFTP | HTTP |
|---|---|---|
| 传输协议 | UDP | TCP |
| 速度 | 慢(小块传输) | 快(流水线传输) |
| 可靠性 | 低(无重传保证) | 高(TCP 保障) |
| 防火墙友好 | 否(随机端口) | 是(80/443) |
| 大文件支持 | 差 | 好 |
| 现代 UEFI 支持 | 基础 | 原生 HTTP Boot |
10.6.2 HTTP 引导服务器配置
# 安装 Apache
$ sudo apt install apache2
# 创建 HTTP 引导目录
$ sudo mkdir -p /var/www/html/grub
$ sudo mkdir -p /var/www/html/images
# 复制 GRUB 文件
$ sudo cp /var/lib/tftpboot/grub/* /var/www/html/grub/
# 配置 GRUB 使用 HTTP
# /var/www/html/grub/grub.cfg
menuentry "Install Debian 12" {
linux http://192.168.1.10/images/vmlinuz auto=true
initrd http://192.168.1.10/images/initrd.gz
}
10.6.3 UEFI HTTP Boot
现代 UEFI 固件原生支持 HTTP 引导,无需 TFTP:
# 在 UEFI 设置中配置:
# Boot Option: HTTP Boot
# URI: http://192.168.1.10/grub/x86_64-efi/core.efi
# DHCP 配置
# dhcp-option=option:bootfile-url,"http://192.168.1.10/grub/x86_64-efi/core.efi"
10.7 NFS 引导
10.7.1 配置 NFS 服务器
# 安装 NFS 服务器
$ sudo apt install nfs-kernel-server
# 导出引导目录
# /etc/exports
/srv/nfs/root 192.168.1.0/24(ro,no_root_squash,no_subtree_check)
/srv/nfs/home 192.168.1.0/24(rw,no_root_squash,no_subtree_check)
# 创建根文件系统
$ sudo debootstrap --arch=amd64 bookworm /srv/nfs/root http://deb.debian.org/debian
# 启动 NFS 服务
$ sudo systemctl restart nfs-kernel-server
10.7.2 从 NFS 引导的 GRUB 配置
menuentry "NFS Root System" {
linux /vmlinuz root=/dev/nfs \
nfsroot=192.168.1.10:/srv/nfs/root \
ip=dhcp rw
initrd /initrd.img
}
10.8 iSCSI 引导
10.8.1 配置 iSCSI 目标
# 安装 iSCSI 目标服务器
$ sudo apt install targetcli-fb
# 创建 iSCSI 目标
$ sudo targetcli
# /backstores/block create disk0 /dev/sdb
# /iscsi create iqn.2026-05.com.example:target0
# /iscsi/iqn.2026-05.com.example:target0/tpg1/luns create /backstores/block/disk0
# /iscsi/iqn.2026-05.com.example:target0/tpg1/acls create iqn.2026-05.com.example:initiator0
# exit
10.8.2 GRUB iSCSI 引导
# 在 GRUB Shell 中连接 iSCSI
grub> insmod net
grub> insmod efinet
grub> net_bootp
grub> insmod http
grub> net_add_dns 8.8.8.8
# 连接 iSCSI 目标
grub> insmod tftp
grub> search --set=root --iscsi iqn.2026-05.com.example:target0
# 或在 grub.cfg 中配置
menuentry "iSCSI Boot" {
set root=(iscsi1:iqn.2026-05.com.example:target0:0:tcp:192.168.1.20)
linux /vmlinuz root=UUID=iscsi-uuid ip=dhcp rw
initrd /initrd.img
}
10.9 无盘工作站
10.9.1 无盘工作站架构
┌──────────────────────────────────────────────────────┐
│ 网络基础设施 │
│ (DHCP + TFTP/HTTP + NFS) │
└──────────┬──────────────┬──────────────┬─────────────┘
│ │ │
┌─────┴────┐ ┌──────┴────┐ ┌──────┴────┐
│ 工作站 1 │ │ 工作站 2 │ │ 工作站 3 │
│(无磁盘) │ │(无磁盘) │ │(无磁盘) │
└──────────┘ └───────────┘ └───────────┘
10.9.2 配置无盘系统
# 1. 创建共享根文件系统
$ sudo debootstrap --arch=amd64 bookworm /nfsroot http://deb.debian.org/debian
# 2. 配置 NFS 导出
/nfsroot 192.168.1.0/24(ro,no_root_squash,no_subtree_check)
# 3. 配置 GRUB 网络引导
# /var/lib/tftpboot/grub/grub.cfg
menuentry "Diskless Workstation" {
net_bootp
linux /vmlinuz root=/dev/nfs nfsroot=192.168.1.10:/nfsroot ip=dhcp rw
initrd /initrd.img
}
10.10 网络引导安全
10.10.1 安全措施
| 措施 | 说明 |
|---|---|
| VLAN 隔离 | 将 PXE 引导网络隔离到独立 VLAN |
| DHCP Snooping | 防止恶意 DHCP 服务器 |
| Secure Boot | 验证引导文件签名 |
| HTTPS | 使用加密传输引导文件 |
| IPsec | 加密网络引导流量 |
| ACL | 限制 TFTP/HTTP 访问 |
10.10.2 使用 HTTPS 引导
# 在 GRUB 中使用 HTTPS
# 需要编译 GRUB 时包含 https 支持
# grub.cfg
set net_default_server=https://boot.example.com
linux (https)/grub/vmlinuz
initrd (https)/grub/initrd.img
10.11 扩展阅读
上一章:第 9 章:Secure Boot | 下一章:第 11 章:故障排除