wsl2 (Windows Subsystem for Linux) usage learning

Posted by cohq82 on Sat, 22 Jan 2022 13:46:01 +0100

wsl2 is not stable yet, and there are many bug s. You can: https://github.com/microsoft/WSL Submit

install

wsl2 can be used for Windows 10 build 18917 Insider version with win10 update.

  • Installation method 1: run powershell as an administrator, execute commands, and then restart the computer

    Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
    
  • Mode 2

Control panel - programs and functions - enable or disable windows functions - Windows subsystem for Linux - tick √

- restart the computer

wsl command

  • view help

    wsl -h
    
  • Set the default version, and select whether linux runs on wsl1 or wsl2 by default

    wsl --set-default-version 2
    
  • To set the running version of a linux distribution, you need to install linux before you can convert it

    wsl --set-version <distro> 2
    

    Example:

    wsl --set-version ubuntu 1
    
  • ubuntu version conversion

    wsl --set-version ubuntu 2
    
  • View installed linux

    wsl --list --verbose
    

or

  wsl -l -v
  • Stop running Linux

    wsl --shutdown
    
  • Start the virtual machine

    • Directly enter the system name: such as Ubuntu, kali, etc
    • Or use the wsl command, for example:

      wsl -d ubuntu
      

Install linux

  • Open the app store and search for {wsl. The store provides, Ubuntu, SUSE Linux, enterprise server 12, Debian, Kail Linux
  • Click Install

Installing ubuntu

  • In the app store, search for Ubuntu and install it

  • Set user name and password

  • Set root password

    sudo passwd root
    
  • Installing docker is the same as normal ubuntu installation of docker

Mode 1:

  sudo apt-get update
  sudo apt-get install -y \
      apt-transport-https \
      ca-certificates \
      curl \
      gnupg-agent \
      software-properties-common
  curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  sudo add-apt-repository \
     "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
     $(lsb_release -cs) \
     stable"
  sudo apt-get update
  sudo apt-get install docker-ce docker-ce-cli containerd.io
  • Start docker

    sudo service docker start
    
  • Run container test

    sudo docker run hello-world
    
  • docker is executed by ordinary users

    sudo usermod -aG docker your-user
    sudo service docker restart
    
  • windows network access linux

    • Run nginx container

      docker run --name nginx -d -p 8888:80 nginx
      
    • linux internal access

      curl http://127.0.0.1:8888
      
    • windows access

    • View linuxIp

      ip addr
      

    Check eth0 ip. Here is 192.168.161.203. Each machine is different

    • Access: open the browser to access http://192.168.161.203:8888 Get nginx page

    • Visit http://127.0.0.1:8888 No results, access denied

  • ubuntu restart system

    • If you execute the restart or shutdown command on Linux, an error will be reported

      ziyun@DESKTOP-JVE2TQQ:/mnt/c/WINDOWS/system32$ sudo reboot
      System has not been booted with systemd as init system (PID 1). Can't operate.
      Failed to talk to init daemon.
      ziyun@DESKTOP-JVE2TQQ:/mnt/c/WINDOWS/system32$ sudo shutdown
      System has not been booted with systemd as init system (PID 1). Can't operate.
      ziyun@DESKTOP-JVE2TQQ:/mnt/c/WINDOWS/system32$
      
    • The wsl command needs to be executed

    • Terminate all running linux

      wsl --shutdown
      
    • Terminate specified linux

      wsl --terminate <distro>
      # or 
      wsl -t <distro>
      

    Example:

    • View linux list

      wsl -l  
      
    • Close ubutnu, no display

      wsl -t Ubuntu
      
  • Start ubuntu

    • Or execute wsl -d ubuntu
    • Or enter ubuntu
  • Export ubuntu

    • Export is similar to that of docker, which is convenient for moving, etc

    • Export to disk d:

      wsl --export Ubuntu D:/ubuntu.tar
      

    It will be completed soon. The tar size of this instance is 1.15G and compressed to 414M

    • Import

      wsl --import Ubuntu D:/ubuntu_dir  D:/ubuntu.tar
      
    • – import: Import

    • ubuntu: import name, customizable

    • D:/ubuntu_dir: import to that directory. The import is successful. There is ext4 Vhdx file

    • D:/ubuntu.tar: source file

report errors

The operation attempted is not supported by the referenced object type

reference resources: https://answers.microsoft.com/zh-hans/insider/forum/all/wsl2%E5%AE%89%E8%A3%85kali/b6dd4c62-1130-4a7c-9462-1e9e5f38dab2 Execute command:

netsh winsock reset

docker for wsl2

reference resources: https://docs.docker.com/docker-for-windows/wsl-tech-preview/

docker windows development edition can use the built-in WSL2 function. Advantages: * use its own Ubuntu system to run docker. Ubuntu * that does not occupy the system app store restarts quickly.

However: * install Windows 10 Insider Preview build 19041 or later* Using a proxy is still likely to report an operation {error that the object type referenced does not support the attempt. Methods refer to the above.