Automated PXE Server and ISO Setup Script
βοΈ Automated PXE Server & ISO Setup Script
Save this as
setup_pxe.sh
and run it using:chmod +x setup_pxe.sh ./setup_pxe.sh
#!/bin/bash
# Exit on any error
set -e
# Configuration
ISO_URL="https://releases.ubuntu.com/20.04/ubuntu-20.04.6-live-server-amd64.iso"
ISO_NAME="ubuntu.iso"
CUSTOM_PORT=9999
SERVER_IP=$(hostname -I | awk '{print $1}')
echo "π§° Updating system and installing required packages..."
sudo apt update
sudo apt install -y dnsmasq apache2 syslinux isolinux genisoimage curl rsync
echo "π₯ Downloading Ubuntu ISO..."
cd ~
curl -L -o $ISO_NAME $ISO_URL
echo "π Mounting and copying ISO files..."
mkdir -p /mnt/iso
sudo mount -o loop $ISO_NAME /mnt/iso
mkdir -p ~/custom-iso
rsync -av /mnt/iso/ ~/custom-iso
sudo umount /mnt/iso
echo "π¦ Adding custom script to open port $CUSTOM_PORT..."
mkdir -p ~/custom-iso/custom
cat <<EOF > ~/custom-iso/custom/openport.sh
#!/bin/bash
iptables -A INPUT -p tcp --dport $CUSTOM_PORT -j ACCEPT
EOF
chmod +x ~/custom-iso/custom/openport.sh
echo "π Creating cloud-init config to run script..."
cat <<EOF > ~/custom-iso/preseed/user-data
#cloud-config
runcmd:
- [ bash, /cdrom/custom/openport.sh ]
EOF
echo "π Rebuilding ISO..."
cd ~/custom-iso
mkisofs -o ~/custom-boot.iso -b isolinux/isolinux.bin -c isolinux/boot.cat \
-no-emul-boot -boot-load-size 4 -boot-info-table -J -R -V "CustomUbuntu" .
echo "πΈοΈ Copying ISO to Apache server root..."
sudo cp ~/custom-boot.iso /var/www/html/
echo "π Setting up TFTP boot files..."
sudo mkdir -p /srv/tftp/pxelinux.cfg
sudo cp /usr/lib/PXELINUX/pxelinux.0 /srv/tftp/
sudo cp /usr/lib/syslinux/modules/bios/* /srv/tftp/
echo "π§Ύ Creating PXE default config..."
sudo tee /srv/tftp/pxelinux.cfg/default > /dev/null <<EOF
DEFAULT linux
LABEL linux
KERNEL /install/vmlinuz
APPEND initrd=/install/initrd.gz auto url=http://$SERVER_IP/custom-boot.iso
EOF
echo "π Configuring dnsmasq..."
sudo tee /etc/dnsmasq.conf > /dev/null <<EOF
interface=eth0
dhcp-range=192.168.1.100,192.168.1.200,12h
dhcp-boot=pxelinux.0
enable-tftp
tftp-root=/srv/tftp
EOF
echo "π Restarting dnsmasq..."
sudo systemctl restart dnsmasq
echo "β
PXE Server is ready!"
echo "π‘ Boot a client on the same network using PXE to install your custom Ubuntu ISO."
echo "π The script will open port $CUSTOM_PORT after boot."
π§ͺ After Running the Script
- Make sure your server and the second computer are on the same local network.
- Boot the second computer into PXE/network boot mode.
- It will load your custom Ubuntu ISO and automatically open port 9999.
πΌοΈ PXE Boot Process Diagram
+----------------+ +----------------+ +----------------+
| | | | | |
| PXE Client | | DHCP Server | | TFTP Server |
| (Your PC) | | (PXE Server) | | (PXE Server) |
| | | | | |
+-------+--------+ +--------+-------+ +--------+-------+
| | |
| 1. Broadcast DHCPDISCOVER | |
|---------------------------->| |
| | |
| 2. DHCP Offer with PXE info | |
|<----------------------------| |
| | |
| 3. TFTP request for boot file (pxelinux.0) |
|-------------------------------------------------------->|
| | |
| 4. Receive boot file | |
|<--------------------------------------------------------|
| | |
| 5. Download kernel and initrd via TFTP |
|<--------------------------------------------------------|
| | |
| 6. Boot into OS | |
| | |
| 7. Run startup script to open port |
| | |
π Step-by-Step Explanation
-
PXE Client Initialization: The client computer is powered on and configured to boot from the network (PXE boot enabled in BIOS/UEFI).
-
DHCP Discovery: The PXE client broadcasts a DHCPDISCOVER message to locate a DHCP server.
-
DHCP Offer: The DHCP server responds with an IP address and provides the location of the boot file (e.g.,
pxelinux.0
) via DHCP options. -
TFTP Boot File Request: The PXE client uses the Trivial File Transfer Protocol (TFTP) to request the boot file from the TFTP server.
-
Boot File Transfer: The TFTP server sends the requested boot file to the PXE client.
-
Kernel and Initrd Download: The PXE client downloads the Linux kernel and initial RAM disk (initrd) files via TFTP.
-
Operating System Boot: The PXE client boots into the operating system using the downloaded kernel and initrd.
-
Startup Script Execution: Upon booting, a predefined startup script runs automatically to open the specified network port (e.g., port 9999).