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.
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
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.
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.
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.
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
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.
Enter
:wq
and press the Enter key to save and close the configuration file.Run the following command to restart the network service:
systemctl restart network