[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

Time:2020-9-26

WSL first experience

WSL Linux subsystem experience native docker, really fragrant!

The Linux subsystem of windows has been out for a long time. Have you experienced it?

I’ll take you to the trouble today,After all, they want to get rid of the massive VMS that take up too much hardware resources, such as VMware

This article is dedicated to you who love to toss, toss it, after wave!

Briefly, the main points of this article are as follows:

  • The whole process of WSL installation
  • The whole process of selective upgrade to WSL 2
  • Experience native docker in WSL 2

What is WSL?

WSL is the abbreviation of windows subsystem for Linux, which means the windows subsystem of Linux version.

Quoted from: Microsoft official website https://docs.microsoft.com/zh…

The Windows Subsystem for Linux lets developers run a GNU/Linux environment — including most command-line tools, utilities, and applications — directly on Windows, unmodified, without the overhead of a virtual machine.

You can:

  • Choose your favorite GNU/Linux distributions from the Microsoft Store.
  • Run common command-line tools such as grep, sed, awk, or other ELF-64 binaries.
  • Run Bash shell scripts and GNU/Linux command-line applications including:

    • Tools: vim, emacs, tmux Languages: NodeJS, Javascript, Python, Ruby, C/C++, C# & F#, Rust, Go, etc. Services: SSHD, MySQL, Apache, lighttpd, MongoDB, PostgreSQL.
  • Install additional software using own GNU/Linux distribution package manager.
  • Invoke Windows applications using a Unix-like command-line shell.
  • Invoke GNU/Linux applications on Windows

In short, Linux’s windows subsystem lets developersNo virtual machine requiredYou can run Linux environment directly on windows, including most command-line tools, programs and applications.

The benefits of using WSL are:

  1. Compared with Linux under virtual machine, WSL takes less resources and is more fluent;
  2. WSL can read and write files under Windows file system directly, which makes file transmission more convenient;
  3. Clipboards interworking, you can directly copy the text content in other places under windows and paste it to WSL;

Note: in fact, I like virtual machine. After all, it simulates the hardware, which is relatively mature and stable.

Turn on WSL support

A shell with administrator rights can install WSL.

Press Win + X to find windows PowerShell (administrator) and copy the execution command.

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

The above command activates the WSL service and then needs to restart the system

After restart, win + R, enter appwiz.cpl , find “start or close windows functions” in the upper left corner, and you will see that this option is selected.

In fact, the above command is equivalent to manually checking this function.

Direct command execution may be more efficient.

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

Install WSL distribution

Search in the windows storeubuntu, select the version you like and install it.

What I choose here isUbuntu 20.04 LTSAfter that, all the content is also written based on WSL Ubuntu.

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

After downloading and installing, the first time it is opened will be initialized for a while

Then set a user name and password

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

At this point, our WSL is installed.

Let’s make it work better!

Apt source change

The official source of Debian / Ubuntu is very slow to visit in China, so let’s replace it with the software source image of Tsinghua University

PS: you can also use the image of alicloud. If I experience it not so fast (maybe my posture is wrong), I don’t recommend it.

  • Execute the following command to backup the apt installation source:
$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
  • VIM editor sources.list :
$ sudo vim /etc/apt/sources.list 
  • Will sources.list Replace with the following

Note: here it isUbuntu 20.04 LTSOf course,

For other versions of Ubuntu, visit [Tsinghua University open source software image station] to find the corresponding version of the image configuration

#The source image is annotated by default to improve the apt update speed. If necessary, you can uncomment it yourself
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

#Pre release software source, not recommended
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

Update software

  • Update the software source in all the software list, you can see the fast in the screen, Hua Hua Hua Hua
$ sudo apt-get update
  • Update software
$ sudo apt-get upgrade
#Update content a little more, about a few minutes, play with the mobile phone...

Configure ssh server

We are used to Using SSH tools like xshell and SecureCRT to connect Linux, so we can get through the SSH function by the way. After all, PowerShell is not very convenient!

The ssh server on WSL is not configured automatically and needs to be re installed manually. First, run the following command to check

$ sudo service ssh stop
$ sudo /usr/sbin/sshd -d

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

If the output information includes the above information, that is, the key cannot be found, the problem can be solved by re installing openssh server

$ sudo apt purge openssh-server
$ sudo apt install openssh-server

Then you need to configure / etc / SSH / sshd_ Config, run VIM with sudo permission, and modify the following three keywords

Remember to delete#

Port 22
#These two lines allow the root account and password to log in
PermitRootLogin yes
PasswordAuthentication yes

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

Then remember to restart the SSH service

$ sudo service ssh restart
$ sudo service ssh status

If you need to log in to the root account with a password, you also need to set the password

$ sudo passwd root

Then you can use SSH tools to connect, local direct localhost

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached


OK, so far, our WSL has been configured.

Enjoy the experience, install nginx, redis, MySQL!


That’s it???

Yeah, it’s almost over.

The above is all about WSL installation.

My guest, do you want to experience wsl2?

Wsl2 was officially launched at the build conference last year.

Wsl2 comes with a real Linux 4.19 kernel, which can bring complete system call compatibility, and can upgrade and maintain directly with the help of automatic update without updating the entire subsystem of Windows Linux.

At the same time, wsl2 will run faster than the first generation of wsl1, improve the I / O performance of file system and compatibility with Linux, and can run docker container directly on the machineI like that, hehe)。

Let’s take a look at the WSL version we installed above,

Execute the orderwsl -l -vIf this is the result, Congratulations, WSL 1!

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

what? My result is not so??? How do I execute command console promptsInvalid command option????
Like the following results, it’s OK. I know you will. After all, I crawled over step by step and continued to look down with me.

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

So, let’s move on,There are barriers to the use of wsl2

Your system needs to meet the versionBuildTo emphasize that the build is actually a preview (unstable) version.

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

Therefore, there are two aspects to consider when upgrading to wsl2,

  • One is whether your Windows version can keep up with it
  • Can you take the risk of upgrading the preview version?

If you can’t satisfy it, then toss WSL 1 is also very good, at least experience the progress of windows.

Upgrade to internal preview first

The steps will not be written
How to get the internal preview version of windows win10

be careful:It’s not that you can use the internal preview immediately after you fill in the information application. I have to wait for windows to push it to you. I’ve been waiting for about three or four days. I find that there is an update, and then I will execute the updatewsl -l -vIt’s normal.

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

After the update, the main interface, the lower right corner, there will be some internal preview version of the logo…

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

are you ready? Now let’s start updating to wsl2 and restart the computer with me…

Update to WSL 2

Enable virtual machine platform optional components

Before installing WSL 2, the virtual machine platform optional feature must be enabled.

Open PowerShell as an administrator and run:

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

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

RestartComputer to complete the WSL installation and update to WSL 2.

Set WSL 2 as the default version

Set WSL 2 to the default version by running the following command from within shell:

#1. WSL provides version conversion tools to convert wsl1 to wsl2,
#Note that the conversion time may be long and wait patiently. If the time is too long and no response, press enter from time to time to see if there is any update
wsl --set-version Ubuntu-20.04 2
#2. Set wsl2 as the default version
wsl --set-default-version 2

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

Now we runwsl -l -v, see that version is already 2!

Congratulations again, we have successfully upgraded WSL 1 to WSL 2!!!

A real Linux kernel system is already in your Windows!

Installing docker CE in wsl2 subsystem Ubuntu

In fact, the purpose of upgrading WSL 2 is to experience the original docker. Ha ha, come on. Since we have all followed here, let’s start!

Installing docker CE
$ curl -skSL https://mirror.azure.cn/repo/install-docker-ce.sh | sh -s -- --mirror AzureChinaCloud

http://mirror.azure.cn/help/d…

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

Start docker and check the docker version
$ sudo service docker start
$ sudo docker version
Run an app?

You said you installed it, but you ran an app to have a look at it?

ok

There are also steps like installing docker on the official website,Verify that the docker engine is installed correctly by running the Hello world image.

$ sudo docker run hello-world

[Amway] WSL Linux subsystem, really fragrant! Complete practice attached

I made it. How about you?

If you are interested, you can follow up with the actual operation. After all, I have this article only after a little pit climbing up. In order to restore the real steps, WSL was installed twice.

Just like or collect is learning? It doesn’t exist!