All Products
Search
Document Center

:How to set a static IP address for an Linux ECS instance

Last Updated:Dec 29, 2020

Introduction

This article describes how to set a static IP address for an ECS instance of Linux.

Background

Alibaba Cloud reminds you that:

  • Before you perform operations that may cause risks, such as modifying instance configurations or data, we recommend that you check the disaster recovery and fault tolerance capabilities of the instances to ensure data security.
  • You can modify the configurations and data of instances including but not limited to Elastic Compute Service (ECS) and Relational Database Service (RDS) instances. Before the modification, we recommend that you create snapshots or enable RDS log backup.
  • If you have authorized or submitted sensitive information such as the logon account and password in the Alibaba Cloud Management Console, we recommend that you modify such information in a timely manner.

By default, the network configuration of the ECS instance is DHCP mode. If you want to change the network configuration to Static Static mode, see this topic.

Note: this article takes CentOS 7.6 operating system as an example.

View and record the IP address and Gateway Information of Linux instances

  1. Log on to the ECS instance of the Linux and run the following command to view the IP address and subnet mask of the instance.
    ifconfig
    The following command output is returned.
    呜呜呜呜.png
  2. Run the following command to view the Gateway Information of the instance.
    route -n
    The following command output is returned.
    {D26B4046-C999-4D0B-AE20-E1DB2A7D17D9}_20200414105215.jpg
  3. You can also refer to the following method to obtain the relevant information through Alibaba Cloud MetaServer.
    1. Run the following commands in sequence to obtain the subnet mask, Gateway Information, and IP address of the instance.
      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

      A similar output is displayed:
      555.jpg
      Note: [$MAC_ID] is the MAC address of the instance.


Modify the network type of an ECS instance

  1. Edit the following file and change the value of BOOTPROTO to static. Add the IP address, Gateway Information, and subnet mask recorded in the steps to the file. Save the settings and exit.
    /etc/sysconfig/network-scripts/ifcfg-eth0
    This is shown in the following figure.
    {FF2B028C-44A2-4F3E-BE56-1A375275DBAD}_20200113145203.jpg
  2. Run the following command to restart the network connection service:
    systemctl restart network

Application scope

  • Elastic Compute Service