This topic describes how to enable the DHCP service on the Linux operating system.
About DHCP
Dynamic Host Configuration Protocol (DHCP) is a network protocol used to automatically assign IP addresses, subnet masks, default gateways, and other network configuration information to network devices.
When a device connects to a network, it sends a request to a DHCP server to obtain the IP address and other configuration information. The DHCP server then assigns an appropriate IP address and other configuration information to the device based on its configured policies and available IP address pool.
Applicable scenario
If your ECS instance or locally stored image uses a static IP address, you must enable DHCP in the image before creating a custom image from the ECS instance or by importing the image. Otherwise, the custom image created may fail to obtain a dynamic IP address.
Procedure
- Enable the DHCP service.
- CentOS Stream 8/9, CentOS 6.x/7.x/8.x, Fedora 33/34/35, Rocky Linux, and AlmaLinux
- Execute the following command in the instance to view the network interface alias.
-
- Execute the following command in the instance to open the network configuration file.
Replace eth0 in the command with the name of the actual network interface. In the example provided in this topic, eth0 is used.
- Press i to enter edit mode, and change the value of the BOOTPROTO parameter to dhcp.
Replace eth0 with the name of the actual network interface. In the example provided in this topic, eth0 is used.
- Press the Esc key, enter :wq, and press Enter to save and exit the file.
- Execute the following command to restart the network service.
- CentOS 8.x and Fedora 33/34/35
- CentOS 7.x, CentOS Stream 8/9, Rocky Linux, and AlmaLinux
- Ubuntu 16 and Debian 9/10/11
- Execute the following command in the instance to view the network interface alias.
-
- Execute the following command to open the network configuration file.
-
- Press i to enter edit mode, and modify the target network interface configuration to the following content.
In the example provided in this topic, eth0 is used. Replace eth0 in the command with the name of the actual network interface.
- Example after modification
-
- Press the Esc key, enter :wq, and press Enter to save and exit the file.
- Execute the following command to restart the network service.
- Execute the following command in the instance to view the network interface alias.
-
- Execute the following command to open the network configuration file.
Replace 01-netcfg.yaml in the command with the actual configuration file.
-
- Press i to enter edit mode, and modify the following configuration items.
In the example provided in this topic, the eth0 network interface is used. Modify the configuration of your actual network interface.
- Example after modification
-
- Press the Esc key, enter :wq, and press Enter to save and exit the file.
- Execute the following command to restart the network service.
- Execute the following command in the instance to view the network interface alias.
-
- Execute the following command to open the network configuration file.
Replace eth0 in the command with the actual name of the network interface.
-
- Press the i key to enter edit mode, and modify the values of the BOOTPROTO and STARTMODE parameters.
- Example after modification
-
- Press the Esc key, enter :wq, and press Enter to save and exit the file.
- Execute the following command to restart the network service.