Problem
Configure a DHCP server on a host machine running Linux.
tl;dr (CentOS)
Install the DHCP service:
yum install dhcp -y
Edit the configuration file:
nano /etc/dhcp/dhcpd.conf
Add the following lines, assuming you want a single subnet at 172.16.1.0 /24:
subnet 172.16.1.0 netmask 255.255.255.0 {
option routers 172.16.1.1;
option subnet-mask 255.255.255.0;
range 172.16.1.2 172.16.1.254;
}
Start and autostart the DHCP server:
service dhcpd start chkconfig dhcp on
Solution
Install via the following commands depending on your flavour of Linux:
RHEL/CentOS:
yum install dhcp -y
Ubuntu earlier than 12.04 or Debian older than Wheezy (7.0):
sudo apt-get install dhcp3-server
Ubuntu 12.04 and later or Debian Wheezy and later:
sudo apt-get install isc-dhcp-serverBasic/Sample Configuration
In its simplest form, DHCP can be set up by editing the DHCP configuration file and appending several lines. The configuration file is in a slightly different location depending on whether you installed DHCP3 Server (Debian 6.x or earlier and Ubuntu 12.04 or earlier), or DHCP/ISC-DHCP (CentOS/RHEL, Ubuntu later than 12.04, and Debian 7.0 or later).
ISC-DHCP or DHCPd:
nano /etc/dhcp/dhcpd.conf
DHCP3:
nano /etc/dhcp3/dhcpd.conf
Then, append the following lines, changing network values to suit your needs:
subnet 172.16.1.0 netmask 255.255.255.0 {
option routers 172.16.1.1;
option subnet-mask 255.255.255.0;
range 172.16.1.100 172.16.1.254;
}
Where:
option-routers is the default gateway for the DHCP subnet.
option subnet-mask is the subnet mask. You can configure multiple subnets by adding the above block for each required subnet and changing values as needed.
range is the IP range that can be assigned to hosts on this subnet, in this case .100 to .254
This config is extremely basic, as it does not take into account DNS, secondary DHCP servers, multiple VLANs or lease timeouts. It merely allows the hosts to receive DHCP leases with default settings and use a router/gateway at 172.16.1.1.
Start the DHCP serverTo start your DHCP server and configure it to autostart on boot. Pick the command based on what version of DHCP you have installed:
ISC-DHCP:
service isc-dhcp-server start
chkconfig isc-dhcp-server on
DHCP3:
service dhcp3-server start
chkconfig dhcp3-server on
DHCPD (CentOS/RHEL):
service dhcpd start
chkconfig dhcpd on
Sample Configuration File
CentOS/RHEL DHCP server also comes with a well-detailed sample DHCP configuration file that lists a large number of configurations to fit possible network designs. It is located in /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample, although the directory may change depending on the version of your DHCP server.
If the above directory does not work, run a find command to locate the file manually:
find / -name dhcpd.conf.sample
You can read it via cat or open it to copy and paste segments via a text editor such as nano.
cat /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample nano /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
- Updated