The Ultimate Guide to Windows Subsystem for Linux (Windows WSL)

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

If you are a Linux user and like to use Windows programs, then WSL is for you. In this article we will outline how to install it, what the benefits of using it are, and some tips and tricks that can help make your experience with WSL even better.

The “install wsl” is a command-line tool that allows users to install Windows Subsystem for Linux (WSL) on their computer. This guide will provide you with the steps needed to get started using WSL.

The Ultimate Guide to Windows Subsystem for Linux (Windows WSL)

The Windows Subsystem for Linux, often known as Windows WSL, is an excellent way for developers to work natively in Linux on their Windows 10 desktop.

You’re in luck with this post if you’ve spent much too much time partitioning hard disks to have numerous Linux systems installed alongside Windows.

Microsoft’s fairy code-mother has chosen to provide you with another option: the Windows Subsystem for Linux (WSL). WSL makes it considerably simpler and more versatile to operate a Linux system alongside Windows.

You’ll discover how to get started with WSL in this video. You’ll learn everything from how to get started to how to utilize several handy utilities, making WSL much more flexible than bash or PowerShell alone.

What exactly is WSL?

WSL, often known as C:WindowsSystem32wsl.exe, is a Windows utility that lets you install a Linux distribution as a Windows Store app.

You can use WSL from a command prompt or a PowerShell terminal since it is a basic Windows application. Later, we’ll go further into that subject. For the time being, it’s necessary to learn a bit more about WSL’s inner workings.

wsl.exewsl.exe

How to Activate WSL

Installing a Linux distribution alongside Windows 10 is required to set up and activate WSL. However, in a manner that permits the two operating systems to communicate with one another.

Prerequisites

You must have a Windows 10 64-bit machine with Build 18917 or later to install WSL on Windows.

By using the winver software, you may determine your Windows 10 build.

1647504101_821_The-Ultimate-Guide-to-Windows-Subsystem-for-Linux-Windows-WSL1809 is the latest version of Windows.

WSL Windows Feature Enablement

WSL is simple to set up and requires no download. It’s only a single Windows function.

Open PowerShell as an administrator and enter the following commands to activate WSL:

Enable-WindowsOptionalFeature -Online -FeatureName Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

How to Run PowerShell as an Administrator is a related topic.

Restart your computer after it’s finished, and you’re done!

WSL: Installing a Linux Distribution

WSL does not, on its own, install any Linux distribution. You’ll need to set one up. Start configuring your Linux distribution after Windows 10 has been restored.

To begin, go to the Microsoft Store and type in ‘WSL.’ A list of Linux distributions should appear shortly.

1647504102_457_The-Ultimate-Guide-to-Windows-Subsystem-for-Linux-Windows-WSL

After that, choose your preferred distribution and install it. Ubuntu 18.04 will be used in this lesson. There are a few different distributions available at the time of this writing, including:

  • LTS Ubuntu 16.04
  • LTS Ubuntu 18.04
  • OpenSUSE Leap 15 is a new release of OpenSUSE.
  • Leap 42 is the latest version of OpenSUSE.
  • SUSE Linux Enterprise Server 12 is a server that runs SUSE Linux Enterprise.
  • SUSE Linux Enterprise Server 15 is a server that runs SUSE Linux Enterprise.
  • Kali Linux is a Linux distribution that focuses on security
  • Debian GNU/Linux is a free and open-source operating system.
  • WSL Remix for Fedora
  • Pengwin
  • WSL Alpine

Choosing the WSL Version

To use WSL version 2 with Linux, you may need to activate it. If that’s the case, you may do so by following the steps below.

In a PowerShell console, type the following commands:

  1. Using wsl -l or wsl —list, you may see which Linux versions you have installed.
  2. Once you have the list, copy the name of the distro you want to run with WSL2 and run wsl –set-version <Distro> 2, replacing <Distro> with the name you copied earlier.
  3. Run wsl -l -v or wsl —list —verbose to see whether the command was successful. This command will provide a complete list of WSL distributions as well as the version of each distribution.

Installing the WSL Linux distributionInstalling the WSL Linux distribution

You may also run wsl —set-default-version 2 to specify WSL2 as your default WSL version for any future distros you install.

WSL is being started.

Open a PowerShell prompt and enter wsl to begin utilizing WSL. If you’ve set up WSL properly, you’ll be greeted with a bash terminal running on your preferred WSL distribution. You may execute any Linux commands you want from here.

A list of all the options provided by wsl.exe when it starts up may be found below.

Command Explanation Example
exec, -e Without using the default shell, the command will be executed. curl google.com wsl -e
Anything following this argument is sent to the default shell. It’s also possible to leave the operator out. curl google.com, curl google.com, curl google.com, curl google.com, curl google.com, curl google.com, curl google.com, curl google.com, curl google.com,
-d distribution Opens a terminal in the shell of the given distribution. Ubuntu-18.04 wsl -d
user, -u As long as the provided user exists on that distribution, it runs the WSL command as that user. Ubuntu-18.04 wsl -d -u tux_user
export The given distribution is exported to a tar file on your local machine. wsl –export wsl –export wsl –ex ./Test-Ubuntu.tar.gz Ubuntu
[–version] import Creates a new WSL distribution from a tar file. The –version argument may be used to indicate the WSL version. C:dataTest-Ubuntu.Test-Ubuntu.tar wsl –import Test-Ubuntu
[Options] list, -l wsl –list  
all List all WSL distributions that have been installed. wsl –all wsl –l –all wsl –l –all
running Only current WSL distributions should be included. wsl –l –running wsl –l –running wsl –l
quiet, -q Only the names of WSL distributions are shown. wsl -l -q
-v -v -v -v -v -v -v – Detailed information on all WSL distributions is shown. wsl -l -v
-s, set-default Sets the selected WSL distribution as the default WSL command distribution. wsl -s Ubuntu-Test
set-default-version Changes the default WSL version for all newly installed distributions on the system. wsl –set-default-version 2 wsl –set-default-version 2 wsl –set
set-version Changes the selected distribution’s WSL version. wsl –version Test-Ubuntu 2 wsl –version Test-Ubuntu 2 wsl –version Test-Ubun
shutdown All running WSL distributions are immediately terminated. wsl –shutdown –shutdown –shutdown –shutdown
-t Terminate The selected WSL distribution has terminated. wsl -t Ubuntu-Test
unregister The given WSL distribution is unregistered. wsl –unregister Test-Ubuntu wsl –unregister Test-Ubuntu wsl –un
help Provide details on how to use WSL. wsl –help

Once you’ve become used to these options, you’ll discover that using WSL to launch and manage programs is much simpler than managing Linux virtual machines on your own.

Quick tip: Run wsl —help to see all the flags and arguments for WSL.

To return to the PowerShell terminal, type exit after you’re done.

WSL allows you to share Windows/Linux resources.

WSL’s ability to effortlessly share Windows and Linux resources is one of its strongest features. File systems, environment variables, network resources, and command-line tools like cmd and PowerShell may all be shared at this time.

The WSL Ubuntu Linux distribution is used in all of the examples in this section. If you’ve opted to download a different distribution, your results may vary.

File Sharing Systems

One of the most valuable things to share with WSL is your file system. Working with two file systems as though they were one is possible with WSL.

In Linux, the Windows 10 file system is mounted as a directory, while in Windows, your Linux file system is mounted as a folder.

Using Environment Variables to Locate the Linux File System from Windows

When you use WSL to install a Linux distribution, it may introduce a Windows environment variable. It will establish an environment variable named UBUNTU HOME in the case of the WSL Ubuntu Linux distribution. From both Windows and WSL Ubuntu, this environment variable links to the Linux /home/ubuntu directory.

The UBUNTU HOME path may be used to execute scripts that need resources from many locations, or to provide a default location for the Windows terminal (covered later).

Examining the UBUNTU HOME environment variable in WSLExamining the UBUNTU HOME environment variable in WSL

A comparable environment variable may be defined by other distros. After installing a new Linux distribution, check the Windows environment variables using the PowerShell command Get-ChildItem -Path $Env: to see if any have been added.

If you wish to place everything in the /home/ubuntu directory, this environment variable shortcut comes in useful. But first, let’s look at how it got there and how else you can get there.

Using the Microsoft Store Packages Folder to access the Linux File System from Windows

Not every WSL distribution will have a convenient method to reference it. It’s critical that you understand how to locate the Linux file system in a different manner.

Because most WSL Linux variants are downloaded via the Microsoft Store, you may find the Linux file system among other Windows Store programs. To identify the directory where your Windows store applications go, go to percent USERPROFILE percent AppDataLocalPackages. Then take ownership of the folder, which is normally password-protected.

In the packages folder, you’ll find a number of subfolders where your Linux distribution file system may be shown. For me, the CanonicalGroupLimited.Ubuntu18.04onWindows 79rhkp1fndgsc folder had the WSL Ubuntu distribution.

The Linux file system may be found by going to the package folder. It’s found in the LocalStaterootfs folder for WSL Ubuntu. This is your Linux distribution’s root directory.

%USERPROFILE% /AppData/Local/Packages/ %USERPROFILE% /AppData/Local/Packages/ %USERPROFILE% /AppData/%USERPROFILE% /AppData/Local/Packages/ %USERPROFILE% /AppData/Local/Packages/ %USERPROFILE% /AppData/

From a Linux perspective, locating the Windows File System

Open WSL in Windows to locate the Windows 10 file system from Linux. WSL will then open a bash terminal for you. By default, this bash terminal will open in your UBUNTU HOME directory.

You’ll also be able to locate the root of your Windows storage volumes. The WSL Linux file system treats each of your Windows letter drives (C, D, E, and so on) as a mounted drive. As long as you have root access, each volume will be mounted as /mnt/c, /mnt/d, and so on.

On WSL, the Bash equivalent of performing Get-ChildItem C:WindowsSystem32 | Select-Object -First 5 is Get-ChildItem C:WindowsSystem32 | Select-Object -First 5.On WSL, the Bash equivalent of performing Get-ChildItem C:WindowsSystem32 | Select-Object -First 5 is Get-ChildItem C:WindowsSystem32 | Select-Object -First 5.

The WSL2 Filesystem is a filesystem that is based on the WSL

The WSL filesystem is quite simple to navigate. Anyone unfamiliar with the structure of a Linux file system may enjoy being able to traverse it using Windows Explorer. However, if you wish to transition to WSL2, it will be a bit more difficult.

WSL2 changes how everything works under the hood for File Sharing Systems. For starters, the filesystem is now a virtual hard disk in vhdx format instead of a directory.

For a WSL Ubuntu distribution, the vhdx file may be found in percent USERPROFILE percent AppDataLocalPackagesCanonicalGroupLimited.Ubuntu18.04onWindows 79rhkp1fndgscLocalState.

VHDX files may be mounted on Windows using the Disk Manager application. However, although the WSL distro is registered, the virtual disks cannot be mounted.

Environment Variables Are Shared

Environment variables are an important feature of every operating system because they make it simple to refer to binaries and executables from anywhere in your code.

The PATH variable was the sole common environment variable between Windows 10 and WSL Linux prior to Windows 10 release 17063. Since then, by utilizing the environment variable WSLENV, it has been allowed to exchange environment variables.

It might seem a bit meta to use the WSLENV environment variable to share other environment variables. You must really place environment variables inside of another environment variable to exchange environment variables between platforms.

Overview

Environment Variables Are Shared is a three-step process below. The only major difference when sharing across Windows/Linux is the switch argument used (full reference below).

  1. In Windows or Linux, define an environment variable.
  2. Set the WSLENV environment variable to the previously specified environment variable, then add a switch argument to it (for Translation of the Path).
  3. In Windows or Linux, look at the environment variable.

Options for Sharing

Using switches, you may make variables accessible in four distinct ways, depending on whatever platform you want the environment variable to appear on (table shown below).

  • The Windows filesystem will only be accessible from inside Windows.
  • Only WSL users will have access to the WSL filesystem.
  • Both WSL Linux and WSL Windows will have access to the WSL filesystem.
  • Both WSL Linux and Windows will have access to the Windows filesystem.
Flag Explanation
/p There is just one route. A variable set using this will be translated and made accessible to both Windows and WSL Linux.
/l Paths are listed below. It’s similar to /p, except it can accept multiple paths. This list will be delimited by semicolons on Windows, whereas colons will be used on WSL Linux.
/u Path on a Unix system. When calling WSL Linux from Windows, a path defined with this option can only be accessible. It’s possible to utilize it with either the /p or /l flags.
/w The route on Windows. This option specifies that a route may only be accessed when Windows is invoked from WSL Linux. It’s possible to utilize it with either the /p or /l flags.

Translation of the Path

The main reason to share environment variables is for Translation of the Path. As you may already know, Windows has user profile folders as Linux has user profile directories, for example. Each user has a predetermined “home folder” like C:Users<username> on Windows and /home/<username> on Linux.

The WSL will convert these folder paths across platforms using the /p and /l commands.

Using Linux to Share and Translate Windows Paths

Using the /p and /l options, you may share a single route or numerous pathways at once.

Assign a value of DESKTOP/p to the WSLENV variable at a Windows command prompt with a Windows environment variable named DESKTOP specified. This enables you to use WSL Linux to access it. Here’s an example of what I’m talking about.

In Windows, you can set variables, and in Linux, you can access them.In Windows, you can set variables, and in Linux, you can access them.

The /l switch may be used to run the same function for many pathways at once.

Linux Paths Shared and Translated with Windows

The technique for sharing and translating a Linux route with Windows is the same as it is for Windows, except that Linux-specific commands are used to establish environment variables.

For a deeper look at Environment Variables Are Shared, check out, this Microsoft article.

Network Resource Sharing

Another useful resource to exchange across Windows and WSL Linux is the networking component.

Network Interfaces: Physical vs. Virtualized

The network interfaces in WSL2 are virtualized. Because WSL2 network instances have virtualized network interfaces, they might have different IP settings than their Windows 10 equivalents.

WSL2 Linux IP addresses utilize Network Address Translation (NAT) to access network resources on Windows at the time of this writing, while Microsoft has said that eliminating NAT is high on their priority list of problems to solve.

The same comparison as before, but this time against a WSL2 distribution.The same comparison as before, but this time against a WSL2 distribution.

DNS Resolving for Clients

WSL will still both generate /etc/resolv.conf and /etc/hosts files to allow for DNS resolution. As long as you don’t explicitly override that behavior in /etc/wsl.conf, DNS Resolving for Clients will continue to work as expected.

Later in this article, you’ll learn more about the wsl.conf file.

Combining PowerShell and Bash

The ability to easily move information between PowerShell and Bash on WSL is one of the best aspects of WSL.

PowerShell –> Bash

Because the WSL executable takes input from the pipeline, you may use PowerShell to invoke the wsl.exe command and receive stdin. This enables you to utilize WSL to send complete objects from PowerShell into WSL, which are subsequently handled by the bash terminal. Here’s an example of what I’m talking about.

PowerShell is sent to grep.PowerShell is sent to grep.

Bash –> PowerShell/Cmd

You may just as easily transmit information from bash in the WSL to PowerShell and cmd. An example of running the Linux ls command and providing the output to the PowerShell Select-Object cmdlet through the pipeline can be seen below.

PowerShell receives Bash output.PowerShell receives Bash output.

As long as both commands are on the system path, you may use various Windows cmd tools from the WSL and transmit the result back to Linux.

Remember that the WSL has access to the Windows PATH variable by default, so it knows what the system path is on both sides.

You may execute ipconfig, a Windows program, from inside the WSL and send the results to the Linux grep command, as seen below. The inverse of using the Linux command which and giving the result to the Windows ipconfig command may also be seen.

Using Linux to run a Windows commandUsing Linux to run a Windows command

Problems with Translation

Passing command output back and forth between bash and PowerShell has certain drawbacks.

The way PowerShell and bash return information is a major issue. PowerShell is an object-oriented programming language, while bash is a command-line interface for manipulating strings. Any PowerShell objects sent to bash will be flattened and converted to a string. Any bash output piped to PowerShell, on the other hand, will be transformed to a string object.

In PowerShell, you can get around this behavior by converting or explicitly casting object types, as seen in the example below. However, you will be disappointed if you expect to be able to transmit items between PowerShell and WSL without any further effort.

Object passing issuesObject passing issues

We now have a legitimate PowerShell object that we can utilize in our script by casting the bash date as the [datetime] class in PowerShell. If you’re building scripts that need to go from Windows to WSL and back, a little tinkering with the code may help.

Install Xfce4 as a Windows Subsystem for Linux GUI.

When command-line tools aren’t cutting it, it’s time to turn to graphical user interfaces. You can install a Linux GUI if you need to run a graphical application on WSL, explore a custom distro, or aren’t acquainted with bash yet.

Xfce

There are several desktop environments for Linux. Xfce is one of the most typical ones to set up for WSL. Xfce is currently at version 4 as of this writing. Other desktop environments are available, but you’ll learn how to set up Xfce4 in this post.

xRDP

You’ll need a service that understands the RDP protocol after you’ve set up a Linux desktop environment. We’ll concentrate on the xRDP server in this tutorial. xRDP is an open source RDP server for Linux that enables you to connect to Linux servers using RDP clients in the same way that you can connect to Windows hosts.

Getting Started

Follow the steps below to connect to a Linux GUI from Windows using Xfce4 and xRDP. When using a WSL terminal, type:

  1. Download and install Xfce4 – Download and install Xfce4 using the command sudo apt-get -y install xfce4 && sudo apt-get -y install xubuntu-desktop. This will take a while. Stand by.
  2. Install the xRDP server — Run sudo apt-get -y install xrdp to download and install xRDP.
  3. Configure xRDP for xfce4 – echo xfce4-session > ~/.xsession
  4. sudo service xRDP restart restart xrdp
  5. ifconfig | grep inet | ifconfig | grep inet | ifconfig | grep inet | ifconfig | grep inet | ifconfig

You should now be able to start an RDP session using Windows 10. Using mstsc, open a remote desktop connection window and enter the Linux IP address from step #5.

If everything works properly, you may connect to the Linux distribution running on your Windows operating system using RDP, as seen below.

Xfce4 and xRDP are Windows Subsystems for Linux GUIs.Xfce4 and xRDP are Windows Subsystems for Linux GUIs.

Helpful Hints

What’s next now that you’ve learned the fundamentals of WSL and how to utilize it? Fortunately, a large number of tools are either designed for or operate well with WSL.

Using wsl.conf to set WSL Configuration Items at Bootup

The WSL has a configuration file called /etc/wsl.conf. This file provides configuration parameters that are executed each time the WSL distro is launched. WSL will assimilate any settings in the wsl.conf file every time the Linux distribution is launched if it exists.

Within the wsl.conf file, you may customize a few different areas.

  • Automount — Mounting disks from the start of Windows
  • Create resolv.conf or the hosts file in the network.
  • Interop – Using Windows to enable or disable interop

Check out the Microsoft Set WSL Launch Settings website for additional information on the wsl.conf file.

Using Visual Studio Code to develop on WSL (VS Code)

WSL is no exception to VS Code’s seeming integration with everything. You may create a workspace on your WSL Distro from inside VS Code and edit it entirely using VS Code on Windows. You don’t even need to be using a terminal!

To use VS Code on Windows with WSL, you’ll need to have VS Code for Windows installed first. Also, make sure you’ve installed the Remote – WSL VS Code plugin.

Once you’ve got the extension installed, you can now connect to it by opening a WSL terminal and running code <workspace>.  <Workspace> is the directory where you’d like to run VS Code from. VS Code will then detect that you are in a WSL distro, open a window, and establish a connection to to the workspace.

Check that everything worked by looking in the bottom left corner of VS Code for the WSL connection icon. You should see the name of your WSL distribution there.

Working with Visual Studio Code and WSLWorking with Visual Studio Code and WSL

You may even communicate directly with the WSL workspace using the built-in terminal. There’s no need to perform git bash commands in a separate window.

Adding the Linux Subsystem to the Windows Terminal

Adding the WSL console to the Windows Terminal is another great use of WSL.

You may add each WSL distribution to its own tab in Windows Terminal. You may also change the appearance of each tab to avoid getting lost.

You may use UBUNTU HOME as the beginning directory for your terminal if you’re running a WSL distribution that sets an environment variable for the user directory, such as UBUNTU HOME.

In PowerShell and Bash, you may check the date.In PowerShell and Bash, you may check the date.

If you’d like a complete video walkthrough on Getting Started WSL to work with the Windows Terminal, check out the TechSnips how-to video below.

 

Final Thoughts

The WSL was created by Microsoft to enable Linux developers to work on Windows. So far, the WSL has been a positive step forward.

The WSL looks to be an important component of Microsoft’s new open-source-friendly strategy. It will be an uphill struggle for Microsoft to compete with Apple for the devices on which developers write their code. WSL, on the other hand, is a powerful card to play.

WSL provides developers with a slew of advantages, including:

  • Running local Linux VMs is much more weighty.
  • Getting rid of the hassle of setting up and maintaining a hypervisor
  • There is no longer a need for multi-partitioned hard disks.
  • There will be no more grub bootloaders.

WSL just starts up and runs, allowing us to code happily ever after.

The “wsl version” command will show the installed version of Windows Subsystem for Linux. If you are on Ubuntu, then the wsl version should be 3.0.x.

Related Tags

  • install wsl2 windows 11
  • windows subsystem for linux has no installed distributions.
  • the windows subsystem for linux optional component is not enabled. please enable it and try again.
  • wsl commands
  • how to use windows subsystem for linux

Table of Content