Linux – Virtual box linux routing

linuxnat;networkingvirtualbox

Sorry for the basic question but I cant figure this one out. I want to set up a small network of linux servers for testing purposes.

So I have a host server running virtual box with the following interface:

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.4  netmask 255.255.255.0  broadcast 192.168.0.255

Then a guest vm with the following networking set up:

eth0      Link encap:Ethernet  HWaddr 08:00:27:EA:15:4F  
          inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0

eth1      Link encap:Ethernet  HWaddr 08:00:27:E3:E2:BC  
          inet addr:172.16.0.1  Bcast:172.16.7.255  Mask:255.255.248.0

And a second vm guest set up as follows:

eth0      Link encap:Ethernet  HWaddr 08:00:27:15:CA:14  
          inet addr:172.16.0.2  Bcast:172.16.7.255  Mask:255.255.248.0
          inet6 addr: fe80::a00:27ff:fe15:ca14/64 Scope:Link

I want to be able to route from vm 2 back to the host server. So I created a route telling vm 2 to send traffic for the 192.169.0.0 network via vm 1:

% route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     172.16.0.1      255.255.255.0   UG    0      0        0 eth0
172.16.0.0      0.0.0.0         255.255.248.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

But I can not ping through to the 192.168.0.0 network from vm 2. Routing table on vm 1 is as follows:

% route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
172.16.0.0      0.0.0.0         255.255.248.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0

the routing table on the host server (running virtual box) is :

% route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 wlan0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

so I guess my problem is that the host server knows nothing of my VM's 172.16.0.0/16 network and can't reply.

How can I fix this, will I have to use iptables and NATing?

Best Answer

I've only ever done this with a windows host, and Linux Guests, but the principle should be the same.

Check the following link, specifically on Host-only networking

http://www.virtualbox.org/manual/ch06.html#networkingmodes

I didn't need to edit the routes at all with this method.

Another route you might want to explore is using Vagrant. http://www.vagrantup.com/ Its excellent for testing out different configurations & setting up new VMs really quickly. Good luck