How to Set Up a PXE Boot Server for Automated Linux Deployment: A Sysadmin's Guide
How to Set Up a PXE Boot Server for Automated Linux Deployment: A Sysadmin's Guide
This tutorial is designed for IT professionals, system administrators, and DevOps enthusiasts who are responsible for deploying and managing multiple Linux machines. Whether you're provisioning a new lab, setting up a server cluster, or automating OS installations in a data center, Preboot Execution Environment (PXE) booting is a foundational skill. By the end of this guide, you will have a functional, internal PXE server capable of network-booting client machines to install Linux automatically. This eliminates the need for physical installation media, saving time and standardizing deployments across your infrastructure.
Who This Tutorial Is For & What You'll Learn
This guide is for individuals with basic Linux command-line knowledge and an understanding of networking concepts (IP addresses, DHCP). You will learn the core components of a PXE system: the DHCP service to deliver network parameters, the TFTP server to host boot files, and the HTTP/NFS server to provide the full Linux installation files. We'll integrate these services on a single server to create a streamlined, automated deployment solution.
Prerequisites and Preparation
Before starting, ensure you have the following ready:
- A Server Machine: A dedicated machine or VM running a Linux distribution (we'll use Ubuntu Server 22.04 LTS for this guide).
- Network Environment: A controlled network segment (like a lab VLAN) is highly recommended. PXE involves modifying DHCP settings, which can disrupt other network devices if not isolated.
- Superuser Access: You will need
sudoor root privileges on the server. - Linux ISO: The installation ISO for your target distribution (e.g., Ubuntu Server, CentOS Stream).
- Client for Testing: A physical machine or VM configured to boot from the network (PXE).
Step 1: Installing the Required Software
First, update your package lists and install the necessary software packages. We will use dnsmasq (a lightweight integrated DHCP and TFTP server) and an HTTP server (apache2 or nginx).
Connect to your server via SSH and run:
sudo apt update
sudo apt install dnsmasq apache2 syslinux-common -y
The syslinux-common package provides the essential PXE bootloader files we need later.
Step 2: Configuring the DHCP and TFTP Service (dnsmasq)
We will configure dnsmasq to serve as both the DHCP server for our PXE clients and the TFTP server hosting the initial boot files.
First, back up the original configuration and create a new one:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.backup
sudo nano /etc/dnsmasq.conf
Add the following configuration, adjusting the interface and IP range for your network. In this example, our server's IP is 192.168.1.10 on interface eth0.
# Listen on this interface
interface=eth0
# Bind to this interface only
bind-interfaces
# DHCP range for clients
dhcp-range=192.168.1.100,192.168.1.200,12h
# Gateway and DNS server (the PXE server itself)
dhcp-option=3,192.168.1.1
dhcp-option=6,192.168.1.1
# PXE Boot Server and Boot File
dhcp-boot=pxelinux.0,pxeserver,192.168.1.10
# Enable integrated TFTP server
enable-tftp
# TFTP root directory
tftp-root=/var/lib/tftpboot
Save and exit the editor. This configuration tells clients to request the pxelinux.0 boot file from this server via TFTP.
Step 3: Setting Up the TFTP Boot Directory
Now, create the TFTP root directory and populate it with the necessary bootloader files from syslinux.
sudo mkdir -p /var/lib/tftpboot
sudo cp /usr/lib/syslinux/modules/bios/* /var/lib/tftpboot/
sudo cp /usr/lib/syslinux/modules/efi64/* /var/lib/tftpboot/ # For UEFI support
sudo cp /usr/lib/syslinux/bios/pxelinux.0 /var/lib/tftpboot/
sudo cp /usr/lib/syslinux/bios/ldlinux.c32 /var/lib/tftpboot/
sudo mkdir /var/lib/tftpboot/pxelinux.cfg
Next, create the main PXE configuration menu file:
sudo nano /var/lib/tftpboot/pxelinux.cfg/default
Add a basic menu. This example points to an Ubuntu installer kernel and initrd.
DEFAULT menu.c32
PROMPT 0
TIMEOUT 50
MENU TITLE PXE Boot Menu
LABEL Install Ubuntu 22.04
MENU LABEL Install Ubuntu 22.04 Server
KERNEL ubuntu-installer/amd64/linux
APPEND vga=788 initrd=ubuntu-installer/amd64/initrd.gz url=http://192.168.1.10/ubuntu/22.04/preseed.cfg quiet ---
Step 4: Preparing the Linux Installation Files
We will use the HTTP server to host the full installation files. Mount your Linux ISO and copy its contents.
sudo mkdir -p /var/www/html/ubuntu/22.04
sudo mount -o loop ~/ubuntu-22.04.3-live-server-amd64.iso /mnt
sudo cp -r /mnt/* /var/www/html/ubuntu/22.04/
sudo umount /mnt
Now, copy the kernel and initrd files specifically needed for PXE booting to the TFTP directory, matching the paths in your menu file.
sudo mkdir -p /var/lib/tftpboot/ubuntu-installer/amd64
sudo cp /var/www/html/ubuntu/22.04/casper/vmlinuz /var/lib/tftpboot/ubuntu-installer/amd64/linux
sudo cp /var/www/html/ubuntu/22.04/casper/initrd /var/lib/tftpboot/ubuntu-installer/amd64/initrd.gz
Step 5: (Optional) Automating with a Preseed File
For true hands-off installation, create a preseed configuration file to answer all the setup prompts automatically. Create a simple preseed file:
sudo nano /var/www/html/ubuntu/22.04/preseed.cfg
You can start with a basic template. A critical line for automation is:
d-i debconf/priority critical
d-i partman-auto/disk string /dev/sda
d-i partman-auto/method string regular
Warning: A poorly configured preseed file can wipe disks without confirmation. Always test in a safe, isolated environment first.
Step 6: Starting Services and Testing
Restart the configured services to apply all changes:
sudo systemctl restart dnsmasq
sudo systemctl restart apache2
sudo systemctl enable dnsmasq apache2
Ensure your firewall allows DHCP (port 67), TFTP (port 69), and HTTP (port 80). Now, boot your client machine (a VM is perfect for testing) and set it to boot from the network (PXE). If successful, you should see the "PXE Boot Menu" from your TFTP server, and selecting the Ubuntu option should begin an automated installation.
Common Issues & Troubleshooting
- Client gets an IP but fails to download
pxelinux.0: Check TFTP server logs (sudo journalctl -u dnsmasq). Verify file permissions in/var/lib/tftpboot/and that the paths indnsmasq.confare correct. - Client receives an IP from another DHCP server: This is why an isolated network is crucial. Your PXE server's DHCP offer might be ignored if another authoritative DHCP server exists on the network.
- Kernel panic after booting: The paths to the kernel (
vmlinuz) and initrd (initrd.gz) in your PXE menu are likely incorrect. Double-check the filenames and locations in the TFTP root. - UEFI clients fail to boot: You need to provide UEFI-compatible boot files (like
syslinux.efi) and create a separate configuration under/var/lib/tftpboot/efi64/.
Conclusion and Next Steps
You have now built a basic but fully functional PXE boot server. This system forms the backbone for automated, large-scale operating system deployments. From here, you can expand your setup by:
- Adding menus for multiple Linux distributions and versions.
- Integrating with advanced automation tools like Foreman, Cobbler, or Canonical's MAAS for a full lifecycle management system.
- Implementing Kickstart files for Red Hat-based distributions alongside Preseed for Debian-based ones.
- Setting up diskless workstations that run entirely from the network.
The true value of PXE is realized in its repeatability and integration into Infrastructure as Code (IaC) and Continuous Integration/Deployment (CI/CD) pipelines, making it a cornerstone of modern, automated IT infrastructure.