Tuesday, August 25, 2015

Access VirtualBox VM via SSH

A virtual machine provides lots of space for development, troubleshooting and learning. Sometimes its required to remotely access the virtual machine guest using SSH from the host. Virtualbox allow users to setup so many different type of virtual machines known as the guest.

The machine installed with VirtualBox is known as the host. In this example, the host is a MS Windows 7 computer with virtual machine guest OS being Centos Linux 6. SSH client such as Putty is run on the host computer and will attempt to connect to the virtual machine. This will also allow multiple guest OS to SSH between one another.

Step 1: Configure VirtualBox networking

Shutdown all guest virtual machines. Open VirtualBox Manager, click on the virtual machine to be configured and choose Settings ->Network. There should already be an existing network for access to the Internet. Nothing needs to be changed if you can already access the Internet from the guest OS.

Guest virtual machine default networking with access to Internet

Choose one of the unused Adapter (example Adapter 2) and configure the settings:
Attached to: Host-only Adapter
Name: VirtualBox Host-Only Ethernet Adapter



On the MS Windows, VirtualBox has already setup its own network 192.168.56.1 as can be seen below. The other IP is the dynamic IP used by host computer to access the Internet.



Step 2: Configure a static IP on guest OS.

Start the guest OS and login. Open a terminal as root and the new network device has been identified but we need to configure the IP to match that of the VirtualBox network 192.168.56.x.


In the case above, VirtualBox has already configured network device eth0 to access Internet. Check eth1 with

# ethtool eth1

The default eth1 if we started that network, it would use DHCP but we want a static IP so that we know the IP for SSH. We now need to create/edit eth1

vi /etc/sysconfig/network-scripts/ifcfg-eth1

Add/modify following lines;

DEVICE="eth1"
BOOTPROTO=static
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
IPADDR=192.168.56.10
NETMASK=255.255.255.0
IPV6INIT=no
NAME="System eth1"

Start the networking for eth1 and check that eth1 is using the new IP. 

Step 3: Restart the guest virtual machine.

Reboot the guest OS and test access to Internet and ability to SSH into the guest OSS from the host computer as can be seen below.

Guest VM network for Internet and SSH

The network routing is shown below.
Guest virtual machine network routing




No comments: