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

  1. PXE Client Initialization: The client computer is powered on and configured to boot from the network (PXE boot enabled in BIOS/UEFI).

  2. DHCP Discovery: The PXE client broadcasts a DHCPDISCOVER message to locate a DHCP server.

  3. 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.

  4. TFTP Boot File Request: The PXE client uses the Trivial File Transfer Protocol (TFTP) to request the boot file from the TFTP server.

  5. Boot File Transfer: The TFTP server sends the requested boot file to the PXE client.

  6. Kernel and Initrd Download: The PXE client downloads the Linux kernel and initial RAM disk (initrd) files via TFTP.

  7. Operating System Boot: The PXE client boots into the operating system using the downloaded kernel and initrd.

  8. Startup Script Execution: Upon booting, a predefined startup script runs automatically to open the specified network port (e.g., port 9999).


PARTNER WITH US TO CREATE A COMPELLING NARRATIVE
FOR YOUR BRAND!

Let's bring your ideas to life, start collaborating with our creative agency and turn your vision into reality.