萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 利用 PXE 網路開機安裝虛擬機器自動化部署

PXE (Preboot Execution Environment) 讓裝置透過網路載入核心與安裝程式,實現無人值守批量安裝。本文記錄在 Ubuntu 宿主機上部署 dnsmasq (DHCP+TFTP)、Apache (Kickstart/Preseed)、Nginx 靜態資源,完成 Ubuntu Server 自動化安裝的全流程。

架構角色

服務功能埠口
dnsmasqDHCP 分派 IP + TFTP 傳送 pxelinux.067/68 (DHCP), 69 (TFTP)
Apache提供 Kickstart (ks.cfg) 與 Preseed (ubuntu-server.seed)80
Nginx (可選)提供安裝鏡像靜態檔案80/8080

1. dnsmasq 配置

apt-get install dnsmasq
vim /etc/dnsmasq.conf
bogus-priv
filterwin2k
interface=eth0
dhcp-range=192.168.100.172,192.168.100.180,12h
dhcp-host=52:54:00:ed:00:f7,192.168.100.178  # 固定客戶端 IP
dhcp-boot=pxelinux.0
enable-tftp
tftp-root=/var/ftpd
dhcp-authoritative
/etc/init.d/dnsmasq restart

2. PXE 開機檔案準備

mkdir /var/www/ubuntu
mount -o loop /bak/kvmimages/ubuntu-11.10-server-amd64.iso /mnt
cp /mnt/preseed/ubuntu-server.seed /var/www/ubuntu
cp -a /mnt/* /var/www/ubuntu
cp -r /mnt/install/netboot/* /var/ftpd/

編輯 /var/ftpd/pxelinux.cfg/default

default linux
label linux
kernel ubuntu-installer/amd64/linux
append ks=https://192.168.100.171/ubuntu/ks.cfg preseed/url=https://192.168.100.171/ubuntu/ubuntu-server.seed vga=normal initrd=ubuntu-installer/amd64/initrd.gz --

3. Apache 虛擬主機

apt-get install apache2
vim /etc/apache2/sites-available/default
ServerName ubuntu
DocumentRoot /var/www/ubuntu
Options ExecCGI FollowSymLinks
AllowOverride all
Order allow,deny
allow from all
ErrorLog /var/log/apache2/error-ubuntu.log
ln -s /etc/apache2/sites-available/default /etc/apache2/sites-enabled/ubuntu
/etc/init.d/apache2 restart

4. Kickstart 設定檔 /var/www/ubuntu/ks.cfg

# 基礎設定
lang en_US
keyboard us
timezone Asia/Chongqing
rootpw --disabled
user hua --fullname hua --password password
reboot
text
install
url --url https://192.168.100.171/ubuntu
bootloader --location=mbr
zerombr yes
clearpart --all --initlabel
part / --fstype ext4 --size 10000
part swap --size 5000
auth --useshadow --enablemd5
network --bootproto=dhcp --device=eth0
firewall --disabled
skipx
%packages
@openssh-server

5. Preseed 設定檔 /var/www/ubuntu/ubuntu-server.seed

d-i partman-auto/init_automatically_partition string some_device_lvm
d-i base-installer/kernel/override-image string linux-server
tasksel tasksel/force-tasks string server
d-i pkgsel/install-language-support boolean false
d-i clock-setup/utc boolean false
d-i debian-installer/splash boolean false
d-i oem-config-udeb/frontend string debconf
oem-config oem-config/steps multiselect language, timezone, keyboard, user, network, tasks

重要注意事項

  • 每次測試必須新建虛擬機:PXE 客戶端 MAC 位址變更,DHCP 租約與狀態會衝突。
  • 生產環境建議使用 Cobbler、Foreman、MAAS 等高階部署工具管理 PXE。
  • UEFI 環境需提供 grubx64.efi、BOOTX64.EFI 並配置 dhcp-boot=BOOTX64.EFI。
  • 現代 Ubuntu (20.04+) 建議使用 cloud-init + autoinstall 取代 Kickstart/Preseed。

PXE 自動化安裝是資料中心批量佈署的基石,掌握其核心元件協作原理,可延伸至裸金屬佈署、Kubernetes 節點初始化等場景。

copyright © 萬盛學電腦網 all rights reserved