This is the 25th day of my participation in the August Challenge

WSL introduction

WSL: Windows Subsystem for Linux

Official documentation: WSL documentation

From the official: The Windows subsystem for Linux lets developers 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 setup

Compare WSL1 with WSL2:

WSL1 emulates Linux by using a translation layer to convert Linux system calls into Windows system calls

WSL2 is a real Linux kernel that runs on a lightweight, maintenace free virtual machine, which means we can get almost a full Linux experience on WSL2, like running Docker, etc

Benefits of WSL:

Rich command line software ecology, convenient software package management, compared with virtual machines, lightweight, easy maintenance, and good integration with Windows, such as file access, network sharing, etc

For example, how WSL2 and Windows exchange files

  • Linux files can be accessed from Windows via the \\ WSL $\

    \ path

  • Windows files are accessed from Linux using the path/MNT /

Install WSL2

Check whether the system supports WSL2:

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

X64 systems require version 1903 or later, internal version 18362 or later

The X64 system requires version 2004 or later, and the internal version is version 19041 or later

Use the Windows logo key + R, then type Winver to view version information

Installation steps:

  1. Enable the WSL and vm platform

    In the Windows Control Panel, open the Windows management page. Enable or disable Windows functions, select Windows subsystem and VM platform for Linux, and restart the PC

    You can also run the following command to enable the preceding functions and restart the PC

    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    Copy the code
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    Copy the code
  2. Download the WSL2 Linux kernel update package

    Download the WSL2 Linux kernel update package for x64 machines and run the update package installer

    Note: ARM64 computers require the ARM version installed

  3. Run the following command in PowerShell to set the default WSL version to WSL2

    wsl --set-default-version 2
    Copy the code
  4. Microsoft Store installation

    Open the Microsoft Store and search for Linux

    Select the distribution you want to install, and after the installation is complete, you will need to set a user name and password for the first startup

    At this point, the WSL2 distribution is installed

  5. Installation package installation

    Download the installation package from docs.microsoft.com/zh-cn/windo…

    Once the download is complete, you have an installation file in

    .appx format that you can double-click to install

    By default, the package is installed in the user directory AppData\Local\Packages\ of drive C. The following operations can be installed to other drive letters:

    Change the. Appx file to. Zip format, unpack it and place it in an unexpected installation directory on drive C, as shown in the figure for installing Ubuntu-20.04

    Run Ubuntu2004.exe and set the user name and password as prompted, as with other distributions

    Ubuntu2004.exe can be used to configure the subsystem. If you forget the root password, run ubuntu2004 config –default-user root to configure the default user root, open the subsystem, and run the passwd command to reset the password

    If the WSL system file has been installed on drive C and you want to migrate the WSL system file to another drive, you can export the WSL system, log out of the SYSTEM, and then import the WSL system file. When importing the WSL system file, specify the path for storing the system file, and set the default user

    Execute in sequence:

    WSL --export ubuntu-20.04 d:\ ubuntu20.04.tar WSL --unregister Ubuntu-20.04 WSL --import ubuntu-20.04 d:\ ubuntu-20.04 D :\ ubuntu20.04.tar --version 2 Ubuntu2004 config --default-user UsernameCopy the code

Configuring the Development Environment

Install the Java

  1. Install using package manager, such as Ubuntu with apt command

    sudo apt install openjdk-8-jdk
    Copy the code
  2. You can also use the installation package, which can be downloaded from tsinghua University open Source software image site

  3. Run the tar command to decompress the installation package to the specified directory

    For example, the downloaded package is openjdk8u-jdk_x64_linux_hotspot_8u292b10.tar. gz

    tar -zxvf ./OpenJDK8U-jdk_x64_linux_hotspot_8u292b10.tar.gz -C ./jdk
    Copy the code
  4. Change the name of the extracted folder, such as jdk8u292-b10 folder

    mkdir ./jdk8
    mv ./jdk8u292-b10/* ./jdk8
    rm -rf ./jdk8u292-b10
    Copy the code

Install maven

  1. Download The Apache Maven installation package from the Official website

  2. Run the decompression installation command

    The tar ZXVF. / apache maven - 3.8.1 - bin. Tar. GzCopy the code
  3. Modifying folder Names

    /maven rm -rf apache-maven-3.8.1/*./maven rm -rf apache-maven-3.8.1Copy the code

Installation Node. Js

  1. Visit the website to download the installation package, download | Node. Js

  2. Run the decompression installation command

    Xz tar - XVF./node-v14.17.4-linux-x64.tar.xz tar - XVF./node-v14.17.4-linux-x64.tar.xzCopy the code
  3. Change to an appropriate folder name, such as node

Configuring environment Variables

  1. Winodws appends environment variables to WSL by default, as follows

    Create the wsl.conf file in the etc directory

    sudo touch /etc/wsl.conf
    Copy the code

    Use the vim command to open it and add the configuration

    [interop]
    appendWindowsPath = false
    Copy the code
  2. Profile file in the user directory and add the environment variable configuration to the bin directory

    export PATH=$PATH:~/jdk/jdk8/bin
    export PATH=$PATH:~/maven/bin
    export PATH=$PATH:~/node/bin
    Copy the code
  3. Use the following command to test whether the configuration is successful

    Java -version # Print the Java version. MVN -v # Print the Maven version. Node -v # Print the Node versionCopy the code

Configure the ZSH terminal

  1. Install the ZSH

    sudo apt install zsh
    Copy the code
  2. Set ZSH as the default shell

    chsh -s /bin/zsh
    Copy the code
  3. Install onmyzsh, default download to ~/. Oh-my-zsh

    sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
    Copy the code
  4. Installing a plug-in

    Type ZSH to switch to the ZSH terminal and pull the plugins to ~/. Oh-my-zsh /custom/plugins

    #Syntax highlighting plugin
    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting
    #Autoprompt plug-in
    git clone https://github.com/zsh-users/zsh-autosuggestions  ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
    Copy the code
  5. Configure the ~ /. ZSHRC

    vi ~/.zshrc
    Copy the code

    The edited content is as follows

    #Add the name of the installed plug-in in parentheses after the plugins
    #Git is its own display git information plug-in, z is its own automatic jump path plug-in
    plugins=(	git
    			z 
                zsh-autosuggestions 
                zsh-syntax-highlighting
             )
    #Make the configuration in.profile take effect
    source ~/.profile
    Copy the code

    To save the edit, run the source ~/ ZSHRC command for. ZSHRC to take effect

Configuring Windows Proxy

On Windows, network applications in the WSL can be accessed through localhost (a later Version of Windows is required)

To use a Windows agent in the WSL, do the following

  1. Test whether the WSL can ping a Windows host

    If no, run the following command on the administrator command line interface (CLI) to add firewall rules

    New-NetFirewallRule -DisplayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow
    Copy the code

    You can also manually add rules as follows

    Open Windows Firewall Advanced Settings

    Create an inbound rule, select Custom => all programs, and set other Settings by default. The scope setting interface is as follows

    Add the IP address of the WSL to the remote IP address. The subnet mask is usually /20

  2. Obtain the WIndows host IP address

    Windows IP is saved in /etc/resolv.conf

    cat /etc/resolv.conf|grep nameserver|awk '{print $2}'
    Copy the code
  3. Set the proxy tool to LAN visible. Proxy tools without this setting can set the listening address to 0.0.0.0

  4. Configure the global proxy in. Profile

    export WIN_SOCKS_PORT=1089
    export WIN_IP=`cat /etc/resolv.conf|grep nameserver|awk '{print $2}'`
    export HTTP_PROXY=socks5://$WIN_IP:$WIN_SOCKS_PORT
    export HTTPS_PROXY=socks5://$WIN_IP:$WIN_SOCKS_PORT
    Copy the code
  5. Test whether the agent is in effect

    curl cip.cc
    Copy the code
  6. Configure proxyChains command line proxy tool

    Install proxychains4

    sudo apt install proxychains4
    Copy the code

    Open /etc/proxychains4.conf and note the number of lines on which socks5 is set, mostly line 115

    # add proxy here ...
    # meanwile
    # defaults set to "tor"Socks5 172.21.112.1 1089Copy the code

    Set up proxyChains configuration in. Profile

    export WIN_IP=`cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }'`
    export WIN_SOCKS_PORT=1089
    #The sed command writes the proxy configuration to line 115 of proxyChains4
    sudo sed -i "157c socks5 $WIN_IP $WIN_SOCKS_PORT" /etc/proxychains4.conf
    Copy the code

    Test proxychains

    proxychains curl cip.cc
    Copy the code