By thomas, Tue, 04/07/2009 - 00:39
The Dynamic Host Configuration Protocol (DHCP) is used to automatically assign ip addresses to clients. Addresses can be assigned randomly or discretely by mac address. The ipaddress of our server is 192.168.0.1, we will configure dhcp to give out addresses to our clients in the range 192.168.16 - 192.168.31. A note of caution, if you are not on your own private network at this point, you need to contact your network administration before starting up your own dhcp server. The first step after installing the dhcp server is to create a new dhcpd.conf. We will keep it as simple as possible. /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;

subnet 192.168.0.0 netmask 255.255.255.0 {
	option routers 192.168.0.1;
	option subnet-mask 255.255.255.0;

	option domain-name	"example.org";
	option domain-name-servers	192.168.0.1;

	option time-offset	-18000;
	
	range dynamic-bootp 192.168.0.16 192.168.0.31;
	default-lease-time 21600;
	max-lease-time 43200;
}
On the version of dhcp installed on our system, the first line ddns-update-style interim; is required by the dhcp server. The subnet section specifies on which subnet we will be serving out addresses. The line which specifies the addresses to give out is range dynamic-bootp 192.168.0.16 192.168.0.31;. This specifies that the range of addresses from 16 to 31 will be given out dynamically (the first available address will be assigned to the next client, starting from the top of the range).

To test the dhcp server, we first start it and check the error log for any messages.

[root@server0 ~]# service dhcpd start; tail -f /var/log/messages
Starting dhcpd:                                            [  OK  ]
May 11 13:07:54 server0 dhcpd: Listening on LPF/eth0/00:11:22:33:44:55/192.168.0/24
May 11 13:07:54 server0 dhcpd: Sending on   LPF/eth0/00:11:22:33:44:55/192.168.0/24
May 11 13:07:54 server0 dhcpd: Sending on   Socket/fallback/fallback-net
If there were an error in our config file, dhcpd would fail to start and would output the reason to /var/log/messages.

Now for completeness we should allow dhcp requests through our firewall, dhcp listens on port 67 (which is known as bootp in /etc/services).

[root@server0 ~]# cd /etc/sysconfig
[root@server0 sysconfig]# iptables -I RH-Firewall-1-INPUT -p tcp --dport 67 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
[root@server0 sysconfig]# iptables -I RH-Firewall-1-INPUT -p udp --dport 67 -j ACCEPT
[root@server0 sysconfig]# iptables-save >iptables
We can now test the dhcp server on a client machine, we will use dhclient to request an address.
[root@client1 ~]# dhclient eth0
Internet Systems Consortium DHCP Client V3.0.5-RedHat
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/

Listening on LPF/eth0/00:11:22:33:44:5a
Sending on   LPF/eth0/00:11:22:33:44:5a
Sending on   Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 8
DHCPOFFER from 192.168.0.1
DHCPREQUEST on eth0 to 255.255.255.255 port 67
DHCPACK from 192.168.0.1
bound to 192.168.0.31 -- renewal in 10290 seconds.
Now that we have verified that our dhcp server is working, we will add a filename and next-server fields to our subnet definition. When machines boot via PXE they download the file specified by filename via tftp from the server specified by next-server*. If you do not run the dhcp server on the same server as your tftp, then you need to specify next-server accordingly. If for instance your tftp server is running on server2, you would put the following in the subnet definition:
next-server server2;
After adding these fields are added to our dhcpd.conf, we have our final dhcpd.conf
/etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;

subnet 192.168.0.0 netmask 255.255.255.0 {
	option routers 192.168.0.1;
	option subnet-mask 255.255.255.0;

	option domain-name	"example.org";
	option domain-name-servers	192.168.0.1;

	option time-offset	-18000;
	range dynamic-bootp 192.168.0.16 192.168.0.31;
	default-lease-time 21600;
	max-lease-time 43200;
	filename "linux-install/pxelinux.0";
	next-server 192.168.0.1;
}
Restart dhcpd to pickup the configuration change. You can now attempt a PXE boot of your client machine, it will fail at this point, but you can verify that pxelinux.0 is being loaded by the client and executed. We'll configure PXE in the next section.
* If you do not specify next-server in your dhcpd.conf, then the PXE client will attempt to broadcast for tftp on the network. To work in this mode, you will need a tftp server that responds to broadcast requests. At the time of writing, the in.tftpd package installed on our system will not do this.