A Windows host runs a VirtualBox guest Linux machine. And you want to connect to this Linux guest machine via SSH from your other PC (or Mac) remotely:

VirtualBox SSH port forwarding

The problem is that by default VirtualBox guest machine is hidden behind the NAT and doesn’t have its ports accessible.

So, we need to:

Almost everything I’ve found in this article and this answer.

Add a new network interface

Open VirtualBox’s Preferences and check if you have everything like on the following screenshots:

VirtualBox Preferences NAT
VirtualBox Preferences host
VirtualBox Preferences adapter
VirtualBox Preferences DHCP

I had a default network already set up, but it didn’t work for me, so I deleted it and created a new one.

Enable additional network adapter

Open the machine’s Settings. Skip the Adapter 1 tab (the one with NAT) and go to Adapter 2 tab. Enable Host-only Adapter:

VirtualBox Settings Host-only

Add port forwarding

And now finally the port forwarding. We will forward from host machine’s port 3022 to guest machine’s standard SSH port 22:

VirtualBox NAT port forwarding
VirtualBox port forwarding

After that start your guest machine with Linux and edit /etc/network/interfaces file (in the guest machine Linux environment). Add these 2 lines to the end (note, that you might need to replace enp0s8 with your interface name, that you can look up in ifconfig / ip a):

auto enp0s8
iface enp0s8 inet dhcp

or, if you want it to have a static address (considering that your VirtualBox host-only adapter has network):

auto enp0s8
iface enp0s8 inet static

Save the file and restart the machine. Check if it got the IP address (ifconfig / ip a) and try to ping it from the host.

Now let’s look at IP-addresses we have:

  • - guest Linux machine behind the NAT;
  • for host-only adapter and 192.168.56.something for the guest-machine (but you need it only for direct communication between host and guest, it’s not relevant for port-forwarding);
  • - external address of the host Windows machine.

So, in order to reach Linux guest machine from any other workstation (beside host) from the same network you need to connect like that:

ssh root@ -p 3022

That will get you connected to 3022 port of the Windows host machine which will forward to 22 port of the Linux guest machine.

Same thing applies to any other port and service. For example, if you have Apache/NGINX/whatever listenning on 80 port inside the guest machine, you can forward to it from 8080 port of the host machine.