LFCS – Setting up a DHCP Server

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
339
Reaction score
369
Credits
11,691
Most people may not deal directly with a Dynamic Host Control Protocol (DHCP) Server. Everyone will connect to the internet at times and their device is assigned an IP Address. A DHCP Server assigns the IP Addresses to devices connecting to the specific network.

We are going to set up a DHCP Server on Server 1, and Server 2 will be get assigned its IP Address from Server 1.

From a DHCP Server, we can assign an IP Address, Subnet Mask, Gateway, DNS Server, etc. to the devices connecting to that network. The network can be a wired or wireless network.

Disable DHCP on VirtualBox

For those of you using VirtualBox to emulate your servers, you will need to stop the DHCP Service on VirtualBox so we can use our own server to manage the service.

These steps are the same for Both Ubuntu and CentOS.

For newer versions of VirtualBox, select 'File', 'Tools' and then 'Network Manager' to open the manager you need to set up a new Network Adapter for use by Virtual Machines.

In the new window that opened, select the 'Host-Only Networks' tab. Then select the 'Create' icon to create a new Host-Only Adapter.

My new adapter is called 'VirtualBox Host-Only Ethernet Adapter #3'. At the bottom of the screen should be a listing of the IP Addresses that are assigned by the DHCP Server for this adapter. Click on the tab for 'DHCP Server' and make sure the checkbox for 'Enable Server' is unchecked.

Open the 'Settings' for Server1 and go to the 'Network' settings. Find the Adapter that is listed as a 'Host-Only Adapter'. For the 'Name', drop-down the list and select the adapter you created previously. In my case, it is 'VirtualBox Host-Only Ethernet Adapter #3'. Do this for all your Servers you are using for these LFCS articles.

Static Address on Server1

A DHCP Server must keep the IP Address. Since the DHCP Server is assigning addresses, the clients need to know the IP Address of the server to renew the IP Address Lease. To renew the address, it must contact the DHCP Server. If the DHCP Server IP Address has changed, it cannot contact the DHCP Server for address renewal.

NOTE: The connection for 'enp0s8' most likely will fail at startup since it cannot find a DHCP Server. Disregard this since we are setting it up as static.

If you are performing this on VirtualBox, start up Server1. When the system starts, open a terminal. Perform the command 'ip a' and you should see that the adapter named 'enp0s8' does not have an IP Address assigned to it since we removed its DHCP Server.

Now, we need to assign a static IP Address to the adapter.

The following is for a CentOS system to assign a static IP Address.

You'll need to edit the file '/etc/sysconfig/network-scripts/ifcfg-enp0s8' with ROOT privileges. The contents should be something like:

Code:
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s8
UUID=14797877-6f7d-46fa-b79b-472b30f61810
DEVICE=enp0s8
ONBOOT=yes
PROXY_METHOD=none
BROWSER_ONLY=no
ETHTOOL_OPTS="autoneg on"
IPV6_PRIVACY=no

Change the line BOOTPROTO=dhcp to BOOTPROTO=none and PEERDNS=yes to PEERDNS=no. If the PEERDNS line is not present, then add the line below. Add the lines:

Code:
IPADDR='192.168.32.100'
NETMASK='255.255.255.0'
NETWORK='192.168.32.0'
DNS1='127.0.0.1'
PEERDNS=NO

Save the changes and reboot the system. Once it has restarted, open a terminal and run 'ip a' to verify that the 'enp0s8' adapter has the new address. If you use my example exactly, the address should be '192.168.32.100'. If you changed it, then verify that it is the address that you set.

For an Ubuntu system, do the following steps.

Edit the file /etc/netplan/01-netcfg.yaml with ROOT privileges and add the following into it:
Code:
network:
version: 2
renderer: networkd
ethernets:
enp0s8:
dhcp4: no
dhcp6: no
addresses: [192.168.32.100/24, ]
nameservers:
addresses: [127.0.0.1]

Save the file and exit your editor. Run the command sudo netplan apply for the changes to take effect. Run ip a to verify that the IP Address is now 192.168.32.100.

NOTE: If any errors occur, verify the configuration file is correct and also that no other running virtual machine has the same IP Address. If needed, choose a different IP Address as the static address.

Install and Configure DHCP

The first part of this section will cover CentOS.

To install DHCP on CentOS, use the command:

sudo yum install dhcp -y

DHCP is installed, and now we need to configure it to work on our network.

There is an example file called '/usr/share/doc/dhcp*/dhcpd.conf.example' if you want to look at one, but we'll create our own at /etc/dhcp/dhcpd.conf. The file exists, but is empty except for comments. Open the file with your editor and add:

Code:
option domain-name-servers 192.168.32.100;
option domain-search "example.com";
authoritative;
default-lease-time 43200;
max-lease-time 43200;
subnet 192.168.32.0 netmask 255.255.255.0 {
range 192.168.32.150 192.168.32.250;
}
host server2 {
hardware ethernet 08:00:27:30:bc:24;
fixed-address 192.168.32.101;
}

The first line designates the IP Addresses of the DHCP Servers. In this case, there is only one. The 'domain-search' is 'example.com' which is the domain name I set on my systems. So, Server1 is server1.example.com which is the Fully Qualified Domain Name (FQDN).

We set the DHCP Server as 'authoritative' to show it is the main DHCP Server. If a group of DHCP Servers exists, then only one is authoritative.

The default lease time is a number in seconds that tells how long the lease for the IP Address is valid. After half the lease time has expired, the system will attempt to negotiate a new lease expiration. The max lease time is how long a system can have a lease on an IP Address before it must get a new lease. In the case above, 43200 is 12 hours. In most cases, in a business, a workday is 9 hours (1 for lunch), so the lease should expire every day after work.

NOTE: We should set all servers to have a static IP Address.

Next, we set the network subnet and subnet mask. A subset of this is to specify the range of IP Addresses that the DHCP Server can lease out to devices. So our server will give out addresses from 192.168.32.150 to 192.168.32.250.

The last section is setting up a static IP Address for Server2. The first line lists the MAC Address of the NIC on Server2 (in this case, enp0s8). To get the MAC Address, run 'ip a' and find the MAC for the adapter in the list.

Save the file and you are ready to start the service:

Code:
sudo systemctl enable dhcpd
sudo systemctl start dhcpd

Everything is ready now for using DHCP on the network.

Now we can set up DHCP on an Ubuntu Server.

To install DHCP, use the command:

sudo apt install isc-dhcp-server -y

To configure the DHCP Server, you need to edit the file /etc/dhcp/dhcpd.conf. You cen comment or remove the existing lines and add:

Code:
authoritative;
subnet 192.168.32.0 netmask 255.255.255.0 {
 range 192.168.32.150 192.168.32.250;
 option domain-name-servers server1.example.com;
 option domain-name "example.org";
 option subnet-mask 255.255.255.0;
 default-lease-time 43200;
 max-lease-time 43200;
}
host server2 {
hardware ethernet 08:00:27:50:f1:f4;
fixed-address 192.168.32.101;
}

Instead of setting the options globally, like with CentOS, I am setting the options specifically for the designated subnet. If multiple subnets are being issued, then we can set the values for each subnet specifically and not globally.

We need to bind the DHCP Server to the required Adapter in the file /etc/default/isc-dhcp-server. Change the line INTERFACESv4=""' to INTERFACESv4="enp0s8". The DHCP Server is now bound to the adapter at enp0s8 for IPv4. You can bind to multiple adapters by separating them with spaces.

To start the service, use the commands:

Code:
sudo systemctl enable isc-dhcp-server
sudo systemctl start isc-dhcp-server

Now the DHCP Service is running on Ubuntu.

Testing DHCP

Start Server2 and check the IP Address, with ip a for the enp0s8 adapter. If the IP Address is not 192.168.32.101, then disconnect the adapter and reconnect it. You can perform a disconnection and re-connection with the command 'sudo netplan apply'.

In the output for ip a you can see the lease time shown in seconds after the label valid_lft.

Conclusion

Setting up a DHCP Server is a basic necessity for a network that doesn't have a router or some such device to act as a DHCP Server.

Most businesses may require a DHCP Server to issue addresses to client systems.

Keep in mind how to set up a DHCP Server not just for the LFCS Exam, but real-life practices.
 
Last edited by a moderator:

Members online


Top