Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

網路安裝 Debian 7 Wheezy 完整教學:以 DNSMASQ 為 PXE 伺服器

編輯:Linux 系統常見問題解答

前言

當伺服器無光碟機、USB 無法開機,或需批量部署時,**PXE 網路安裝**是最佳選擇。本文使用輕量級 DNSMASQ** 整合 DNS、DHCP、TFTP 三合一服務,引導客戶端安裝 Debian 7 Wheezy。

架構圖

+----------------+       DHCP/TFTP        +----------------+
|  PXE Server    | <------------------- > |  Client (BIOS) |
|  (DNSMASQ)     |       Boot Image       |  (Net Boot)    |
+----------------+                        +----------------+

步驟 1:安裝 DNSMASQ

apt-get update
apt-get install -y dnsmasq

步驟 2:設定靜態 IP(前提)

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

步驟 3:配置 DNSMASQ 整合服務

# 備份原設定
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

步驟 4:準備 Debian 7 安裝映像至 TFTP 根目錄

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/ 等

步驟 5:調整 pxelinux.cfg/default(可選:自動化安裝)

# 預設選單項目
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

步驟 6:啟動服務與防火牆

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

步驟 7:客戶端開機網路啟動

  1. 進入 BIOS,啟用 Network Boot (PXE) 並設為第一優先序。
  2. 儲存重啟,客戶端將取得 IP、下載 pxelinux.0、進入 Debian 安裝選單。
  3. 選擇安裝選項,即可開始網路安裝。

進階:Preseed 全自動應答檔

# 在 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

常見問題排查

  • Client 卡在 "Loading pxelinux.0": 檢查 TFTP 根目錄權限、防火牆 69/udp、dnsmasq tftp-root 路徑。
  • 無法取得 IP: 確認 dnsmasq dhcp-range 與介面綁定正確,且網段無其他 DHCP 伺服器衝突。
  • 安裝過程無法下載套件: 檢查 mirror 來源可達性,或改用本地鏡像源。

結語

DNSMASQ 以單一服務整合 DHCP+TFTP+DNS,極大簡化 PXE 部署複雜度。配合 Preseed 應答檔,可實現 Debian/Ubuntu 完全無人值守安裝,適合機房批量佈署。

Copyright © Windows教程網 All Rights Reserved