SSH to VirtualBox Host-Only Adapter Interface from WSL2 Machine

DevOps Jul 11, 2023

Alternative to Port Forwarding method I wrote here, there are popular host-only method.

Step 1: Add Another Adapter as ‘Host-only’

Setting > Network > Adapter 2
Select drop down choose “Host-only Adapter”.

Step 2: Fix IP to the VM

Now we have new network adapter.

Check on Host machine
In the host machine, in my case Window, you must see the interface like below:

ipconfig /all

Mine is 192.168.56.1 LAN.
We need to fix our VM a valid IP that fall into this network, i.e., 192.168.56.XXX.

Now we go to the VM
We will turn on the network adapter and fix the IP.
Check the network adapter with:

ip link

Your adapter recently added is show at the bottom. Mine is enp0s8.
The interface is down by default , we need to bring it up.

sudo ip link set enp0s8 up

Then we fix the ip

sudo ip addr add 192.168.56.11/24 dev enp0s8

Now check with

ip addr

You must see the IP in the inet section like below

Now it is working.

Step 3: Connect from WSL2

It is great that Host-only network is bridged to WSL2 by default.
Now we can directly connect to it without any forwarding. Good!

ssh ubuntu-01@192.168.56.11

Here we go!

Appendix

If you come from other source on the internet, you might find the following differences:

Note: interface file
The file in the below folder is not available in Ubuntu 18.04 + anymore

/etc/network/interface

They move to use https://netplan.io/. So we ignore this.

Note: ifup is not found
The command like below is not valid anymore.

sudo ifup enp0s8

You need to install extra library but recommend to use ip command like above tutorial

Note: IP Command
If you insert the wrong IP, you can delete it by

sudo ip addr del 192.168.56.11/24 dev enp0s8

or if you’re a bit lazy you can delete it all

sudo ip addr flush dev enp0s8

Tags

TeamCMD

We are CODEMONDAY team and provide a variety of content about Business , technology, and Programming. Let's enjoy it with us.