Overview
By default, Alibaba Cloud Elastic Computing Service (ECS) instances use the Dynamic Host Configuration Protocol (DHCP) to obtain IP addresses. If the default DHCP configuration does not meet your needs, this topic describes how to configure a static IP address for a network interface card (NIC) on an ECS instance or an ECS Bare Metal Instance.
Details
Before you configure a static IP address, obtain the IP address, subnet mask, and gateway of the Linux instance.
View and record the NIC information of the Linux instance
Log on to the Linux ECS instance. Run the following command to view the IP address and subnet mask of the instance.
ifconfigThe output is similar to the following.

Run the following command to view the route table and gateway of the instance.
route -nThe output is similar to the following.
Alternatively, you can obtain the information from the Alibaba Cloud MetaServer.
Run the following commands to obtain the subnet mask, gateway, 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-ipv4sThe output is similar to the following.
NoteNote: [$MAC_ID] is the MAC address of the instance.
The method for configuring a static IP address for a NIC varies depending on the Linux distribution. Select the procedure for your operating system:
Alibaba Cloud Linux
Ubuntu
CentOS
Debian
SUSE Linux
openSUSE
Anolis OS
Fedora
Alibaba Cloud Linux 2
Alibaba Cloud Linux 2 instances use network-scripts for network services. To set a static IP address, add the IP address, subnet mask, and gateway to the NIC configuration file, and then restart the network service. Follow these steps:
Run the following command to edit the
/etc/cloud/cloud.cfgconfiguration file.vim /etc/cloud/cloud.cfgPress
ito switch to edit mode. Add the following configuration above the# Example datasource configsection to disable automatic network configuration in cloud-init. This prevents the NIC configuration file from being overwritten.network: config: disabledThe output is similar to the following.

After you make the changes, press the
Esckey, enter:wq, and press Enter to save and close the file.
Run the following command to edit the
/etc/sysconfig/network-scripts/ifcfg-eth0configuration file.vim /etc/sysconfig/network-scripts/ifcfg-eth0Press
ito switch to edit mode. Change the value of BOOTPROTO to static. Add the IP address, gateway, and subnet mask that you recorded earlier to the file. The modified content is shown below.DEVICE=eth0 BOOTPROTO=static ONBOOT=yes IPADDR=192.168.X.68 NETMASK=255.255.255.0 GATEWAY=192.168.X.253 DNS1=[$DNS1] DNS2=[$DNS2]NoteNote:
BOOTPROTO: The NIC mode. By default, Alibaba Cloud ECS instances use DHCP. Change this to `static`.
eth0: The name of the NIC to configure. This topic uses `eth0` as an example. Replace this with the actual name of the NIC in your environment.
IPADDR: The IP address. This topic uses `192.168.X.68` as an example. Replace this with the actual value for your environment.
NETMASK: The subnet mask. This topic uses `255.255.255.0` as an example. Replace this with the actual value for your environment.
GATEWAY: The gateway address. This topic uses `192.168.X.253` as an example. Replace this with the actual value for your environment.
After you make the changes, press the
Esckey, enter:wq, and press Enter to save and close the file.
Run the following command to restart the network service and apply the configuration.
systemctl restart network
Ubuntu 18/20
Most common distributions use the NetworkManager service for network management. However, Ubuntu 18 and later versions use `systemd-networkd`. The following steps describe how to configure a static IP address on Ubuntu 18 and Ubuntu 20:
Refer to Step 1 in the Alibaba Cloud Linux 2 section to disable automatic network configuration in cloud-init. This prevents the NIC configuration file from being overwritten.
Run the following command to edit the
/etc/netplan/50-cloud-init.yamlconfiguration file.vim /etc/netplan/50-cloud-init.yamlPress
ito switch to edit mode. Add the following configuration to the bottom of the file to set custom DNS information. Change the value of dhcp4 to no. Add the IP address, gateway, and subnet mask that you recorded earlier to the file.nameservers: addresses:[$DNS1, $DNS2]The modified content is shown below.
network: version: 2 ethernets: eth0: dhcp4: no addresses: [192.168.X.68/24] gateway4: 192.168.X.253 match: macaddress: 00:16:3e:18:d1:61 set-name: eth0 nameservers: addresses: [$DNS1,$DNS2]NoteNote:
eth0: The name of the NIC to configure. This topic uses `eth0` as an example. Replace this with the actual name of the NIC in your environment.
dhcp4: The default network configuration for ECS instances is DHCP. Change this to `no` to disable DHCP for the NIC.
addresses: The IP address. This topic uses `192.168.X.68/24` as an example. Replace this with the actual value for your environment.
gateway4: The gateway address. This topic uses `192.168.X.253` as an example. Replace this with the actual value for your environment.
match: The MAC address to configure. This topic uses
00:16:3e:18:d1:61as an example. Replace this with the actual value for your environment.nameservers: The custom DNS information. Enter your custom DNS IP addresses as needed.
After you make the changes, press the
Esckey, enter:wq, and press Enter to save and close the file.
Run the following command to apply the NIC configuration.
netplan applyRun the following command to create a symbolic link for the
/run/systemd/resolve/resolv.conffile. This prevents your configuration from being reverted when the/etc/resolv.conffile is updated.sudo ln -snf /run/systemd/resolve/resolv.conf /etc/resolv.conf
CentOS 6/7/8, Alibaba Cloud Linux 3, Anolis OS 7/8, and Fedora 33/34
Network service
The network service in CentOS 6 and CentOS 7 is called Network.
CentOS 8 and Alibaba Cloud Linux 3 use NetworkManager as their network service.
The Anolis OS 7 operating system uses the Network service type.
The network service for Anolis OS 8, Fedora 33, and Fedora 34 is NetworkManager.
DNS service: For easier maintenance, you can manage network and DNS settings using the network service. This means that the DNS settings are also configured in the NIC configuration file.
Refer to Step 1 in the Alibaba Cloud Linux 2 section to disable automatic network configuration in cloud-init. This prevents the NIC configuration file from being overwritten.
Run the following command to edit the
/etc/sysconfig/network-scripts/ifcfg-eth0configuration file.vim /etc/sysconfig/network-scripts/ifcfg-eth0Press
ito switch to edit mode. Change the value of BOOTPROTO to static. Add the IP address, gateway, and subnet mask that you recorded earlier to the file. Also, enter the addresses for DNS1 and DNS2. The modified content is shown below.BOOTPROTO=static DEVICE=eth0 ONBOOT=yes STARTMODE=auto TYPE=Ethernet USERCTL=no IPADDR=192.168.X.68 NETMASK=255.255.255.0 GATEWAY=192.168.X.253 PEERDNS=no DNS1=[$DNS1] DNS2=[$DNS2]NoteNote:
BOOTPROTO: The NIC mode. By default, Alibaba Cloud ECS instances use DHCP. Change this to `static`.
eth0: The name of the NIC to configure. This topic uses `eth0` as an example. Replace this with the actual name of the NIC in your environment.
IPADDR: The IP address. This topic uses `192.168.X.68` as an example. Replace this with the actual value for your environment.
NETMASK: The subnet mask. This topic uses `255.255.255.0` as an example. Replace this with the actual value for your environment.
GATEWAY: The gateway address. This topic uses `192.168.X.253` as an example. Replace this with the actual value for your environment.
DNS1 and DNS2: The custom DNS information. Enter your custom DNS IP addresses as needed.
After you make the changes, press the
Esckey, enter:wq, and press Enter to save and close the file.
The commands to restart the network service vary by operating system. To restart the network service, choose the command that is appropriate for your environment:
CentOS 6:
service network restartCentOS 7/Anolis OS 7:
systemctl restart networkFor the following operating systems, reload all NIC configurations by running
nmcli c reload, and then activate the NIC by runningnmcli d connect eth0.CentOS 8
Alibaba Cloud Linux 3
Anolis OS 7/8
Fedora 33/34
Debian 9/10 and Ubuntu 16
Debian 9, Debian 10, and Ubuntu 16 use the `networking` service. The following steps describe how to configure a static IP address on these systems:
Run the following command to edit the
/etc/network/interfacesconfiguration file.vim /etc/network/interfacesPress
ito switch to edit mode. Change the value of iface eth0 inet to static. Add the IP address, gateway, and subnet mask that you recorded earlier to the file. The modified content is shown below.auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.X.68 netmask 255.255.255.0 gateway 192.168.X.253 dns-nameservers [$DNS1] [$DNS]NoteNote:
iface eth0 inet: The NIC mode. By default, Alibaba Cloud ECS instances use DHCP. Change this to `static`.
eth0: The name of the NIC to configure. This topic uses `eth0` as an example. Replace this with the actual name of the NIC in your environment.
address: The IP address. This topic uses `192.168.X.68` as an example. Replace this with the actual value for your environment.
netmask: The subnet mask. This topic uses `255.255.255.0` as an example. Replace this with the actual value for your environment.
gateway: The gateway address. This topic uses `192.168.X.253` as an example. Replace this with the actual value for your environment.
dns-nameservers: The custom DNS information. Enter your custom DNS IP addresses as needed.
After you make the changes, press the
Esckey, enter:wq, and press Enter to save and close the file.
Run the following command to restart the network service and apply the configuration.
systemctl restart network
SUSE Linux 12, SUSE Linux 15, and openSUSE 15
SUSE Linux operating systems use the Wicked network management tool. The following steps describe how to configure a static IP address and custom DNS on SUSE Linux 12, SUSE Linux 15, and openSUSE 15:
Run the following command to edit the
/etc/sysconfig/network/ifcfg-eth0configuration file.vim /etc/sysconfig/network/ifcfg-eth0Press
ito switch to edit mode. Change the value of BOOTPROTO to static. Add the IP address and subnet mask that you recorded earlier to the file. The modified content is shown below.BOOTPROTO='static' STARTMODE='auto' IPADDR='192.168.X.68' NETMASK='255.255.255.0'NoteNote:
BOOTPROTO: The NIC mode. By default, Alibaba Cloud ECS instances use DHCP. Change this to `static`.
IPADDR: The IP address. This topic uses `192.168.X.68` as an example. Replace this with the actual value for your environment.
NETMASK: The subnet mask. This topic uses `255.255.255.0` as an example. Replace this with the actual value for your environment.
After you make the changes, press the
Esckey, enter:wq, and press Enter to save and close the file.
Run the following command to edit the
/etc/sysconfig/network/routesgateway configuration file.vim /etc/sysconfig/network/routesPress
ito switch to edit mode. Add the `default 192.168.X.253` content to the file.After you make the changes, press the
Esckey, enter:wq, and press Enter to save and close the file.
Run the following command to edit the
/etc/resolv.confconfiguration file.vim /etc/resolv.confPress
ito switch to edit mode. Change the value of thenameserverparameter to your custom DNS IP address.nameserver [$DNS1] nameserver [$DNS2]NoteNote: Change the values of both
nameserverparameters to your custom DNS IP addresses.After you make the changes, press the
Esckey, enter:wq, and press Enter to save and close the file.
Run the following command to edit the
/etc/sysconfig/network/configconfiguration file.vim /etc/sysconfig/network/configPress
ito switch to edit mode. Delete the value of theNETCONFIG_MODULES_ORDERparameter, but keep the double quotation marks (""), as shown below.NETCONFIG_MODULES_ORDER=""After you make the changes, press the
Esckey, enter:wq, and press Enter to save and close the file.
Run the following command to restart the network service.
service network restart
Configure a custom DNS
On some Linux instances, DNS settings are reset or lost after you configure a static IP address and restart the server. This causes network failures. For these instances, you must configure a custom DNS service after you set the static IP address. The method to configure a custom DNS varies by Linux operating system. For more information, see Configure a custom DNS. If the DNS information is overwritten or lost after a server restart, it indicates that the configuration failed. If this occurs, use the following method to reconfigure the DNS.
Run the following command to delete the
/etc/resolv.confDNS configuration file.rm -rf /etc/resolv.confRun the following command to create a new
/etc/resolv.conffile and write the DNS configuration to it.echo "nameserver 114.114.114.114" > /etc/resolv.conf
Applicable to
Elastic Compute Service