Skip to main content

Setting up a PXE-Server with CentOS 7

Setting up a PXE server can be very usefull in home and enterprise networks, whether you want to provision a bunch of machines or just be lazy when it comes to OS installations (kickstart is your friend).
For this setup, I'm using a fully up-to-date CentOS 7 installation.

For making PXE possible, we need a few services which we can install by running the following command:
 [archy@pxe-server ~]$ sudo yum -y install dhcp syslinux tftp-server nfs-utils  
I think most of them are pretty self explanatory. Syslinux is a bunch of bootloaders for network-booting. When everything is installed, let's dig in to the configuration.

First up, the dhcp-config, I'll configure a simple subnet.
 [archy@pxe-server ~]$ sudo vim /etc/dhcp/dhcpd.conf 
 subnet 172.31.10.0 netmask 255.255.255.0 {  
     range 172.31.10.100 172.31.10.200;  
     option subnet-mask 255.255.255.0;  
     # my 2 ipa-servers as dns ...  
     option domain-name-servers 172.31.10.250, 172.31.10.251;  
     # ... and ntp-servers  
     option ntp-servers 172.31.10.250, 172.31.10.251;  
     option domain-name "archyslife.lan";  
     option routers 172.31.10.254;  
     option broadcast-address 172.31.10.255;  
     # this is PXE specific  
     filename "pxelinux.0";  
     next-server "172.31.10.10";  
 }  
The PXE specific parameters tell the client to look for the 'pxelinux.0'-file on the server '172.31.10.10'. We will get to syslinux later, but for now, we need to download a installation.iso from the centos-mirrors (or use an existing one) to copy the necessary files.
 [archy@pxe-server ~]$ wget http://merlin.fit.vutbr.cz/mirrors/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-1708.iso  
and mount the iso temporary to the /mnt folder to copy the necessary files to the tftpboot-directory.
 [archy@pxe-server ~]$ sudo mount -o loop CentOS-7-x86_64-Everything-1708.iso /mnt  
 [archy@pxe-server ~]$ sudo mkdir /var/lib/tftpboot/centos7  
 [archy@pxe-server ~]$ sudo cp /mnt/images/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7  
With that done, we need to create a local repo of the installation disk. I'll cover the installation using nfs, but ftp:// and http:// are also possible to use.
 [archy@pxe-server ~]$ sudo mkdir --parents /images/CentOS-1708  
 [archy@pxe-server ~]$ sudo cp -r /mnt/* /images/CentOS-1708  
 [archy@pxe-server ~]$ sudo chmod -R 755 /images/CentOS-1708  
 [archy@pxe-server ~]$ sudo umount /mnt  
 [archy@pxe-server ~]$ sudo vim /etc/exports  
and add the following content
 /images/CentOS-1708  172.31.10.0/24(ro,secure,async,no_subtree_check)  
We already installed the syslinux-bootloaders. They are located in the '/usr/share/syslinux' directory. We will copy all of its content to our tftp root directory, which is by default '/var/lib/tftpboot'.
 [archy@pxe-server ~]$ sudo cp -r /usr/share/syslinux/* /var/lib/tftpboot  
With the bootloaders copied, the dhcp-server set up and the images copied, there is the pxe-menu still left to work on. Create the pxelinux.cfg folder and the default file which will store our configuration.
 [archy@pxe-server ~]$ sudo mkdir /var/lib/tftpboot/pxelinux.cfg/  
 [archy@pxe-server ~]$ sudo touch /var/lib/tftpboot/pxelinux.cfg/default  
And this is what I will add to my configuration file
 default menu.c32   
 prompt 0   
 timeout 300   
 ONTIMEOUT   
 menu title ######## PXE Boot Menu ########  
 label 1   
 menu label ^1) Boot from local drive   
 localboot 0x00  
 label 2  
 menu label ^2) Install CentOS 7 using local Repo with Kickstart   
 kernel centos7/vmlinuz   
 append initrd=centos7/initrd.img ks=http://web01.archyslife.lan/ks-minimal.cfg inst.stage2=nfs:172.31.10.10:/images/CentOS-1708   
 label 3  
 menu label ^3) Install CentOS 7 using local Repo without Kickstart   
 kernel centos7/vmlinuz   
 append initrd=centos7/initrd.img inst.stage2=nfs:172.31.10.10:/images/CentOS-1708   
This gives us three options in the boot menu, first being the boot from the local hdd, second being the pxe-installation using a kickstart file and third being the pxe-installation not using a kickstart file. In the second configuration I will fetch my kickstart-file from my internal webserver and use the nfs-share to fetch the necessary files to start the installation automatically.

All that is left to do now, is restart and enable the services and configure the firewall. There is no SELinux configuration necessary for this configuration. You would have to change the contexts of the tftpboot-folder if you would use non-standard folders.
 [archy@pxe-server ~]$ sudo systemctl restart dhcpd.service  
 [archy@pxe-server ~]$ sudo systemctl restart nfs.service  
 [archy@pxe-server ~]$ sudo systemctl enable dhcpd.service  
 [archy@pxe-server ~]$ sudo systemctl enable nfs.service  
 [archy@pxe-server ~]$ sudo systemctl enable tftp.serice  
 [archy@pxe-server ~]$ sudo firewall-cmd --add-service={dhcp,nfs,tftp} --permanent  
 [archy@pxe-server ~]$ sudo firewall-cmd --add-port=20048/tcp --permanent  
 [archy@pxe-server ~]$ sudo firewall-cmd --reload  
That's it from the server site. Feel free to spin up any VM and test it out yourself.
For debugging, have a look at the logs created. Some useful commands are listed below.
 journalctl -xn dhcpd.service  
 journalctl -xn nfs.service  
 journalctl -xn tftp.service  
 tail -f /var/log/messages  
Feel free to comment and / or sugguest a topic.

Comments