Tuesday, April 26, 2016

Networking VirtualBox Ubuntu VM Guests On OS X Host

This is something I was trying to set up since the last few weeks for a Data Architecture & Design POC I am currently working on.
All the help I could muster from researching the web, though was useful in a lot of ways yet somehow not complete enough to get me to my objective without a bit of a struggle...maybe my lack of NW expertise stood in the way:-)
Nonetheless, now that I have effectively cracked this riddle I thought what better way to give back to the community I borrowed, learned and benefitted from than to publish a complete set of instructions on 'how to'.
The PlatformOS X El Capitan 10.11.4 (15E65) host OS on my MacBook Air (Early 2015) host laptop PC + VirtualBox 5.0.18 r106667 (100% free & awesome virtualization SW from Oracle) on the host + Ubuntu Xenial (16.04) server OS (100% free & awesome Linux distro from Ubuntu) based guest VM.
Even though the following instructions are specific to the above platforms I used and tested this set up on, there's no reason for me to believe that the concepts are likely to be radically different for any other similar platforms based on UNIX and / or Linux with any other virtualization SW on the same.
Step 1: Set up your VirtualBox SW and then your Ubuntu VM. I'll not delve into this as the focus of the topic is the VPN set up once your guest VM is up and running on your host environment. So once you are all set with that, please proceed to Step 2 below.
Step 2: First, shutdown your VM in order to be able to make any virtual HW alterations to it. By default VirtualBox NATs the primary NIC for your VM and I will strongly recommend that you leave it that way. Now launch VirtualBox and click 'VirtualBox' -> 'Preferences...' options from the top menu bar. When the 'VirtualBox - Network' dialogue opens up click the 'Network' -> 'Host-only Networks' tabs therein and add 'vboxnet0' by clicking on the topmost icon with a '+' sign on it on the right panel, as shown below-
Now click on the bottommost pen / brush like icon on the right panel, as shown in the above dialogue. Note down the IPv4 Address (this is the internal VPN Router / Gateway address) and the IPv4 Network Mask in the 'Adapter' tab in the dialogue that pops up, as shown below...you will use (and might as well need to modify in rare cases) these at a later stage-
Step 3: Click on the 'Network' option on the right panel bottom, as shown below-
Add a second 'Adapter 2' (note that the 'Enable Network Adapter' box should be checked) when the dialogue pops up, as shown below-
Step 4: Now start up your VM and run 'ifconfig -a | less' once you are logged into it. You should see 2 NICs listed, as in my case namely 'enp0s3' which is the NATted primary NIC (you will access the web thru this) and 'enp0s8' which is the Host-only secondary NIC (you will internally access your host and guests thru this), respectively as shown below. Note down their names (they may be different in your case than mine) as well as the 'Bcast' address for the secondary Host-only NIC i.e., '192.168.56.255' as in my case (this is the vital part which every post on the web skips invariably, at least all that I have seen thus far)-
Now first backup the '/etc/network/interfaces' file and then edit the same to look as shown below-
Please note that in case you do not wish to assign static IP address to your secondary NIC as I have (in my case '192.168.56.14' being the one), you may comment out the last 3 lines in the 'etc/network/interfaces' file as shown above and replace the line 'iface enp0s8 inet static' with the line 'iface enp0s8 inet dhcp' to use dynamically assigned IP address instead. Of course you'll need to enable your 'vboxnet0 DHCP Server' for this to work, but I'll not cover that topic here. However, you could find the following SlideShare deck very useful for a theoretical insight...
Once you save the file, you could test your NICs by running 'sudo ifup enp0s3' and 'sudo ifup enp0s8' respectively (as in my case). When done, shutdown your VM.
Start up your VM and run the ping tests between your host and guest plus some public internet address like www.google.com and VOILA you are golden with your own VPN!!!
Let me know if this post helps you in any way so I may feel encouraged to share more such tips and tricks to mutually make our lives a bit easier and our work a bit more fun, thanks to all of you from whom I learn something new every day:-)

No comments:

Post a Comment