All Products
Search
Document Center

:Configure DHCP on a Linux instance

Last Updated:Feb 28, 2026

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.

DistributionConfig file pathRestart command
Alibaba Cloud Linux 3/etc/sysconfig/network-scripts/ifcfg-eth0sudo nmcli c reload then sudo nmcli d connect eth0
Alibaba Cloud Linux 4/etc/NetworkManager/system-connections/*.nmconnectionsudo nmcli c reload then sudo nmcli d connect eth0
Alibaba Cloud Linux 2/etc/sysconfig/network-scripts/ifcfg-eth0sudo systemctl restart network
CentOS 6/etc/sysconfig/network-scripts/ifcfg-eth0sudo service network restart
CentOS 7/etc/sysconfig/network-scripts/ifcfg-eth0sudo systemctl restart network
CentOS 8/etc/sysconfig/network-scripts/ifcfg-eth0sudo nmcli c reload then sudo nmcli d connect eth0
Ubuntu 16 and earlier/etc/network/interfacessudo systemctl restart networking (Ubuntu 14: sudo reboot)
Ubuntu 18 and later/etc/netplan/*.yamlsudo netplan apply
Debian 11 and earlier/etc/network/interfacessudo systemctl restart networking
Debian 12/etc/netplan/*.yamlsudo netplan apply
Red Hat 6/etc/sysconfig/network-scripts/ifcfg-eth0sudo service network restart
Red Hat 7/etc/sysconfig/network-scripts/ifcfg-eth0sudo systemctl restart network
Red Hat 8 and 9/etc/sysconfig/network-scripts/ifcfg-eth0sudo nmcli c reload then sudo nmcli d connect eth0
Red Hat 10/etc/NetworkManager/system-connections/*.nmconnectionsudo nmcli c reload then sudo nmcli d connect eth0
SUSE and OpenSUSE/etc/sysconfig/network/ifcfg-eth0sudo systemctl restart wicked

Step 1: Log on to the ECS instance

This step applies to all distributions.

  1. Go to ECS console - Instance. In the top navigation bar, select the target region and resource group.

  2. 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

  1. Edit the network configuration file.

    sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
  2. Set the following parameters, then save and exit.

    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    TYPE=Ethernet
  3. Restart the network service.

    1. Reload all NIC configurations.

      sudo nmcli c reload
    2. Activate the network interface.

      sudo nmcli d connect eth0

Alibaba Cloud Linux 4

  1. Identify the active connection name. Example output: In this example, eth0 is the NAME of the active network connection.

    sudo nmcli connection show --active
    NAME  UUID                                  TYPE      DEVICE
    eth0  1dd9a779-d327-56e1-8454-c65e2556c12c  ethernet  eth0
    lo    0973d353-93a1-41f8-8b7b-5922ade582c6  loopback  lo
  2. Find the configuration file for that connection. Replace <id_name> with the NAME from the previous step. Example output:

    sudo find /etc/NetworkManager/system-connections/ -type f -exec grep -l "id=<id_name>" {} \;
    /etc/NetworkManager/system-connections/1.nmconnection
  3. Open the file and verify that method under [ipv4] is set to auto. Replace <network_configuration_file_path> with the path from the previous step.

    sudo vi <network_configuration_file_path>
  4. Restart the network service.

    1. Reload all NIC configurations.

      sudo nmcli c reload
    2. Activate the network interface.

      sudo nmcli d connect eth0

Alibaba Cloud Linux 2

  1. Edit the network configuration file.

    sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
  2. Set the following parameters, then save and exit.

    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    TYPE=Ethernet
  3. Restart the network service.

    sudo systemctl restart network

CentOS

CentOS 6

  1. Edit the network configuration file.

    sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
  2. Set the following parameters, then save and exit.

    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    TYPE=Ethernet
  3. Restart the network service.

    sudo service network restart

CentOS 7

  1. Edit the network configuration file.

    sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
  2. Set the following parameters, then save and exit.

    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    TYPE=Ethernet
  3. Restart the network service.

    sudo systemctl restart network

CentOS 8

  1. Edit the network configuration file.

    sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
  2. Set the following parameters, then save and exit.

    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    TYPE=Ethernet
  3. Restart the network service.

    1. Reload all NIC configurations.

      sudo nmcli c reload
    2. Activate the network interface.

      sudo nmcli d connect eth0

Ubuntu

Ubuntu 16 and earlier

  1. Edit the network configuration file.

    sudo vi /etc/network/interfaces
  2. Set the following parameters, then save and exit.

    auto lo
    iface lo inet loopback
    
    auto eth0
    iface eth0 inet dhcp
  3. Restart the network service.

    • For Ubuntu 14, run sudo reboot to restart the system and apply the configuration.

    • For other versions, run sudo systemctl restart networking.

Ubuntu 18 and later

  1. 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.yaml
  2. Edit the active configuration file. Replace <network_configuration_file_path> with the path from the previous step.

    sudo vi <network_configuration_file_path>
  3. Set the following parameters, then save and exit.

    network:
      version: 2
      renderer: networkd
      ethernets:
        eth0:
          dhcp4: yes
          dhcp6: no
  4. Apply the configuration.

    sudo netplan apply

Debian

Debian 11 and earlier

  1. Edit the network configuration file.

    If the interfaces file contains source /etc/network/interfaces.d/*, edit the network configuration in /etc/network/interfaces.d/<network_config_file>.
    sudo vi /etc/network/interfaces
  2. Set the following parameters, then save and exit.

    auto lo
    iface lo inet loopback
    
    auto eth0
    iface eth0 inet dhcp
  3. Restart the network service.

    sudo systemctl restart networking

Debian 12

  1. 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.yaml
  2. Edit the active configuration file. Replace <network_configuration_file_path> with the path from the previous step.

    sudo vi <network_configuration_file_path>
  3. Set the following parameters, then save and exit.

    network:
      version: 2
      renderer: networkd
      ethernets:
        eth0:
          dhcp4: yes
          dhcp6: no
  4. Apply the configuration.

    sudo netplan apply

Red Hat

Red Hat 6

  1. Edit the network configuration file.

    sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
  2. Set the following parameters, then save and exit.

    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    TYPE=Ethernet
  3. Restart the network service.

    sudo service network restart

Red Hat 7

  1. Edit the network configuration file.

    sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
  2. Set the following parameters, then save and exit.

    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    TYPE=Ethernet
  3. Restart the network service.

    sudo systemctl restart network

Red Hat 8 and 9

  1. Edit the network configuration file.

    sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
  2. Set the following parameters, then save and exit.

    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    TYPE=Ethernet
  3. Restart the network service.

    1. Reload all NIC configurations.

      sudo nmcli c reload
    2. Activate the network interface.

      sudo nmcli d connect eth0

Red Hat 10

  1. Identify the active connection name. Example output: In this example, eth0 is the NAME of the active network connection.

    sudo nmcli connection show --active
    NAME  UUID                                  TYPE      DEVICE
    eth0  1dd9a779-d327-56e1-8454-c65e2556c12c  ethernet  eth0
    lo    0973d353-93a1-41f8-8b7b-5922ade582c6  loopback  lo
  2. Find the configuration file for that connection. Replace <id_name> with the NAME from the previous step. Example output:

    sudo find /etc/NetworkManager/system-connections/ -type f -exec grep -l "id=<id_name>" {} \;
    /etc/NetworkManager/system-connections/1.nmconnection
  3. Open the file and verify that method under [ipv4] is set to auto. Replace <network_configuration_file_path> with the path from the previous step.

    sudo vi <network_configuration_file_path>
  4. Restart the network service.

    1. Reload all NIC configurations.

      sudo nmcli c reload
    2. Activate the network interface.

      sudo nmcli d connect eth0

SUSE and OpenSUSE

  1. Edit the network configuration file.

    sudo vi /etc/sysconfig/network/ifcfg-eth0
  2. Set the following parameters, then save and exit.

    BOOTPROTO='dhcp'
    STARTMODE='auto'
  3. 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 eth0

In 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.136

A successful response confirms that the instance can communicate over the network.