Skip to content

Instantly share code, notes, and snippets.

@cfebs
Last active February 16, 2022 19:49
  • Star 14 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
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: https://gist.github.com/pjdietz/5768124 just with a few fixes for the host only network

Setup

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: 192.168.99.1

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
address 192.168.99.2
netmask 255.255.255.0
network 192.168.99.1

Remember to replace these two things

  • 192.168.99.1 with the value of the host-only network IP from the virtualbox GUI
  • 192.168.99.2 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 10.0.2.15

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: 0.0.0.0
Host Port: 2222
Guest IP: 10.0.2.15
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.

@marcin-kulik
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:)

@mvranicar64
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.

network:
    ethernets:
        enp0s3:
            dhcp4: true
        enp0s8:
            dhcp4: true
            dhcp6: false
            addresses: [192.168.99.2/24]
            gateway4: 192.168.99.1
    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