Pages

Thursday, June 30, 2016

Networking in Virtual box !!!


Once virtual machine been setup, there is obvious requirement to connect to that particular Virtual machine via putty, winscp and access deployed application via browsers. Although, the default network configuration(NAT) does not allow you to do it. 

At very high level, below are the minimum requirement a user must be looking for in terms of connectivity to that particular Virtual machine. 

Virtual Machine Connectivity Requirement-

  • Guest (VM) machine should be accessible to Host (Local Laptop/Desktop) Machine for multiple purpose e.g. ssh access and http access etc. using putty, winscp and browsers.  
  • If you have group of Guest machines e.g. Guest1 and Guest 2, both should be communicating to each other. 
  • Guest machine should also have internet connectivity to install various OS packages etc.

Various Networking Option available in Virtual Box.

Virtual box has given various different network adapter types to achieve various Networking requirement. However, it’s bit TDS job to achieve all of the above listed requirement using single adapter type. At least I struggle a bit to achieve the same, it might be due to the fact that, I am not a Network Expert person but since I worked out how to achieve this, I thought it’s good idea to share this info as many of us need similar setup.



Virtual Box Adapters Types -

Not attached -  In this mode, VirtualBox reports to the guest that a network card is present, but that there is no connection – as if no Ethernet cable was plugged into the card. This way it is possible to “pull” the virtual Ethernet cable and disrupt the connection, which can be useful to inform a guest operating system that no network connection is available and enforce a reconfiguration.

Network Address Translation (NAT) - If all you want is to browse the Web, download files and view e-mail inside the guest, then this default mode should be sufficient for you.

NAT Network - The NAT network is a new NAT flavour introduced in VirtualBox 4.3.

Bridged networking - This is for more advanced networking needs such as network simulations and running servers in a guest. When enabled, VirtualBox connects to one of your installed network cards and exchanges network packets directly, circumventing your host operating system’s network stack.

Internal networking This can be used to create a different kind of software-based network which is visible to selected virtual machines, but not to applications running on the host or to the outside world.

Host-only networking This can be used to create a network containing the host and a set of virtual machines, without the need for the host’s physical network interface. Instead, a Virtual network interface (similar to a loopback interface) is created on the host, providing connectivity among virtual machines and the host.

Here is small matrix I have created to summarize various attempts which I have tried out but only one combination worked “NAT + Host only adapter” to achieve my requirement listed above.
In My POC I have two VM Guest1 and Guest2, trying to establish the connectivity as listed in above.

* NS – Stand for Not accessible
* A – Stand for accessible



NAT + Host Only Adapter Implementation steps


I tried all different adapters given in Virtual BOX but at last I find “NAT + HOST Only Adapter” combination work best to achieve the requirement which I was looking for.

To implement the same, we need to add two network adapters in each VM. One Network Adapter is NAT using that we can access the internet and another is Host-only using that we can access the Guest from Host and Guest to Guest connectivity.

Each VM will get three IP, one starting from 10.0.X.X (Nat), 192.168.56.X (Static IP for host Only Interface) and 192.168.121.X (DHCP server IP used for Nat Interface). The only glitch in this approach is when you change network e.g. from Office network to home network then Natted IP breaks and you need to restart the Network service again “/sbin/service network restart”. Moreover, the great thing is that, since host-only adapter using static IP, it does not break which result no change in deployed application such as accessing database servers, apache web server and deployed application etc.

Follow the below steps to implement the same.

Step1:

Add a host-only virtual network interface in your Laptop/Desktop and assigned Static IP. Navigate to Virtual Box – File >> Preference >> Network>> Host Only Network and Click on Add button.



This will create Ethernet adapter as per below snap -




Step2:

Make sure DHCP Server option in disabled.



Step 3:

Now, select both VM machine one after one and add two network adapters 1) NAT adapter 2) Host-Only Adapter. Also click on MAC Address refresh button, so that it will get new virtual MAC address.

Adapter 1-


Adapter 2 –



Note: Repeat the same step for Guest2 Virtual machine as well.


Step 4:
Now, start the both virtual Machine.

Note: While booting VM will fails to bind eth1 (Host-only) adapter first time.

Once VM up and running run command “system-config-network” and select eth1 network interface >> click on Edit button >> assigned static IP >> click on Ok button >> click on Activate button.

Guest1 Config:
Address: 192.168.56.101
Subnet Mask: 255.255.255.0
Default Gateway: 192.168.56.0

Guest 2 Config:
Address: 192.168.56.102
Subnet Mask: 255.255.255.0
Default Gateway: 192.168.56.0



Step5:

Restart the Network Service “/sbin/service network restart”

Guest1 /sbin/ifconfig snap - 


Guest2 /sbin/ifconfig snap -



Test Internet connection -



Test Guest to Guest connectivity -



Test Host to Guest connectivity -