All Products
Search
Document Center

:Configure a static IP for a Linux ECS instance

Last Updated:Jun 21, 2026

This topic explains how to configure a static IP address for a Linux ECS instance.

Background

By default, an ECS instance is configured to use Dynamic Host Configuration Protocol (DHCP) mode.

Note

This example uses an ECS instance that runs CentOS 7.6.

Procedure

Step 1: View the IP address and gateway

  1. Log on to the Linux ECS instance. Run the following command to view the IP address and subnet mask of the instance.

    ifconfig

    The output is similar to the following:

    [root@aliyuntest ~]# ifconfig
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 17.xxx.xxx.xx  netmask 255.255.255.0  broadcast 172.xxx.xxx.x
            ether 00:16:3e:0c:4f:c3  txqueuelen 1000  (Ethernet)
            RX packets 285116  bytes 217057111 (207.0 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 143703  bytes 40663281 (38.7 MiB)
            TX errors 0  dropped 0  overruns 0  carrier 0  collisions 0
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 509  bytes 102908 (100.4 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 509  bytes 102908 (100.4 KiB)
            TX errors 0  dropped 0  overruns 0  carrier 0  collisions 0
  2. Run the following command to view the gateway of the instance.

    route -n

    The output is similar to the following:

    [root@alxxx      : ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         172.xxx.xxx.53  0.0.0.0         UG    0      0        0 eth0
    169.xxx.0       0.0.0.0         255.255.0.0     U     1002   0        0 eth0
    172.xxx.0       0.0.0.0         255.255.255.0   U     0      0        0 eth0

Alternatively, run the following commands to get the subnet mask, gateway, and IP address from the instance metadata. For more information, see 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

The output is similar to the following:

[root@aliyuntest ~]# curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/00:16:3e:0c:4f:c3/netmask
255.255.255.0[root@aliyuntest ~]#
[root@aliyuntest ~]# curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/00:16:3e:0c:4f:c3/gateway
172.xxx.xxx[root@aliyuntest ~]#
[root@aliyuntest ~]# curl http://100.100.100.200/latest/meta-data/network/interfaces/macs/00:16:3e:0c:4f:c3/private-ipv4s
["172.xxx.xxx.0"][root@aliyuntest ~]#
[root@aliyuntest ~]#
Note

Replace [$MAC_ID] with the MAC address of your ECS instance.

Step 2: Change the network type to static

  1. Run the following command to edit the configuration file. Set BOOTPROTO to static, and add the IP address, gateway, and subnet mask that you recorded in the previous step.

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

    Example:

    DEVICE=eth0
    BOOTPROTO=static
    ONBOOT=yes
    IPADDR=172.xxx.xxx
    GATEWAY=17x.xxx.253
    NETMASK=255.255.255.0
  2. Enter :wq and press Enter to save and exit.

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

    systemctl restart network