Need help?

Our experts have had an average response time of 11.7 minutes in August 2021 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

Setup Secondary Network Interface in Ubuntu EC2

by | Aug 15, 2021

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.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF