#!/bin/bash
# ============================================================
# Automated PXE Server Setup
# Supported OS : RHEL / Rocky / AlmaLinux 8 & 9
# Author : ChatGPT
# ============================================================
set -e
# -----------------------------
# Variables
# -----------------------------
PXE_SERVER_IP="192.168.1.10"
SUBNET="192.168.1.0"
NETMASK="255.255.255.0"
RANGE_START="192.168.1.100"
RANGE_END="192.168.1.200"
GATEWAY="192.168.1.1"
OS_NAME="rhel9"
ISO_MOUNT="/mnt/rhel9"
HTTP_ROOT="/var/www/html/${OS_NAME}"
TFTP_ROOT="/var/lib/tftpboot"
PXE_CFG="${TFTP_ROOT}/pxelinux.cfg"
# -----------------------------
# Install Required Packages
# -----------------------------
echo "Installing required packages..."
dnf install -y \
dhcp-server \
tftp-server \
syslinux \
httpd \
xinetd \
wget \
firewalld
# -----------------------------
# Enable Services
# -----------------------------
systemctl enable --now firewalld
systemctl enable --now httpd
systemctl enable --now tftp.socket
systemctl enable --now dhcpd
# -----------------------------
# Configure Firewall
# -----------------------------
echo "Configuring firewall..."
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=dhcp
firewall-cmd --permanent --add-service=tftp
firewall-cmd --reload
# -----------------------------
# Create TFTP Structure
# -----------------------------
echo "Creating PXE boot directories..."
mkdir -p ${PXE_CFG}
mkdir -p ${TFTP_ROOT}/images/${OS_NAME}
# -----------------------------
# Copy PXE Boot Files
# -----------------------------
echo "Copying PXE boot files..."
cp /usr/share/syslinux/pxelinux.0 ${TFTP_ROOT}/
cp /usr/share/syslinux/menu.c32 ${TFTP_ROOT}/
cp /usr/share/syslinux/libutil.c32 ${TFTP_ROOT}/
cp /usr/share/syslinux/libcom32.c32 ${TFTP_ROOT}/
# -----------------------------
# Mount ISO
# -----------------------------
echo "Mounting ISO..."
mkdir -p ${ISO_MOUNT}
echo "Please place RHEL ISO at /root/rhel9.iso"
mount -o loop /root/rhel9.iso ${ISO_MOUNT}
# -----------------------------
# Copy Installation Files
# -----------------------------
echo "Copying installation files..."
mkdir -p ${HTTP_ROOT}
cp -r ${ISO_MOUNT}/* ${HTTP_ROOT}/
# -----------------------------
# Copy Kernel and Initrd
# -----------------------------
echo "Copying kernel and initrd..."
cp ${ISO_MOUNT}/images/pxeboot/vmlinuz \
${TFTP_ROOT}/images/${OS_NAME}/
cp ${ISO_MOUNT}/images/pxeboot/initrd.img \
${TFTP_ROOT}/images/${OS_NAME}/
# -----------------------------
# Create PXE Menu
# -----------------------------
echo "Creating PXE menu..."
cat <<EOF > ${PXE_CFG}/default
DEFAULT menu.c32
PROMPT 0
TIMEOUT 100
ONTIMEOUT local
MENU TITLE Automated PXE Boot Menu
LABEL local
MENU LABEL Boot from Local Disk
LOCALBOOT 0
LABEL ${OS_NAME}
MENU LABEL Install RHEL 9
KERNEL images/${OS_NAME}/vmlinuz
APPEND initrd=images/${OS_NAME}/initrd.img \
ip=dhcp \
inst.repo=http://${PXE_SERVER_IP}/${OS_NAME}
LABEL rescue
MENU LABEL Rescue Installed System
KERNEL images/${OS_NAME}/vmlinuz
APPEND initrd=images/${OS_NAME}/initrd.img rescue
EOF
# -----------------------------
# Configure DHCP
# -----------------------------
echo "Creating DHCP configuration..."
cat <<EOF > /etc/dhcp/dhcpd.conf
default-lease-time 600;
max-lease-time 7200;
authoritative;
allow booting;
allow bootp;
subnet ${SUBNET} netmask ${NETMASK} {
range ${RANGE_START} ${RANGE_END};
option routers ${GATEWAY};
option subnet-mask ${NETMASK};
option domain-name-servers 8.8.8.8;
next-server ${PXE_SERVER_IP};
filename "pxelinux.0";
}
EOF
# -----------------------------
# SELinux Adjustments
# -----------------------------
echo "Configuring SELinux..."
setsebool -P tftp_home_dir on
restorecon -Rv ${TFTP_ROOT}
# -----------------------------
# Restart Services
# -----------------------------
echo "Restarting services..."
systemctl restart dhcpd
systemctl restart httpd
systemctl restart tftp.socket
# -----------------------------
# Display Status
# -----------------------------
echo
echo "========================================"
echo " PXE SERVER SETUP COMPLETED"
echo "========================================"
echo
echo "PXE Server IP : ${PXE_SERVER_IP}"
echo "HTTP Repo : http://${PXE_SERVER_IP}/${OS_NAME}"
echo "TFTP Root : ${TFTP_ROOT}"
echo
echo "Boot target systems using PXE network boot."
echo
Thursday, May 14, 2026
How to setup Automated PXE Server Setup Script
.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment