How to create virtualbox host-only network and access the vm remotely within same network

I have already written this information on my blog. I believe this could be useful for others. 

This information is related to accessing your vm within same network remotely, such as machines connected in LAN. I have read various information about connecting to VM from the same machine however I could not find information such as how to connect to VM within same network from different machine, so I thought it would worth sharing this information. 

The below concept can be implemented at your organization or at enterprise level with less or more modifications in configuration as per the requirement.

In order to create this setup you would require two machines. One machine should run the VirtualBox as a server and other machine should run the client terminal to access the VM remotely.




Bridged Network in VirtualBox.

What I understand from different resources that VM bridge network is used when we need our vm to be accessible in our network same as our host machine which is hosting the VM. The purpose of this configuration is that, VM would have access as same as the host.



The current setup has DHCP server enabled on VirtualBox, it means the network interface will have the dynamic ip assigned to it for a certain time after that it will require new lease.


In my case it would be following, this would require to turn off your vm.
 sudo shutdown -P now



To find out network interface and the dynamic ip, in my case it would enp0s9, because I have configured enps08 for VirtualBox NAT configuration. You would require these configs on PC2 and Oracle VirtualBox setup. My current vm is running ubuntu 18.04.1 LTS.





Finding out DHCP dynamic ips, using below commands

sudo cat /var/lib/NetworkManager
sudo cat /var/lib/NetworkManager/dhclient-804ff673-cc7b-35f9-9e2d-464eee817ea3-enp0s9.lease


After this has been verified it would require to turn off your vm. You have to enable bridge adapter on your vm.


After enabling the bridge adapter your vm would behave same as your host machine in network. Therefore you should be able to reach to your vm from PC1.  After enabling the bridge adapter you need to turn on your vm.




If you receive successful response from ping command it means your both machines and vm are in the same host network.

Now we will require to enable remote log in from PC1 and PC2 to VM.

VM will require ssh server on ubuntu to allow remote user to login in vm.

For more information regarding ssh https://www.ssh.com/ssh#the-ssh-protocol

sudo apt update
sudo apt install openssh-server

After successful installation we can try ssh locally by connecting from virtualbox. By default every Linux machine has the ip address 127.0.0.1 and it refers to the localhost itself.

sudo systemctl status ssh this will give the information about ssh, if the ssh service is up and running.

ssh localhost would give us below output.




When you try to ssh to localhost for first log in ssh will ask the ECDSA key of the host. It will ask you to verify that key that it received from host machine is the correct. This process will know that you are connecting to the server you intended to connect and not from different source. When you type yes to this fingerprint of that server will be saved in ~/.ssh/known_hosts file in case of windows it would be the same file but different location. When you log in next time ssh would not require to verify the fingerprint from same source machine. If the ssh is installed correctly you should be able to log in from local vm using ssh.


To allow remote connection to ssh, we need to configure ubuntu's default firewall which is ufw. Which is sudo ufw allow ssh.

We need to check open ports using sudo ufw status



From PC2 you should be able to login into vm as well. I have used Cygwin terminal on my PC2 windows machine. You can install from this url https://www.cygwin.com/

You should see below output from PC2 when you try to ssh into vm.




From PC1 I have installed putty to login via ssh. As I have already added ecdsa key to my known host list file so the ssh would not prompt me to save the fingerprint again.



In case if you come across any issue such as below, you need to remove host entry from your host file on your vm hosting machine either PC1 or PC2 that should work without any issue.





Feel free to let me know if you find any trouble configuring the above and allowing machines to connect to vm remotely within your network.





Comments