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

Linux 網絡安裝 (PXE/Kickstart) 全自動化部署實戰:DHCP + TFTP + FTP + Kickstart 完整流程

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

架構概覽


+----------------+     DHCP/TFTP      +----------------+
|  PXE Client    | <----------------> |  Install Server |
|  (空機器)      |     FTP/HTTP       |  (RHEL/CentOS)  |
+----------------+                    +----------------+

核心服務:dhcpd 分配 IP 並指向啟動檔、tftpd 傳遞啟動載入器/內核、vsftpd/httpd 提供安裝源、kickstart 實現無人值守應答。

環境準備(安裝服務器)

# 1. 安裝核心套件
yum install -y tftp-server vsftpd dhcp syslinux xinetd

# 2. 關閉防火牆與 SELinux(測試環境)
systemctl stop firewalld; systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

服務配置詳解

1. xinetd + TFTP

# /etc/xinetd.d/tftp
service tftp
{
    socket_type     = dgram
    protocol        = udp
    wait            = yes
    user            = root
    server          = /usr/sbin/in.tftpd
    server_args     = -s /var/lib/tftpboot -v
    disable         = no          # 關鍵:yes→no
    per_source      = 11
    cps             = 100 2
    flags           = IPv4
}
systemctl enable --now xinetd

2. DHCP 服務

# /etc/dhcp/dhcpd.conf
subnet 192.168.4.0 netmask 255.255.255.0 {
    range 192.168.4.30 192.168.4.50;
    option routers 192.168.4.1;
    option domain-name-servers 192.168.4.1;
    filename "pxelinux.0";           # PXE 啟動檔
    next-server 192.168.4.103;       # TFTP 服務器 IP
}
systemctl enable --now dhcpd

3. FTP 安裝源

# 掛載 ISO
mkdir -p /var/ftp/pub/rhel8
mount -o loop RHEL-8.x.iso /var/ftp/pub/rhel8

# vsftpd 配置匿名讀取
# /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
anon_root=/var/ftp/pub
systemctl enable --now vsftpd

4. PXE 啟動檔案準備

cd /var/ftp/pub/rhel8/images/pxeboot
cp vmlinuz initrd.img /var/lib/tftpboot/

# syslinux 引導載入器
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp /usr/share/syslinux/vesamenu.c32 /var/lib/tftpboot/

# 菜單配置目錄
mkdir -p /var/lib/tftpboot/pxelinux.cfg
cat > /var/lib/tftpboot/pxelinux.cfg/default <<'EOF'
default vesamenu.c32
prompt 1
timeout 600

label linux
  menu label ^Install RHEL 8
  kernel vmlinuz
  append initrd=initrd.img inst.repo=ftp://192.168.4.103/pub/rhel8 quiet

label kickstart
  menu label ^Auto Install (Kickstart)
  kernel vmlinuz
  append initrd=initrd.img inst.repo=ftp://192.168.4.103/pub/rhel8 ks=ftp://192.168.4.103/pub/ks.cfg quiet
EOF

Kickstart 應答檔製作

# 安裝圖形化工具
yum install -y system-config-kickstart

# 啟動 GUI 生成 ks.cfg,關鍵設定:
# - 安裝來源:FTP → ftp://192.168.4.103/pub/rhel8
# - 磁碟分區:自動/自定
# - 網路:DHCP
# - Root 密碼、時區、套件選擇
# - 保存至 /var/ftp/pub/ks.cfg

ks.cfg 關鍵片段


# 安裝來源
url --url=ftp://192.168.4.103/pub/rhel8

# 磁碟清空並自動分區
clearpart --all --initlabel
autopart --type=lvm

# 網路
network --bootproto=dhcp

# 時區
timezone Asia/Shanghai --utc

# Root 密碼(已加密)
rootpw --plaintext RedHat123

# 套件選擇
%packages
@^minimal-environment
%end

# 後腳本
%post
systemctl enable sshd
%end

啟動驗證

  1. 創建新虛擬機,網卡選 橋接模式(與安裝服務器同網段)
  2. 開機進入 PXE 菜單,選擇 Auto Install (Kickstart)
  3. 全程無人值守,約 10-20 分鐘完成

常見故障排查

現象原因解決
Client 無 IPDHCP 未啟動/網段錯誤檢查 dhcpd 狀態、subnet 與服務器 IP 一致
TFTP 傳輸超時防火牆阻擋 UDP 69關閉防火牆或放行 69/udp
無法掛載安裝源FTP 路徑/權限錯誤確認 anon_root、檔案權限、SELinux
Kickstart 報錯ks.cfg 語法/路徑錯誤用 ksvalidator ks.cfg 驗證

擴展:HTTP 替代 FTP(更快、穩定)

yum install -y httpd
ln -s /var/ftp/pub/rhel8 /var/www/html/rhel8
systemctl enable --now httpd
# ks.cfg 中 inst.repo=https://192.168.4.103/rhel8

生產環境建議

  • 使用 Cobbler / Foreman / Satellite 管理大規模佈署
  • 配置 DNS、DHCP 保留 IP、MAC 綁定
  • Kickstart 含郵件通知、資產錄入、監控註冊等後腳本
  • 定期驗證 ISO 校驗和、更新補丁包
Copyright © Windows教程網 All Rights Reserved