VirtualBox connection from host to VM through Nat and NatNetwork
In this article, I will quickly demonstrate how to connect from Host to VM (Virtual Machine) using SSH by using NAT or NAT Network.
I am going to show you both scenarios, let’s start with NAT. First of all, create a VM. In my demonstration I will be using Bodhi Linux as my VM(s) and Windows 10 as the Host Operating System. To install an OS on VirtualBox, I suggest the following article: https://itsfoss.com/install-linux-in-virtualbox/ since I wont be covering that here. Also make sure you have SSH server installed and started. Since this article doesn’t cover setting up SSH server, you can find the resource for Debian based systems here: https://www.cyberciti.biz/faq/ubuntu-linux-install-openssh-server/.
Setting up NAT connection
For NAT example, we only need to set up one VM. First of all, let’s check the IP address:
Our VM’s IP Address is 10.0.2.15 and we are connected to the NAT Gateway with IP 10.0.2.2. To illustrate, our network mesh looks something like this:
By default, router blocks almost all the ports for security, so does the Operating System. To connect to the VM via SSH, we need to open port 22. We do this by opening port 22 on the VM and then forward the port. Our port forwarding setup will look as such:
In this configuration, we have Guest port set to 22 (SSH default port). Port forwarding in our example will allow us to connect with SSH from port 1333 since our VM Network adapter will translate it and send the request to port 22 on the VM. We do not need to set up Guest IP and Host IP in this scenario.
Now that we have everything set up, start the VM again. Let’s connect to the VM using putty with the following settings:
Since the VM is running on the host machine, we just supply the localhost IP (127.0.0.1) and the port 1333. And we successfully connect to the VM.
Setting up NAT Network connection(s)
Suppose we have 2 or more VMs and we want to connect to both of them through the Host. First we must set both machines to NAT Network. By default, no NAT Network is provided, so we will create one. Go to Virtual box management windows, go to File > Preferences click on Network and then click create NAT Network. Name it anything you want, in our case we’ll just leave the default name NatNetwork. Now let’s open our VMs and check their IP’s:
Our first VM has an IP of 10.0.2.15 and the second one has 10.0.2.4. We can confirm that they are connected to the same network by having the same Gateway IP, which is 10.0.2.1 in our case. Network mesh look something like this:
Now open the the Nat Network settings again (File > Preferences > Network) and right click on the NAT Network and click Edit NAT Network:
Default Network name and CIDR are fine (you can edit them as you wish), but it is required to use port forwarding, so click on the Port Forwarding button:
In our scenario, we use the same concept as for NAT, but since there are more machines, supplying their IP’s and map them to different port is one of the solutions:
It is not required to restart the virtual machines, but when windows firewall prompts that VirtualBox wants to change firewall settings, click allow. Now we should be able to connect to the VM1 through localhost 127.0.0.1:1333 and to VM2 through 127.0.0.1:1334: