You have a virtual GNU/Linux or Windows machine on VirtualBox and want to establish an SSH connection with it. Here's what you need to do.
Terminology:
host: Your own machine running VirtualBox.
guest: The virtual machine you created on VirtualBox
Steps
Shutdown the guest OS if it's already running.
Open the settings screen of the guest machine on VirtualBox and navigate to the Network section.
In the Adapter 1 tab, set the Attached to to NAT.
Expand the Advanced settings below and click on the Post Forwarding.
Using the + button, create a new rule with the following values.
- Name:
<name>Protocol: TCPHost IP: <empty>Host Port: 3987Guest IP: <empty>Guest Port: 22Save and close. That's all for setting up the network.
Run your guest machine and let its operating system start.
Using the appropriate command according to your operating system, start the sshd service. E.g., systemctl start sshd on a GNU/Linux distribution with systemd.
Try to connect to the guest via SSH: ssh root@localhost -p 3987
In this way, your SSH connection on the port 3987 will be forwarded to your guest machine's SSH port.
