Set a static IP address for a Linux instance

更新时间:
复制 MD 格式

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

  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. 呜呜呜呜.png

  2. Run the following command to view the route table and gateway of the instance.

    route -n

    The output is similar to the following.

  3. Alternatively, you can obtain the information from the Alibaba Cloud MetaServer.

    1. 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-ipv4s

      The output is similar to the following. 555.jpg

      Note

      Note: [$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 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:

  1. Run the following command to edit the /etc/cloud/cloud.cfg configuration file.

    vim /etc/cloud/cloud.cfg
    1. Press i to switch to edit mode. Add the following configuration above the # Example datasource config section to disable automatic network configuration in cloud-init. This prevents the NIC configuration file from being overwritten.

      network:
        config: disabled

      The output is similar to the following.

      Dingtalk_20201029153704.jpg

    2. After you make the changes, press the Esc key, enter :wq, and press Enter to save and close the file.

  2. Run the following command to edit the /etc/sysconfig/network-scripts/ifcfg-eth0 configuration file.

    vim /etc/sysconfig/network-scripts/ifcfg-eth0
    1. Press i to 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]
      Note

      Note:

      • 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.

    2. After you make the changes, press the Esc key, enter :wq, and press Enter to save and close the file.

  3. 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:

  1. 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.

  2. Run the following command to edit the /etc/netplan/50-cloud-init.yaml configuration file.

    vim /etc/netplan/50-cloud-init.yaml
    1. Press i to 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]

    2. 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]
      Note

      Note:

      • 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:61 as an example. Replace this with the actual value for your environment.

      • nameservers: The custom DNS information. Enter your custom DNS IP addresses as needed.

    3. After you make the changes, press the Esc key, enter :wq, and press Enter to save and close the file.

  3. Run the following command to apply the NIC configuration.

    netplan apply

  4. Run the following command to create a symbolic link for the /run/systemd/resolve/resolv.conf file. This prevents your configuration from being reverted when the /etc/resolv.conf file 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.

  1. 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.

  2. Run the following command to edit the /etc/sysconfig/network-scripts/ifcfg-eth0 configuration file.

    vim /etc/sysconfig/network-scripts/ifcfg-eth0
    1. Press i to 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]
      Note

      Note:

      • 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.

    2. After you make the changes, press the Esc key, enter :wq, and press Enter to save and close the file.

  3. 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 restart

    • CentOS 7/Anolis OS 7: systemctl restart network

    • For the following operating systems, reload all NIC configurations by running nmcli c reload, and then activate the NIC by running nmcli 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:

  1. Run the following command to edit the /etc/network/interfaces configuration file.

    vim /etc/network/interfaces
    1. Press i to 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]
      Note

      Note:

      • 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.

    2. After you make the changes, press the Esc key, enter :wq, and press Enter to save and close the file.

  2. 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:

  1. Run the following command to edit the /etc/sysconfig/network/ifcfg-eth0 configuration file.

    vim /etc/sysconfig/network/ifcfg-eth0
    1. Press i to 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'
      Note

      Note:

      • 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.

    2. After you make the changes, press the Esc key, enter :wq, and press Enter to save and close the file.

  2. Run the following command to edit the /etc/sysconfig/network/routes gateway configuration file.

     vim /etc/sysconfig/network/routes
    1. Press i to switch to edit mode. Add the `default 192.168.X.253` content to the file.

    2. After you make the changes, press the Esc key, enter :wq, and press Enter to save and close the file.

  3. Run the following command to edit the /etc/resolv.conf configuration file.

    vim /etc/resolv.conf
    1. Press i to switch to edit mode. Change the value of the nameserver parameter to your custom DNS IP address.

      nameserver [$DNS1] nameserver [$DNS2]
      Note

      Note: Change the values of both nameserver parameters to your custom DNS IP addresses.

    2. After you make the changes, press the Esc key, enter :wq, and press Enter to save and close the file.

  4. Run the following command to edit the /etc/sysconfig/network/config configuration file.

    vim /etc/sysconfig/network/config
    1. Press i to switch to edit mode. Delete the value of the NETCONFIG_MODULES_ORDER parameter, but keep the double quotation marks (""), as shown below.

      NETCONFIG_MODULES_ORDER=""

    2. After you make the changes, press the Esc key, enter :wq, and press Enter to save and close the file.

  5. 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.

  1. Run the following command to delete the /etc/resolv.conf DNS configuration file.

    rm -rf /etc/resolv.conf

  2. Run the following command to create a new /etc/resolv.conf file and write the DNS configuration to it.

    echo "nameserver 114.114.114.114" > /etc/resolv.conf

Applicable to

  • Elastic Compute Service