当前位置:网站首页>Wsl2 environment setup

Wsl2 environment setup

2022-06-09 11:43:00 allworldg

preface

My computer configuration is not high , Open the virtual machine and run linux Always think too laggy . Only recently learned windows Already wsl2—— A relatively lightweight virtual machine software . So this blog tends to focus on , Save records for multiple use .

Environmental Science

  1. The host machine windows11
  2. The goal is : The installation is based on WSL2 Of Ubuntu20.04 Virtual machines and some common software environments .

WSL2 install

You can refer directly to Microsoft Official website manual , Enter the command wsl --install The one-time default installation is completed . But my local configuration is not high , So I use step-by-step installation , To customize the location of virtual machines .

  1. Enable windows function
    Set up -> Applications and functions -> Procedures and functions -> Enable or close windows function

     Insert picture description here

  2. start-up windows Of linux Subsystem and virtual machine platform , Open as Administrator Powershell, Run the command

    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    
  3. Restart the computer for the settings to take effect .

  4. download linux Kernel update package and run update .

    https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

  5. ( Optional ) If you want to default to wsl2, Then set the command wsl --set-default-version 2 . If you want to use it at the same time wsl1 and wsl2, You can download different linux Release settings version .

    wsl --set-version Ubuntu-18.04 2
    wsl --set-version Ubuntu-20.04 1
    
  6. download ubuntu20.04 https://aka.ms/wslubuntu2004.( Maybe it's a network problem. The download is very slow , I switched to the Microsoft Store to download it quickly )

  7. Change the file suffix to .zip, Then decompress

  8. double-click exe start-up , Set the account and password according to the prompt , installation is complete .

    image-20220425153222566

Configure alicloud image

  1. In order to improve the Linux Package manager download software speed , modify apt-get Mirror source . Open Alibaba cloud's ubuntu Mirror website https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.3e221b11YfnocC
  2. According to the prompt, overwrite the configuration content to /etc/apt/sources.list In file ( Remember to back up first ).
  3. And then run the command sudo apt-get update

------------------------------------ The following installation contents are optional -----------------------------------------

install jdk

Directly in apt Find a download in the source .

sudo apt search jdk
udo apt install openjdk-11-jdk
java -v # Test after downloading 

install maven

  1. download : It can be used directly apt Source download maven3.6.3. But because I used to use 3.6.3 Various versions build bug, So I'd better be honest on and off the official website .( Get into maven Official website , Download suffix is bin.tar.gz Version of )

    wget https://dlcdn.apache.org/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz # Download package 
    tar -zxf apache-maven-3.8.5-bin.tar.gz
    
  2. Configure environment variables

    sudo vim /etc/profile
    export M2_HOME=/opt/apache-maven-3.8.5
    M2_HOME = /opt/apache-maven-3.8.5
    export PATH=${M2_HOME}/bin:$PATH # Add to end of file 
     Run after exiting  source /etc/profile Let variables take effect 
    mvn -v # Printing the publication number indicates that the configuration is successful 
    
  3. To configure maven Alibaba cloud image , Get... Here Mirror address

    cd /opt/apache-maven-xxx/conf  # Enter the conf Folder 
    vim settings.xml
     Just paste the code at the specified location .
    

install mysql

  1. download mysql 8.0 Install the configuration package -> decompression -> to update apt Source -> install mysql 8.0 server

    wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb # download 
    sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb # Unzip and select configuration 
    sudo apt update # to update 
    sudo apt install mysql-server # download mysql 8.0
    
  2. Create user , Support remote connection

    1. function sudo service mysql start Start database .
    2. because mysql8 Of root By default, the account encryption plug-in adopts sock plug-in unit ( namely mysql root The account will only allow the external system user name to be the same name when logging in root The user logs in ). So install mysql It doesn't matter if there is an initialization password , direct sudo mysql Open the can .
    sudo mysql
    use mysql;
    create user '#userName'@'#host' identified by '#passWord'; # Create the role ,#host Set to %, Allow others on behalf of the account ip Address connection 
    grant all on *.* to '#username'@'%'; # It's easy , Given all permissions .
    flush privileges; # Refresh Authorization 
    
  3. Modify the configuration file

    sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
     take  bind-address  notes 
     restart mysql The service can be 
    

install python3

Because of my linux Environment comes with itself python, So for the time being, just use . Here also back up a method on the network ( Add source and download ).

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.6

install c and c++

sudo apt update
sudo apt install build-essential # One time installation gcc,g++ and make

install redis

  1. You can use it directly sudo apt install redis Download the version that comes with the warehouse , It's more convenient . Here's a demo of downloading the official website version .
  2. Download and install
    wget https://github.com/redis/redis/archive/7.0.0.tar.gz # Download installation package 
    tar -zxf 7.0.0.tar.gz # decompression 
    mv redis /opt/ #  Move to custom location 
    sudo make ; # install  ( Optional )
    sudo make install PREFIX=/opt/redis-7.0.0 # Customize the installation location and install ( Optional )
    ./opt/redis-7.0.0/bin/redis-server  # function redis-server, If the operation screen appears, it is successful 
    
  3. Create configuration files and service script files
    cd /opt/redis-7.0.0
    cp redis.conf ./bin/6379.conf# Copy a copy of the configuration file to the software directory , The port number is used as the name for convenience when registering script services later .
    cp /opt/redis-7.0.0/utils/redis_init_script /etc/init.d/redis # Copy script file , convenient service Command to start 
    
  4. Modify script file
    The following parameters are modified according to your actual situation .
    sudo vim /etc/init.d/redis
         14	REDISPORT=6379
         15 EXEC=/opt/redis-7.0.0/bin/redis-server
         16 CLIEXEC=/opt/redis-7.0.0/bin/redis-cli
         17
         18 PIDFILE=/var/run/redis_${REDISPORT}.pid
         19 CONF="/opt/redis-7.0.0/bin/${REDISPORT}.conf"
    
  5. Modify the configuration file
    sudo vim /opt/redis-7.0.0/bin/6379.conf
    protected-mode no # Turn off protection mode , Allow writing and reading of remote connections .
    daemonize yes # Background operation 
    # bind 127.0.0.1 -::1 # Comment on this trip , Allow remote connections 
    
  6. function sudo service redis start

Fix ip

I feel wsl2 At present, one of the more criticized points is wsl2 Of ip Is dynamic , Every time it is completely closed and then opened wsl2 Will update ip, Although... Can be used at present localhost visit wsl2 Server running in , But I still want to try to fix ip. Browse the Forum 、 Blogs and open source communities , Find a simpler way . Write a script on the host ( Can be modified by yourself linux Release name and ip)

wsl -d Ubuntu-20.04 -u root ip addr add 192.168.50.16/24 broadcast 192.168.50.255 dev eth0 label eth0:1 # Note that it must be root
netsh interface ip add address "vEthernet (WSL)" 192.168.50.88 255.255.255.0

Every time you restart wsl Then run the script with administrator privileges .

ps : You don't have to right-click the script every time to use the method of administrator privileges .

  1. Right click to create a shortcut
  2. Enter properties , Click Advanced... Under the shortcut
    300
  3. Check the administrator to run .

Self starting script

wsl2 Not yet systemctl, Therefore, the software cannot start automatically , The open source community provides some of its own imitations Systemctl Software , But it is not perfect yet , So I use it Turn it on + Run scripts automatically To achieve a fool like self starting .

  1. Create a script on the host ( Or you can add it directly to fix it ip After the script )
  2. Input wsl /home/allworldg/startup.sh
  3. Create... In the same location within the virtual machine /home/allworldg/startup.sh
  4. Edit according to your own situation startup.sh , for example
     echo '123123'| sudo -S service mysql start # Auto input sudo Password 
     echo '123123'| sudo -S service redis start
    

Step on the pit

  1. Because of the modification /etc File permissions , Lead to sudo Can't use .
    1. reason : Every time you edit /etc All documents have to be added sudo, It's not good , The picture came directly sudo chown "myusername" -R /etc , The result was later used sudo Wrong orders .
      sudo: /etc/sudoers is owned by uid 1000, should be 0
      sudo: no valid sudoers sources found, quitting
      sudo: unable to initialize policy plugin
      
    2. Solution : function pkexec /usr/sbin/visudo Command is enough .
    3. Because this environment is in wsl2 Next , Using the above command cannot successfully execute , So there are other ways . After consulting, we know that , about WSL for , Can be in CMD perhaps Windows PowerShell Log in with the following command root account number :wsl -u root . And then modify sudoers and sudoers.e that will do .
      chown root:root /etc/sudoers
      chown -R root:root /etc/sudoers.d
      
原网站

版权声明
本文为[allworldg]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091053267303.html