Unix & Linux: How to properly set up 2 network interfaces in CentOS (running in VirtualBox)?
Channel: Roel Van de Paar
Date: 12/08/2020

Unix & Linux: How to properly set up 2 network interfaces in CentOS (running in VirtualBox)?


The Question: I have a VirtualBox machine running CentOS 6.5. I've created 2 network adapters
for it in VirtualBox's networking settings for the machine. The first is NAT
which I want the guest to use to connect to the Internet and the second I set
to Host-only which I will use to SSH and access web server from the host
computer. https://forums.virtualbox.org/viewtopic.php?f=35&t=42385 is what I
want to achieve but I can't seem to find the /etc/network/interfaces file on
CentOS 6.5 so I presume configuration is done differently.
Having setup both NAT and Host-only adapters, I started the VM and did an ip
addr show and I have 2 interfaces, eth0 and eth2 (I don't know why eth1 was
somehow skipped). So I configure both ifcfg-ethX files in /etc/sysconfig/
network-scripts, setting eth0 to be DHCP (for NAT) and eth2 to be static IP
(for Host-only). For eth0, ONBOOT=yes while for eth2, ONBOOT=no. With this
setup I can ping the Internet i.e. ping -c 3 www.google.com and it gets a
response so I know I can connect to the Internet (while eth2 is down).
Next, I ifup eth2. From the host machine, I SSH using the static IP I've set
and I can connect without issue. But when I do ping -c www.google.com this time
around, it fails and I know I've lost my connection to the Internet even if
eth0 is still up. Doing a route -n shows me a tabular output with eth2 taking
up the first and last rows while eth0 is in between (sorry I can't post the
actual output because I deleted the VM out of frustration...).
I've created a new CentOS VM and would like to proceed with the same set-up. My
networking-fu is close to non-existent so I'm a n00b at it. I would like some
tips on how to do it properly (i.e. how to set the NAT adapter to use eth0 and
set the Host-only adapter to use eth1 and not eth2, how to make eth0 still
usable even if eth1/eth2 is up and running).
Update
So I've got the new VM up and running and it seems to work now if I have the
first adapter set to host-only and the second adapter set to NAT. It's set up
like so:
VirtualBox network settings
 * Network adapter 1: Host-only (vboxnet0 IP address is set to 10.3.0.1,
 network mask 255.255.255.0 and DHCP is off)
 * Network adapter 2: NAT (DHCP)
CentOS VM /etc/sysconfig/network-scripts/ifcfg-ethX settings
# ifcfg-eth0
DEVICE=eth0
HWADDR=08:00:27:EC:6C:B9
TYPE=Ethernet
UUID=1339bc30-...
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
DHCPCLASS=
IPADDR=10.3.0.100
NETMASK=255.255.255.0
GATEWAY=10.3.0.1

# ifcfg-eth2
DEVICE=eth2
HWADDR=08:00:27:EB:73:BA
... (Please watch the video for the full question)

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user Hauke Laging (https://unix.stackexchange.com/users/32191), user baktin (https://unix.stackexchange.com/users/8323), and the Stack Exchange Network (http://unix.stackexchange.com/questions/115464). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.