VirtualBox port forwarding
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:
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:
Add a new network interface
Open VirtualBox’s Preferences and check if you have everything like on the following screenshots:
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
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
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
auto enp0s8 iface enp0s8 inet dhcp
or, if you want it to have a static address (considering that your VirtualBox host-only adapter has
auto enp0s8 iface enp0s8 inet static address 192.168.56.105 netmask 255.255.255.0
Save the file and restart the machine. Check if it got the IP address (
ip a) and try to ping it from the host.
Now let’s look at IP-addresses we have:
10.0.2.15- guest Linux machine behind the NAT;
192.168.56.101for host-only adapter and
192.168.56.somethingfor the guest-machine (but you need it only for direct communication between host and guest, it’s not relevant for port-forwarding);
192.168.1.3- 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 firstname.lastname@example.org -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.