1. Uninstall the ssh server
sudo apt-get remove openssh-server
2. Install ssh server
sudo apt-get install openssh-server
3. Modify the ssh server configuration
sudo vim /etc/ssh/sshd_config
The following items need to be modified:
Port 2222 #默认的是22,但是windows有自己的ssh服务,也是监听的22端口,所以这里要改一下
UsePrivilegeSeparation no
PasswordAuthentication yes
AllowUsers youusername # 这里改成你登陆WSL用的用户名(可以添加多个用户名,用空格分隔)
4. Start the ssh server
sudo service ssh --full-restart
5. View the ip address of wsl2
hostname -I (大写字母I)
Now you can use xshell to log in to ubuntu on windows, the IP is 127.0.0.1 or the ip address you want to view, but please note that the cmd window cannot be closed yet. After shutting down, the sshd service will also be shut down, and the connection will be disconnected. This problem has not yet found a solution.