6 years, 8 months ago

Enabling SSH to a local VirtualBox guest on Mac

I am currently working on OpenBSD 5.5 (amd64) running in a virtual machine using VirtualBox on my Mac. After installing the guest machine, I expected being able to ssh to it via the IP assigned to the virtual network interface:

# ifconfig em0
inet 127.0.0.1 netmask 0xff000000
em0:
[...]
 inet 10.0.2.15 netmask 0xffffff00 broadcast 10.0.2.255

In my case I had configured the network adapter with NAT (Native Address Translation), which means that the host machine acts as DHCP server and router for the guest.
Hoping I could reach the guest via its IP in the NAT virtal network, I switched to the OS X terminal and did:

$ ssh aj@10.0.2.15

… and no luck. Something is in the way.

It seems that, in order to ssh to a Virtualbox guest, you need to enable port forwarding between the host and the guest(s) you want to access. Open the VM settings > Network and select the network adapter (in my case Adapter 1). Create a new rule with the following attributes:

  • Name: SSH-host (or whatever you like)
  • Protocol: TCP
  • Host:
  • Host port: 2222 (this is the TCP port you will use on localhost to connect to the guest)
  • Guest IP:
  • Guest Port: 22 (this is the SSH TCP port)

Press OK and open a prompt on your host machine to connect to the VM:

# ssh localhost -p 2222
Last login: Wed Jun 11 19:56:32 2014 from 10.0.2.2
OpenBSD 5.5 (GENERIC.MP) #315: Wed Mar  5 09:37:46 MST 2014

The rule opens a TCP port (2222) on localhost of the host machine, which is then tunneled to any arbitrary port on the target machine (port 22). In order to enable easy to my VMs, I copy my public key across using ssh-copy-id:

$ ssh-copy-id localhost -p 2222 -l aj

Voila!