Linux 系统安装
概述
Linux 系统安装方式多样,从传统的光盘安装到现代化的网络批量部署。企业环境中,PXE(Preboot eXecution Environment)网络安装是实现大规模自动化部署的核心技术。
安装方式概览
| 方式 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| 光盘/U盘 | 单机安装 | 简单直观 | 效率低、介质易损 |
| 网络安装(HTTP/FTP/NFS) | 少量服务器 | 无需物理介质 | 需配置服务器 |
| PXE 网络引导 | 批量部署 | 自动化、无人值守 | 配置复杂 |
| Kickstart | 自动化安装 | 可定制、可重复 | 需预先配置 |
| Cobbler | 企业级部署 | 集中管理、Web界面 | 学习成本高 |
PXE 安装详解
PXE 原理
PXE(Preboot eXecution Environment,预启动执行环境)是 Intel 开发的技术,允许计算机通过网络启动操作系统。
工作流程
┌─────────────────────────────────────────────────────────────────────────────┐
│ PXE 启动流程 │
└─────────────────────────────────────────────────────────────────────────────┘
客户端(PXE) 服务端
┌─────────┐ ┌─────────────────────────────────────┐
│ │ │ ┌─────────┐ ┌─────────┐ ┌──────┐ │
│ BIOS │ │ │ DHCP │ │ TFTP │ │ HTTP │ │
│ /UEFI │ │ │ Server │ │ Server │ │/FTP │ │
└────┬────┘ │ └────┬────┘ └────┬────┘ └──┬───┘ │
│ └───────┼────────────┼──────────┼─────┘
│ ①PXE启动
▼
┌─────────┐
│ 网卡PXE │
│ ROM │
└────┬────┘
│ ②DHCP Discover (广播)
├─────────────────────────────────────────────► DHCP
│ │
│ ③DHCP Offer (IP + TFTP地址 + 引导文件名)
│◄─────────────────────────────────────────────┤
│ │
│ ④TFTP请求引导文件 (pxelinux.0/grubx64.efi)
├─────────────────────────────────────────────► TFTP
│ │
│ ⑤传输引导加载程序
│◄─────────────────────────────────────────────┤
│ │
│ ⑥请求引导配置文件 (pxelinux.cfg/default)
├─────────────────────────────────────────────► TFTP
│ │
│ ⑦传输配置文件
│◄─────────────────────────────────────────────┤
│ │
│ ⑧加载内核(vmlinuz)和initrd
├─────────────────────────────────────────────► TFTP
│ │
│ ⑨传输内核和initrd
│◄─────────────────────────────────────────────┤
│ │
│ ⑩内核启动,通过HTTP/FTP/NFS获取安装源
├─────────────────────────────────────────────► HTTP/FTP/NFS
│ │
│ ⑪传输安装文件
│◄─────────────────────────────────────────────┤
│
▼
┌─────────┐
│ 开始安装│
└─────────┘
核心组件
| 组件 | 功能 | 常用软件 |
|---|---|---|
| DHCP 服务器 | 分配 IP 地址,提供 TFTP 服务器地址和引导文件名 | isc-dhcp-server, dnsmasq |
| TFTP 服务器 | 传输引导加载程序、内核、initrd | tftpd-hpa, dnsmasq |
| 文件服务器 | 提供安装源(ISO 内容) | Apache, Nginx, FTP, NFS |
| 引导加载程序 | 加载内核 | pxelinux, GRUB2, iPXE |
| Kickstart/Preseed | 自动化安装配置 | anaconda, debian-installer |
PXE 服务端搭建
环境准备
# 服务器信息
服务端 IP: 192.168.1.100
网段: 192.168.1.0/24
安装系统: CentOS 8 / Rocky Linux 8 / Ubuntu 22.04
# 关闭防火墙和 SELinux(测试环境)
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config安装必要软件
# CentOS/Rocky Linux
dnf install -y dhcp-server tftp-server httpd syslinux syslinux-tftpboot
# Ubuntu/Debian
apt install -y isc-dhcp-server tftpd-hpa apache2 pxelinux配置 DHCP 服务器
DHCP 配置文件
# /etc/dhcp/dhcpd.conf
# 全局配置
option domain-name "example.com";
option domain-name-servers 8.8.8.8, 8.8.4.4;
default-lease-time 600;
max-lease-time 7200;
# PXE 相关选项
option space pxelinux;
option pxelinux.magic code 208 = string;
option pxelinux.configfile code 209 = text;
option pxelinux.pathprefix code 210 = text;
option pxelinux.reboottime code 211 = unsigned integer 32;
# 子网配置
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.150 192.168.1.200;
option routers 192.168.1.1;
option broadcast-address 192.168.1.255;
# PXE 引导配置
next-server 192.168.1.100; # TFTP 服务器地址
filename "pxelinux.0"; # BIOS 引导文件
# filename "grubx64.efi"; # UEFI 引导文件(UEFI 环境)
# 或者根据客户端架构自动选择
class "pxeclients" {
match if substring(option vendor-class-identifier, 0, 9) = "PXEClient";
if option architecture-type = 00:07 {
filename "grubx64.efi"; # x86_64 UEFI
} else {
filename "pxelinux.0"; # x86_64 BIOS
}
}
}
# 固定 IP 地址(可选,针对特定主机)
host server1 {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.1.10;
next-server 192.168.1.100;
filename "pxelinux.0";
}启动 DHCP 服务
# 检查配置语法
dhcpd -t -cf /etc/dhcp/dhcpd.conf
# 启动服务
systemctl enable --now dhcpd
# 查看状态
systemctl status dhcpd
# 查看日志
journalctl -u dhcpd -f配置 TFTP 服务器
TFTP 配置文件
# /etc/xinetd.d/tftp (CentOS 7 及更早版本)
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot -c
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
# Ubuntu/Debian: /etc/default/tftpd-hpa
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/var/lib/tftpboot"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="-l -s -c"准备 PXE 引导文件
# 创建 TFTP 根目录
mkdir -p /var/lib/tftpboot
# 复制 PXE 引导文件(CentOS/Rocky)
# BIOS 引导
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp /usr/share/syslinux/{menu.c32,vesamenu.c32,ldlinux.c32,libcom32.c32,libutil.c32} /var/lib/tftpboot/
# 或者从安装 ISO 复制
mount -o loop Rocky-8.10-x86_64-dvd.iso /mnt
cp /mnt/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos8/
cp /mnt/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32,menu.c32,vesamenu.c32} /var/lib/tftpboot/
# UEFI 引导文件
cp /mnt/EFI/BOOT/grubx64.efi /var/lib/tftpboot/
# 或安装 shim 和 grub2-efi
dnf install -y shim grub2-efi-x64
cp /boot/efi/EFI/centos/shimx64.efi /var/lib/tftpboot/
cp /boot/efi/EFI/centos/grubx64.efi /var/lib/tftpboot/TFTP 目录结构
/var/lib/tftpboot/
├── pxelinux.0 # BIOS 引导加载程序
├── pxelinux.cfg/
│ └── default # BIOS 引导菜单配置
├── menu.c32 # 菜单模块
├── vesamenu.c32 # 图形菜单模块
├── ldlinux.c32 # 引导模块
├── libcom32.c32 # 库文件
├── libutil.c32 # 工具库
├── grubx64.efi # UEFI 引导加载程序
├── grub.cfg # UEFI 引导菜单配置
├── centos8/
│ ├── vmlinuz # CentOS 8 内核
│ └── initrd.img # CentOS 8 初始化镜像
├── rocky9/
│ ├── vmlinuz
│ └── initrd.img
└── ubuntu22/
├── vmlinuz
└── initrd
启动 TFTP 服务
# CentOS 8+
systemctl enable --now tftp
# Ubuntu/Debian
systemctl enable --now tftpd-hpa
# 验证 TFTP 服务
tftp localhost
tftp> get pxelinux.0
tftp> quit
# 检查端口
ss -ulnp | grep :69配置 HTTP 服务器(安装源)
准备安装文件
# 创建 HTTP 目录
mkdir -p /var/www/html/centos8
mkdir -p /var/www/html/rocky9
mkdir -p /var/www/html/ubuntu22
# 挂载并复制 ISO 内容
# CentOS 8
mount -o loop CentOS-8-x86_64-DVD.iso /mnt
cp -r /mnt/* /var/www/html/centos8/
umount /mnt
# Rocky Linux 9
mount -o loop Rocky-9.4-x86_64-dvd.iso /mnt
cp -r /mnt/* /var/www/html/rocky9/
umount /mnt
# Ubuntu 22.04
mount -o loop ubuntu-22.04-live-server-amd64.iso /mnt
cp -r /mnt/* /var/www/html/ubuntu22/
umount /mnt
# 设置权限
chmod -R 755 /var/www/html/配置 Kickstart 文件
# 创建 Kickstart 目录
mkdir -p /var/www/html/kickstart
# CentOS/Rocky Linux Kickstart 示例
cat > /var/www/html/kickstart/centos8.cfg << 'EOF'
# CentOS 8 自动化安装配置
# ==============================================
# 安装方式
install
url --url=http://192.168.1.100/centos8
# 键盘和语言
keyboard us
lang en_US.UTF-8
# 网络配置
network --bootproto=dhcp --device=link --activate
network --hostname=server.example.com
# 时区
timezone Asia/Shanghai --utc
# root 密码 (使用 openssl passwd -6 "password" 生成)
rootpw --iscrypted $6$randomsalt$hashedpassword
# 普通用户
user --name=admin --password=$6$randomsalt$hashedpassword --groups=wheel
# 引导加载程序
bootloader --location=mbr --boot-drive=sda
# 分区方案
clearpart --all --initlabel --drives=sda
part /boot --fstype=xfs --size=1024 --ondisk=sda
part /boot/efi --fstype=efi --size=512 --ondisk=sda # UEFI 需要
part pv.01 --size=1 --grow --ondisk=sda
volgroup vg_root pv.01
logvol / --fstype=xfs --name=lv_root --vgname=vg_root --size=20480
logvol swap --fstype=swap --name=lv_swap --vgname=vg_root --size=4096
logvol /var --fstype=xfs --name=lv_var --vgname=vg_root --size=10240
logvol /home --fstype=xfs --name=lv_home --vgname=vg_root --size=1 --grow
# 防火墙和 SELinux
firewall --enabled --ssh
selinux --enforcing
# 软件包
%packages
@^minimal-environment
@base
@core
openssh-server
vim-enhanced
wget
curl
net-tools
%end
# 安装后脚本
%post --log=/root/ks-post.log
#!/bin/bash
# 更新系统
dnf update -y
# 配置 NTP
dnf install -y chrony
systemctl enable chronyd
# 自定义配置
echo "Installation completed at $(date)" >> /root/install.log
%end
# 完成后重启
reboot
EOF
# Ubuntu 22.04 autoinstall (cloud-init)
cat > /var/www/html/kickstart/ubuntu22-user-data << 'EOF'
#cloud-config
autoinstall:
version: 1
interactive-sections: []
locale: en_US.UTF-8
keyboard:
layout: us
identity:
hostname: ubuntu-server
username: admin
password: "$6$randomsalt$hashedpassword"
ssh:
install-server: true
allow-pw: false
authorized-keys:
- ssh-rsa AAAA... admin@example.com
storage:
layout:
name: lvm
packages:
- openssh-server
- vim
- wget
- curl
late-commands:
- echo "Installation completed" > /target/root/install.log
user-data:
disable_root: false
shutdown: reboot
EOF启动 HTTP 服务
# 启动 Apache
systemctl enable --now httpd
# 验证访问
curl http://localhost/centos8/
# 检查端口
ss -tlnp | grep :80配置 PXE 引导菜单
BIOS 引导菜单 (pxelinux.cfg/default)
mkdir -p /var/lib/tftpboot/pxelinux.cfg
cat > /var/lib/tftpboot/pxelinux.cfg/default << 'EOF'
DEFAULT menu.c32
PROMPT 0
TIMEOUT 300
MENU TITLE PXE Network Boot Menu
MENU BACKGROUND pxelinux.png
# CentOS 8
LABEL centos8
MENU LABEL ^1. CentOS 8 Installation
KERNEL centos8/vmlinuz
APPEND initrd=centos8/initrd.img inst.repo=http://192.168.1.100/centos8 ks=http://192.168.1.100/kickstart/centos8.cfg
LABEL centos8-manual
MENU LABEL ^2. CentOS 8 Manual Installation
KERNEL centos8/vmlinuz
APPEND initrd=centos8/initrd.img inst.repo=http://192.168.1.100/centos8
# Rocky Linux 9
LABEL rocky9
MENU LABEL ^3. Rocky Linux 9 Installation
KERNEL rocky9/vmlinuz
APPEND initrd=rocky9/initrd.img inst.repo=http://192.168.1.100/rocky9
LABEL rocky9-ks
MENU LABEL ^4. Rocky Linux 9 Auto Install
KERNEL rocky9/vmlinuz
APPEND initrd=rocky9/initrd.img inst.repo=http://192.168.1.100/rocky9 ks=http://192.168.1.100/kickstart/rocky9.cfg
# Ubuntu 22.04
LABEL ubuntu22
MENU LABEL ^5. Ubuntu 22.04 Installation
KERNEL ubuntu22/vmlinuz
APPEND initrd=ubuntu22/initrd auto=true url=http://192.168.1.100/kickstart/ubuntu22-preseed.cfg priority=critical
LABEL ubuntu22-auto
MENU LABEL ^6. Ubuntu 22.04 Auto Install
KERNEL ubuntu22/vmlinuz
APPEND initrd=ubuntu22/initrd auto=true url=http://192.168.1.100/kickstart/ubuntu22-user-data priority=critical
# 内存测试
LABEL memtest
MENU LABEL ^7. Memory Test (Memtest86+)
KERNEL memtest/memtest86+
# 本地启动
LABEL local
MENU LABEL ^0. Boot from local disk
LOCALBOOT 0
EOFUEFI 引导菜单 (grub.cfg)
cat > /var/lib/tftpboot/grub.cfg << 'EOF'
set timeout=30
set default=0
menuentry 'CentOS 8 Installation' {
linuxefi centos8/vmlinuz inst.repo=http://192.168.1.100/centos8 ks=http://192.168.1.100/kickstart/centos8.cfg
initrdefi centos8/initrd.img
}
menuentry 'CentOS 8 Manual Installation' {
linuxefi centos8/vmlinuz inst.repo=http://192.168.1.100/centos8
initrdefi centos8/initrd.img
}
menuentry 'Rocky Linux 9 Installation' {
linuxefi rocky9/vmlinuz inst.repo=http://192.168.1.100/rocky9
initrdefi rocky9/initrd.img
}
menuentry 'Ubuntu 22.04 Installation' {
linuxefi ubuntu22/vmlinuz url=http://192.168.1.100/ubuntu22 auto=true priority=critical
initrdefi ubuntu22/initrd
}
menuentry 'Boot from local disk' {
exit
}
EOF客户端 PXE 启动配置
BIOS 设置
- 重启服务器,进入 BIOS/UEFI 设置(通常按 F2、F12、Del 或 Esc)
- 启用网络引导(Network Boot / PXE Boot)
- 将网络引导设置为首选启动设备
- 保存并重启
常见 BIOS 设置位置
| 厂商 | 进入 BIOS | 启动菜单 | PXE 选项位置 |
|---|---|---|---|
| Dell | F2 | F12 | BIOS Settings > Boot Settings |
| HP | F10 | F9 | Advanced Options > Boot Order |
| Lenovo | F1 | F12 | Startup > Boot Priority |
| Supermicro | Del | F11 | Boot > Boot Option #1 |
| VMware VM | F2 | Esc | BIOS > Boot |
iPXE 高级引导
iPXE 是 PXE 的增强版本,支持更多协议:
# 安装 iPXE
dnf install -y ipxe-bootimgs
# 复制 iPXE 引导文件
cp /usr/share/ipxe/undionly.kpxe /var/lib/tftpboot/
cp /usr/share/ipxe/ipxe.efi /var/lib/tftpboot/
# 修改 DHCP 配置使用 iPXE
# /etc/dhcp/dhcpd.conf
if exists user-class and option user-class = "iPXE" {
filename "http://192.168.1.100/boot.ipxe";
} else {
filename "undionly.kpxe";
}
# 创建 iPXE 引导脚本
cat > /var/www/html/boot.ipxe << 'EOF'
#!ipxe
menu PXE Boot Menu
item centos8 CentOS 8 Installation
item rocky9 Rocky Linux 9 Installation
item ubuntu22 Ubuntu 22.04 Installation
item shell iPXE Shell
item exit Exit to BIOS
choose target && goto ${target}
:centos8
kernel http://192.168.1.100/centos8/vmlinuz inst.repo=http://192.168.1.100/centos8 ks=http://192.168.1.100/kickstart/centos8.cfg
initrd http://192.168.1.100/centos8/initrd.img
boot
:rocky9
kernel http://192.168.1.100/rocky9/vmlinuz inst.repo=http://192.168.1.100/rocky9
initrd http://192.168.1.100/rocky9/initrd.img
boot
:ubuntu22
kernel http://192.168.1.100/ubuntu22/vmlinuz auto=true url=http://192.168.1.100/ubuntu22 priority=critical
initrd http://192.168.1.100/ubuntu22/initrd
boot
:shell
shell
:exit
exit
EOF使用 dnsmasq 简化部署
dnsmasq 集成了 DHCP、TFTP 和 DNS 服务,适合小型环境:
# 安装 dnsmasq
dnf install -y dnsmasq
# 配置文件 /etc/dnsmasq.conf
# ==============================================
# DNS 配置
port=53
domain-needed
bogus-priv
domain=example.com
local=/example.com/
# DHCP 配置
dhcp-range=192.168.1.150,192.168.1.200,255.255.255.0,12h
dhcp-option=option:router,192.168.1.1
dhcp-option=option:dns-server,192.168.1.100
dhcp-option=option:domain-name,example.com
# PXE 配置
# BIOS 引导
dhcp-match=set:pxe,option:client-arch,0
dhcp-boot=tag:pxe,pxelinux.0
# UEFI 引导
dhcp-match=set:efi32,option:client-arch,6
dhcp-match=set:efi64,option:client-arch,7
dhcp-match=set:efi64,option:client-arch,9
dhcp-boot=tag:efi32,grubia32.efi
dhcp-boot=tag:efi64,grubx64.efi
# TFTP 配置
enable-tftp
tftp-root=/var/lib/tftpboot
# 日志
log-dhcp
log-queries
# 启动服务
systemctl enable --now dnsmasq完整部署示例脚本
#!/bin/bash
# ==============================================
# PXE 服务器一键部署脚本
# 适用于 CentOS 8 / Rocky Linux 8
# ==============================================
# 配置变量
PXE_SERVER_IP="192.168.1.100"
PXE_SUBNET="192.168.1.0"
PXE_NETMASK="255.255.255.0"
PXE_RANGE="192.168.1.150 192.168.1.200"
PXE_GATEWAY="192.168.1.1"
PXE_DNS="8.8.8.8"
TFTP_ROOT="/var/lib/tftpboot"
HTTP_ROOT="/var/www/html"
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# 检查 root 权限
if [ "$EUID" -ne 0 ]; then
log_error "请使用 root 权限运行此脚本"
exit 1
fi
# 关闭防火墙和 SELinux
log_info "配置防火墙和 SELinux..."
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 安装软件包
log_info "安装必要软件包..."
dnf install -y dhcp-server tftp-server httpd syslinux syslinux-tftpboot
# 创建目录
log_info "创建目录结构..."
mkdir -p $TFTP_ROOT/pxelinux.cfg
mkdir -p $TFTP_ROOT/centos8
mkdir -p $HTTP_ROOT/centos8
mkdir -p $HTTP_ROOT/kickstart
# 复制 PXE 引导文件
log_info "配置 PXE 引导文件..."
cp /usr/share/syslinux/pxelinux.0 $TFTP_ROOT/
cp /usr/share/syslinux/{menu.c32,vesamenu.c32,ldlinux.c32,libcom32.c32,libutil.c32} $TFTP_ROOT/
# 配置 DHCP
log_info "配置 DHCP 服务器..."
cat > /etc/dhcp/dhcpd.conf << EOF
option domain-name "example.com";
option domain-name-servers $PXE_DNS;
default-lease-time 600;
max-lease-time 7200;
subnet $PXE_SUBNET netmask $PXE_NETMASK {
range $PXE_RANGE;
option routers $PXE_GATEWAY;
next-server $PXE_SERVER_IP;
filename "pxelinux.0";
}
EOF
# 配置 PXE 菜单
log_info "配置 PXE 引导菜单..."
cat > $TFTP_ROOT/pxelinux.cfg/default << 'EOF'
DEFAULT menu.c32
PROMPT 0
TIMEOUT 300
MENU TITLE PXE Network Boot Menu
LABEL centos8
MENU LABEL CentOS 8 Installation
KERNEL centos8/vmlinuz
APPEND initrd=centos8/initrd.img inst.repo=http://PXE_SERVER_IP/centos8
LABEL local
MENU LABEL Boot from local disk
LOCALBOOT 0
EOF
sed -i "s/PXE_SERVER_IP/$PXE_SERVER_IP/g" $TFTP_ROOT/pxelinux.cfg/default
# 配置 TFTP
log_info "配置 TFTP 服务器..."
cat > /etc/xinetd.d/tftp << 'EOF'
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot -c
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
EOF
# 设置权限
log_info "设置目录权限..."
chmod -R 755 $TFTP_ROOT
chmod -R 755 $HTTP_ROOT
# 启动服务
log_info "启动服务..."
systemctl enable --now dhcpd
systemctl enable --now tftp
systemctl enable --now httpd
# 验证服务状态
log_info "验证服务状态..."
systemctl is-active dhcpd || log_error "DHCP 服务启动失败"
systemctl is-active tftp || log_error "TFTP 服务启动失败"
systemctl is-active httpd || log_error "HTTP 服务启动失败"
echo ""
log_info "=========================================="
log_info "PXE 服务器部署完成!"
log_info "=========================================="
echo ""
log_info "下一步操作:"
echo "1. 挂载 ISO 并复制文件:"
echo " mount -o loop CentOS-8-x86_64-DVD.iso /mnt"
echo " cp /mnt/isolinux/{vmlinuz,initrd.img} $TFTP_ROOT/centos8/"
echo " cp -r /mnt/* $HTTP_ROOT/centos8/"
echo ""
echo "2. 创建 Kickstart 文件到 $HTTP_ROOT/kickstart/"
echo ""
echo "3. 客户端设置 PXE 网络引导"故障排查
常见问题诊断
# 1. 检查服务状态
systemctl status dhcpd
systemctl status tftp
systemctl status httpd
# 2. 检查端口监听
ss -ulnp | grep -E ':(67|69)' # DHCP:67, TFTP:69
ss -tlnp | grep :80 # HTTP:80
# 3. 查看 DHCP 租约
cat /var/lib/dhcpd/dhcpd.leases
# 4. 测试 TFTP 服务
tftp localhost
tftp> get pxelinux.0
tftp> quit
# 5. 测试 HTTP 服务
curl http://localhost/centos8/
# 6. 查看 TFTP 日志
journalctl -u tftp -f
# 7. 查看 DHCP 日志
journalctl -u dhcpd -f
# 8. 抓包分析
tcpdump -i eth0 port bootps or port tftp or port http -vv常见错误及解决方案
| 错误现象 | 可能原因 | 解决方案 |
|---|---|---|
| PXE-E11: ARP timeout | DHCP 未响应 | 检查 DHCP 服务状态、防火墙 |
| PXE-E32: TFTP open timeout | TFTP 服务未启动或文件不存在 | 启动 TFTP 服务、检查文件路径 |
| PXE-E35: TFTP read timeout | TFTP 传输失败 | 检查文件权限、网络连通性 |
| PXE-E3F: No boot filename | DHCP 未提供引导文件名 | 检查 DHCP 配置 filename 参数 |
| TFTP file not found | 文件路径错误 | 检查 TFTP 根目录文件结构 |
| Kernel panic | 内核或 initrd 损坏 | 重新复制 ISO 中的文件 |
| Unable to find installation source | HTTP 服务问题 | 检查 HTTP 服务和文件路径 |
调试技巧
# 在客户端启动时按 Tab 编辑内核参数
# 添加调试参数
vmlinuz initrd=initrd.img inst.repo=http://192.168.1.100/centos8 inst.debug
# 进入 dracut 紧急 shell
rd.break
# 检查网络
ip addr
ping 192.168.1.100
# 手动加载模块
modprobe tg3Cobbler 高级部署
Cobbler 是一个自动化安装管理工具,简化 PXE 配置:
# 安装 Cobbler
dnf install -y cobbler cobbler-web
# 配置 Cobbler
vim /etc/cobbler/settings.yaml
# server: 192.168.1.100
# next_server: 192.168.1.100
# manage_dhcp: true
# 同步配置
cobbler sync
# 导入 ISO
cobbler import --name=centos8 --path=/mnt
# 创建系统配置
cobbler system add --name=server1 --profile=centos8-x86_64 \
--interface=eth0 --mac=00:11:22:33:44:55 \
--ip-address=192.168.1.10 --hostname=server1.example.com
# 查看 Cobbler 配置
cobbler list
cobbler distro list
cobbler profile listKickstart 配置详解
Kickstart 文件结构
| 部分 | 说明 |
|---|---|
| 命令部分 | 安装配置(键盘、语言、分区等) |
| %packages | 软件包选择 |
| %pre | 安装前脚本(解释器执行) |
| %post | 安装后脚本(chroot 环境) |
| %addon | 附加组件配置 |
常用命令
# 安装源
cdrom # 光盘安装
url --url=http://server/centos8 # HTTP 安装
nfs --server=192.168.1.100 --dir=/centos8 # NFS 安装
# 引导配置
bootloader --location=mbr # MBR 引导
bootloader --location=none --drive=sda # 不安装引导加载程序
# 分区
clearpart --all --initlabel # 清除所有分区
ignoredisk --only-use=sda # 只使用 sda
# 简单分区
part / --fstype=xfs --size=20480 --grow
part swap --fstype=swap --size=4096
# LVM 分区
part pv.01 --size=1 --grow
volgroup vg_root pv.01
logvol / --fstype=xfs --name=lv_root --vgname=vg_root --size=20480
# 加密分区
part / --fstype=xfs --size=20480 --encrypted --passphrase=secret
# 网络
network --bootproto=dhcp # DHCP
network --bootproto=static --ip=192.168.1.10 --netmask=255.255.255.0 --gateway=192.168.1.1 --nameserver=8.8.8.8
# 用户
rootpw --plaintext password # 明文密码(不推荐)
rootpw --iscrypted $6$... # 加密密码
user --name=admin --groups=wheel --password=$6$...
# 安全
firewall --enabled --ssh --port=80:tcp
firewall --disabled
selinux --enforcing
selinux --disabled
# 其他
timezone Asia/Shanghai --utc
keyboard us
lang en_US.UTF-8
services --enabled=sshd,chronyd
services --disabled=postfix软件包选择
%packages
# 基础环境
@^minimal-environment # 最小安装
@^server-product-environment # 服务器环境
@^gnome-desktop-environment # GNOME 桌面
# 软件组
@base
@core
@development
@network-server
# 单个软件包
vim-enhanced
wget
curl
htop
# 排除软件包
-postfix
-chrony
# 必须安装(即使依赖问题)
%packages --ignoremissing
package-name
%end安装脚本
# 安装前脚本
%pre --log=/tmp/ks-pre.log
#!/bin/bash
# 获取磁盘信息
DISK=$(lsblk -d -o NAME,SIZE,TYPE | grep disk | head -1 | awk '{print $1}')
echo "clearpart --all --drives=$DISK" > /tmp/partition-include
# 检测内存大小
MEM=$(free -m | awk '/^Mem:/{print $2}')
if [ $MEM -gt 8192 ]; then
echo "part swap --fstype=swap --size=8192" >> /tmp/partition-include
else
echo "part swap --fstype=swap --size=4096" >> /tmp/partition-include
fi
%end
# 安装后脚本
%post --log=/root/ks-post.log
#!/bin/bash
# 配置 yum 源
cat > /etc/yum.repos.d/local.repo << EOF
[local]
name=Local Repository
baseurl=http://192.168.1.100/centos8
enabled=1
gpgcheck=0
EOF
# 更新系统
dnf update -y
# 安装额外软件
dnf install -y docker-ce kubectl
# 配置 SSH
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
# 配置防火墙
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
# 配置 chrony
cat > /etc/chrony.conf << EOF
server time.example.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
EOF
systemctl enable chronyd
# 标记安装完成
echo "Installation completed at $(date)" >> /root/install.log
%end