針對大規模機房、測試環境批量部署、無光驅機器安裝等場景,本文介紹基於 PXE + Kickstart + 網路安裝源的 RedHat Enterprise Linux 全自動化部署方案。僅需客戶機支援 PXE 啟動,即可實現「開機 → 選擇網路啟動 → 完全無人值守完成安裝」的高效流程。
ks.cfg) 預先定義分區、套件選擇、網路、使用者、啟動載入器等所有安裝參數。| 角色 | 服務 | 關鍵檔案/目錄 |
|---|---|---|
| 啟動服務器 | DHCP + TFTP | /etc/dhcpd.conf, /tftpboot/pxelinux.0 |
| 安裝服務器 | NFS / HTTP / FTP | /var/www/html/rhel (或 /exports/rhel) |
| 客戶機 | PXE Client | 網卡支援 PXE,BIOS 設為網路啟動 |
啟動服務器與安裝服務器可合部署在同一台物理機上。
yum install dhcp tftp-server syslinux -y
option domain-name "example.com";
ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
authoritative;
subnet 192.168.138.0 netmask 255.255.255.0 {
range 192.168.138.100 192.168.138.200;
option routers 192.168.138.1;
option domain-name-servers 192.168.138.1;
next-server 192.168.138.10; # TFTP 服務器 IP
filename "pxelinux.0"; # PXE 啟動檔
}
# 固定 IP 綁定 MAC (可選)
host client01 {
hardware ethernet 00:0C:29:70:24:5B;
fixed-address 192.168.138.30;
}
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /tftpboot
disable = no
per_source = 11
cps = 100 2
}
# 複製 syslinux 啟動載入器
cp /usr/share/syslinux/pxelinux.0 /tftpboot/
mkdir -p /tftpboot/pxelinux.cfg
# 複製內核與 initrd (從安裝光碟 images/pxeboot/)
cp /mnt/rhel/images/pxeboot/vmlinuz /tftpboot/
cp /mnt/rhel/images/pxeboot/initrd.img /tftpboot/
default vesamenu.c32
prompt 0
timeout 300
label auto-install
menu label ^Auto Install RHEL 7.9 (Kickstart)
kernel vmlinuz
append initrd=initrd.img ks=https://192.168.138.10/ks/ks.cfg ksdevice=bootif ip=dhcp
label local
menu label Boot from ^Local Disk
localboot 0xffff
systemctl enable --now dhcpd tftp xinetd
systemctl restart dhcpd tftp xinetd
mkdir -p /var/www/html/rhel7.9
mount -o loop RHEL-7.9-Server-x86_64-dvd.iso /var/www/html/rhel7.9
# 或解壓 ISO 內容至該目錄
yum install httpd -y
systemctl enable --now httpd
# 防火牆放行 HTTP
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
#version=RHEL7
auth --enableshadow --passalgo=sha512
url --url=https://192.168.138.10/rhel7.9
text
firstboot --disable
keyboard us
lang en_US.UTF-8
network --bootproto=dhcp --device=link --activate
rootpw --plaintext RedHat123
timezone Asia/Shanghai --utc
bootloader --location=mbr --boot-drive=sda
zerombr
clearpart --all --initlabel --drives=sda
autopart --type=lvm
reboot
%packages
@core
@base
chrony
vim
net-tools
%end
%post --interpreter=/bin/bash
# 後安裝腳本:配置 SSH、YUM 源、主機名等
systemctl enable sshd
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
%end
pxelinux.0 進入選單。ks.cfg、內核、initrd,並從 HTTP 源安裝所有套件。在 pxelinux.cfg/default 中新增多個 label,配合不同 ks.cfg (如 ks-web.cfg, ks-db.cfg),即可實現「一鍵選擇部署 Web 節點 / DB 節點 / 測試環境」等差異化部署。
| 現象 | 可能原因 | 解決 |
|---|---|---|
| 卡在 "Waiting for IP" | DHCP 未授權 / 防火牆阻擋 | 檢查 dhcpd 日誌、開放 67/68 UDP |
| TFTP 傳輸超時 | TFTP 目錄權限 / SELinux | chcon -R -t tftpdir_t /tftpboot |
| Kickstart 解析失敗 | ks.cfg 語法錯誤 / URL 不可達 | 瀏覽器驗證 ks.cfg URL、檢查 %pre/%post 腳本 |
| 安裝源 404 | HTTP 目錄結構不匹配 | 確認 .treeinfo、repodata 存在於 DocumentRoot |
PXE + Kickstart 方案一次建置、長期受益。對於百台級以上機房,配合 Cobbler、Foreman 或 Ansible 進一步自動化服務器全生命週期管理,才是運維終極形態。