All Products
Search
Document Center

:How do I configure a static IP address for a Linux ECS instance?

Last Updated:Jul 30, 2024

This topic describes how to configure a static IP address for a Linux Elastic Compute Service (ECS) instance.

Background information

By default, the IP address assignment method of ECS instances is set to Dynamic Host Configuration Protocol (DHCP). If you want to change the IP address assignment method of an ECS instance to Static IP, perform the operations that are described in this topic.

Note

In the following example, an ECS instance that runs CentOS 7.6 is used.

Procedure

Step 1: Obtain the IP address and gateway of the Linux ECS instance

  1. Log on to the Linux ECS instance for which you want to configure a static IP address. Run the following command to query the IP address and subnet mask of the Linux ECS instance:

    ifconfig

    A command output similar to the following one is displayed.

    image

  2. Run the following command to query the gateway of the Linux ECS instance:

    route -n

    A command output similar to the following one is displayed.

    image

Alternatively, run the following commands in sequence to obtain the IP address and gateway of the Linux ECS instance by using the instance metadata.

For more information, see Obtain instance metadata.

curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/[$MAC_ID]/netmask
curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/[$MAC_ID]/gateway
curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/[$MAC_ID]/private-ipv4s

Command outputs similar to the following ones are displayed.

image

Note

In the preceding commands, replace [$MAC_ID] with the actual Media Access Control (MAC) address of the Linux ECS instance.

Step 2: Change the network type of the ECS instance

  1. Run the following command to modify the network configuration file. Set the BOOTPROTO parameter to static and add the IP address, gateway, and subnet mask that you obtained in the preceding step to the network configuration file.

    vim /etc/sysconfig/network-scripts/ifcfg-eth0

    The following figure shows how to modify the configurations in the network configuration file.

    image

  2. Enter :wq and press the Enter key to save and close the configuration file.

  3. Run the following command to restart the network service:

    systemctl restart network