Centos – How to properly set up 2 network interfaces in CentOS (running in VirtualBox)

centosnetworkingvirtualbox

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. This tutorial over at the VirtualBox forums 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
TYPE=Ethernet
UUID=1339bc30-...
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp

Result of ip route

10.0.3.0/24  dev  eth2  proto  kernel  scope  link  src  10.0.3.15
10.3.0.0/24  dev  eth0  proto  kernel  scope  link  src  10.3.0.100
169.254.0.0/16  dev  eth0  scope  link  metric  1002
169.254.0.0/16  dev  eth2  scope  link  metric  1003
default via 10.0.3.2 dev eth2

Result of ip addr

1:  lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
     inet6 ::1/128 scope host
        valid_lft forever preferred_lft forever
2:  eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link/ether 08:00:27:ec:6c:b9 brd ff:ff:ff:ff:ff:ff
     inet 10.3.0.100/24 brd 10.3.0.255 scope global eth0
     inet6 fe80::a00:27ff:feec:6cb9/64 scope link
        valid_lft forever preferred_lft forever
3:  eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link/ether 08:00:27:eb:73:ba brd ff:ff:ff:ff:ff:ff
     inet 10.0.0.15/24 brd 10.0.3.255 scope global eth2
     inet6 fe80::a00:27ff:feeb:73ba/64 scope link
        valid_lft forever preferred_lft forever

Best Answer

I guess the problem is (or: was) that the gateway definition collides with DHCP. Somehow the interface order seems to influence that.

The basic problem is: How shall routing be done with two network cards? As I understand you the host-only card shall be used for connections to the host only. Thus you should not define any gateway for this interface at all. But maybe this works only if the first interface uses DHCP.

In addition you may consider avoiding DHCP completely. The NAT interface works with a static configuration, too.