Wondering how to setup a Secondary Network Interface in Ubuntu EC2? We can help you.
Here, at Bobcares, we assist our customers with several AWS queries as part of our AWS Support Services.
Today, let us see how we can make the secondary network interface work in the Ubuntu Amazon EC2 instance.
Secondary Network Interface in Ubuntu EC2
In order to make the secondary interface work after we create it, we need to perform the following:
- Configure the routing table.
- Set up rules in the custom routing table policy database so that traffic for the secondary interface uses the new routing table.
Setup Secondary Network Interface in Ubuntu EC2
Moving ahead, let us see how our Support Techs perform this for our customers.
Configure Ubuntu 14.04 or 16.04
With a single file, we can create the secondary interface configuration file, configure the routing table, and set routing policy rules for Ubuntu.
To do so, we need to either become the root user with sudo -i or run all commands with sudo.
1. Initially, to get the name of the primary network interface, we run:
ip a | grep ^[[:digit:]]
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc pfifo_fast state UP group default qlen 1000 3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
Here, the primary interfaces are eth0, eth1, and so on.
However, for instances that support enhanced networking, we may find naming inconsistency
To avoid inconsistency we can add the interface at launch time, or reboot the instance.
Or, if the interface is running, we can change the name using:
ip link set eth0 name ens4 && ip link set ens4 up
2. Then we create a configuration file for the secondary interface.
vi /etc/network/interfaces.d/51-eth1.cfg
The following is an example for a single IP address.
Here, we need to change the IP address and gateway to our scenario. Also, note that the gateway must be the first valid IP address in the subnet.
auto eth1
iface eth1 inet static
address xxx.xx.xx.xxx
netmask xxx.xxx.xxx.0
# Gateway configuration
up ip route add default via xxx.xx.xx.x dev eth1 table 1000
# Routes and rules
up ip route add xxx.xx.xx.xxx dev eth1 table 1000
up ip rule add from xxx.xx.xx.xxx lookup 1000
The following is an example of multiple IP addresses.
auto eth1
# Enter one or more IP settings
iface eth1 inet static
address xxx.xx.xx.xxx
netmask xxx.xxx.xxx.x
iface eth1 inet static
address xxx.xx.xx.xx
netmask xxx.xxx.xxx.x
# Default gateway for eth1
up ip route add default via xxx.xx.xx.x dev eth1 table 1000
# A route for every IP
up ip route add xxx.xx.xx.xxx dev eth1 table 1000
up ip route add xxx.xx.xx.xx dev eth1 table 1000
# A policy rule for every IP
up ip rule add from xxx.xx.xx.xxx lookup 1000
up ip rule add from xxx.xx.xx.xx lookup 1000
3. After that, we 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-gw
4. We add the following lines to the restrict-default-gw file.
case ${interface} in
eth0)
;;
*)
unset new_routers
;;
esac
5. Eventually, we restart the network:
Ubuntu 14.04:
(ifdown eth1 && ifup eth1)
Ubuntu 16.04:
systemctl restart networking
Configure Ubuntu 18.04 and 20.04
1. Initially, we create a configuration file for the secondary interface:
vi /etc/netplan/51-eth1.yaml
2. We add the following lines to the 51-eth1.yaml file:
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: 1000
The preceding example YAML file configures two IP addresses on the secondary interface (eth1).
To find the CIDR range to use in the YAML file, our Support Techs recommend the steps below:
1. Open the Amazon EC2 console, go to Instances, and then select the instance.
2. On the Networking tab, scroll to Network interfaces and note the Subnet ID of the secondary network interface.
3. Open the Amazon VPC console, select Subnets, and then note the IPv4 CIDR range listed for the subnet ID.
4. Finally, we apply the network configuration:
netplan --debug apply
[Need help with the setup? We’d be happy to assist]
Conclusion
In short, we saw how our Support Techs configure secondary networks interface for our customers.
where should i get this IP?
– 172.31.24.153/20
– 172.31.28.195/20
Hi,
These are the ip ranges to setup network interfaces for ethernet.
Please contact our support team via live chat, if you are having any issues with the setup.