- How to Setup DHCP Server and Client on CentOS and Ubuntu
- Installing DHCP Server in CentOS and Ubuntu
- Configuring DHCP Server in CentOS and Ubuntu
- Configuring DHCP Clients
- DHCP Client Setup on CentOS
- DHCP Client Setup on Ubuntu
- 10 dhclient (DHCP Client) command examples in Linux
- 10 dhclient (DHCP Client) command examples in Linux
- Example 1: How to Check dhclient command version
- Example 2: How to Get a DHCP IP on lease
- Example 3: How to use verbose mode(-v)
- Example 4: How to release lease IP from an Interface
- Example 5: How to obtain IPV6 address using dhclient command
- Example 6: How to stop the running DHCP client without releasing the current lease
- Example 7: How to Change UDP Listening Port
- Example 8: How to Specify Server IP Address
- Example 9: How to Check all the options available with dhclient tool
- Example 10: How to Check Man page of dhclient tool
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.
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
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
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
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.
For more information, see the dhcpd and dhcpd.conf man pages.
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.
10 dhclient (DHCP Client) command examples in Linux
In this article, we will look into 10 dhclient command examples in Linux. dhclient is a free and open source DHCP client tool used for configuring one or more network interfaces using the Dynamic Host Configuration Protocol, BOOTP protocol, or if these protocols fail, by statically assigning an address. Using dhclient tool, a DHCP client system can request an IP address from a Central DHCP Server which which maintains a list of IP addresses which may be assigned on one or more subnets. Here we will see some of the real time examples of dhclient command in below section.
10 dhclient (DHCP Client) command examples in Linux
Example 1: How to Check dhclient command version
To check the current version of dhclient tool, you need to run dhclient —version command as shown below.
Example 2: How to Get a DHCP IP on lease
You can simply run sudo dhclient command to request a DHCP IP on lease from the available DHCP Server.
Please note that if you already have a lease IP generated then running sudo dhclient command will show you RTNETLINK answers: File exists on the output.
cyberithub@ubuntu:~$ sudo dhclient
Example 3: How to use verbose mode(-v)
If you want to use verbose mode then you need to use -v option with dhclient command as shown below.
Please note that you need to either have sudo or root access to run dhclient command. Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.
cyberithub@ubuntu:~$ sudo dhclient -v Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s8/08:00:27:5f:48:3a Sending on LPF/enp0s8/08:00:27:5f:48:3a Listening on LPF/enp0s3/08:00:27:16:c6:22 Sending on LPF/enp0s3/08:00:27:16:c6:22 Sending on Socket/fallback DHCPREQUEST for 192.168.0.104 on enp0s8 to 255.255.255.255 port 67 (xid=0x4dcd9939) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 67 (xid=0x606bc4b3) DHCPACK of 192.168.0.104 from 192.168.0.1 (xid=0x3999cd4d) .
Example 4: How to release lease IP from an Interface
If you need to release the leased IP and stop the running DHCP client as previously recorded in the PID file then you need to use -r option. In this example we are releasing IP from enp0s3 network interface using sudo dhclient -r -v enp0s3 command as shown below.
Example 5: How to obtain IPV6 address using dhclient command
If you want to use the DHCPv6 protocol to obtain whatever IPv6 addresses are available along with configuration parameters then you need to use -6 option with dhclient command as shown below.
cyberithub@ubuntu:~$ sudo dhclient -6 -v Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on Socket/enp0s3 Sending on Socket/enp0s3 PRC: Soliciting for leases (INIT). XMT: Forming Solicit, 0 ms elapsed. XMT: X-- IA_NA 27:16:c6:22 XMT: | X-- Request renew in +3600 XMT: | X-- Request rebind in +5400 XMT: Solicit on enp0s3, interval 1070ms. .
Example 6: How to stop the running DHCP client without releasing the current lease
If you need to stop the running DHCP client without releasing the current lease IP then you need to use -x option with dhclient command as shown below.
Example 7: How to Change UDP Listening Port
If you want to change the UDP port number on which the DHCP client should listen and transmit to then you need to use -p option with port number specified as an argument. In this example, we are using UDP Port 556 on which dhclient would listen and transmit to using sudo dhclient -v -p 556 enp0s3 command as shown below.
cyberithub@ubuntu:~$ sudo dhclient -v -p 556 enp0s3 binding to user-specified port 556 Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s3/08:00:27:16:c6:22 Sending on LPF/enp0s3/08:00:27:16:c6:22 Sending on Socket/fallback DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) DHCPREQUEST for 192.168.0.108 on enp0s3 to 255.255.255.255 port 555 (xid=0x7513614a) .
Example 8: How to Specify Server IP Address
If you want to change the DHCP Server address from where the IP address needs to be leased out then you need to use -s option and specify the destination server IP address or its fully qualified domain name as shown below. For example, here we are using destination server address as 192.168.0.106 .
cyberithub@ubuntu:~$ sudo dhclient -v -s 192.168.0.106 enp0s3 Internet Systems Consortium DHCP Client 4.4.1 Copyright 2004-2018 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/enp0s3/08:00:27:16:c6:22 Sending on LPF/enp0s3/08:00:27:16:c6:22 Sending on Socket/fallback DHCPREQUEST for 192.168.0.108 on enp0s3 to 192.168.0.106 port 67 (xid=0x6d4db49) DHCPREQUEST for 192.168.0.108 on enp0s3 to 192.168.0.106 port 67 (xid=0x6d4db49) DHCPREQUEST for 192.168.0.108 on enp0s3 to 192.168.0.106 port 67 (xid=0x6d4db49) .
Example 9: How to Check all the options available with dhclient tool
To check all the options available with dhclient command you can use dhclient -h or dhclient —help as shown below.
cyberithub@ubuntu:~$ dhclient -h Usage: dhclient [-4|-6] [-SNTPRI1dvrxi] [-nw] [-p ] [-D LL|LLT] [—dad-wait-time ] [—prefix-len-hint ] [—decline-wait-time ] [—address-prefix-len ] [-s server-addr] [-cf config-file] [-df duid-file] [-lf lease-file] [-pf pid-file] [—no-pid] [-e VAR=val] [-sf script-file] [interface]* dhclient
Example 10: How to Check Man page of dhclient tool
To check the man page of dhclient tool, you need to use man dhclient command as shown below.
cyberithub@ubuntu:~$ man dhclient dhclient(8) System Manager's Manual dhclient(8) NAME dhclient - Dynamic Host Configuration Protocol Client SYNOPSIS dhclient [ -4 | -6 ] [ -S ] [ -N [ -N. ] ] [ -T [ -T. ] ] [ -P [ -P. ] ] -R ] [ -i ] [ -I ] [ -4o6 port ] [ -D LL|LLT ] [ -p port- number ] [ -d ] [ -df duid-lease-file ] [ -e VAR=value ] [ -q ] [ -1 ] [ -r | -x ] [ -lf lease-file ] [ -pf pid-file ] [ --no-pid ] [ -cf con‐ fig-file ] [ -sf script-file ] [ -s server-addr ] [ -g relay ] [ -n ] [ -nw ] [ -w ] [ --dad-wait-time seconds ] [ --prefix-len-hint length ] [ --decline-wait-time seconds ] [ -v ] [ --version ] [ if0 [ . ifN ] ] DESCRIPTION The Internet Systems Consortium DHCP Client, dhclient, provides a means for configuring one or more network interfaces using the Dynamic Host Configuration Protocol, BOOTP protocol, or if these protocols fail, by statically assigning an address.