WSL Tutorial (1) Installation & Basic Config

1. What is WSL

Windows Subsystem for Linux (WSL for short) is a compatibility layer for running Linux binary executable files (ELF format) natively on Windows 10 and Windows Server 2019.

  • Wikipedia

To use WSL, you need:

You must be running Windows 10, version 2004 and later (build 19041 and later) or Windows 11 to use the following commands.
If you are using an earlier version, please refer to the manual installation page.

And enable "Windows Subsystem for Linux":

Control Panel

The following installation and other operation reference:

Installing Linux on Windows Using WSL
https://learn.microsoft.com/zh-CN/windows/wsl/install-manual

and

Windows Server Installation Guide
https://learn.microsoft.com/en-us/windows/wsl/install-on-server

2, WSL installation

Note:
If the following command prompts that the command "wsl" cannot be found, you can replace "wsl" in the command with "wsl.exe".
Unless otherwise specified, the following commands can be executed using cmd or powershell, but need to be run as an administrator.

FAQ:
If you complete the following steps, but crashes or exits with an error when starting WSL, then you can choose a solution:

  1. Set the default version to WSL 1:
PowerShell
1
wsl --set-default-version 1
  1. Enable Hyper-V in the control panel, then reboot.

2.1, Windows Server 2022, Windows 10 and above

On Windows 10 and above, you can search for the installation of Linux distributions in the Microsoft Store, such as Ubuntu:

Ubuntu from the Microsoft Store

Or you can use the command line:

PowerShell
1
wsl --install

Install the default subsystem (Ubuntu).

use

PowerShell
1
wsl --list --online

All supported systems can be listed:

abbreviationsystem name
UbuntuUbuntu
DebianDebian GNU/Linux
kali-linuxKali Linux Rolling
openSUSE-42openSUSE Leap 42
SLES-12SUSE Linux Enterprise Server v12
Ubuntu-16.04Ubuntu 16.04 LTS
Ubuntu-18.04Ubuntu 18.04 LTS
Ubuntu-20.04Ubuntu 20.04 LTS

The distribution can be specified with -d. For example:

PowerShell
1
wsl --install -d Ubuntu-20.04

2.2, Windows Server 2019

Before you can run a Linux distribution on Windows, you must enable the Windows Subsystem for Linux optional feature and reboot.

Execute in PowerShell:

PowerShell
1
Enable-WindowsOptionalFeature-Online-FeatureNameMicrosoft-Windows-Subsystem-Linux

Enable WSL

Find the desired distribution on the Linux distribution download page, and download it to the folder (although it is Windows Server, it doesn’t even have a browser installed? IE is also available)

If there is no browser, execute:

PowerShell
1
Invoke-WebRequest -Uri https://aka.ms/wslubuntu2004 -OutFile Ubuntu.appx -UseBasicParsing

Download Ubuntu 20.04 LTS.

Then execute:

PowerShell
1
Add-AppxPackage .\Ubuntu.appx

to install.

3, configure environment variables

implement

PowerShell
1
2
$userenv = [System.Environment]::GetEnvironmentVariable("Path", "User")
[System.Environment]::SetEnvironmentVariable("PATH", $userenv + ";C:\Users\Administrator\Ubuntu", "User")

Now, you can start your Ubuntu subsystem using Ubuntu.exe or wsl.

4, start and login

After the startup is complete, you will be asked to configure UNIX username and password, that is, the account password, which can be filled in casually, but the account cannot be root.

Configure account password

After that, WSL will log in with this account by default.

Therefore, you need to add sudo prefix to enter commands in WSL, or use sudo -i directly to switch to root.

5. Suggestions and Precautions

The default source is very, very slow, you can refer to:

Server Optimization (2) Replacing Software Sources
https://blog.tsinbei.com/archives/237/

The difference between WSL and virtual machines is that WSL core hosts share ports, so pay attention to possible port conflicts:

SSH Server For Windows

This issue will be addressed in the next article.

WSL Tutorial (1) Installation & Basic Config

https://blog.tsinbei.com/en/archives/685/

Author
Hsukqi Lee
Posted on

2023-05-18

Edited on

2023-05-18

Licensed under

CC BY-NC-ND 4.0

Comments

Name
Mail
Site
None yet