The Dynamic Host Configuration Protocol (DHCP) allows you to specify network parameters on a server and have client computers query the server for their information such as IP, netmask, gateway, DNS, etc. In addition to not having to statically assign network information to numerous clients, you also do not need to specify the IP of the DHCP server as this discovery is done via broadcast packets; the caveat to this is that you must have one DHCP server per broadcast domain. In case it's not blatantly obvious, the power of DHCP is that if anything changes on your network such as the IP of a DNS server, you only need to edit one configuration file even if you have hundreds of clients.
The DHCP server I am using is called 'dhcpd' (oddly enough) and my current version is dhcp-3.0pl2-i386-1. This howto is not meant to be in-depth but rather just a general overview of some of the common features for the dhcpd server. If you need to do more esoteric configurations please
man dhcpd.conf for detailed information.
The following is a common
dhcpd.conf file. Below I will dissect this file and explain what each line does. Keeping with the convention of my other Linux Answers, all computer-specific information will be highlighted in
blue and will most likely need to be changed.
Code:
ddns-update-style none;
subnet 192.168.1.0 netmask 255.255.255.0
{
range 192.168.1.100 192.168.1.200;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
option domain-name-servers 123.123.123.10, 123.123.123.20;
option routers 192.168.1.1;
host slackbox
{
hardware ethernet 00:50:AB:AB:AB:AB;
fixed-address 192.168.1.7;
}
host winbox
{
hardware ethernet 00:06:CD:CD:CD:CD;
fixed-address 192.168.1.8;
}
}
The first thing we need to do is set a Dynamic DNS update style. Since DynDNS is beyond the scope of this howto, I am going to set the style to
none but if this is something you want to do, then the manpages have tons of info on it.
Code:
ddns-update-style none;
Next we must specify what
subnet and
netmask we will be working on. Note that you can have many subnet configurations within the single dhcpd.conf file. Each subnet group is bound together by curly braces
{ }
Code:
subnet 192.168.1.0 netmask 255.255.255.0
Note that every command from here on will only pertain to the subnet specified above. This will be true until we reach the closing curly brace
} as noted above.
Now we will specify what range of IP addresses we want to be made available for clients using DHCP. This option is very handy when used in conjunction with a firewall because you know exactly what IP addresses came from a client using DHCP and you can exercise restrictions upon them as necessary.
Code:
range 192.168.1.100 192.168.1.200;
This next line is going to look a bit redundant because we are setting the netmask again even though we set it in the subnet declaration above, but it's recommended in the manpages so we are going to do it.
Code:
option subnet-mask 255.255.255.0;
Next we specify the broadcast address for our subnet. This address
always ends in 255 for a subnet mask of 255.255.255.0
Code:
option broadcast-address 192.168.1.255;
We will definitely want to tell our clients what servers to use for DNS in order to resolve hostnames to IP addresses
Code:
option domain-name-servers 123.123.123.10, 123.123.123.20;
The next option tells our clients what IP address to use for their
gateway. This IP address generally ends in
.1 but does not have to. The box with this IP should be configured as a router and be able to forward packets accordingly.
Code:
option routers 192.168.1.1;
If you wanted you could stop here but I thought I would show you a cool little feature that I like to use. Even though DHCP gives out IP address dynamically, it also has the ability to reserve an IP address for a certain computer. In this sense it's almost as if the client computer has a static IP even though it uses DHCP to get it. This is useful if you want to be able to put entries in your
/etc/hosts file and not have to worry about the entry becoming invalid over time.
The first thing we must do is to specify a name for the computer as a helpful identifier
Note that similarly to the subnet grouping, we are now starting a sub-group as seen by the addition of the curly braces. This allows us to have multiple host definitions within one subnet group.
This next line is what allows us to uniquely identify one computer from another. The
hardware ethernet address is the same as the
MAC address. This information can be found by running the command
ifconfig <interface> | grep HWaddr on a client computer for linux and
ipconfig /all for a client computer running windows.
Code:
hardware ethernet 00:50:AB:AB:AB:AB;
And finally this next line tells the dhcpd server what IP address you always want to be assigned to this computer. Note that I intentionally make all IP's assigned this way
outside of the DHCP range we specified earlier. This is not a must as the dhcp server is smart enough to not give out two IP's simultaneously but it helps in being able to quickly recognize which clients used this feature.
Code:
fixed-address 192.168.1.7;
This concludes this DHCP howto. As an added bonus I have included the init script I made for my Slackware box, however this script should work on many other distros. Please make sure you edit the 4 configuration options between the hashmark lines accordingly.
Code:
#!/bin/sh
#
# /etc/rc.d/rc.dhcpd
#
# Start/stop/restart the DHCP daemon.
#
# To make dhcpd start automatically at boot, make this
# file executable: chmod 755 /etc/rc.d/rc.dhcpd
#
#############################################
CONFIGFILE="/etc/dhcpd.conf"
LEASEFILE="/var/state/dhcp/dhcpd.leases"
INTERFACES="eth1"
OPTIONS="-q"
#############################################
dhcpd_start() {
if [ -x /usr/sbin/dhcpd -a -r $CONFIGFILE ]; then
echo "Starting DHCPD..."
/usr/sbin/dhcpd -cf $CONFIGFILE -lf $LEASEFILE $OPTIONS $INTERFACES
# /usr/sbin/dhcpd -q $INTERFACES
fi
}
dhcpd_stop() {
killall dhcpd
}
dhcpd_restart() {
dhcpd_stop
sleep 2
dhcpd_start
}
case "$1" in
'start')
dhcpd_start
;;
'stop')
dhcpd_stop
;;
'restart')
dhcpd_restart
;;
*)
# Default is "start", for backwards compatibility with previous
# Slackware versions. This may change to a 'usage' error someday.
dhcpd_start
esac
To start up your brand new dhcpd server simply run the command
Code:
/etc/rc.d/rc.dhcpd start
As always, questions and comments are welcome. Enjoy!