Configuring WSL on Windows Server 2022 for Visual Studio Code Remote Access

Over the last few weeks and months, I have spent a lot of time researching and experimenting with Linux on a very low level – mostly directly on the C and assembly level in combination with the official source code. For that reason, I had initially built a dedicated Ubuntu virtual machine to perform my research and my experiments. But a dedicated virtual machine for this purpose is a quite huge overhead.

Therefore, I have tried to reproduce my research efforts directly in WSL – the Windows Subsystem for Linux, which is available on the most recent Windows builds – including Windows Server 2022. In today’s blog posting I want to show you how you can install WSL on Windows Server 2022, and how to configure it for a remote access through Visual Studio Code – which runs on my Mac Book Pro/ M1/M2 Air – it doesn’t really matter.

Installing WSL on Windows Server 2022

With WSL – the Windows Subsystem for Linux – you can run Linux based applications directly on Windows. WSL runs on top of a traditional Linux Kernel that runs on a very lightweight utility Hyper-V virtual machine. For that reason, Microsoft offers you a modified version of the Linux Kernel which is optimized for running on top of Windows.
Windows offers you a lot of different Linux distributions that you can install for WSL. The command gives you here an overview:

wsl.exe -l -o

I ran this command on a fresh Windows Server 2022 installation, and it returns you the following available distributions to install:

The available Linux Distributions for WSL

I have decided for the Ubuntu 20.04 distribution, because I’m quite familiar with it, and it is one of the most well-known ones. You can install it with the following command:

wsl.exe ‐‐install -d Ubuntu20.04

Installing a Linux Distribution

As you can see from the picture, the command creates a lightweight Hyper-V VM, downloads the customized Linux Kernel, and installs it into the VM. To make the installation complete, you need a reboot of your machine. After the reboot, the configuration of the chosen Linux distribution happens. In the case of Ubuntu, you must provide a username and a password. And finally, you are landing on the traditional Linux bash shell – on a Windows system.

Configuring a Linux Distribution

The bash shell is the same as under a traditional Linux installation, so you also have access to the apt package manager and install additional tools like ifconfig.

Configuring WSL for Remote Access

By now we have a running Linux distribution running within WSL. In the past when I worked directly within a Ubuntu virtual machine, I have used Visual Studio Code as my development environment. The cool thing about Visual Studio Code is that is available on all major OS platforms (Windows, Linux, Mac OS), and it is a very lightweight installation. In addition, Visual Studio Code also supports a Remote Development feature, where you use a Docker Container, a remote machine, or the Windows Subsystem for Linux for your development environment. Then you can use the local Visual Studio Code installation to connect to your remote development environment.

I want to show you now the necessary steps how to configure your WSL-based Linux installation for remote access with Visual Studio Code. One of the prerequisites of the remote development feature of Visual Studio Code is the presence of a SSH server on your remote machine. Windows supports already for a few years SSH, so this is not a big deal for us. To install the OpenSSH client and server on Windows Server 2022, you can use the following commands on a PowerShell prompt:

Add-WindowsCapability -Online -Name OpenSSH.Client

Add-WindowsCapability -Online -Name OpenSSH.Server

Installing the OpenSSH Client

Installing the OpenSSH Server

After the installation of the OpenSSH components, you must make sure that the port 22 is not
blocked on the Windows Firewall, and that the SSH service is up and running. I
have also changed here the startup type to Automatic, so that the SSH services is started when Windows starts up.

Starting the OpenSSH Server

After these steps you can try to connect through SSH to Windows on the MacOS terminal.

Connecting through SSH

To be able to connect from Visual Studio Code to your remote development environment, you must
install the extension Remote – SSH in the first step.

Configuring Visual Studio Code

Then you can use the command Remote-SSH: Connect to Host within Visual Studio Code to connect to your remote development environment.

Connecting to a remote machine

As soon as you have the remote connection established, you will land on the Windows terminal.
And from there you must run the wsl.exe command, which connects to your default installed WSL Linux distribution:

Running the bash shell

And from there you are in a full-blown Linux environment without ever directly installing a Linux virtual machine. In my case, I have installed a few additional tools like gcc, nasm, and gdb to be able to do the interesting stuff 🙂

Running the GDB debugger inside WSL

Summary

If you want to work these days with Linux, you don’t really need a full-blown Linux system. With WSL – the Windows Subsystem for Linux – it is quite easy to run a Linux Kernel on top of Windows and to be able to run native Linux applications side-by-side on Windows. And with the remote development capabilities of Visual Studio Code, you can even work on your local machine against a remote machine. Microsoft has really done a lot of interesting enhancements around cross-platform development!

Thanks for your time,

-Klaus

Leave a Comment

Your email address will not be published. Required fields are marked *

Do you want to master SQL Server like an expert?

Checkout my SQLpassion Online Trainings!

Only EUR 229 incl. 20% VAT