Question: How can I make my secondary network interface work in my Ubuntu EC2 instance?

I want to make my secondary network interface work in my Ubuntu Amazon Elastic Compute Cloud (Amazon EC2) instance.

Short descriptionWarning: The use of a second elastic network interface is intended for advanced users. Do this only if you can't use a single network interface and you must attach two network interfaces from the same subnet to one instance. To avoid asymmetric routing issues, use a single elastic network interface, or place duplicate elastic network interfaces into non-overlapping subnets.Adding a secondary network interface to a non-Amazon Linux EC2 instance causes traffic flow issues. These issues occur because the primary and secondary network interfaces are in the same subnet, and there's one routing table with one gateway. Traffic that comes into the secondary network interface leaves the instance using the primary network interface. But this isn't allowed, because the secondary IP address doesn't belong to the MAC address of the primary network interface.To make the secondary interface work after creating it, do the following:1.    Configure the routing table.2.    Set up rules in the custom routing table policy database so that traffic for the secondary interface uses the new routing table.Before you start, note that Amazon EC2 instances are in the AWS Cloud. This means that not all use-cases benefit from having multiple interfaces. The following examples demonstrate situations when you might not need to use a secondary network interface:Increasing network throughput: Because limits are set based on the instance type and size, network throughput doesn't increase. For more information, see Amazon EC2 instance types.Increasing Elastic IP addresses: If you have few Elastic IP addresses per interface, you might not need to add more interfaces to get more Elastic IP addresses. Most applications work well with the Domain Name System. For example, Apache can use name-based virtual hosts (on the Apache website).Note: The steps for adding a secondary network interface are different for each of the following Ubuntu versions:Ubuntu 14.04Ubuntu 16.04Ubuntu 18.04Ubuntu 20.04ResolutionNote: Although the steps in the following resolution are tested on the listed Linux versions, you might need slight modifications for your configuration's custom settings.Configuring Ubuntu 14.04 or 16.04You can create the secondary interface configuration file, configure the routing table, and set routing policy rules for Ubuntu using a single file.All procedures must be run with root user privileges. Either become the root user with sudo -i or run all commands with sudo.1.     To get the name of the primary network interface, run the following command:ip a | grep ^[[:digit:]]You receive an output that's similar to the following message:1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 10002: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc pfifo_fast state UP group default qlen 10003: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000Important: In this example, the primary interfaces are named eth0, eth1, and so on. However, for instances that support enhanced networking, such as the m4 and m5 family type, you might see a naming inconsistency. For example, the primary might be named ens3 if the secondary is named eth0. This naming inconsistency happens when you add the secondary interface while the instance is running.To avoid a naming inconsistency, add the interface at launch time or reboot the instance. Or, if the interface is running, you can change the name using the following command:ip link set eth0 name ens4 && ip link set ens4 up2.    Create a configuration file for the secondary interface. In the following example, change eth1 to match your secondary interface name as found in step 1:vi /etc/network/interfaces.d/51-eth1.cfgThe following command is an example for a single IP address of 172.31.21.115 and with a gateway of 172.31.16.1 on the secondary interface. Replace the example IP address and gateway with your own. Also note that your gateway must be the first valid IP address in your subnet:auto eth1iface eth1 inet static address 172.31.21.115netmask 255.255.240.0# Gateway configurationup ip route add default via 172.31.16.1 dev eth1 table 1000# Routes and rulesup ip route add 172.31.21.115 dev eth1 table 1000up ip rule add from 172.31.21.115 lookup 1000The following command is an example for multiple IP addresses. In this example, the IP addresses are 172.31.21.115 and 172.31.18.46, and the gateway is 172.31.16.1. Replace the example IP addresses and gateway with your own:auto eth1# Enter one or more IP settingsiface eth1 inet staticaddress 172.31.21.115netmask 255.255.240.0iface eth1 inet staticaddress 172.31.18.46netmask 255.255.240.0# Default gateway for eth1up ip route add default via 172.31.16.1 dev eth1 table 1000# A route for every IPup ip route add 172.31.21.115 dev eth1 table 1000up ip route add 172.31.18.46 dev eth1 table 1000# A policy rule for every IPup ip rule add from 172.31.21.115 lookup 1000up ip rule add from 172.31.18.46 lookup 10003.    Create the restrict-default-gw file to prevent the default gateway from being overwritten on the main table:vi /etc/dhcp/dhclient-enter-hooks.d/restrict-default-gw4.    Add the following lines to the restrict-default-gw file. Be sure to change eth0 to your primary interface name as found in step 1:case ${interface} in  eth0)    ;;  *)    unset new_routers    ;;esac5.    Restart the network.See the following command for Ubuntu 14.04:(ifdown eth1 && ifup eth1)See the following command for Ubuntu 16.04:systemctl restart networkingConfiguring Ubuntu 18.04 and 20.04Ubuntu 18.04 and 20.04 use the Netplan networking configuration. Note that Netplan uses YAML format. This means that indentation is crucial. The following Netplan example uses two-space indentation.Note: Run all commands with root user privileges. Either become the root user with sudo -i or run all commands with sudo.1.    Create a configuration file for the secondary interface:vi /etc/netplan/51-eth1.yaml2.    Add the following lines to the 51-eth1.yaml file. Make sure you edit the following example to match your use case:network:  version: 2  renderer: networkd  ethernets:    eth1:      addresses:       - 172.31.24.153/20       - 172.31.28.195/20      dhcp4: no      routes:       - to: 0.0.0.0/0         via: 172.31.16.1 # Default gateway         table: 1000       - to: 172.31.24.153         via: 0.0.0.0         scope: link         table: 1000       - to: 172.31.28.195         via: 0.0.0.0         scope: link         table: 1000      routing-policy:        - from: 172.31.24.153          table: 1000        - from: 172.31.28.195          table: 1000This example YAML file configures two IP addresses on the secondary interface (eth1).Note: To find the CIDR range to use in the YAML file, complete the following steps:1.    Open the Amazon EC2 console, select Instances, and then select the instance.2.    On the Networking tab, scroll to Network interfaces, and note the Subnet ID of your secondary network interface.3.    Open the Amazon Virtual Private Cloud (Amazon VPC) console, select Subnets, and then note the IPv4 CIDR range listed for the subnet ID.4.    Apply the network configuration:netplan --debug apply

Related informationConfigure route tables