All Products
Search
Document Center

:How to Configure the Network as DHCP in a Linux Image

更新時間:Jul 14, 2022

Overview

Create a custom image by using ECS or OSS import. If the network configuration in the image is a static IP address, you need to modify network interface controller attribute to DHCP to ensure that you can dynamically obtain the IP address when you create a new ECS instance by using the image. This topic describes how to configure mainstream Linux operating systems.

Error message

Different Linux operating systems can be used to configure the network as DHCP in a Linux image as follows:

Redhat series

The supported operating systems are as follows:

  • CentOS 6/7/8
  • Red Hat 6/7/8/9
  • Anolis 7/8
  • Fedora 33/34/35
  • Alibaba Cloud Linux 2/3
  • CentOS Stream
  • Rocky Linux 
  • AlmaLinux  

To connect the AD domain to the LDAP service, perform the following steps:

  1. Connect to an ECS instance. For more information about how to connect to an ECS instance, see Use password or key authentication to log on to a Linux instance.
  2. Run the following command to edit the network configuration file:
    vi /etc/sysconfig/network-scripts/ifcfg-eth0 
  3. Modify the following network configuration items.
    BOOTPROTO=dhcp
    DEVICE=eth0
    ONBOOT=yes
    STARTMODE=auto
    TYPE=Ethernet
    USERCTL=no




  4. Press the Esc key, enter the :wq, and press Enter to save and exit the file editing.
  5. Restart the network service.
    • For CentOS 6 and Red Hat 6 operating systems, run the following command:
      service network restart
    • For CentOS 7, Red Hat 7, Anolis 7, and Alibaba Cloud Linux 2 operating systems, run the following command:
      systemctl restart network
    • Anolis 8, fedora 33/34/35, CentOS 8 and Red Hat 8 Alibaba Cloud Linux 3 operating systems, perform the following operations:
      1. Run the following command to reload all network interface controller configurations:
         nmcli c reload
      2. Run the following command to activate network interface controller:
        nmcli d connect eth0

Debian series

Supported operating systems are as follows:

  • Ubuntu 14/16

  • Debian /8/9/10/11

  • Ubuntu 18/20

To configure Ubuntu 14/16 and Debian /8/9/10/11 operating systems, perform the following operations:

  1. Connect to the ECS instance and run the following command to open the network configuration file.
    vim /etc/network/interfaces
  2. Modify the following network configuration items.
    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet dhcp
    Note:
    • auto eth0 :eth0 indicates that the alias of network interface controller of the ifconfig output pair on your own machine matches.
    • iface eth0 inet dhcp: indicates that the IP address acquisition method is DHCP.
  3. Press the Esc key, enter the :wq, and press Enter to save and exit the file editing.
  4. Run the following command to restart the network service:
    service networking restart
    or
    systemctl restart networking
    Note :Ubuntu 14 does not apply to the preceding two commands. You can run the reboot command to restart the instance.

Configure the Ubuntu 18/20 operating system as follows:

  1. Connect to the ECS instance and run the following command to open the network configuration file.
    vim  /etc/netplan/99-eth0.yaml
  2. Add the following network interface controller configurations.
    network:
      version: 2
      renderer: networkd
      ethernets:
        eth0:
          dhcp4: yes
          dhcp6: no
  3. Press the Esc key, enter the :wq, and press Enter to save and exit the file editing.
  4. Run the netplan apply command to make the configuration take effect.

SLES series

Supported operating systems are as follows:

  • SUSE 11/12/15
  • OpenSUSE 42/15
  1. Connect to the ECS instance and run the following command to edit the network configuration file:
    vi /etc/sysconfig/network/ifcfg-eth0
  2. Modify the following network configuration items.
    BOOTPROTO='dhcp'
    STARTMODE='auto'
  3. After the configuration is complete, run the following command to restart the network service.
    systemctl restart wicked

References

The names of network services in different Linux and versions and the commands to restart network services are as follows:

OS

Version

Network service name

Restart network service commands

CentOS

6

network

service network restart

7

network

systemctl restart network

8

NetworkManager

systemctl restart  NetworkManager

Debian

8

networking

systemctl  restart  networking

9

networking

systemctl  restart  networking

10

networking

systemctl  restart  networking

Ubuntu

14

networking

service  networking restart

16

networking

systemctl  restart networking

18

systemd-networkd

systemctl  restart systemd-networkd

20

systemd-networkd

systemctl  restart systemd-networkd

Sles

15

wicked

systemctl  restart wicked

12

wicked

systemctl  restart wicked

Opensuse

15

wicked

systemctl  restart wicked

Aliyun Linux 2

2

systemd-networkd

systemctl  restart  systemd-networkd

Ideal For

  • ECS