VirtualBox Networking Setup

Every time I go to set up VirtualBox and several VMs I always forget how I setup the networking for them. Once again, I went through the pain of trying to figure it all out, so I might as well document it for future use.

The Problem

When creating virtual machines, especially virtual linux servers, I want them to be able to do the following things:

  1. Connect to the Internet
  2. Connect over SSH from my Host Machine to the Guest Machine
  3. Have the servers connect to each other on a private internal network

Out of the box, a VM is configured for #1, however it cannot do #2 and #3. There are some solutions that do some archaic port forwarding by issuing special commands from the Terminal. This is just painful, and if you just want one port to work, that’s manageable, but what if you want ssh, ftp, myql, http, https, etc? Any on multiple VMs? What a nightmare! What is painful is when googling for a solution, almost every “answer” is this crazy port forwarding method. There is a much cleaner, better way!

Solution – Host to Guest Connectivity

Screen shot 2010-03-22 at 12.15.38 AM

Go into your VM’s settings and go to “Network”, it will list options for 4 different adapters. Instead of hacking one adapter to do all the work, just setup more adapters for your configuration. For adapter #2, set it to Host-only. This enables an IP range for communicating from the Host to the Guest and vice versa. Start up your VM, and if you are using something like Ubuntu, edit your /etc/network/interfaces file so it looks like this:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

### ADD THESE LINES
auto eth1
iface eth1 inet dhcp

After adding these lines, run the command:

sudo /etc/init.d/networking restart

Now VirtualBox is configured to host this traffic over the 192.168.56.x IP range (I’m not sure if/where you can change this). So do an ifconfig on your guest machine, and it should show you the IP it has been assigned. If you want, you can change your /etc/network/interfaces so it can be statically assigned. You should now be able to SSH into your box on it’s IP:

ssh -l justin 192.168.56.101

Solution – Guest to Guest Communication

Now, what if I have a Cassandra server, a Web Server, and a MySQL server each on their own VMs. What you do is create a third adapter set to “Internal Network”. Then in your /etc/network/interfaces file you set eth2 to a static IP of your choosing. I typically use the 10.0.0.x range. So each server is respectively 10.0.0.10, 10.0.0.20, and 10.0.0.30. However, over the 10.0.0.x range there is no DHCP, no Gateway, no nothing, just direct communications over these IPs.

Conclusion

So please, for the love of everything good, don’t resort to crazy port forwarding, complicated XML file editing, solutions for VirtualBox. Just use each type of interface configuration for what you need. Thank you!

11 thoughts on “VirtualBox Networking Setup

  1. I change the network adapter settings for the VM to use the “Bridged Adapter”, which has worked great so far. If you are on a private network (192.168.X.X, 10.X.X.X, etc.) that is already handing out IPs via DHCP then bridged networking is a snap.

    Like

  2. For automating port-forwarding configuration, I’ve created VBoxTool: http://vboxtool.sourceforge.net/
    Within one configuration file, you can control *all* port forwarding ports for *all* sessions all at once, issued by only one command (vboxtool autostart).

    Like

  3. as same as VMWare ! it’s so easy!

    Like

  4. I usually install a virtual instance of Vyatta 6.0 to link the host only network to the internal only networks. That way I get to play routing too.

    Like

  5. As pointed out by Joseph Scott, if all you want to do is to enable #1, #2, and #3 for the VM, just use “Bridged Adapter”.

    If one needs control of types of network access, an easy way is to apply ufw on a “bridged adapter”. Say you have two virtual machines:
    1) full Internet access on VM1 (inbound+outbound)
    2) outbound Internet only on VM2
    3) full network between VM1, VM2 and host
    Assuming the host has IP 192.168.1.10, VM1 is 192.168.1.11 and VM2 is 192.168.1.12, all you need to do is:
    on VM1 and host: nothing
    on VM2:
    sudo ufw deny from any
    sudo ufw allow from 192.168.1.10
    sudo ufw allow from 192.168.1.11

    Like

  6. Hi Justin

    Just wanted to say thank you – this post helped me.

    Something that had me stumped for a while and could be useful to others….

    After all the messing about and experimenting with Windows 2003 networking configurations, I couldn’t ping one of my virtual machines, but IT could ping the other – anyway.

    I eventually realised I had Windows Firewall turned on the machine I couldn’t ping. Basic, I know, I am new.

    Thanks again
    Mulk

    Like

  7. Many thanks for this. I just started using Virtual Box again after a break of several years. Last time I needed to communicate from host to guest, I spent the day twiddling with very fiddly and complicated instructions. Your way is a LOT easier and took a couple of minutes.

    Like

  8. Thanks a lot !! Sometimes we miss the obvious 🙂

    Like

  9. Thanks a lot.
    Amazing article!

    Like

  10. Christoffer Palm July 4, 2012 — 11:10 am

    Great article! Worked like a charm! 🙂

    Like

Leave a Reply to Gary Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close