Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

基於網絡的 RedHat Linux 無人值守自動安裝部署方案

編輯:Linux 教學

方案概述

針對大規模機房、測試環境批量部署、無光驅機器安裝等場景,本文介紹基於 PXE + Kickstart + 網路安裝源的 RedHat Enterprise Linux 全自動化部署方案。僅需客戶機支援 PXE 啟動,即可實現「開機 → 選擇網路啟動 → 完全無人值守完成安裝」的高效流程。

核心技術棧

  1. PXE (Preboot Execution Environment):網卡 ROM 內建,透過 DHCP 獲取 IP 與啟動檔案位置,經 TFTP 下載啟動載入器。
  2. Kickstart:RedHat 專屬無人值守安裝技術,透過應答檔 (ks.cfg) 預先定義分區、套件選擇、網路、使用者、啟動載入器等所有安裝參數。
  3. 網路安裝源 (NFS/HTTP/FTP):安裝程式從網路服務器讀取 RPM 套件與映像檔,無需本地光碟。

架構角色與服務部署

角色服務關鍵檔案/目錄
啟動服務器DHCP + TFTP/etc/dhcpd.conf, /tftpboot/pxelinux.0
安裝服務器NFS / HTTP / FTP/var/www/html/rhel (或 /exports/rhel)
客戶機PXE Client網卡支援 PXE,BIOS 設為網路啟動

啟動服務器與安裝服務器可合部署在同一台物理機上。

步驟一:配置啟動服務器 (DHCP + TFTP)

1. 安裝套件

yum install dhcp tftp-server syslinux -y

2. 配置 DHCP (/etc/dhcpd.conf)

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;
}

3. 配置 TFTP (/etc/xinetd.d/tftp)

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
}

4. 準備 PXE 啟動檔案

# 複製 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/

5. 建立 PXE 選單 (/tftpboot/pxelinux.cfg/default)

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

6. 啟動服務

systemctl enable --now dhcpd tftp xinetd
systemctl restart dhcpd tftp xinetd

步驟二:配置安裝服務器 (HTTP + Kickstart)

1. 掛載安裝映像並共享

mkdir -p /var/www/html/rhel7.9
mount -o loop RHEL-7.9-Server-x86_64-dvd.iso /var/www/html/rhel7.9
# 或解壓 ISO 內容至該目錄

2. 安裝與配置 HTTP 服務

yum install httpd -y
systemctl enable --now httpd
# 防火牆放行 HTTP
firewall-cmd --permanent --add-service=http
firewall-cmd --reload

3. 編寫 Kickstart 應答檔 (/var/www/html/ks/ks.cfg)

#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

步驟三:客戶機安裝驗證

  1. 客戶機 BIOS 設定第一啟動裝置為 Network (PXE)。
  2. 開機,網卡透過 DHCP 獲取 IP,下載 pxelinux.0 進入選單。
  3. 選擇 "Auto Install RHEL 7.9" 或等待倒數自動進入。
  4. 安裝程式自動下載 ks.cfg、內核、initrd,並從 HTTP 源安裝所有套件。
  5. 全程無需人工干預,完成後自動重啟進入系統。

進階擴展:多版本/多配置選單

在 pxelinux.cfg/default 中新增多個 label,配合不同 ks.cfg (如 ks-web.cfg, ks-db.cfg),即可實現「一鍵選擇部署 Web 節點 / DB 節點 / 測試環境」等差異化部署。

常見問題排查

現象可能原因解決
卡在 "Waiting for IP"DHCP 未授權 / 防火牆阻擋檢查 dhcpd 日誌、開放 67/68 UDP
TFTP 傳輸超時TFTP 目錄權限 / SELinuxchcon -R -t tftpdir_t /tftpboot
Kickstart 解析失敗ks.cfg 語法錯誤 / URL 不可達瀏覽器驗證 ks.cfg URL、檢查 %pre/%post 腳本
安裝源 404HTTP 目錄結構不匹配確認 .treeinfo、repodata 存在於 DocumentRoot

結語

PXE + Kickstart 方案一次建置、長期受益。對於百台級以上機房,配合 Cobbler、Foreman 或 Ansible 進一步自動化服務器全生命週期管理,才是運維終極形態。

Copyright © Windows教程網 All Rights Reserved