Easily SSH into a VirtualBox machine

2021-07-28 2 min read

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

  1. Shutdown the guest OS if it's already running.

  2. Open the settings screen of the guest machine on VirtualBox and navigate to the Network section.

  3. In the Adapter 1 tab, set the Attached to to NAT.

  4. Expand the Advanced settings below and click on the Post Forwarding.

  5. Using the + button, create a new rule with the following values.

    • Name: <name>
    • Protocol: TCP
    • Host IP: <empty>
    • Host Port: 3987
    • Guest IP: <empty>
    • Guest Port: 22
  6. Save and close. That's all for setting up the network.

  7. Run your guest machine and let its operating system start.

  8. 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.

  9. 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.

Date: 2021-07-28
Categories: gnu linux