Configure multiple network interfaces in Azure Linux virtual machines

Note

CentOS referenced in this article is a Linux distribution and will reach End Of Life (EOL). Consider your use and plan accordingly. For more information, see CentOS End Of Life guidance.

This article discusses how to configure multiple virtual network interfaces in Azure Linux virtual machines (VMs) that run the most common Linux distributions.

Summary

You can create an Azure VM that has multiple network interfaces attached to it. A common scenario is to have different subnets for front-end and back-end connectivity, or a network that's dedicated to a monitoring or backup solution.

This article provides the required configuration for multiple network interfaces to work in an Azure Linux VM that's based on the following sample scenario:

  • The VM has two or more network interfaces in the same subnet.
  • The VM has two or more network interfaces in different subnets but in the same Virtual Network (VNET).

For details, see the following screenshots:

In each scenario, the connectivity can be tested from any VM in the same VNET.

Important

This same process could also be followed in VMs with more than two NICs.

Configure guest OS for multiple network interfaces

When you add multiple network interfaces to a Linux VM, you have to create routing rules. These rules enable the VM to send and receive traffic that belongs to a specific network interface. Otherwise, traffic can't be processed correctly. For example, traffic that belongs to eth1 can't be processed correctly by the defined default route.

The following sections provides the required configuration for two network interfaces to work in Linux VMs that run the most common Linux distributions.

Note

Run all the commands in the following sections by using root privileges (by switching to the root or by using the sudo command utility).

In each section, assume that the VM has two network interfaces that have any of the following settings:

  • Routing (the output of the sudo ip route show command):

    • Two NICs in the same subnet:

      default via 10.0.1.1 dev eth0 proto static metric 100
      10.0.1.0/24 dev eth0 proto kernel scope link src 10.0.1.4 metric 100
      10.0.1.0/24 dev eth1 proto kernel scope link src 10.0.1.5 metric 101
      168.63.129.16 via 10.0.1.1 dev eth0 proto dhcp metric 100
      169.254.169.254 via 10.0.1.1 dev eth0 proto dhcp metric 100
      
    • Two NICs in different subnets but in the same VNET:

      default via 10.0.1.1 dev eth0 proto static metric 100
      10.0.1.0/24 dev eth0 proto kernel scope link src 10.0.1.4 metric 100
      10.0.2.0/24 dev eth1 proto kernel scope link src 10.0.2.5 metric 101
      168.63.129.16 via 10.0.1.1 dev eth0 proto dhcp metric 100
      169.254.169.254 via 10.0.1.1 dev eth0 proto dhcp metric 100
      
  • Interfaces (the output of the sudo ip address show command):

    • Two NICs in the same subnet:

      lo: inet 127.0.0.1/8 scope host lo
      eth0: inet 10.0.1.4/24 brd 10.0.1.255 scope global eth0
      eth1: inet 10.0.1.5/24 brd 10.0.1.255 scope global eth1
      
    • Two NICs in different subnets but in the same VNET:

      lo: inet 127.0.0.1/8 scope host lo
      eth0: inet 10.0.1.4/24 brd 10.0.1.255 scope global eth0
      eth1: inet 10.0.2.5/24 brd 10.0.2.255 scope global eth1
      
  1. Add two routing tables to the /etc/iproute2/rt_tables file by running the following commands (you need one entry per NIC):

    sudo echo "200 eth0-rt" >> /etc/iproute2/rt_tables
    sudo echo "201 eth1-rt" >> /etc/iproute2/rt_tables
    

    If more network interfaces are attached to the VM, add extra routing tables (for example, 202 eth2-rt, 203 eth3-rt, and so on).

  2. Make sure a configuration file exists for each network interface in the /etc/sysconfig/network-scripts/ directory. You can create new network interface configuration files based on the ifcfg-eth0 configuration file (modify the DEVICE line and remove the DHCP_HOSTNAME and HWADDR lines from the new file). To do this, run the following commands:

    sudo cat /etc/sysconfig/network-scripts/ifcfg-eth0 > /etc/sysconfig/network-scripts/ifcfg-eth1
    sudo sed -i 's/DEVICE=eth0/DEVICE=eth1/' /etc/sysconfig/network-scripts/ifcfg-eth1
    sudo sed -i '/DHCP_HOSTNAME/d' /etc/sysconfig/network-scripts/ifcfg-eth1
    sudo sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth1
    
  3. To make the change persistent and applied during network stack activation, edit the files /etc/sysconfig/network-scripts/ifcfg-eth0 and /etc/sysconfig/network-scripts/ifcfg-eth1 (ifcfg-eth2, ifcfg-eth3, and so on, if the VM has more than two network interfaces) and change the value of NM_CONTROLLED from yes to no. To do this, run the following commands:

    sudo cp -rp /etc/sysconfig/network-scripts/ifcfg-eth0 /tmp/ifcfg-eth0.bkp
    sudo cp -rp /etc/sysconfig/network-scripts/ifcfg-eth1 /tmp/ifcfg-eth1.bkp
    sudo sed -i 's/NM_CONTROLLED=yes/NM_CONTROLLED=no/' /etc/sysconfig/network-scripts/ifcfg-eth0
    sudo sed -i 's/NM_CONTROLLED=yes/NM_CONTROLLED=no/' /etc/sysconfig/network-scripts/ifcfg-eth1
    

    Note

    Verify that the NM_CONTROLLED=no line is added to both the /etc/sysconfig/network-scripts/ifcfg-eth0 and /etc/sysconfig/network-scripts/ifcfg-eth1 files by using the cat /etc/sysconfig/network-scripts/ifcfg-eth* command. If the line isn't in the files, add it manually by using the sudo echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0 and sudo echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-eth1 commands.

  4. After modifying this configuration, restart network services to apply the changes by running the following command:

    sudo systemctl restart network
    
  5. Create corresponding rule and route files by using any text editor (in the following examples, the vi editor is used), and add appropriate rules and routes to each file. Use the following steps to create one set of rule-eth# and route-eth# files per network interface. Replace the IP address and subnet information accordingly in every step. If you have more network interfaces, create the same set of rule-eth# and route-eth# files for each interface by using the corresponding IP address, network, and gateway details.

    • Create rules and routes for eth0:

      1. To create the rule file for eth0, open the file /etc/sysconfig/network-scripts/rule-eth0.

        sudo vi /etc/sysconfig/network-scripts/rule-eth0
        
      2. Add the following content to the rule file. Replace the IP address accordingly, make sure you specify the IPv4 address in the configuration, and preserve the 32-bit value:

        from 10.0.1.4/32 table eth0-rt
        to 10.0.1.4/32 table eth0-rt
        
      3. To create the route file for eth0, open the file /etc/sysconfig/network-scripts/route-eth0.

        sudo vi /etc/sysconfig/network-scripts/route-eth0
        
      4. Add the following content to the route file. Replace the network and gateway values accordingly.

        10.0.1.0/24 dev eth0 table eth0-rt
        default via 10.0.1.1 dev eth0 table eth0-rt
        
    • Create rules and routes for eth1:

      1. To create the rule file for eth1, open the file /etc/sysconfig/network-scripts/rule-eth1.

        sudo vi /etc/sysconfig/network-scripts/rule-eth1
        
      2. Add the following content to the rule file. Replace the IP address accordingly, make sure that you specify the IPv4 address in the command, and preserve the 32-bit value.

        • Two NICs in the same subnet:

          from 10.0.1.5/32 table eth1-rt
          to 10.0.1.5/32 table eth1-rt
          
        • Two NICs in different subnets but in the same VNET:

          from 10.0.2.5/32 table eth1-rt
          to 10.0.2.5/32 table eth1-rt
          
      3. To create the route file for eth1, open the file /etc/sysconfig/network-scripts/route-eth1.

        sudo vi /etc/sysconfig/network-scripts/route-eth1
        
      4. Add the following content to the route file. Replace the network and gateway values accordingly.

        • Two NICs in the same subnet:

          10.0.1.0/24 dev eth1 table eth1-rt
          default via 10.0.1.1 dev eth1 table eth1-rt
          
        • Two NICs in different subnets but in the same VNET:

          10.0.2.0/24 dev eth1 table eth1-rt
          default via 10.0.2.1 dev eth1 table eth1-rt
          
  6. To apply the changes, run the following command to restart the network service:

    sudo systemctl restart network
    

    The routing rules are now correctly set, and connectivity should work from any network interface. You can test the connectivity by using Secure Shell (SSH) or pinging both IPs from a VM in the same VNET.

  7. Verify that the current routes and rules are loaded by using the following commands:

    sudo ip route show
    sudo ip rule show
    

Important

If you still have issues communicating with the second NIC, restart the VM by using the sudo reboot command, repeat step 7, and test the connectivity again.

Contact us for help

If you have questions or need help, create a support request, or ask Azure community support. You can also submit product feedback to Azure feedback community.