How to Setup DHCP Server and Client on CentOS and Ubuntu

DHCP (short for Dynamic Host Configuration Protocol) is a client/server protocol that enables a server to automatically assign an IP address and other related configuration parameters (such as the subnet mask and default gateway) to a client on a network.

DHCP is important because it prevents a system or network administrator from manually configuring IP addresses for new computers added to the network or computers that are moved from one subnet to another.

The IP address assigned by a DHCP server to a DHCP client is on a “lease”, the lease time normally varies depending on how long a client computer is likely to require the connection or the DHCP configuration.

In this article, we will explain how to configure a DHCP server in CentOS and Ubuntu Linux distributions to assign IP address automatically to a client machine.

Installing DHCP Server in CentOS and Ubuntu

The DCHP server package is available in the official repositories of mainstream Linux distributions, installing is quite easy, simply run the following command.

# yum install dhcp		        #CentOS
$ sudo apt install isc-dhcp-server	#Ubuntu

Once the installation is complete, configure the interface on which you want the DHCP daemon to serve requests in the configuration file /etc/default/isc-dhcp-server or /etc/sysconfig/dhcpd.

# vim /etc/sysconfig/dhcpd		 #CentOS
$ sudo vim /etc/default/isc-dhcp-server	 #Ubuntu

For example, if you want the DHCPD daemon to listen on eth0, set it using the following directive.

DHCPDARGS=”eth0”

Save the file and exit.

Configuring DHCP Server in CentOS and Ubuntu

The main DHCP configuration file is located at /etc/dhcp/dhcpd.conf, which should contain settings of what to do, where to do something and all network parameters to provide to the clients.

This file basically consists of a list of statements grouped into two broad categories:

  • Global parameters: specify how to carry out a task, whether to carry out a task, or what network configuration parameters to provide to the DHCP client.
  • Declarations: define the network topology, state a clients is in, offer addresses for the clients, or apply a group of parameters to a group of declarations.

Now, open and edit the configuration file to configure your DHCP server.

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

------------ On Ubuntu ------------
$ sudo vim /etc/dhcp/dhcpd.conf				

Start by defining the global parameters which are common to all supported networks, at the top of the file. They will apply to all the declarations:

option domain-name "tecmint.lan";
option domain-name-servers ns1.tecmint.lan, ns2.tecmint.lan;
default-lease-time 3600; 
max-lease-time 7200;
authoritative;

Next, you need to define a sub-network for an internal subnet i.e 192.168.1.0/24 as shown.

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            "tecmint.lan";
        option domain-name-servers      192.168.1.1;
        range   192.168.10.10   192.168.10.100;
        range   192.168.10.110   192.168.10.200;
}

Note that hosts which require special configuration options can be listed in host statements (see the dhcpd.conf man page).

Now that you have configured your DHCP server daemon, you need to start the service for the mean time and enable it to start automatically from the next system boot, and check if its up and running using following commands.

------------ On CentOS ------------ 
# systemctl start dhcpd
# systemctl enable dhcpd
# systemctl enable dhcpd

------------ On Ubuntu ------------
$ sudo systemctl start isc-dhcp-server
$ sudo systemctl enable isc-dhcp-server
$ sudo systemctl enable isc-dhcp-server

Next, permit requests to the DHCP daemon on Firewall, which listens on port 67/UDP, by running.

------------ On CentOS ------------ 
# firewall-cmd --zone=public --permanent --add-service=dhcp
# firewall-cmd --reload 

#------------ On Ubuntu ------------
$ sudo ufw allow 67/udp
$ sudo ufw reload

Configuring DHCP Clients

Finally, you need to test if the DHCP server is working fine. Logon to a few client machines on the network and configure them to automatically receive IP addresses from the server.

Modify the appropriate configuration file for the interface on which the clients will auto-receive IP addresses.

DHCP Client Setup on CentOS

On CentOS, the interface config files ate located at /etc/sysconfig/network-scripts/.

# vim /etc/sysconfig/network-scripts/ifcfg-eth0

Add the options below:

DEVICE=eth0
BOOTPROTO=dhcp
TYPE=Ethernet
ONBOOT=yes

Save the file and restart network service (or reboot the system).

# systemctl restart network

DHCP Client Setup on Ubuntu

On Ubuntu 16.04, you can configure all interface in the config file /etc/network/interfaces.

   
$ sudo vi /etc/network/interfaces

Add these lines in it:

auto  eth0
iface eth0 inet dhcp

Save the file and restart network services (or reboot the system).

$ sudo systemctl restart networking

On Ubuntu 18.04, networking is controlled by the Netplan program. You need to edit the appropriate file under the directory /etc/netplan/, for example.

$ sudo vim /etc/netplan/01-netcfg.yaml 

Then enable dhcp4 under a specific interface for example under ethernets, ens0, and comment out static IP related configs:

network:
  version: 2
  renderer: networkd
  ethernets:
    ens0:
      dhcp4: yes

Save the changes and run the following command to effect the changes.

$ sudo netplan apply 

For more information, see the dhcpd and dhcpd.conf man pages.

$ man dhcpd
$ man dhcpd.conf

In this article, we have explained how to configure a DHCP server in CentOS and Ubuntu Linux distributions. If you need more clarification on any point, you can ask a question via the feedback form below, or simply share your comments with us.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

11 Comments

Leave a Reply
  1. No subnet declaration for em1.
    ** Ignoring requests on em1.

    If this is not what you want, please write a subnet declaration in your dhcpd.conf file for the network segment to which interface em1 is attached.

    Reply
  2. There is an error in this code:

    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            "tecmint.lan";
            option domain-name-servers      192.168.1.1;
            range   192.168.10.10   192.168.10.100;
            range   192.168.10.110   192.168.10.200;
    }
    

    Must be:

    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            "tecmint.lan";
            option domain-name-servers      192.168.1.1;
            range   192.168.1.10   192.168.1.100;
            range   192.168.1.110   192.168.1.200;
    }
    
    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.