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.
When creating virtual machines, especially virtual linux servers, I want them to be able to do the following things:
- Connect to the Internet
- Connect over SSH from my Host Machine to the Guest Machine
- 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
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.
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”
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.
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).
as same as VMWare ! it’s so easy!
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.
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
sudo ufw deny from any
sudo ufw allow from 192.168.1.10
sudo ufw allow from 192.168.1.11
hımm i agree
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.
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.
Thanks a lot !! Sometimes we miss the obvious 🙂
Thanks a lot.
Great article! Worked like a charm! 🙂