Tuesday, September 9, 2014

[Quick Tips] : How to configure PXE boot server in Linux using Red Hat 6

PXE is an abbreviation for Preboot eXecution Environment which describes a client server standardized environment to boot from a network a software assembly on a client i.e. an Operating System. It is also pronounced as "pixie" and is mostly used to boot the client machine with a installation media stored on the PXE server using network interface.

In this article I will show you step by step guide to configure a PXE boot server using http and ftp in which you can use either one suiting your requirement.

I will be using Red Hat Linux 6 (32-bit) for my purpose

Server IP: 192.168.1.6

Pre-requisites

  • dhcp
  • tftp-server
  • syslinux
  • http/ftp (any one)
Install the required packages using yum
# yum -y install dhcp tftp-server syslinux http ftp vsftpd

Prepare installation media on PXE server

Next we need to copy all the files from the installation media(CD/DVD,ISO) to our PXE server.

You can also mount the media file on the PXE server in case you don't want to copy all the files but using that way you will only be able to configure your PXE server for one OS. For configuring multiple OS you will have to copy the OS files into separate directory for different OS.

In my case I want to confiure a PXE server to install CentOS 6.2

Let us create separate directory to save all the installation files
# mkdir -p /var/lib/tftpboot/images/centos/6/i386/
# mkdir -p /var/lib/tftpboot/images/centos/6/x86_64/

Next copy the installation files from the installation media.If you have iso images of the OS you can use WinSCP(on windows) to copy all the files. If the image is mounted on your Linux machine then you can copy using scp command.

To skip the lenghty process as of now we will just mount the dvd to relevant destination.
# mount /dev/sr0 /var/lib/tftpboot/images/centos/6/i386/
mount: block device /dev/sr0 is write-protected, mounting read-only

NOTE: In my case the cdrom is mounted on /dev/sr0 which can be different for you.

Configure HTTP/FTP server

You can use either of the mentioned servers for your purpose. But I will show you the configuration of all three so that you can choose any one as per your requirement.

HTTP server
# vi /etc/httpd/conf/httpd.conf
At the end of the file add the following lines
<VirtualHost 192.168.1.6:80>
    ServerAdmin root@test.example.com
    DocumentRoot /var/lib/tftpboot/images
    ServerName test.example.com
    ErrorLog logs/test.example.com-error_log
    CustomLog logs/test.example.com-access_log common
</VirtualHost>

<Directory /var/lib/tftpboot/images>
AllowOverride None
Options Indexes FollowSymlinks
Order allow,deny
Allow from all
</Directory>

Restart the httpd services
# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

Manually browse to the server ip and verify if you can see all the files.
http://192.168.1.6/centos/6/i386/

FTP server
# vi /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
anon_root=/var/lib/tftpboot/images

Manually browse to the server ip and verify if you can see all the files.
ftp://192.168.1.6/centos/6/i386/

Restart the services
# /etc/init.d/vsftpd restart
Shutting down vsftpd:                                      [  OK  ]
Starting vsftpd for vsftpd:                                [  OK  ]

Configure TFTP server

Once these packages are installed copy the below files from the specified directory to /var/lib/tftpboot
# cp /usr/share/syslinux/pxelinux.0     /var/lib/tftpboot/
# cp /usr/share/syslinux/chain.c32     /var/lib/tftpboot/
# cp /usr/share/syslinux/menu.c32     /var/lib/tftpboot/
# cp /usr/share/syslinux/memdisk     /var/lib/tftpboot/
# cp /usr/share/syslinux/mboot.c32     /var/lib/tftpboot/

Next we will create the configuration file required for tftp server
# mkdir /var/lib/tftpboot/pxelinux.cfg
Create a new file "default" under "/var/lib/tftpboot/pxelinux.cfg" and add the below entry

For HTTP server
# vi /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu.c32
PROMPT 0
TIMEOUT 100
ONTIMEOUT Local

MENU TITLE PXE Menu

MENU seperator
LABEL CentOS 6.2
KERNEL images/centos/6/i386/images/pxeboot/vmlinuz
APPEND initrd=images/centos/6/i386/images/pxeboot/initrd.img method=http://192.168.1.6/centos/6/i386 devfs=nomount

MENU seperator
LABEL Local
LOCALBOOT 0Here two things which you need to change

KERNEL - defines the location from where the PXELINUX bootloader will load
APPEND - defines the location for PXE initrd image file to load

For FTP server
There is not much change for ftp server just replace the below line in the above file
APPEND initrd=images/centos/6/i386/images/pxeboot/initrd.img method=ftp://192.168.1.6/centos/6/i386 devfs=nomount

Enable the tftp service in xinetd
# vi /etc/xinetd.d/tftp
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s 
/var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

Restart the relevant services
# /etc/init.d/xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]

Configure DHCP server

# vi /etc/dhcp/dhcpd.conf
option domain-name "example.com";
option domain-name-servers test.example.com;
default-lease-time 600;
max-lease-time 7200;
authoritative;

subnet 192.168.1.0 netmask 255.255.255.0 {
range dynamic-bootp 192.168.1.20 192.168.1.25;
option broadcast-address 192.168.1.255;
option routers 192.168.1.1;

  allow booting;
        allow bootp;

        next-server 
192.168.1.6;
        filename "pxelinux.0";
}

IMPORTANT NOTE: In your dhcp server make sure you add these lines
        next-server 192.168.1.6;
        filename "pxelinux.0";
as these define the address of your tftp server and the file to look for after getting the IP Address from dhcp server

Restart the relevant services
# service dhcpd restart
Shutting down dhcpd:                                       [  OK  ]
Starting dhcpd:                                            [  OK  ]

Make sure the services start after reboot
# chkconfig httpd on
# chkconfig xinetd on
# chkconfig dhcpd on

Iptables rules

For DHCP server
# iptables -I INPUT -m state --state NEW -p udp --dport 69 -j ACCEPT
For HTTP server
# iptables -I INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
For FTP server
# iptables -I INPUT -m state --state NEW -p tcp --dport 21 -j ACCEPT
You are all set to test your PXE server. Boot a machine and select the option of Network Boot from Bios. You should see the below screen

No comments: