How to experience using Linux using Windows

1. Background

Insert image description here

    When I was browsing Douyin in the morning, I came across a blogger introducing WSL. Since I rarely operate the Linux environment on my local computer, let’s take a look at how this is different from the traditional vmware workstation and virtualbox virtual machines, and how to install WSL. And use; also declare in advance that WSL is not recommended for actual development testing and verification, causing some compatibility issues, it is better to use a VM independent environment.

2. Introduction to WSL

Insert image description here

    WSL is the abbreviation of windows subsystem for linux, which is a subsystem of windows 10. It is a linux operating system that can run under windows. It also has multiple distribution versions to choose from. According to the official introduction, it allows developers to run the GNU/Linux environment - including most command line tools, utilities and applications - as-is without the overhead of a traditional virtual machine or dual-boot setup. It has now evolved to WSL 2, which supports the Windows Subsystem for Linux to run ELF64 Linux binaries on Windows. Its main goal is to improve file system performance, as well as add full system call compatibility. The new architecture WSL 2 changes the way Linux binaries interact with Windows and computer hardware and is compatible with the WSL 1 architecture;

    The main differences between WSL 1 and WSL 2 are the use of a real Linux kernel within the hosted VM (compared to the full Linux kernel of WSL 1.0), support for full system call compatibility, and performance across Linux and Windows operating systems. WSL 2, the current default version when installing Linux distributions, uses the latest and greatest virtualization technology to run the Linux kernel inside a lightweight utility virtual machine (VM). Below is a comparison table from the official website:

Insert image description here
Note: WSL 2 is only available in Windows 11 or Windows 10 version 1903, build 18362, or later. You can view your internal and discovery builds by pressing "Win + R" and typing winver. It also works with VMware 15.5.5+ and VirtualBox 6+;

The traditional VM experience may start slowly, be independent, and consume a lot of resources; WSL 2 has almost all the advantages of WSL 1: seamless integration between Windows and Linux, short startup time, low resource usage, and no need for VM Configuration or management. Although VM is used in WSL 2, the VM is managed and run behind the scenes and is still indifferent to users. In addition, the Linux kernel in WSL 2 is built by Microsoft based on the latest stable version branch and has been specially developed for WSL 2. Adjustment, optimized for size and performance; because WSL 2 includes its own Linux kernel and has complete system call compatibility, it no longer needs an intermediate layer for conversion like WSL 1; WSL 2 also has a resource problem, WSL 2's memory usage scales with usage. This automatically returns to Windows when the process frees the memory. However, WSL 2 will not release cached pages in memory back to Windows before closing the WSL instance. This presents a problem, if you have long-running WSL sessions or access a very large number of files, this cache may exhaust your Windows memory.

The official website recommends developers to try the VS Code remote WSL extension, so that you can not only use Linux command line tools to store project files on the Linux file system, but also use VS Code on Windows in an Internet browser. Author, edit, debug, or run projects without any of the performance degradation associated with working across Linux and Windows file systems.

Resource links: Install WSL with a single command now available in Windows 10 version 2004 and higher , WSL2-Linux-Kernel library , official website documentation , WSL 2 kernel , WSL development environment configuration guide

3. WSL installation and use

1) Confirm the host OS version. Run winver to confirm that your OS version meets: Windows 10 version 2004 and higher (Build version 19041 and higher) or Windows 11; and your CPU has "virtualization support" turned on. ;Similar to the pre-run check of vmware workstation and virtualbox;

Insert image description here

2) Preparation before installation of WSL 2:

1. Enable the Windows subsystem for Linux: Open powershell and execute:

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

Insert image description here

2. Enable virtual platform support: Open powershell as administrator and execute:

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

Insert image description here

Or install the following functions through the graphical interface: "Windows Subsystem for Linux" and "Virtual Machine Platform", as shown below

Insert image description here

3) The administrator opens CMD:

C:Windows\system32\chcp 936       //修改cmd串口显示未中文,返回如下
Acitive code page: 936

C:Windows\system32\ wsl.exe      //打开默认的 Linux 发行版,如果没安装的话,可用来验证
适用于 Linux 的 Windows 子系统没有已安装的分发版。
可以通过访问 Microsoft Store 来安装分发版:
https://aka.ms/wslstore

C:Windows\system32\wsl --list --online   //查看可用发行版列表,默认采用Ubuntu,可简写为:wsl -l -o

C:Windows\system32\wsl --install    //安装完成后重启,重启后会开启Ubuntu vm的提示框,提示配置用户名和密码

C:Windows\system32\wsl --install -d <DistroName>    //安装指定发行版

C:Windows\system32\wsl --set-default-version <Version#>   //将默认版本设置为 WSL 1 或 WSL 2

C:Windows\system32\wslwsl -s <DistributionName>       //或 wsl --setdefault <DistributionName>,更改默认安装的发行版

C:Windows\system32\wsl -l -v //列出已安装的发行版

C:Windows\system32\wsl pwd //查看当前目录路径在 WSL 中的装载位置

C:Windows\system32\wsl --status   //查看当前安装版本信息

#终止指定的分发
C:Windows\system32\wsl -t <Linux分发版名称>
 
#终止所有运行的分发及 WSL 轻型工具虚拟机
C:Windows\system32\wsl --shutdown
#卸载
C:Windows\system32\wsl --unregister <Linux分发版名称>

#更新 WSL2 内核到最新版本
C:Windows\system32\wsl --update
 
# 还原到 WSL2 内核的先前版本
C:Windows\system32\wsl --update --rollback

You can also search for "ubuntu" from the "Microsoft Store" that comes with Windows, and select an ubuntu distribution to install; for example, ubuntu20.04, after installation, you can find it in our software list and click to enter;
Insert image description here

4. Appendix: Frequently Asked Questions

4.1 Installation failed with error 0x80070003

Windows Subsystem for Linux can only run on the system drive (usually the C: drive). Please make sure the distribution is stored on your system drive: Open Settings -> System -> Storage -> More storage settings: Change where new content is saved"

4.2 WslRegisterDistribution fails with error 0x8007019e

This is usually because the "Windows Subsystem for Linux" optional component is not enabled: Open "Control Panel" -> "Programs and Features" -> "Turn Windows features on or off" -> Check "Windows Subsystem for Linux" ”, or use the PowerShell cmdlets described at the beginning of this article.

4.3 Installation failed with error 0x80070003 or error 0x80370102

Please make sure virtualization is enabled within your computer's BIOS. Instructions on how to do this vary from machine to machine and will most likely be under CPU related options. WSL2 requires the CPU to support the Second Level Address Translation (SLAT) feature introduced in Intel Nehalem processors (Intel Core 1st generation) and AMD Opteron. Even if the virtual machine platform is successfully installed, older CPUs (such as Intel Core 2 Duo) cannot run WSL2.

4.4 Error when trying to upgrade: Invalid command line option: wsl --set-version Ubuntu 2

Make sure that Windows Subsystem for Linux is enabled and that you are using Windows build 18362 or later. To enable WSL, run this command with administrator privileges at a PowerShell prompt: Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux.

4.5 The requested operation cannot be completed due to a limitation of the virtual disk system. The virtual hard disk file must be uncompressed and unencrypted, and cannot be sparse

Uncheck "Compress content" (or "Encrypt content" if it is checked) by opening your Linux distribution's profile folder. It should be located in a folder on the Windows file system, similar to: USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited…
In this Linux distribution profile, there should be a LocalState folder. Right-click this folder to display a menu of options. Select Properties > Advanced, and then make sure that the "Compress content to save disk space" and "Encrypt content to protect data" checkboxes are deselected (unchecked). If you're asked whether you want to apply this to the current folder or all subfolders and files, select "Only this folder" since you're just clearing the compression flag. After doing this, the wsl --set-version command should work properly.

4.6 The word "wsl" is not recognized as the name of a cmdlet, function, script file, or executable program

Make sure that the Windows Subsystem for Linux optional component is installed. Additionally, if you are using an ARM64 device and run this command from PowerShell, you will receive this error. Instead, run wsl.exe from PowerShell Core or from a command prompt.

4.7 Error: This update only applies to computers with Windows Subsystem for Linux

To install the Linux kernel update MSI package, WSL is required and should be enabled first. If it fails, you will see the following message: This update only applies to machines with the Windows Subsystem for Linux.
There are three possible reasons for this message:

1. You are still using an old version of Windows that does not support WSL 2. See step #2 for version requirements and links to update.
2. WSL is not enabled. You need to go back to step #1 and make sure the optional WSL feature is enabled on your computer.
3. After enabling WSL, you need to restart it for it to take effect. Please restart your computer and try again.

4.8 Bug: WSL 2 requires an update to its kernel components

This error is encountered if the Linux kernel package is missing from the %SystemRoot%\system32\lxss\tools folder. To resolve this issue, install the Linux kernel update MSI package in step #4 of the installation instructions. You may need to uninstall the MSI from Add or Remove Programs and then reinstall it.

4.9 Forgot WSL password

If you have forgotten your Linux distribution password, open PowerShell and use the following command to enter the root directory of the default WSL distribution: wsl -u root

If you need to update a forgotten password in a non-default distribution, use the command: wsl -d Debian -u root, replacing Debian with the name of the target distribution.

With the WSL distribution open at the root level within PowerShell, you can use this command to update the password: passwd , where is the username of the account in DISTRO for which you forgot the password.

You will be prompted to enter your new UNIX password and then confirm it. After you are informed that the password has been successfully updated, close WSL within PowerShell using the following command: exit

Guess you like

Origin blog.csdn.net/ximenjianxue/article/details/132788857