Kategorien: Compute Cloud
How to add a second (third, …) network interface to a VM?
By default a VM has only one network interface and the DHCP assignment works out of the box. With two or more interfaces, there’s a problem. Every interface tries to set the default route, but there can be only one. So, the first interface to be set up wins, and it’s not given that it is the first one specified in the template. The solution consists in specifying the priority, or metric, of the interfaces. Each one sets the default gateway given by the DHCP but with a different priority.
If you plan to have more than one network interface in your VM, then give maximum priority (or lowest metric) to the public one, otherwise the VM won’t be reachable. As being said, if the VM has only only one network card, then there is no need to specify a metric.
Now, some details on how to configure the network card of a VM. We consider here some well-known Linux distributions.
Debian, Ubuntu and derivatives
Debian and Ubuntu have only one file, /etc/network/interfaces. A typical example is
auto lo
iface lo inet loopback
auto eth0
iface eht0 inet dhcp
It is obvious that the loopback interface and eth0 are brought up automatically at boot (auto) and then configured.
For each additional interface, the auto statement, the iface definition, and the entry
metric 200
should be added
auto lo
iface lo inet loopback
auto eth0
iface eht0 inet dhcp
auto eth1
iface eht1 inet dhcp
metric 200
Ubuntu 18 and later (Netplan)
version: 2
ethernets:
ens3:
dhcp4: true
match:
macaddress: fa:16:3e:30:36:0c # MAC address of first network interface
mtu: 1500
set-name: ens3
ens6:
dhcp4: true
dhcp4-overrides:
route-metric: 200
match:
macaddress: fa:16:3e:30:36:0d # MAC address of second network interface
mtu: 1500
set-name: ens6
Fedora, CentOS and derivatives
The approach is very similar to SUSE. For each interface there is a script located in /etc/sysconfig/network-scripts, named ifcfg-, such as /etc/sysconfig/network-scripts/ifcfg-eth0 and so on.
The content should be
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
For every other interface, the DEVICE and the file name should be adapted, while the parameter METRIC=200 should be added. METRIC increases for every new interface. A higher metric means a decreasing priority of the route associated to that interface. The public interface has to be the one without the METRIC parameter, that is the one whose default route is the preferred.
SUSE
for each interface there is a script named /etc/sysconfig/network/ifcfg-, such as ifcfg-eth0, ifcfg-eth1 and so on. The content should be
DEVICE=eth0 #this is an example, change to eth1 in ifcfg-eth1 ...
STARTMODE=auto
BOOTPROTO=dhcp4
DHCLIENT_PRIMARY_DEVICE=yes
The value of the DEVICE directive should be adapted accordingly (i.e., eth1) while DHCLIENT_PRIMARY_DEVICE should be changed to no, then save the new file in a consistent way, i.e., ifcfg-eth1.
Remember: the device attached to the public network needs DHCLIENT_PRIMARY_DEVICE=yes, for all the others it should be set to no.