Setup Windows Subsystem for Linux (WSL 2) on Windows 10

WSL 2, Windows Subsystem for Linux (the second iteration, which probably breaks backwards compatibility) facilitates Linux development without leaving Windows and of course Visual Studio, so here is how to set things up.

If your are not on a Windows Insiders build it's a longer procedure, which is what I list here. The idea is to enable WSL in Windows 10, install a Linux distro from Microsoft Store, and run Linux using the WSL virtual machine.

Microsoft Docs: Windows Subsystem for Linux Documentation

Install WSL 2 on Windows 10

Your Windows version might complicate things, so it seems from the notes about dependency on Windows versions, strewn all over the documentation. The setup worked for me in December 2020 on:

Windows 10 Pro
Version 2004 (OS Build 19041.685)

Microsoft has made a mess with a variety of terminals/shells on Windows. Here is one that has better font rendering and seems to be able to integrate several kinds. It presents the PowerShell at start.

To clean up older WSL (version one?) run this in Windows PowerShell,

wsl --unregister legacy

To enable WSL run this in Windows PowerShell,

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

then enable the Virtual Machine Platform

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Download the latest Linux Kernel Update Package (whatever that is, and I'm sure this link is going to die soon) and install it

Set WSL 2 as the default version

wsl --set-default-version 2

Install a Linux Distribution from Microsoft Store, I chose Debian, it's just 78MB

You can have a combination of Linux OS and WSL versions active. To check what you have, run

wsl --list --verbose
NAME      STATE           VERSION
* Debian    Running         2

I have one, Debian, and WSL 2. If I had more, I'd set one as default;

wsl --set-version Debian 2

If ever you need to shut everything down, run;

wsl --shutdown

which immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine.

Though I don't know how to start it.

Prep the Linux Instance

In Windows Terminal, in the drop-down list next to the tab, a Linux penguin icon named Debian now shows up. This is the shell to your Linux running in WSL 2.

Debian shell in Windows Terminal

Install the following software

apt-get install openssh-server g++ gdb make ninja-build rsync zip

and ensure SSH service is running

service ssh start

which should tell you

[ ok ] Starting OpenBSD Secure Shell server: sshd.

SSH into the Linux Instance from Cygwin

When we installed some software in the previous section we had installed the OpenSSH Server and started it. Now configure it by editing /etc/ssh/sshdconfig_, where you change the existing strings to this

Port 2222
PasswordAuthentication yes

and start/restart the SSH server

sudo service ssh restart

and see

[ ok ] Restarting OpenBSD Secure Shell server: sshd.

We need the Linux instance's IP address, so we find that out by

ip addr list

that outputs the eth0 section, a relevant snippet from which, on my system, reads

4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:e8:cd:90 brd ff:ff:ff:ff:ff:ff
    inet brd scope global eth0

from where I pick the IP address:

In a Cygwin terminal connect to the WSL-Linux using

ssh rup@ -p 2222

which prompts for a password and let's you in. You must have been prompted for a new username when you had installed the Linux distro from Microsoft Store at the time of setting up WSL 2.

I haven't yet been able to make public key authentication work.