How to Configure a Cloud Server to Use DHCP for IP Address Allocation?

24-01-2024 02:36:42

Cloud servers typically have the ability to flexibly bind and unbind IP addresses, hence these are not fixed in the network properties but are automatically assigned via DHCP (Dynamic Host Configuration Protocol). When a user's incorrect manipulation of the network properties leads to connectivity issues with the cloud server, it becomes necessary to reconfigure DHCP. This article explains how to set up DHCP on various Linux operating systems.

CentOS 7

Begin by inspecting the current network configuration file. Execute the following command to view it.

# ls /etc/sysconfig/network-scripts/ifcfg-*
ifcfg-eth0   ifcfg-lo

The result shows that the current network adapter is named eth0. Next, edit this configuration file.

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

Replace the file with the content below, substituting eth0 with the actual network adapter name.

DEVICE="eth0"
BOOTPROTO="dhcp"
ONBOOT="yes"
TYPE="Ethernet"
NM_CONTROLLED="no"
IPV6_AUTOCONF="yes"
IPV6INIT="yes"
NOZEROCONF="yes"

Be sure to delete any static route files in the system, then restart the network or reboot the operating system.

# rm /etc/sysconfig/network-scripts/route-eth0 -f
# service network restart

CentOS 8

First, inspect the current network configuration file. Execute the following command to view it.

# ls /etc/sysconfig/network-scripts/ifcfg-*
ifcfg-enp1s0

The result shows the network adapter named as enp1s0. Next, edit this configuration file.

# nano /etc/sysconfig/network-scripts/ifcfg-enp1s0

Replace the file with the content below, substituting enp1s0 with the actual network adapter name.

TYPE="Ethernet"
DEVICE="enp1s0"
ONBOOT="yes"
BOOTPROTO="dhcp"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"

Restart the network or reboot the operating system. The command to restart the network is as follows, replace enp1s0 with the actual network adapter name.

# nmcli con load /etc/sysconfig/network-scripts/ifcfg-enp1s0
# nmcli con up 'System enp1s0'

Ubuntu

First, inspect the current network configuration file. Execute the following command to view it.

# ls /etc/netplan/
10-enp1s0.yaml

The result shows the network adapter named as enp1s0. Next, edit this configuration file.

# nano /etc/netplan/10-enp1s0.yaml

Replace the file with the content below, substituting enp1s0 with the actual network adapter name. Note that YAML files require proper indentation, so pay attention to the format while editing.

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

Restart the network or reboot the operating system.

# netplan apply

Debian 9/Debian 10

Edit the network configuration file.

# nano /etc/network/interfaces

Replace the file with the content below, substituting enp1s0 at three places with the actual network adapter name.

# This file describes the network interfaces available on your system

#source /etc/network/interfaces.d/*

auto lo
iface lo inet loopback

allow-hotplug enp1s0
iface enp1s0 inet dhcp
iface enp1s0 inet6 auto

Restart the network or reboot the operating system.

systemctl restart networking.service

Fedora

Execute the following command to reset the network properties to DHCP, replacing enp1s0 at three places with the actual network adapter name.

nmcli connection modify enp1s0 ipv4.method auto
nmcli connection down enp1s0
nmcli connection up enp1s0

FreeBSD

Edit the network configuration file.

# vi /etc/rc.conf

Replace the file with the content below, substituting 'example' with the actual hostname, and 'vtnet0' with the actual network adapter name.

hostname="example"
sshd_enable="YES"
ntpd_enable="YES"
static_routes="linklocal"
devmatch_blacklist="virtio_random.ko"
sendmail_submit_enable="NO"
ifconfig_vtnet0="DHCP"

Reboot the operating system.

# reboot