WSL2: development environment installation

written in front

1. Install WSL2

  • Run CMD as an administrator and execute the following commands for WSL and the default Ubuntu distribution for Linux:
wsl --install
  • After the installation is complete, restart the computer, and enter the following command to view the installed WSL version information;

view version

2. Initialize Ubuntu on WSL2

  • Open the start menu and click Ubuntu to bring up the terminal of the Ubuntu system (just like a remote server!):

Start the Ubuntu terminal

  • Set the username and password as follows:
    Set username and password

  • After the configuration is complete, execute the following command in CMD to view the installed Lunix release version:

View the Linux version

  • Note that after restarting the computer, you need to open the Ubuntu terminal once to start Ubuntu;
  • Some other errors are resolved as follows:

1. Error: 0x80040326 Error code: Wsl/Service/0x80040326

3. Install a cross-platform terminal

  • This is mainly because CMD can only execute Windows commands, and the commands of the Linux distribution corresponding to WSL need to be executed by other terminals, which will appear confusing. Therefore, you can download Windows terminal software to manage these different terminals in a unified manner. (The interface is also better looking);
  • Download address of Windows terminal: https://apps.microsoft.com/store/detail/windows-terminal-preview/9N8G5RFZ9XK3?hl=zh-cn&gl=cn ;
  • After installation, you can switch between different console terminals in the Windows terminal , such as CMD, PowerShell and Ubuntu, etc.:

windows terminal

  • In addition, upgrade PowerShell to Powershell7 or above, because the version that comes with Windows is relatively low:

Upgrade PowerShell

Fourth, the necessary tools to install the remote server

  • From this step, the configuration process is similar to using a virtual machine or a real remote server;
  • Some correspondences are as follows:
    • Remote server: Ubuntu under WSL;
    • Remote router: Windows;
    • Local development/client: Windows;

1. Replace the source

  • Execute the following command to open the source configuration file:
sudo vim /etc/apt/sources.list
  • Add the source connection in it as follows:
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

2. Install the necessary tools

  • Execute the following command to install the necessary tools:
sudo apt update && sudo apt upgrade -y
sudo apt install curl wget git ca-certificates build-essential net-tools -y
sudo apt install g++ gdb make ninja-build rsync zip -y

5. Configure the SSH of the remote server

1. Install SSH service on the server

  • Execute the following command on Ubuntu to reinstall the SSH service:
# 卸载原有的ssh
sudo apt remove openssh-server
# 重新安装ssh
sudo apt install openssh-server -y
# 启动ssh服务
sudo service ssh start
# 查看ssh服务状态
sudo service ssh status
# 设置ssh开机自启动
sudo systemctl enable ssh
  • View the ip address on the server:
    view ip

  • Then you can connect to the server locally with the following command:

ssh connection

  • Note that after restarting the server, you need to restart the SSH service, so that the local can connect remotely through SSH;

2. Fixed server ip address

  • The port forwarding technique is used here:

    • Port forwarding : Corresponding a certain port of the router with a certain ip of the intranet, external messages can be forwarded from the port to the ip of the intranet, and can only be forwarded in one direction;
    • Port mapping : Map a certain port of the router to an ip of the intranet, external messages can be sent to the ip of the intranet through this port, and ip of the internal network can also send messages to the outside world through this port, which is a two-way mapping ;
  • Open the ssh configuration file in the server:

sudo vim /etc/ssh/sshd_config
  • Set up port forwarding, the forwarded port is 2000:

port forwarding

  • This is equivalent to matching the port 2000 of the local computer (acting as the server) with the ssh of WSL (acting as the server), and the port number 2000 of the access server can be forwarded to the ssh service of WSL;

  • Execute the following command to make the changes on the server take effect:

# 打开防火墙的2000端口
sudo ufw allow 2000
# 重启ssh服务
sudo service ssh restart
# 重启防火墙
sudo ufw reload
  • Configure port translation in PowerShell as an administrator:
netsh interface portproxy add v4tov4 listenport=2100 listenaddress=0.0.0.0 connectport=2000 connectaddress=localhost

Configure port translation

  • This is equivalent to forwarding the messages monitored by port 2100 of the local computer (acting as a router) to port 2000 of the local computer (acting as a server);
  • Use the following command to view the monitoring of port 2100 of the local computer:
netstat -ano | findstr :2100

Open the port to listen

  • If no result is returned after executing the above command, you also need to start the IP Helper service in the service :

open service

  • Finally, you can access the ssh service of the server by accessing port 2100 of the local computer (router):

access ssh

6. Use VS Code to manage remote file systems

1. Install the plugin

  • Search and install the remote development plugin in the extension of VS Code :

install plugin

  • If the installation fails, remember to check whether VS Code has been upgraded to the latest version;

2. Add SSH configuration

  • After installation, the remote resource manager will appear in the left column ;
  • Open the remote resource manager, select remote , and click the "+" button on the right side of the SSH column below ;
  • Enter the SSH connection command in the pop-up command line;

Enter the ssh command

  • Select the configuration file to be updated, choose the first one here;
    Select the updated configuration file
  • This adds a SSH remote host connection configuration to the configuration file;

3. Connect to the remote host

  • Click the green button in the lower left corner to open the remote window:

open remote window

  • Select Connect to Host in the pop-up options ;

Choose to connect to host

  • Select the newly created remote host address:

Select remote host configuration

  • Of course, you can also directly enter user@hostthe remote host address in the format here;
  • Enter the password in the upper command line of the pop-up window to connect to the file system of the remote host:

enter password

  • Select Open Folder , select the folder to open in the above command line (usually select the root directory):
    select open folder

  • Then, you can see all the files under the folder on the left, and you can easily view and edit the contents of the files:

View and edit files

7. Use VS for remote development

1. Install the required development packages

  • Control Panel- > Programs- > Programs and Features- > Uninstall or change the program , find the installed VS software, right-click and change to bring up the VS installer;

Bring up the installer

  • Check the following installation packages to install;

Check the installation package

Check the installation package

2. Create a new project

  • After the installation is complete, create a Linux C++ project:

select item

create project

3. Configure remote connection

  • Open Tools- > Options and configure remote connections:

Configure remote connection

connect to remote host

  • Open Project -> xxx Properties -> General -> Platform Toolset , select WSL2 GCC Toolset ;

4. Write code and run the program

  • Create a new .cppfile, and then click GDB to debug the program to migrate the source code to the server and compile the program on the server, and finally execute it in the console;
  • The Linux console of VS can be brought up in Debug -> Linux console ;

run the program

  • Then /projectsunder the path of the server, you can find the created TinyWebserver project, which contains source code, relocatable object files .oand executable object files .out;

makefile on the server

5. Create a folder

  • By creating folders, you can classify and manage code files for different purposes;
  • Click the Show All Files button in the upper right corner of the Solution Explorer panel to display the directory structure in the File Explorer;

show all files

  • Then right-click to add to create a new folder ;

Guess you like

Origin blog.csdn.net/weixin_43992162/article/details/129437692