How to setup a PXE boot server with NFS on CentOS7

Last updated on 01st December 2017

PXE (Pre-boot eXecution Environment) is a protocol based on BootP, DHCP and TFTP and it is most frequently used for remote booting and / or installation of an Operating system on multiple computers within the same network. PXE runs on a client-server environment where a PXE server provides the boot and install images. The client is simply a PXE enabled Network Interface Card (NIC) on a blank system that receives the images from the PXE server for booting and installation.

PXE boot client-server environment is widely used for network installation of Linux Operating Systems and remote booting of diskless machines with a minimal or small size Operating System. This article shows you how to setup a Network Installation Server using PXE boot and NFS on a machine running CentOS 7 Operating System.

What we need

  • A server machine with CentOS installed.
  • A client machine with a PXE supported network card.
  • CentOS 7 DVD ISO
  • Network connectivity between the server and client machines
  • Internet access on the server to install additional packages.

A network install server depends on the following three services:

  • DHCP Server to provide IP address and other network configuration to clients.
  • TFTP Server to provide the boot files.
  • HTTP, FTP or NFS server to provide the installation image.

The above three services can be running on separate servers in your network or you can install them all on the same server. For this walk through we will be installing them all on the same server and the installation image will reside on a NFS share.

Install and Configure NFS Server

The first step is to install NFS and to do this, login as root user and install the nfs-utils package.

# yum install nfs-utils

Create a directory to store the installation files (for example /srv/nfs/centos7-install) and export it to make it available remotely. To configure NFS exports, edit the file /etc/exports.

# mkdir -p /srv/nfs/centos7-install
# vi /etc/exports

Add the following line which gives all the machines in the 10.10.1.0/24 subnet read-write access to /srv/nfs/centos7-install directory.

/srv/nfs/centos7-install 10.10.1.0/24(rw,async,no_root_squash)

Save and exit the file and run the following command for changes to take effect.

# exportfs -rv

Allow NFS through Firewall

Edit the file /etc/sysconfig/nfs and uncomment the following entries:

MOUNTD_PORT=892
STATD_PORT=662
STATD_OUTGOING_PORT=2020

Edit the file /etc/modprobe.d/lockd.conf to set the TCP and UDP ports for NFS lock manager

options lockd nlm_tcpport=34323
options lockd nlm_udpport=56513

Restart NFS config and NFS server.

# systemctl restart nfs-config.service
# systemctl restart nfs-server.service
# systemctl enable nfs-server.service

Get the list of all ports that are used by rpcbind.

# rpcinfo -p
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper
    100005    1   udp    892  mountd
    100005    1   tcp    892  mountd
    100005    2   udp    892  mountd
    100005    2   tcp    892  mountd
    100005    3   udp    892  mountd
    100005    3   tcp    892  mountd
    100024    1   udp    662  status
    100024    1   tcp    662  status
    100003    3   tcp   2049  nfs
    100227    3   tcp   2049  nfs_acl
    100003    3   udp   2049  nfs
    100227    3   udp   2049  nfs_acl
    100021    1   udp  56513  nlockmgr
    100021    3   udp  56513  nlockmgr
    100021    1   tcp  34323  nlockmgr
    100021    3   tcp  34323  nlockmgr

All the above ports / services must be added to firewall.

# firewall-cmd --add-service=nfs --permanent
# firewall-cmd --add-service=rpc-bind --permanent
# firewall-cmd --add-port=892/tcp --permanent
# firewall-cmd --add-port=892/udp --permanent
# firewall-cmd --add-port=56513/udp --permanent
# firewall-cmd --add-port=34323/tcp --permanent
# firewall-cmd --reload

To verify the list of mount points, run:

# showmount -e 10.10.1.1
Export list for 10.10.1.1:
/srv/nfs/centos7-install 10.10.1.0/24

Install TFTP server

Login as root and install the tftp-server and xinetdpackages.

# yum install tftp-server
# yum install xinetd

Enable and start TFTP.

# systemctl enable xinetd
# systemctl enable tftp
# systemctl start tftp
# systemctl start xinetd

Add firewall rule to allow TFTP connections.

# firewall-cmd --add-service=tftp --permanent

Setup boot and Install images

Download the CentOS installation DVD ISO image.

Mount the ISO image and copy the all files to NFS share.

# mkdir /mnt/iso
# mount -t iso9660 -o loop CentOS-7-x86_64-DVD-1708 /mnt/iso/
# cp -pr /mnt/iso/* /srv/nfs/centos7-install/

Create the directory structure to store the images and PXE configuration.

# mkdir -p /var/lib/tftpboot/pxelinux/pxelinux.cfg
# mkdir -p /var/lib/tftpboot/pxelinux/images/centos7

Copy the kernel an initial ramdisk file from the NFS share to TFTP root folder.

# cp -p /srv/nfs/centos7-install/images/pxeboot/vmlinuz /var/lib/tftpboot/pxelinux/images/centos7/
# cp -p /srv/nfs/centos7-install/images/pxeboot/initrd.img /var/lib/tftpboot/pxelinux/images/centos7/

Extract the files vesamenu.c32 and pxelinux.0 from syslinux package and copy it to TFTP root folder.

# cp /srv/nfs/centos7-install/Packages/syslinux-4.05-13.el7.x86_64.rpm /tmp/
# cd /tmp
# rpm2cpio syslinux-4.05-13.el7.x86_64.rpm | cpio -dimv
# cp -p ./usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/pxelinux/
# cp -p ./usr/share/syslinux/vesamenu.c32 /var/lib/tftpboot/pxelinux/

Create a file named default inside the folder /var/lib/tftpboot/pxelinux/pxelinux.cfg with the below contents.

default vesamenu.c32
timeout 300

display boot.msg

MENU TITLE PXE Boot Menu

label linux
  menu label ^Install CentOS
  MENU DEFAULT
  kernel images/centos7/vmlinuz
  append initrd=images/centos7/initrd.img inst.stage2=nfs:10.10.1.1:/srv/nfs/centos7-install quiet

label rescue
  menu label ^Rescue Installed System
  kernel images/centos7/vmlinuz
  append initrd=images/centos7/initrd.img inst.stage2=nfs:10.10.1.1:/srv/nfs/centos7-install rescue quiet

label local
  menu label Boot From ^Local Drive
  localboot 0xffff

Save and exit the file.

Install and configure DHCP server

Login as root and install dhcp package.

# yum install dhcp

Edit /etc/dhcp/dhcpd.conf file to add network configuration and to configure different boot images for BIOS based and UEFI based clients. The following is a sample dhcpd.conf file for a server that is on 10.10.1.0/24 network.

option space pxelinux;
option pxelinux.magic code 208 = string;
option pxelinux.configfile code 209 = text;
option pxelinux.pathprefix code 210 = text;
option pxelinux.reboottime code 211 = unsigned integer 32;
option architecture-type code 93 = unsigned integer 16;

subnet 10.10.1.0 netmask 255.255.255.0 {
  option routers 10.10.1.254;
  range 10.10.1.2 10.10.1.253;

  class "pxeclients" {
    match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
    next-server 10.10.1.1;

    if option architecture-type = 00:07 {
      filename "uefi/shim.efi";
      } else {
      filename "pxelinux/pxelinux.0";
    }
  }
}

Enable and start DHCP.

# systemctl enable dhcpd
# systemctl start dhcpd

Testing Network Install Server

Your PXE server is now ready and you can start testing by running a network install on a client machine. The client machine used for testing must be on the same subnet as the server. Power on your client machine and change the boot sequence to boot from LAN. You should see a similar screen like below when the client obtains an IP address from the DHCP server and start to load the boot image.

PXE Boot
PXE Boot - DHCP, TFTP

You will then see the boot menu where you have three choices - Install CentOS, Start Rescue mode or boot from local disk.

PXE Boot Menu
PXE Boot - Menu

If you have selected Install CentOS option in the boot menu then you will see the first screen of the installer.

Network Install
Network Install

Also Read


Post a comment

Comments

saikrisnna | June 25, 2023 2:22 PM |

how to do the same using a docker image

abdou | January 6, 2023 2:12 PM |

i got this error when install dracut-initqueue[582]: mount:no medium found on /dev/sr0

Mike T | May 2, 2019 6:41 PM |

Great guide, thank you! I am curious how to set this up with an external DHCP server, what needs to be done in that case?

Biju M | December 2, 2017 12:01 AM |

very helpful..thanks very much...