Build a PXE Boot Server on Ubuntu with dnsmasq and iPXE: High-Speed Network OS Installation

Ubuntu tutorial - IT technology blog
Ubuntu tutorial - IT technology blog

Background & Why You Should Ditch Manual USB Flashing

Reinstalling the operating system on 20–30 workstations or an entire server rack using USB flash drives is an enormous time sink. You find yourself constantly plugging and unplugging drives while babysitting each machine until installation finishes. On top of that, juggling five or six USB drives for different Ubuntu and Windows releases makes it all too easy to plug in the wrong version.

PXE (Preboot Execution Environment) solves this problem cleanly. All you need to do is plug in an Ethernet cable, power on the machine, and select network boot. The client machine automatically connects to your local server to fetch the kernel and run the installer.

Historically, system administrators had to set up separate instances of ISC-DHCP-Server and TFTP-HPA. That setup was rather cumbersome. The combination of dnsmasq and iPXE offers a much leaner and more elegant solution:

  • dnsmasq: Bundles DHCP, DNS, and TFTP into a single lightweight service. In DHCP Proxy mode, dnsmasq supplies boot information without interfering with the existing IP address pool managed by your primary router.
  • iPXE: A major upgrade over traditional PXE. Legacy TFTP relies on UDP transfers crawling at only 1–2 MB/s, taking nearly 25 minutes to pull a 2GB ISO file. With iPXE, you can retrieve the kernel and ISO over HTTP, saturating Gigabit bandwidth (110–115 MB/s) in just 20–30 seconds. In addition, iPXE seamlessly detects both Legacy BIOS and UEFI systems.

I have deployed this setup in production across an office network of around 60 machines. The MikroTik router continues handling standard DHCP and internet routing as usual, while the PXE server intervenes only when a client requests network boot.

Installing Required Packages

Prepare an Ubuntu server (preferably 22.04 LTS) assigned a static IP. In this guide, our server is assigned 192.168.1.50. Run ip -br a to find your exact network interface name (commonly eth0 or enp3s0).

Install dnsmasq, ipxe, and the nginx web server:

sudo apt update
sudo apt install -y dnsmasq ipxe nginx

Stop the dnsmasq service so you can safely edit its configuration file:

sudo systemctl stop dnsmasq

Detailed PXE Boot Server Configuration

1. Preparing the TFTP Boot Directory

Create the root directory for the TFTP server, then copy the two iPXE binaries to their target location:

sudo mkdir -p /var/lib/tftpboot

# Boot file for Legacy BIOS machines
sudo cp /usr/lib/ipxe/undionly.kpxe /var/lib/tftpboot/

# Boot file for 64-bit UEFI machines
sudo cp /usr/lib/ipxe/ipxe.efi /var/lib/tftpboot/

Set permissions so the dnsmasq process can read the files:

sudo chown -R nobody:nogroup /var/lib/tftpboot
sudo chmod -R 755 /var/lib/tftpboot

2. Configuring dnsmasq as a DHCP Proxy and TFTP Server

Back up the default configuration file:

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
sudo nano /etc/dnsmasq.conf

Paste the following configuration into the file. The key parameter here is dhcp-range=...,proxy. This option turns dnsmasq into a DHCP Proxy: your existing router continues leasing IP addresses as usual, while dnsmasq simply injects boot file parameters for PXE clients:

# Disable built-in DNS server since it is not needed
port=0

# Server network interface (change to match your actual NIC name)
interface=eth0

# Enable internal TFTP server
enable-tftp
tftp-root=/var/lib/tftpboot

# DHCP Proxy on current subnet (adjust to match your actual LAN subnet)
dhcp-range=192.168.1.0,proxy,255.255.255.0

# Match client firmware architecture (x86 BIOS or x64 UEFI)
dhcp-match=set:bios,option:client-arch,0
dhcp-match=set:efi-x86_64,option:client-arch,7
dhcp-match=set:efi-x86_64,option:client-arch,9

# Stage 1: Serve iPXE binary via TFTP according to firmware standard
dhcp-boot=tag:bios,undionly.kpxe
dhcp-boot=tag:efi-x86_64,ipxe.efi

# Stage 2: Once iPXE is loaded, redirect client to HTTP menu
dhcp-userclass=set:ipxe,iPXE
dhcp-boot=tag:ipxe,http://192.168.1.50/boot.ipxe

3. Creating a Boot Menu with an iPXE Script

One of the strongest features of iPXE is its ability to render intuitive boot menus using simple scripting syntax. Create a boot.ipxe file in Nginx’s web root directory:

sudo nano /var/www/html/boot.ipxe

Sample contents for the menu file:

#!ipxe

set server_ip 192.168.1.50
set base_url http://${server_ip}

:start
menu Network Boot Menu - ITFromZero
item --gap --             ---------------- Operating Systems ----------------
item ubuntu2204          Install Ubuntu Server 22.04 LTS (Automated)
item memtest             Run Memory Test
item --gap --             ---------------- Advanced Options ----------------
item shell               Drop to iPXE Shell
item reboot              Reboot Computer
item exit                Boot from Local Hard Drive

choose --timeout 10000 --default ubuntu2204 target && goto ${target}

:ubuntu2204
echo Loading Ubuntu 22.04 Installer...
kernel ${base_url}/ubuntu/vmlinuz ip=dhcp url=${base_url}/ubuntu/ubuntu-22.04.4-live-server-amd64.iso autoinstall ds=nocloud-net;s=${base_url}/autoinstall/ initrd=initrd
initrd ${base_url}/ubuntu/initrd
boot

:memtest
kernel ${base_url}/memtest.bin
boot

:shell
shell

:reboot
reboot

:exit
exit

4. Extracting the Kernel and Staging the Ubuntu ISO

Create a directory for the installer files on Nginx, then mount the ISO image to copy the kernel (vmlinuz) and initial ramdisk (initrd):

sudo mkdir -p /var/www/html/ubuntu
sudo mkdir -p /mnt/iso

# Download Ubuntu Server 22.04.4 ISO
wget https://releases.ubuntu.com/22.04/ubuntu-22.04.4-live-server-amd64.iso -O /tmp/ubuntu.iso

# Temporarily mount ISO to extract the kernel
sudo mount -o loop /tmp/ubuntu.iso /mnt/iso
sudo cp /mnt/iso/casper/vmlinuz /var/www/html/ubuntu/
sudo cp /mnt/iso/casper/initrd /var/www/html/ubuntu/
sudo cp /tmp/ubuntu.iso /var/www/html/ubuntu/ubuntu-22.04.4-live-server-amd64.iso
sudo umount /mnt/iso

Restart the services and enable them to start automatically on system boot:

sudo systemctl restart nginx
sudo systemctl restart dnsmasq
sudo systemctl enable dnsmasq nginx

Verifying Syntax and Monitoring Connections

Before testing on a client machine, verify your dnsmasq configuration file for any syntax errors:

sudo dnsmasq --test

If the terminal prints dnsmasq: syntax check OK, the service is ready. Open two terminal windows to monitor logs in real time.

Terminal 1 monitors the DHCP Proxy handshake and boot file delivery:

sudo journalctl -u dnsmasq -f

Terminal 2 monitors client ISO download requests over HTTP:

sudo tail -f /var/log/nginx/access.log

Power on the client machine, press the hotkey to enter the Boot Menu (F12 on Dell/Lenovo, F8 on Asus, or F11 on HP), and select Network Boot / Onboard NIC. Within 2–3 seconds, the blue graphical iPXE menu will appear. Hit Enter, and the kernel downloads in an instant, bringing up the familiar Ubuntu installer right before your eyes.

Share: