wsl安装与配置详情

wsl安装与配置详情

社蕙 81 2023-12-11

安装WSL+Ubuntu

WSL(Windows Subsystem Linux) 是适用于Linux的Windows虚拟子系统,它基于Linux的发行版本Ubuntu并使用GCC进行C/C++的编译运行工作。打开

  • 控制面板 > 程序 > 启动或关闭Windows功能
    • 选择适用于Linux的Windows子系统,安装时选择WSL2版本,当然也可以使用命令更换WSL版本。安装过程可能会重启系统。
    • 选择虚拟机平台
    • 选择Hyper-V
  $ wsl --install
  $ wsl --update
  $ wsl.exe --install -d Ubuntu-22.04

更多内容可以参考另一篇文章Windows下CUDA环境配置
如果不知道密码,那sudo的密码可能就是当前windows账号的密码。

启用SSL

这么做的本意是希望能从任何地方ssh到我实验室PC的WSL上,但是在一个非常关键的步骤上卡住了,那就是要设置路由器转发。但是还是记录一下,说不定日后能访问到宿舍的WSL。

参考文章是WSL 2 Setup for SSH Remote Access,这篇相当之详细,爆杀知乎的教程。

在wsl里搭建好ssh server

  sudo apt install openssh-server
  sudo vi /etc/ssh/sshd_config
  # 取消Port 22和PasswordAuthentication yes的注释
  sudo service ssh restart
  ip addr | grep eth0
  # inet后面的ip是<wsl_internal_ip>,记下来后面有用

在这一步有两种验证方式,一种是在bash中输入systemctl status sshd,另一种是在Windows终端中输入ssh <username>@localhost

在Windows设置转发

  netsh interface portproxy add v4tov4 `
    listenaddress=<windows_local_ip> `
    listenport=<listen_port_on_windows> ` # Any unused port   number
    connectaddress=<wsl_internal_ip> `
    connectport=<ssh_service_port>    
  # 可以输入netsh interface portproxy show all查看确认

  netsh advfirewall firewall add rule `
    name="<any_name>" `
    dir=in `
    action=allow `
    protocol=TCP `
    localport=<listen_port_on_windows>`
  
  # 这一步也可以到“高级网络设置-入站规则里面手动新建”

配置路由器端口转发

Navigate to Port Forwarding settings in the router’s settings menu.

Configure the router to forward traffic from a specific external port on the public internet to a corresponding local destination port on your windows computer identified by its IP address.

The specific external port can be set to any value of your choice. The local destination port is <listen_port_on_windows> . The IP address of your Windows machine is <windows_local_ip> .

尝试连接

  • LAN:ssh <wsl_username>@<windows_ip> -p <listen_port_on_windows>
  • WAN:ssh <wsl_username>@<public_wan_ip> -p <external_ssh_port>

安装thefuck

直接访问https://github.com/nvbn/thefuck按照环境运行提供的命令。

trouble shooting

我一开始没有看到thefuck wiki这个页面,里面提供了关于thefuck调试的方法,当然我没有使用里面的内容,但是看来覆盖到了我遇到的问题,之后可以一试。

解决thefuck/fuck找不到的问题

打开bash配置文件code ~/.bashrc,加入下面的内容,然后source一下。

PATH="$PATH:$HOME/.local/bin"
eval $(thefuck --alias)

解决wsl上fuck命令运行极慢的问题

我一开始找到的解决方案来自Slow rule eval on WSL
#1036
,解决方法也许不同但问题的都是相同的:

Try echo $PATH and you probably gonna see a long list there, and that's the reason the fuck is so slow. You can do what's suggested here, microsoft/WSL#1640 (comment), and restart your linux distro.

就是wsl上默认的path太长太长了,thefuck要找半天你可能错在哪里。所以解决方案就是,ban掉其中来自windows的路径,改掉wsl的设置,然后在外面应用:

Instead of trying to fiddle with the path or the registry, just disable the feature.

in WSL:

  sudo vi /etc/wsl.conf

add:

  [interop]
  appendWindowsPath = false

then in Windows find your distro name and terminate it so the config changes are picked up:

  wsl.exe --list
  wsl.exe --terminate <distro_name>

当时脑子一抽没有注意到一件事情,就是这样做vscode的路径也没有掉了,因此要么先which code记录一下位置。如果亡羊补牢的话就是(VScode装在默认位置):

export PATH="$PATH:/mnt/c/Program Files/Microsoft VS Code/bin"

这样VScode就堂堂复活了。