Skip to content

Instantly share code, notes, and snippets.

Last active February 16, 2022 19:49
Show Gist options
  • Save cfebs/57a5bbd3312732ad34ac3ffb7fa80056 to your computer and use it in GitHub Desktop.
Save cfebs/57a5bbd3312732ad34ac3ffb7fa80056 to your computer and use it in GitHub Desktop.
Ubuntu guest virtualbox on Windows 10 host with ssh port forwarding

If you want:

  • a Linux guest runnning on a Windows host
  • the ability to ssh to the guest from the Windows host
  • the ability to ssh to the guest from another machine on your home network

About 90% of this comes from: just with a few fixes for the host only network


1 Create a host only network in the Virtualbox GUI

  • File -> Preferences -> Network -> Host-Only Networks (tab) -> + icon

Make note of the network IP address in the GUI, for example:

2 Create a new VM and install Ubuntu Server (tested with 16.04.2)

In the machine settings you should add the Host-only adapter

  • Settings -> Network -> Adapter2 -> Attached to: (select Host-only Adapter) then select the Host only network you just created

Do not select DHCP, we want the guest to assign a static ip

3 Make sure sshd is installed and running

After installation you should double check that sshd is running sudo service sshd status

If not installed sudo apt install sshd, it should start on port 22

4 Configure static host only interface

If you ifconfig vs ifconfig -a you will notice that one interface extra is listed but it's not up and has no IP.

Make note of this interface name, example: enp0s8 or eth1

sudo nano /etc/network/interfaces

Add the following configuration

# The host-only network interface
auto enp0s8
iface enp0s8 inet static

Remember to replace these two things

  • with the value of the host-only network IP from the virtualbox GUI
  • with a invented IP address on that subnet, simply picking something besides 1 and 255 will do (that is where the .2 comes from)

Now sudo reboot

When the machine comes up ifconfig and see if the interface has everything applied

You should be able to test by sshing or telnetting to the host only ip

5 Port forwarding from host to guest

Port forwarding is setup in the Vbox machine settings Network tab again.

This time, select Apdapter 1, the NAT adapter, and unfold Advanced, click Port Forwarding

On your guest ifconfig again and make note of the other interface, it will probably have an ip like

Create a new port forwarding rule. Security note: this will expose a port on your home network directly to the Vbox ssh port, so be aware of those consequences.

Protocol: TCP
Host IP:
Host Port: 2222
Guest IP:
Guest POrt: 22

Make sure to replace your Guest IP with the NAT ip.

6 🍺

Now from another machine on your network like your shitty chromebook you should be able to:

ssh -p 2222 user@HOSTIP

Where HOSTIP is the ip of the host running the VM on your home network.

This is a dream setup for me at home.

Copy link

For some reason ifconfig and ifconfig -a were giving me the same output. I assumed enp0s8 was the one that needed to be added since you have it here as well. It is working like a charm, thank you:)

Copy link

mvranicar64 commented Mar 17, 2020

I followed these directions with a few minor changes.
First, since I am on Ubuntu 18.04.4 LTS, which uses netplan, instead of editing files in /etc/network/interfaces, I had to edit /etc/netplan/50-cloud-init.yaml as follows (IP's and dhcp settings may differ for your needs). Despite what this yaml file's internal comments said about being overwritten on reboot, it was the file that I had to edit and after many reboots, it was never overwritten.

            dhcp4: true
            dhcp4: true
            dhcp6: false
            addresses: []
    version: 2

Also, following the original author's setup for port forwarding worked for me, but my SSH command from the host was different. I had to use port 22 as follows.

ssh -p 22 user@HOSTIP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment