You can configure an Elastic Compute Service (ECS) Linux instance to obtain its network settings through Dynamic Host Configuration Protocol (DHCP). With DHCP, your instance automatically receives its IP address and related network configuration, which simplifies setup and prevents manual configuration errors.
You must configure DHCP before you create a custom image from an instance. If the instance uses a static IP address, new instances launched from that image inherit the same static configuration, which can cause IP address conflicts or network connectivity issues.
Quick reference
The following table lists the configuration file path and restart command for each supported distribution.
| Distribution | Config file path | Restart command |
|---|---|---|
| Alibaba Cloud Linux 3 | /etc/sysconfig/network-scripts/ifcfg-eth0 | sudo nmcli c reload then sudo nmcli d connect eth0 |
| Alibaba Cloud Linux 4 | /etc/NetworkManager/system-connections/*.nmconnection | sudo nmcli c reload then sudo nmcli d connect eth0 |
| Alibaba Cloud Linux 2 | /etc/sysconfig/network-scripts/ifcfg-eth0 | sudo systemctl restart network |
| CentOS 6 | /etc/sysconfig/network-scripts/ifcfg-eth0 | sudo service network restart |
| CentOS 7 | /etc/sysconfig/network-scripts/ifcfg-eth0 | sudo systemctl restart network |
| CentOS 8 | /etc/sysconfig/network-scripts/ifcfg-eth0 | sudo nmcli c reload then sudo nmcli d connect eth0 |
| Ubuntu 16 and earlier | /etc/network/interfaces | sudo systemctl restart networking (Ubuntu 14: sudo reboot) |
| Ubuntu 18 and later | /etc/netplan/*.yaml | sudo netplan apply |
| Debian 11 and earlier | /etc/network/interfaces | sudo systemctl restart networking |
| Debian 12 | /etc/netplan/*.yaml | sudo netplan apply |
| Red Hat 6 | /etc/sysconfig/network-scripts/ifcfg-eth0 | sudo service network restart |
| Red Hat 7 | /etc/sysconfig/network-scripts/ifcfg-eth0 | sudo systemctl restart network |
| Red Hat 8 and 9 | /etc/sysconfig/network-scripts/ifcfg-eth0 | sudo nmcli c reload then sudo nmcli d connect eth0 |
| Red Hat 10 | /etc/NetworkManager/system-connections/*.nmconnection | sudo nmcli c reload then sudo nmcli d connect eth0 |
| SUSE and OpenSUSE | /etc/sysconfig/network/ifcfg-eth0 | sudo systemctl restart wicked |
Step 1: Log on to the ECS instance
This step applies to all distributions.
Go to ECS console - Instance. In the top navigation bar, select the target region and resource group.
Go to the details page of the target instance. Click Connect and select Workbench. Follow the prompts on the page to log on to the terminal.
Step 2: Configure DHCP
Select the section that matches your Linux distribution and version.
Alibaba Cloud Linux
Alibaba Cloud Linux 3
Edit the network configuration file.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0Set the following parameters, then save and exit.
BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes TYPE=EthernetRestart the network service.
Reload all NIC configurations.
sudo nmcli c reloadActivate the network interface.
sudo nmcli d connect eth0
Alibaba Cloud Linux 4
Identify the active connection name. Example output: In this example,
eth0is theNAMEof the active network connection.sudo nmcli connection show --activeNAME UUID TYPE DEVICE eth0 1dd9a779-d327-56e1-8454-c65e2556c12c ethernet eth0 lo 0973d353-93a1-41f8-8b7b-5922ade582c6 loopback loFind the configuration file for that connection. Replace
<id_name>with theNAMEfrom the previous step. Example output:sudo find /etc/NetworkManager/system-connections/ -type f -exec grep -l "id=<id_name>" {} \;/etc/NetworkManager/system-connections/1.nmconnectionOpen the file and verify that
methodunder[ipv4]is set toauto. Replace<network_configuration_file_path>with the path from the previous step.sudo vi <network_configuration_file_path>Restart the network service.
Reload all NIC configurations.
sudo nmcli c reloadActivate the network interface.
sudo nmcli d connect eth0
Alibaba Cloud Linux 2
Edit the network configuration file.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0Set the following parameters, then save and exit.
BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes TYPE=EthernetRestart the network service.
sudo systemctl restart network
CentOS
CentOS 6
Edit the network configuration file.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0Set the following parameters, then save and exit.
BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes TYPE=EthernetRestart the network service.
sudo service network restart
CentOS 7
Edit the network configuration file.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0Set the following parameters, then save and exit.
BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes TYPE=EthernetRestart the network service.
sudo systemctl restart network
CentOS 8
Edit the network configuration file.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0Set the following parameters, then save and exit.
BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes TYPE=EthernetRestart the network service.
Reload all NIC configurations.
sudo nmcli c reloadActivate the network interface.
sudo nmcli d connect eth0
Ubuntu
Ubuntu 16 and earlier
Edit the network configuration file.
sudo vi /etc/network/interfacesSet the following parameters, then save and exit.
auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcpRestart the network service.
For Ubuntu 14, run
sudo rebootto restart the system and apply the configuration.For other versions, run
sudo systemctl restart networking.
Ubuntu 18 and later
Find the active network configuration file. Example output: Netplan applies configurations alphabetically. In this example, the last file,
/etc/netplan/99-eth0.yaml, overrides earlier ones and is the active configuration.ls /etc/netplan/*.yaml | sort/etc/netplan/100-eth0.yaml /etc/netplan/50-cloud-init.yaml /etc/netplan/99-eth0.yamlEdit the active configuration file. Replace
<network_configuration_file_path>with the path from the previous step.sudo vi <network_configuration_file_path>Set the following parameters, then save and exit.
network: version: 2 renderer: networkd ethernets: eth0: dhcp4: yes dhcp6: noApply the configuration.
sudo netplan apply
Debian
Debian 11 and earlier
Edit the network configuration file.
If the
interfacesfile containssource /etc/network/interfaces.d/*, edit the network configuration in/etc/network/interfaces.d/<network_config_file>.sudo vi /etc/network/interfacesSet the following parameters, then save and exit.
auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcpRestart the network service.
sudo systemctl restart networking
Debian 12
Find the active network configuration file. Example output: Netplan applies configurations alphabetically. In this example, the last file,
/etc/netplan/99-eth0.yaml, overrides earlier ones and is the active configuration.ls /etc/netplan/*.yaml | sort/etc/netplan/100-eth0.yaml /etc/netplan/50-cloud-init.yaml /etc/netplan/99-eth0.yamlEdit the active configuration file. Replace
<network_configuration_file_path>with the path from the previous step.sudo vi <network_configuration_file_path>Set the following parameters, then save and exit.
network: version: 2 renderer: networkd ethernets: eth0: dhcp4: yes dhcp6: noApply the configuration.
sudo netplan apply
Red Hat
Red Hat 6
Edit the network configuration file.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0Set the following parameters, then save and exit.
BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes TYPE=EthernetRestart the network service.
sudo service network restart
Red Hat 7
Edit the network configuration file.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0Set the following parameters, then save and exit.
BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes TYPE=EthernetRestart the network service.
sudo systemctl restart network
Red Hat 8 and 9
Edit the network configuration file.
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0Set the following parameters, then save and exit.
BOOTPROTO=dhcp DEVICE=eth0 ONBOOT=yes TYPE=EthernetRestart the network service.
Reload all NIC configurations.
sudo nmcli c reloadActivate the network interface.
sudo nmcli d connect eth0
Red Hat 10
Identify the active connection name. Example output: In this example,
eth0is theNAMEof the active network connection.sudo nmcli connection show --activeNAME UUID TYPE DEVICE eth0 1dd9a779-d327-56e1-8454-c65e2556c12c ethernet eth0 lo 0973d353-93a1-41f8-8b7b-5922ade582c6 loopback loFind the configuration file for that connection. Replace
<id_name>with theNAMEfrom the previous step. Example output:sudo find /etc/NetworkManager/system-connections/ -type f -exec grep -l "id=<id_name>" {} \;/etc/NetworkManager/system-connections/1.nmconnectionOpen the file and verify that
methodunder[ipv4]is set toauto. Replace<network_configuration_file_path>with the path from the previous step.sudo vi <network_configuration_file_path>Restart the network service.
Reload all NIC configurations.
sudo nmcli c reloadActivate the network interface.
sudo nmcli d connect eth0
SUSE and OpenSUSE
Edit the network configuration file.
sudo vi /etc/sysconfig/network/ifcfg-eth0Set the following parameters, then save and exit.
BOOTPROTO='dhcp' STARTMODE='auto'Restart the network service.
sudo systemctl restart wicked
Verify the configuration
After you restart the network service, verify that the instance obtained an IP address through DHCP.
Run the following command to check the IP address assigned to the network interface:
ip addr show eth0In the output, look for an inet line that shows the assigned IP address. If an IP address is displayed, DHCP is working correctly.
You can also check network connectivity:
ping -c 4 100.100.2.136A successful response confirms that the instance can communicate over the network.