Installation of Windows Subsystem (WSL) for Linux

Reference materials:
https://docs.microsoft.com/zh-cn/windows/wsl/install-win10
https://docs.microsoft.com/zh-cn/windows/wsl/user-support
https://juejin .cn/post/6844903845097635854#heading-9


1. What is WSL

Windows Subsystem for Linux (WSL for short) is a compatibility layer for natively running Linux binary executable files (ELF format) on Windows 10. It was developed by Microsoft and Canonical in cooperation. The goal is to enable the pure Ubuntu 14.04 "Trusty Tahr" image to be downloaded and decompressed to the user's local computer, and the tools and utilities in the image can run natively on this subsystem.

The Windows subsystem for Linux allows developers to run the GNU/Linux environment as is-including most command-line tools, utilities, and applications-without the overhead of traditional virtual machines or dual-boot setups.

can be realised:

  • Choose your preferred GNU/Linux distribution in the Microsoft Store.
  • Run commonly used command line software tools (such as grep, sed, awk) or other ELF-64 binary files.
  • Run Bash shell scripts and GNU/Linux command line applications, including: tools : vim, emacs, tmux; languages : NodeJS, Javascript, Python, Ruby, C/ C++, C# and F#, Rust, Go, etc.; services : SSHD, MySQL, Apache, lighttpd, MongoDB, PostgreSQL.
  • Use your own GNU/Linux distribution manager to install other software.
  • Invoke Windows applications using a Unix-like command line shell.
  • Invoke GNU/Linux applications on Windows.

1.1 The difference between Windows Subsystem for Linux (WSL) and virtual machines

Compared with virtual machines, Windows Subsystem for Linux has the following advantages:

  1. Performance is better.
    Since the implementation is more low-level, the performance is much better than the virtual machine.
  2. File system shared with Windows.
    The subsystem can directly access all files in the hard disk, and can even execute .exe files.
  3. Cross-platform execution
    A very powerful function of the subsystem is that it can be executed cross-platform. Windows programs are executed in Linux, and Linux programs are executed in Windows.
    For example, in the Windows command line, you can directly use bash: input in the command line. bash -c "sudo apt-get update"
    Insert picture description here
    Insert picture description here
    Another example: to execute a Windows program in the Linux subsystem, you only need to add the .exe suffix to the command to execute it directly:ipconfig.exe
    Insert picture description here

2. What is WSL2

WSL 2 is a new version of the Windows subsystem architecture for Linux, which supports the Windows subsystem for Linux to run ELF64 Linux binaries on Windows. Its main goal is to improve file system performance and add full system call compatibility.

This new architecture changes the way these Linux binaries interact with Windows and computer hardware, but still provides the same user experience as in WSL 1, the currently widely available version.

A single Linux distribution can run in WSL 1 or WSL 2 architecture. Each release can be upgraded or downgraded at any time, and you can run WSL 1 and WSL 2 releases in parallel. WSL 2 uses a new architecture that benefits from running a real Linux kernel.

3. Install WSL2

3.1 Enabling the Windows Subsystem for Linux

3.1.1 Method One

You need to enable the "Windows Subsystem for Linux" optional feature before you can install the Linux distribution on Windows.

Open PowerShell as an administrator and run:

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

3.1.2 Method Two

Open the control panel, click Programs
Insert picture description here
and then click to enable or disable Windows features.
Insert picture description here
Tick ​​the Windows Subsystem for Linux.
Insert picture description here

3.1.3 Restart (optional)

It is recommended to now go to Section 4 and update to WSL 2.

  • But if you only want to install WSL 1, you can now restart your computer and proceed to Section 8: Install the selected Linux distribution .
  • To update to the WSL 2, Please Do not restart your computer , and then proceed to the fourth quarter.

4. Check the requirements for running WSL 2

To update to WSL 2, you need to run Windows 10.

  • For x64 systems: version 1903 or higher, use build 18362 or higher.
  • For ARM64 system: version 2004 or higher, use internal version 19041 or higher.
  • Versions lower than 18362 do not support WSL 2. Use the Windows Update Assistant to update the Windows version.

To check the Windows version and build number, select the Windows logo key + R, then type "winver" and select "OK". (Or enter the ver command at the Windows command prompt).

To update, update to the latest Windows version in the "Settings" menu.
Insert picture description here

5. Start the virtual machine function

Before installing WSL 2, you must enable the "Virtual Machine Platform" optional feature.

Open PowerShell as an administrator and run:

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

Restart the computer to complete the WSL installation and update to WSL 2.

6. Download the Linux kernel update package

Download address: WSL2 Linux kernel update package for x64 computers
If you can’t download it, you can also download it from this address: https://pan.baidu.com/s/18TRirPxWaVvCcXFB4epLLQ
Extraction code: 2222

Note:
If you are using an ARM64 computer, please download the ARM64 package . If you are unsure of the type of their own computer, open a command prompt or PowerShell, and enter: systeminfo | find "System Type".

Run the downloaded update package to install it. (Double click to run-you will be prompted to provide elevated permissions, select "Yes" to approve this installation.)

After the installation is complete, please proceed to the next step.

7. Set WSL 2 as the default version

Open PowerShell, and then run the following command when installing a new Linux distribution to set WSL 2 as the default version:

wsl --set-default-version 2

8. Install the selected Linux distribution

Open the Microsoft Store and choose your preferred Linux distribution.
Insert picture description here
Here is an example of installing Ubuntu, click Ubuntu, and then click Get.
Insert picture description here
When you start the newly installed Linux distribution for the first time, a console window will open, and you will be asked to wait one or two minutes for the files to be decompressed and stored on your computer. All future start times should be less than one second.
Insert picture description here

Then, you need to create a user account and password for the new Linux distribution.

9. Create a user account and password for the new Linux distribution

After enabling WSL and installing the Linux distribution from the Microsoft Store, you will be asked to complete the first step when opening the newly installed Linux distribution is to create an account, including a username and password.

note:

  • This username and password are specific to each individual Linux distribution installed , and are not related to Windows usernames .
  • After creating a username and password, the account will be the default user of the release and will be automatically logged in at startup.
  • This account will be regarded as a Linux administrator and can run sudo(Super User Do) management commands.
  • Each Linux distribution that runs on the Windows Subsystem for Linux (WSL) has its own Linux user account and password. Whenever a distribution is added, reinstalled, or reset, a Linux user account must be configured.
    Insert picture description here

9.1 Update and upgrade package

Most distributions come with an empty package directory or the simplest package directory. We strongly recommend updating the package catalog regularly and using the preferred package manager of the release to upgrade the installed packages. For Debian/Ubuntu, please use apt:

sudo apt update && sudo apt upgrade

Insert picture description here

Windows does not automatically update or upgrade Linux distributions. Most Linux users tend to control this task themselves.

9.1.1 Configure mirror source

If the download speed is too slow, you can configure the mirror source

vim /etc/apt/sources.list

Then add something to the front of the document.

# 阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

Insert picture description here

After saving and exiting, update the source.

apt-get update
apt-get upgrade

Insert picture description here

9.2 Reset Linux password

To change the password, open the Linux distribution (such as Ubuntu) and enter the following command:
passwd

The system will ask you to enter your current password, then ask for a new password, and then confirm the new password.

9.3 Forgot password

  1. Open PowerShell, and use the following command to enter the default root directory of 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 rootand Debianreplaced with the name of the target distribution.
  2. After opening the WSL release at the root level in PowerShell, you can use this command to update the password:, passwd <WSLUsername>where <WSLUsername>is the username of the account in DISTRO, and you forgot its password.
  3. You will be prompted to enter a new UNIX password and then confirm the password. After being told that the password has been successfully updated, use the following command to close the WSL in PowerShell: exit.

At this point, the Linux distribution fully integrated with the Windows operating system has been successfully installed and set up!

10. Install Windows Terminal ((Windows Terminal)) (optional)

Windows terminal can enable multiple tabs (quickly switch between multiple Linux command lines, Windows command prompt, PowerShell, Azure CLI, etc.), create key bindings (shortcuts for opening or closing tabs, copy and paste, etc.) Mode key), use the search function, and use custom themes (color scheme, font style and size, background image/blur/transparency). For more detailed information, please click: What is a Windows terminal

10.1 Installation

You can install the Windows terminal from the Microsoft Store .
If you cannot access the Microsoft Store, an internal version is posted on the GitHub release page. If you install from GitHub, the terminal will not be automatically updated to the new version.
Insert picture description here

10.2 First run

You can open Windows Terminal through the start menu.
Insert picture description here
You can also enter wt in the command line window to open Windows Terminal.
Insert picture description here
When you open the terminal after installation, it will be launched with PowerShell as the default configuration file in the opened tab.
Insert picture description here

10.3 Dynamic configuration file

If you have installed the WSL distribution or multiple versions of PowerShell, the terminal will automatically create a configuration file for you. Learn more about dynamic profiles on the dynamic profiles page.

10.4 Open a new tab

Press ctrl+shift+tor select the + (plus) button to open the default profile of the new tab. To open other configuration files, select ˅ (arrow) next to the + button to open the drop-down menu. Then you can select the configuration file you want to open.
Insert picture description here

10.5 Open a new pane

You can use panes to run multiple shells in parallel. To open the pane, you can use alt+shift+d. This key binding will open the duplicate pane of the focus profile. Learn more about the panes on the panes page.

10.6 Configuration

To customize the settings of the Windows terminal, select "Settings" from the drop-down menu. This will open the settings.json file in the default text editor. (The default text editor is defined in the Windows settings.) The
terminal supports customizing the global properties that affect the entire application, the configuration file properties that affect the settings of each configuration file, and the key bindings that allow you to interact with the terminal using the keyboard.

10.7 Command line parameters

You can use command line parameters to launch the terminal in a specific configuration. These parameters allow opening terminals with specific tabs and panes through custom profile settings. Learn more about the command line parameters on the command line parameters page .

11. Linux and WIN file system switching

11.1 Enter Linux file system from WIN

11.1.1 Open Ubuntu directly

Insert picture description here
Insert picture description here

11.1.2 Open WSL in the command line window (command prompt)

After opening the command line window, enter wsl to start.
Insert picture description here
Or enter it in the command line window bash.exe.
Insert picture description here

11.1.3 Use Windows Terminal to open wsl

First of all, Windows Terminal.
Insert picture description here
You can enter WSL by clicking the triangle under the + sign postcode.
Insert picture description here
Insert picture description here

You can also directly enter wsl in powershell to open WSL.
Insert picture description here

11.2 Enter WIN file system from Linux

The files in the Windows path can be directly accessed from the subsystem, and the disks in Windows are mounted under the /mnt directory.

Enter cd /mntto enter.
Insert picture description here

Guess you like

Origin blog.csdn.net/mahoon411/article/details/112493353