02.05.2021

How to Configure Multiple Network Interfaces on Ubuntu 20.04

In situations where there are 2 or more network interfaces with public IP addresses on the Ubuntu 20.04 server, additional settings must be made to ensure the server is available at these additional addresses. This is because the default routing policy sends all network packets through 1 interface, which has a default route. In this case, it does not matter through which interface the packet got to the server. Therefore, even with the correct Netplan configuration, the server will not respond to additional IP addresses without additional settings. Let’s take a look at how to configure multiple network interfaces in Ubuntu 20.04.

Netplan automatic configuration

Linux Serverspace servers are designed to make life easier for users. They have the function of configuring network interfaces in automatic mode when they are added in the control panel or during server creation. But in order to ensure the operation of policy-based routing and as a result of the server’s availability on several network interfaces at once, we need to make changes to the Netplan settings. To prevent our settings from being overwritten during a reboot, we need to disable automatic network configuration. After you make sure that the required number of interfaces has been added to the server, create a file on it:

nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

And add a parameter to it:

network: {config: disabled}

Now subsequent changes in Netplan will not be automatically overwritten on reboot.

Configuring Policy-Based Routes

Policy-Based routing powered by tables. Each table has its own routes and policies. Let’s check the presence of the iproute2 package in the system. If not, install it.

apt install iproute2

Open the file with routing tables:

nano /etc/iproute2/rt_tables

And add at the end the number of tables corresponding to the amount of interfaces you are configuring:

800 800
801 801

Record format: priority (number) space table name (number).
Now open the Netplan file with network interface settings. It is located along the path /etc/netplan/ and has the .yaml extension. In my case, the path is as follows:

nano /etc/netplan/50-cloud-init.yaml

Each interface has a settings block in this file. At the end of this block, we will add settings for policies and routing rules for the interface. In this file, it is important to observe the number of spaces before each parameter and value, and not to use tabs instead of spaces. Focus on indentation in existing entries and add by analogy.

For each network interface, add routes and routing-policy blocks. The final record for 1 interface will look like this:

network:
    version: 2
    ethernets:
        enp0s5:
            addresses:
            - 78.89.90.12/24
            gateway4: 78.89.90.1
            match:
                macaddress: 54:43:32:21:10:09
            mtu: 1500
            nameservers:
                addresses:
                - 8.8.8.8
                - 1.1.1.1
                - 8.8.4.4
                search: []
            set-name: enp0s5
            routes:
                - to: 0.0.0.0/0
                  via: 78.89.90.1
                  table: 800
            routing-policy:
                - from: 78.89.90.12
                  table: 800
                  priority: 300

For other interfaces, the entries will be the same, starting with the interface name (enp0s5), but others should not have the gateway4 parameter, since there should be one default gateway. Routes and routing-policy settings:

  • to — destination address of the packet. For our purposes — any (0.0.0.0/0)
  • via — gateway address for this interface
  • table — name of the routing table. The same values must be specified in routes and routing-policy for one interface, but be different from interface to interface. We created these tables in the rt_tables file
  • from — the source address of the package. For the purposes of this manual, this is the IP address of the interface
  • priority — a required numeric value

After making the settings and saving the file, check the configuration for errors:

netplan generate

Output like this will indicate an error if present (in this case, a space is missing.):

/etc/netplan/50-cloud-init.yaml:23:12: Invalid YAML: inconsistent indentation:
           routes:
           ^

If the config is compiled correctly, then the output will be absent. This means it’s time to apply the changes:

netplan apply

After applying the changes, the network interfaces configured in this way will become available from the outside at their public IP addresses.