Dynamic Host Configuration Protocol (DHCP) is a network protocol that automatically assigns TCP/IP information to client machines. Each DHCP client connects to the centrally located DHCP server, which returns the network configuration (including the IP address, gateway, and DNS servers) of that client.
How DHCP works ?
01. Installing DHCP :
yum -y install dhcp
The configuration file is /etc/dhcp/dhcpd.conf
and it may be empty.
The sample configuration file can be found at /usr/share/doc/dhcp-<version>/dhcpd.conf.sample
. You should use this file to help you configure /etc/dhcp/dhcpd.conf
.
DHCP also uses the file /var/lib/dhcpd/dhcpd.leases
to store the client lease database.
02. Run DHCP service :
systemctl enable dhcpd systemctl start dhcpd systemctl status dhcpd
It may NOT work unless we edit the configuration file correctly , Don’t worry , just continue .
03. Open Firewall Port :
firewall-cmd --permanent --add-service=dhcp firewall-cmd --reload
04. Define a subnet :
Edit the configuration file to provide DHCP service for specific IP subnet or range, for example :
To define a subnet of 10.1.1.0 with netmask of 255.255.255.0 , append the following :
subnet 10.1.1.0 netmask 255.255.255.0 {
}
Then you should define the allowed range from this subnet (example from 50 to 150 IP) :
subnet 10.1.1.0 netmask 255.255.255.0 {
range 10.1.1.50 10.1.1.150 ;
}
You can add options as you need,
If you add option inside subnet definition , it will affect only this subnet.
If you define options outside subnet definitions , it will affect all subnet definitions.
the syntax is :
option option-name argument;
To define gateway or router IP :
option routers 10.1.1.1;
To define DNS servers :
option domain-name-servers 8.8.8.8;
To define domain search :
option domain-search “example.com”;
You may search for more options as you need, for example to setup a PXE server , you will setup DHCP with more options, read this
Any configuration line inside a subnet definition must end with ” ; “ .
Example :
vim /etc/dhcp/dhcpd.conf
subnet 192.168.1.0 netmask 255.255.255.0 { option routers 192.168.1.1; option subnet-mask 255.255.255.0; option domain-search "example.com"; option domain-name-servers 192.168.1.1; option time-offset -18000; # Eastern Standard Time range 192.168.1.10 192.168.1.100; }
Now Restart dhcpd service , and test it.
systemctl restart dhcpd
For more examples:
vim /usr/share/doc/dhcp*/dhcpd.conf.example
where * represent the package version.
That is it, i hope it was simple, thanks for joining me.
Enjoy !.