All Products
Search
Document Center

:Configure DHCP on a Linux instance

Last Updated:Nov 13, 2025

Configuring an instance to use DHCP instead of a static IP address lets it automatically get a network configuration. This simplifies setup and helps prevent manual configuration errors. Before you create a custom image, you must configure the instance to use DHCP. This ensures that new instances created from the image do not inherit a static IP, which can cause IP address conflicts or network connectivity issues.

Alibaba Cloud Linux

Alibaba Cloud Linux 3

  1. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

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

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

    1. Reload all network interface card (NIC) configurations.

      sudo nmcli c reload
    2. Activate the NIC.

      sudo nmcli d connect eth0

Alibaba Cloud Linux 4

  1. Log on to the ECS instance.

    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.

  2. Modify the network configuration file.

    1. Identify the active connection's ID name to find its configuration file.

      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 

      In this example, eth0 is the NAME of the active network connection.

    2. Determine the path of the network configuration file.

      Replace <id_name> with the NAME from the previous step.

      sudo find /etc/NetworkManager/system-connections/ -type f -exec grep -l "id=<id_name>" {} \;
      /etc/NetworkManager/system-connections/1.nmconnection

      The example output shows the file path is /etc/NetworkManager/system-connections/1.nmconnection.

    3. Check the file and ensure that the 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>
  3. Restart the network service.

    1. Reload all NIC configurations.

      sudo nmcli c reload
    2. Activate the NIC.

      sudo nmcli d connect eth0

Alibaba Cloud Linux 2

  1. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

      sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 
    2. Ensure the file contains 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. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

      sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 
    2. Ensure the file contains 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. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

      sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 
    2. Ensure the file contains 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. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

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

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

    1. Reload all network interface card (NIC) configurations.

      sudo nmcli c reload
    2. Activate the NIC.

      sudo nmcli d connect eth0

Ubuntu

Ubuntu 16 and earlier

  1. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

      sudo vi /etc/network/interfaces
    2. Ensure the file contains 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. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Determine the active network configuration file path.

      ls /etc/netplan/*.yaml | sort
      /etc/netplan/100-eth0.yaml
      /etc/netplan/50-cloud-init.yaml
      /etc/netplan/99-eth0.yaml

      Netplan applies configurations alphabetically. In this example, the last file, /etc/netplan/99-eth0.yaml, overrides earlier ones and is the active configuration.

    2. Edit the network configuration file.

      Replace <network_configuration_file_path> with the path from the previous step.

      sudo vi <network_configuration_file_path>
    3. Ensure the file contains the following parameters, then save and exit.

      network:
        version: 2
        renderer: networkd
        ethernets:
          eth0:
            dhcp4: yes
            dhcp6: no
  3. Restart the network service.

    sudo netplan apply

Debian

Debian 11 and earlier

  1. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

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

      If the interfaces file contains source /etc/network/interfaces.d/*, edit the network configuration in /etc/network/interfaces.d/<network_config_file>.
      auto lo
      iface lo inet loopback
      
      auto eth0
      iface eth0 inet dhcp 
  3. Restart the network service.

    sudo systemctl restart networking

Debian 12

  1. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Determine the active network configuration file path.

      ls /etc/netplan/*.yaml | sort
      /etc/netplan/100-eth0.yaml
      /etc/netplan/50-cloud-init.yaml
      /etc/netplan/99-eth0.yaml

      Netplan applies configurations alphabetically. In this example, the last file, /etc/netplan/99-eth0.yaml, overrides earlier ones and is the active configuration.

    2. Edit the network configuration file.

      Replace <network_configuration_file_path> with the path from the previous step.

      sudo vi <network_configuration_file_path>
    3. Ensure the file contains the following parameters, then save and exit.

      network:
        version: 2
        renderer: networkd
        ethernets:
          eth0:
            dhcp4: yes
            dhcp6: no
  3. Restart the network service.

    sudo netplan apply

Red Hat

Red Hat 6

  1. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

      sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 
    2. Ensure the file contains 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. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

      sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 
    2. Ensure the file contains 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. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

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

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

    1. Reload all network interface card (NIC) configurations.

      sudo nmcli c reload
    2. Activate the NIC.

      sudo nmcli d connect eth0

Red Hat 10

  1. Log on to the ECS instance.

    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.

  2. Modify the network configuration file.

    1. Identify the active connection's ID name to find its configuration file.

      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 

      In this example, eth0 is the NAME of the active network connection.

    2. Determine the path of the network configuration file.

      Replace <id_name> with the NAME from the previous step.

      sudo find /etc/NetworkManager/system-connections/ -type f -exec grep -l "id=<id_name>" {} \;
      /etc/NetworkManager/system-connections/1.nmconnection

      The example output shows the file path is /etc/NetworkManager/system-connections/1.nmconnection.

    3. Check the file and ensure that the 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>
  3. Restart the network service.

    1. Reload all NIC configurations.

      sudo nmcli c reload
    2. Activate the NIC.

      sudo nmcli d connect eth0

SUSE and OpenSUSE

  1. Log on to the ECS instance.

    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.

  2. Modify the network configuration to use DHCP.

    1. Edit the network configuration file.

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

      BOOTPROTO='dhcp'
      STARTMODE='auto'
  3. Restart the network service to apply the configuration.

    sudo systemctl restart wicked