How to Configure DHCP server on CentOS 7 / Ubuntu 18.04 / 16.04 / Debian 9

0

Dynamic Host Configuration Protocol is a network protocol used to dynamically distribute network configuration parameters, such as IP addresses, gateway, and DNS for network interfaces. DHCP Server assigns IP addresses automatically to all systems, so system admin need not assign IP addresses manually to client machines in the network. DHCP is the best option for bigger environments, holds thousands of systems.

Environment

Make sure to have only one DHCP server in your environment. If you have a router/switch which provides the functionality of DHCP, then you need to disable it.

  1. CentOS 7 64bit Minimal server (DHCP Server) OR Ubuntu 18.04 /16.04  / Debian 9 64bit (DHCP Server)
  2. CentOS 7 64bit Minimal server (DHCP Client) OR Ubuntu 18.04 / 16.04 Desktop (DHCP Client)

Note: You must a assign a static IP address to your DHCP server.

Install and configure DHCP server on CentOS 7

First, let us see how to install and configure DHCP server on CentOS 7 64bit. The should also work on CentOS 6.x and other older versions.

Install DHCP server and client using the below command.

# yum install dhcp

Once the packages are installed, copy the sample configuration file to /etc/dhcp directory.

# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf

Now, edit dhcpd.conf file.

# vi /etc/dhcp/dhcpd.conf

Define the subnet, range of IP addresses, domain and domain name servers as below:

[...]
# Configuring subnet and iprange
 subnet 192.168.12.0 netmask 255.255.255.0 {
 range 192.168.12.100 192.168.12.200;
# Specify DNS server ip and additional DNS server ip 
 option domain-name-servers 8.8.8.8, 8.8.4.4;
# Specify Domain Name
 option domain-name "itzgeek.local";
# Default Gateway
 option routers 192.168.12.2;
 option broadcast-address 192.168.12.255;
# Specify Default and Max lease time
 default-lease-time 600;
 max-lease-time 7200;
 }
[...]

If you want to assign a fixed IP address to your client, you have to enter it’s MAC id and the IP address in the following stanza. For example, to assign a fixed IP address 192.168.12.110 to the client, stanza will look like below.

[...]
host mywindows-client {
 hardware ethernet 00:0C:29:05:A7:CB; 
 fixed-address 192.168.12.110; 
} 
[...]

Now, start the dhcpd service and make it start automatically on system reboot.

# systemctl restart dhcpd
# systemctl enable dhcpd

If you face any issues in restarting the DHCP service, then consider updating the SELinux context or disable SELinux permanently on CentOS 7 / RHEL 7.

/sbin/restorecon -v /etc/dhcp/dhcpd.conf

That’s it. Now, skip to ‘Configure DHCP Clients’ section and configure your clients to get IP addresses automatically from the DHCP server.

Install and configure DHCP server on Ubuntu 18.04 / 16.04 / Debian 9

First, let us see how to install and configure DHCP server on Ubuntu 14.04. The should also work on Ubuntu 15.04 / 14.10 / 13.04 / 13.10 and other older versions.

Install DHCP server and client using the below command.

$ sudo apt-get install isc-dhcp-server

Once the packages are installed, we have to assign on what interfaces should the DHCP server (dhcpd) serve DHCP requests. In our case, have only one Interface on my system (eth0), so we will assign eth0.

Edit file /etc/default/isc-dhcp-server.

$ sudo nano /etc/default/isc-dhcp-server

Mention eth0 in the file.

# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="eth0"

Now, edit dhcpd.conf file,

$ sudo nano /etc/dhcp/dhcpd.conf

Do the changes as shown below.

Define the subnet, range of IP addresses, domain and domain name servers as below:

[...]
# Configuring subnet and iprange
 subnet 192.168.12.0 netmask 255.255.255.0 {
 range 192.168.12.100 192.168.12.200;
# Specify DNS server ip and additional DNS server ip 
 option domain-name-servers 8.8.8.8, 8.8.4.4;
# Specify Domain Name
 option domain-name "itzgeek.local";
# Default Gateway
 option routers 192.168.12.2;
 option broadcast-address 192.168.12.255;
# Specify Default and Max lease time
 default-lease-time 600;
 max-lease-time 7200;
 }
[...]

If you want to assign a fixed IP address to your client, you have to enter it’s MAC id and the IP address in the following stanza. For example, to assign a fixed IP address 192.168.12.110 to the client, stanza will look like below.

[...]
host mywindows-client {
 hardware ethernet 00:0C:29:05:A7:CB; 
 fixed-address 192.168.12.110; 
} 
[...]

Now, start the dhcpd service and make it start automatically on system reboot.

# sudo service isc-dhcp-server restart

Configure DHCP Clients

Now, you need to configure client machines to get IP address automatically from DHCP server. You can go through below articles.

That’s All.

Further Reading

You might also like

How to Configure DHCP server on CentOS 7 / Ubuntu 18.04 / 16.04 / Debian 9

0