Howto access a Virtualbox guest machine throught ssh (or how to port forwarding)

This is not an original article (see references), it’s just an archived one “por si las moscas”.

Introduction

By default, the network connection in Virtualbox is set to NAT, that is every packet coming from the Guest machine is modified so that it seems as it has come from the Host machine. In this way it’s easy for the Guest machine to connect to all the rest of the network (the Internet included) but nobody can start a connection with the Guest Machine since it’s hidden behind the Host one.

So, if you want to use a server service in your Guest machine (i.e. Apache or SSH) you have two choices:

  1. pass to Virtualbox Host network connection;
  2. make Virtualbox forward all the packets arriving to a certain port of the Host machine.

This article will describe how to do the latter, in particular in the case of the ssh server. This is an interesting case because it allows you to simulate very well a quite common condition: connecting to a remote Linux headless machine.

We have a guest machine with a running ssh server which accepts connections on the TCP port 22.
Our goal is to make any packet arriving at a given TCP port (i.e. 2222) of the host machine, to be forwarded to the TCP port 22 of the guest machine.
Fortunately, there is Virtualbox command which permits to do it almost instantly: VBoxManage.

Configuration needed

In our case we will use Debian as the guest machine name (quote in case guest machine name contains spaces), here are the commands that you have to type in the host machine (real one) console as your user (if you use another user it will try to look for its virtual machines):

user@machine $ VBoxManage list vms
VirtualBox Command Line Management Interface Version 1.5.6_OSE
(C) 2005-2008 innotek GmbH
All rights reserved.
Name: debian
Guest OS: Linux 2.6
UUID: 814e25f4-451e-4582-8d34-71a1cd437cdd
Config file: /mnt/extra/virtualizacion/virtualbox/debian/debian.xml
Memory size: 195MB
[…]
user@machine $ VBoxManage setextradata debian “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort” 2222
VirtualBox Command Line Management Interface Version 1.5.6_OSE
(C) 2005-2008 innotek GmbH
All rights reserved.
user@machine $ VBoxManage setextradata debian “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort” 22
VirtualBox Command Line Management Interface Version 1.5.6_OSE
(C) 2005-2008 innotek GmbH
All rights reserved.
user@machine$ VBoxManage setextradata debian “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol” TCP
VirtualBox Command Line Management Interface Version 1.5.6_OSE
(C) 2005-2008 innotek GmbH
All rights reserved.

HostPort,GuestPort,Protocol are just string examples and can be changed without any consequence. Just remember your choices if you want to remove them later.
I guess you should be careful with pcnet substring, as maybe it should have something to see with your selected network card type.

Testing

Once you have typed the above commands, you need to close the guest machine (a reboot won’t be sufficient), restart it and then connect via ssh with:

anyuser@machine$ ssh -l >user< -p 2222 localhost

Replace localhost with the host machine ip address if you are connecting from another computer.

By the way, you can check which customizations have been already set for your Guest Machine with VBoxManage by typing:

user@machine$ VBoxManage getextradata debian enumerate
VirtualBox Command Line Management Interface Version 1.5.6_OSE
(C) 2005-2008 innotek GmbH
All rights reserved.
Key: GUI/LastWindowPostion, Value: 0,6,1028,820
Key: GUI/Fullscreen, Value: off
Key: GUI/AutoresizeGuest, Value: off
Key: GUI/LastCloseAction, Value: powerOff
Key: GUI/SaveMountedAtRuntime, Value: yes
Key: GUI/Seamless, Value: off
Key: VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort, Value: 2222
Key: VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort, Value: 22
Key: VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol, Value: TCP

or remove one, for example “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort”, by setting it without any value:

user@machine$ VBoxManage setextradata “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort”

Links and references

5 thoughts on “Howto access a Virtualbox guest machine throught ssh (or how to port forwarding)”

  1. I guess that VirtualBox under window$ should have the same binaries that on Linux. If this is true, use window$ console and give it a try.
    If it works, let us know.
    Regards

  2. In Windows you can use this commands to get the same result, just make sure your path contains the correct path to the VBoxManage executable or run it directly from its directory.

    c:/>VBoxManage setextradata “Linux Guest” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol” TCP
    c:/>VBoxManage setextradata “Linux Guest” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort” 22
    c:/>VBoxManage setextradata “Linux Guest” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort” 2222

    Regards

Comments are closed.