07.06.2023

How to Configure Multiple Network Interfaces on Ubuntu 18.04

To ensure the correct operation of several network interfaces with public IP addresses on Ubuntu 18.04 at the same time, it is not enough just to write basic parameters for them in Netplan. In this case, all packets will leave the server through the default gateway, which means that the server will be available only at one public IP address. This is the minus of routing by the destination address. Here we will add the necessary settings in Netplan to make multiple network interfaces work correctly on Ubuntu 18.04.

Disable Netplan autotuning

When you create an Ubuntu 18.04 server in Serverspace or add new network interfaces to it from the control panel, the connection parameters are automatically configured. In addition, during a system reboot, the Netplan configuration file is also overwritten with the actual parameters. Therefore, any changes made in manual mode will be lost. To avoid this, let’s disable this function.
First, make sure that you have added the required number of network adapters to the server, and then create a file:

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

With the following content:

network: {config: disabled}

After saving the file, the function of automatic configuration of network interfaces will be disabled.

Routing for multiple network interfaces

The task of ensuring the availability of the server via multiple network connections and IP addresses will be resolved with the use of policy-based routing, namely on the basis of the sender's address. Thus, each packet will be sent from the server through the same address through which it got to it. First of all, you need to create routing tables for this purpose, and for this you need the iproute2 package. If it is not present in the system, you need to install it:

apt-get install iproute2

Now open the file:

nano /etc/iproute2/rt_tables

It is necessary to add as many tables to its end as there are network interfaces to be configured. One table is a single line of two numbers separated by a space. The first number is the priority and the second is the table name. For example:

60 60
61 61
62 62

Thus, we will add 3 tables. Now it's time to add routing settings to Netplan, open its config at /etc/netplan/ with the .yaml extension (the file name may differ on your system):

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

Netplan is sensitive to formatting in the config file. It is very important to keep the correct number of spaces and not to use tabs, otherwise the result will not pass the check. A valid example is shown below. It shows a block of settings for one interface (enp0s5), you will see the same blocks in the config for each of the connections. You need to add the routes and routing-policy settings in each of the blocks, and leave the rest of the settings as they are. Explanation of the added values:

  • to - any destination subnet IP - 0.0.0.0/0
  • via - IP gateway of the current network connection
  • table - name of one of the tables added to rt_tables. Must match for routes and routing-policy of each connection. But for different interfaces - different tables.
  • from - IP address of this interface
  • priority - can be left as is
network:
    version: 2
    ethernets:
        enp0s5:
            addresses:
            - 12.34.56.78/24
            gateway4: 12.34.56.1
            match:
                macaddress: ee:ff:gg:hh:ii:jj
            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: 12.34.56.1
                  table: 60
            routing-policy:
                - from: 12.34.56.78
                  table: 60
                  priority: 300

After finishing editing the configuration, let’s make sure it is correct:

netplan generate

If the output is empty, then everything was done correctly. If there are errors, you will see something like this:

/etc/netplan/50-cloud-init.yaml:24:1: Invalid YAML: tabs are not allowed for indent:
		- to: 0.0.0.0/0
^

Tabs were found in this example. After fixing the errors and successfully passing the validation, apply the changes:

netplan apply

Now the server will be accessible to all IP addresses configured in this way.