Page 1 of 1

Two-way host/guest networking with static IP on guest VM

Posted: 10. Apr 2009, 22:26
by jamie.krug
I'm trying to find a way to allow two-way communication between host and guest VMs where the configuration will work on any network I connect to with my laptop host. I very quickly accomplished this with VirtualBox 2.2 bridged networking, but the IPs were dependent upon the DHCP server of whichever network my laptop connects to. This means that I can't necessarily set a static IP on my guest and have it work on all networks (this is required so I can add hosts entries on my host to point to Web apps on my guest VMs).

I'm relatively new to VirtualBox, and still relatively new to Linux. I'm trying to setup my ideal Web development environment on my laptop using VirtualBox 2.2 on my Ubuntu 8.04 Desktop (32-bit) host. My host is where I do my coding and where various application code lives. I'm using guest VMs (usually Ubuntu 8.04 Server) to test application code various Web server and database configurations.

So, here's what I'd like to be able to do:
1. Connect to any guest VM from host (e.g., to browse to VM Web servers to test Web apps).
2. Guest VMs connect to one another (e.g., Web server on one VM connecting to a database server on another VM).
3. Host and all guests need to connect to the Internet (for OS updates, downloads, etc.).

I've spent quite a bit of time reading up on VirtualBox networking and Linux (Ubuntu) network configurations, and I can achieve all of the above in a couple ways, but what I'm not sure there's a way to have the host always access the guest VMs on consistent (static) IPs, and have it work on any network that my laptop connects to.

After trying the bridged networking option, I tried using two adapters on each guest -- NAT for connecting to the outside world (and also back to host) and Internal for guest VM to guest VM communication (with static IPs) -- but this model does not allow my host to ping the guest VMs.

Should I just give up this quest and choose static guest IPs that will work on *most* networks, with bridged networking, or is there a "silver bullet" to have my cake and eat it too? ;-)

Thanks!
Jamie

Re: Two-way host/guest networking with static IP on guest VM

Posted: 10. Apr 2009, 22:32
by vbox4me2
Your easiest solution is to put a router between your laptop and the lan you connect to and use a not commonly used local ip range on your end of the router, plug the wan side into any foreign lan. This can also be done in software but I prefer the 30$ router solution anyday.

Re: Two-way host/guest networking with static IP on guest VM

Posted: 10. Apr 2009, 23:55
by Sasquatch
If I understand the new Host-Only option correctly, adding several VMs to it will get you point 1) and 2). Poing 3), the internet, is done through adding a second NIC to each VM and put it on NAT mode.

Re: Two-way host/guest networking with static IP on guest VM

Posted: 11. Apr 2009, 03:09
by jamie.krug
@vbox4me2: That's a very interesting solution, and I thank you for it, but I'd prefer to avoid carrying a router with my laptop (lazy:).

@Sasquatch: You're absolutely right! I read the 2.2 user manual's networking section like 5 times and it just seemed vague and confusing, especially the new host-only section (at least for a n00b). In any case, I think I have exactly what I was looking for...

Thank you both for helping a n00b (and quick!). Here's what I've done, in case this might help other newbies out there...

For each of two guest VMs (one Ubuntu 8.04 Desktop 32-bit and one Ubuntu 8.04 Server 64-bit) I set two network adapters; the first NAT and the second Host-only.

I then ran the ifconfig command from my host OS (Ubuntu 8.04 Desktop) terminal and noticed this new virtual network adapter, supplied by VirtualBox 2.2's Host-only Network:

Code: Select all

vboxnet0  Link encap:Ethernet  HWaddr 00:76:62:6e:65:74  
          inet addr:192.168.56.1  Bcast:192.168.56.255  Mask:255.255.255.0
So, I took note of the 192.168.56.1, and so I added the following to my first guest VM's /etc/network/interfaces file to reflect a static IP in this range:

Code: Select all

auto eth1
iface eth1 inet static
	address 192.168.56.2
	netmask 255.255.255.0
	gateway 10.0.2.2
I made a similar change for my second guest VM, only using the next IP in the range, 192.168.56.3. Also notice the gateway of 10.0.2.2 -- this is the IP of the host via the first NAT adapter. I learned from http://opensourceexperiments.wordpress. ... ista-host/ that this allows the guests Internet access, but I don't think I fully understand this part.

There it is! With a "sudo /etc/init.d/networking restart" of my guest VM networks, I'm now able to:
  • Ping both guests from the host (192.168.56.2 and 192.168.56.3);
    Ping each guest from the other;
    Ping the host from each guest (at both 192.168.56.1 and 10.0.2.2);
    Access the Internet from host and all guests (assuming the host has an Internet connection:) -- this works fine with my laptop wired or on wireless.
Cheers,
Jamie

Re: Two-way host/guest networking with static IP on guest VM

Posted: 11. Apr 2009, 15:34
by Sasquatch
I strongly advise you to remove the gateway line in your interfaces file. By having that gateway, all traffic to any other machine will go through the NAT interface first. A gateway is never needed on a Peer to Peer network. Get a couple of computers, hook them on a switch, set an IP and Netmask only and they will all be able to access each other, without the gateway. A gateway is needed if you need to go to a different network, like internet.

Re: Two-way host/guest networking with static IP on guest VM

Posted: 11. Apr 2009, 16:03
by vbox4me2
Sasquatch wrote:A gateway is needed if you need to go to a different network, like internet.
A gateway is used to go via a different network then the current one IF not superseeded by routing rules AND if metric values with higher values do not superseed the chosen one, routing rules can superseed metric values.

Re: Two-way host/guest networking with static IP on guest VM

Posted: 11. Apr 2009, 16:13
by Sasquatch
vbox4me2 wrote:
Sasquatch wrote:A gateway is needed if you need to go to a different network, like internet.
A gateway is used to go via a different network then the current one IF not superseeded by routing rules AND if metric values with higher values do not superseed the chosen one, routing rules can superseed metric values.
Still, it's not needed for that interface/network. No need to make it more complicated.

Re: Two-way host/guest networking with static IP on guest VM

Posted: 12. Apr 2009, 20:29
by jamie.krug
Sasquatch wrote:
I strongly advise you to remove the gateway line in your interfaces file. By having that gateway, all traffic to any other machine will go through the NAT interface first. A gateway is never needed on a Peer to Peer network. Get a couple of computers, hook them on a switch, set an IP and Netmask only and they will all be able to access each other, without the gateway. A gateway is needed if you need to go to a different network, like internet.
Like I said, I didn't quite understand the need for that gateway, but it was the first I tried, based on another person's post (for an older version of VirtualBox) and it worked :) It makes sense that Internet access would be routed through the NAT interface first, as you've stated. I've removed the gateway line from the interfaces file of both my guest VMs and all still works great, as desired. Guests can still talk to one another, and the host, and Internet. The host is also able to access both guests.

Thanks!