Configuring the Windows Subsystem for Linux

Configuring the Windows Subsystem for Linux

Overview

The Windows Subsystem for Linux is a fully featured Linux environment that gives you a Linux terminal and application support on Windows and Windows Server.

The Windows Subsystem for Linux is useful for software development as well as supporting systems automation and administration tasks. Here are a handful of use-cases that stand out.

  • Software Development (Python, Javascript, .Net Core)
  • Shell Scripting (Bash, Powershell)
  • Systems Automation (Terraform, Ansible, Packer, ARM, Bicep, Cloud Formation)
  • Docker Desktop (Linux Containers, Dev Containers)

Installation

The Linux subsystem requires support for virtualization which may not be enabled by default. For physical hardware this means enabling Virtualization in your BIOS settings however for Virtual Machines you will need to enable some form of nested virtualization.

Virtual Machine Specific Configuration

VMware

To enable virtulization on a VMware vSphere Virtual Machine, you will need to do the following.

  1. Shutdown your guest operating System and Power-Down your Virtual Machine
  2. Right click the virtual machine, and select edit settings
  3. Expand CPU and select "Expose Hardware Assisted Virtulization to the Guest OS"
  4. Click OK, and Power on your Virtual Machine

Hyper-V

To enable virtulization on a Hyper-V Virtual Machine, you will need to do the following.

  1. Shutdown your guest operating System and Power-Down your Virtual Machine.
  2. Open an Administrative Powershell Prompt
  3. Run the following command
``` Powershell 
Set-VMProcessor -VMName <VMName> -ExposeVirtualizationExtensions $true  
``` 
  1. Start your virtual machine

Windows Configuration

  1. Right-Click on the Start-Menu and Select System
  2. In Search, Type Turn Windows features on or off, select Turn Windows Features on or off, and click OK when prompted for permissons.
  3. Enable Windows Subsystem for Linux and Enable Virtual Machine Platform
  4. Reboot your system
  5. Open an Administrative command prompt and run the following commands  
``` Powershell  
wsl --update  
wsl --install --distribution ubuntu 

WSL Networking

The Windows Subsystem for Linux works really well on a server, virtual machine, or desktop, however things get more complicated on a system with multiple network adapters or VPN connections. To configure a laptop and make the Linux subsystem more resilient to changing network conditions, I have used the following configuration with a really good experience.

  1. On your windows laptop, create a new file in the following folder "%userprofile%" named ".wslconfig"
  2. Add the following text to .wslconfig and save  
```
[wsl2]  
networkingMode=mirrored  
dnsTunneling=true 
  1. Shutdown the WSL with the following command
``` Powershell
wsl --shutdown
  1. Launch your linux subsystem

Common Issues

  • Windows Subsystem for Linux is Grayed Out
    – The most common reason for the Subsystem for Linux being grayed out when you try to enable the windows feature is your system or virtual machine needs to have hardware  assisted virtulization 
  • Unable to install applications or connect to the internet from the linux subsystem
    – The most common cause for issues connecting to external sources from your linux subsystem will often be DNS configuration. Look at what DNS server is configured in /etc/resolv.conf to see if it's what you are expecting. When roaming from a corporate network to a home network, something needs to manage your subsystem's DNS configuration to keep behaving as expected