當伺服器無光碟機、USB 無法開機,或需批量部署時,**PXE 網路安裝**是最佳選擇。本文使用輕量級 DNSMASQ** 整合 DNS、DHCP、TFTP 三合一服務,引導客戶端安裝 Debian 7 Wheezy。
+----------------+ DHCP/TFTP +----------------+ | PXE Server | <------------------- > | Client (BIOS) | | (DNSMASQ) | Boot Image | (Net Boot) | +----------------+ +----------------+
apt-get update apt-get install -y dnsmasq
PXE 伺服器必須有固定 IP,且為該網段 DHCP 唯一權威來源。
# /etc/network/interfaces 範例
auto eth0
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
# 備份原設定 mv /etc/dnsmasq.conf /etc/dnsmasq.conf.backup # 建立新設定 /etc/dnsmasq.conf interface=eth0 bind-interfaces dhcp-range=192.168.1.100,192.168.1.200,12h dhcp-boot=pxelinux.0 enable-tftp tftp-root=/var/lib/tftpboot pxe-service=x86PC,"Install Debian",pxelinux # DNS 功能(可選) # no-resolv # server=8.8.8.8
mkdir -p /var/lib/tftpboot cd /var/lib/tftpboot # 下載 Debian 7 netboot 映像(amd64 為例) wget https://ftp.debian.org/debian/dists/wheezy/main/installer-amd64/current/images/netboot/netboot.tar.gz tar -xzf netboot.tar.gz # 解壓後會有 pxelinux.0、pxelinux.cfg/default、debian-installer/ 等
# 預設選單項目
default install
label install
menu label ^Install Debian 7
kernel debian-installer/amd64/linux
append initrd=debian-installer/amd64/initrd.gz vga=788 -- quiet
# 若需全自動安裝,append 加入:
# auto=true url=tftp://192.168.1.10/preseed.cfg
service dnsmasq restart # 防火牆開放:DHCP(67/udp), TFTP(69/udp), DNS(53/udp/tcp) iptables -A INPUT -p udp --dport 67 -j ACCEPT iptables -A INPUT -p udp --dport 69 -j ACCEPT
# 在 TFTP 根目錄建立 preseed.cfg d-i debian-installer/locale string en_US d-i keyboard-configuration/xkb-keymap select us d-i netcfg/choose_interface select auto d-i mirror/country string manual d-i mirror/http/hostname string ftp.debian.org d-i mirror/http/directory string /debian d-i clock-setup/utc boolean true d-i time/zone string UTC d-i partman-auto/method string lvm d-i partman-lvm/confirm boolean true d-i partman/confirm_write_new_label boolean true d-i partman/choose_partition select finish d-i partman/confirm boolean true d-i passwd/root-password password yourpassword d-i passwd/root-password-again password yourpassword d-i passwd/user-fullname string Debian User d-i passwd/username string debian d-i passwd/user-password password userpass d-i passwd/user-password-again password userpass tasksel tasksel/first multiselect standard d-i pkgsel/include string openssh-server d-i grub-installer/only_debian boolean true d-i finish-install/reboot_in_progress note
在 pxelinux.cfg/default 的 append 行加入:
auto=true priority=critical url=tftp://192.168.1.10/preseed.cfg
DNSMASQ 以單一服務整合 DHCP+TFTP+DNS,極大簡化 PXE 部署複雜度。配合 Preseed 應答檔,可實現 Debian/Ubuntu 完全無人值守安裝,適合機房批量佈署。