我在WINDOWS上玩WSL(ubuntu)

平时开发工作都是基于LINUX的,所以习惯使用linux的很多工具。对于windows的命令行,cmd 和power shell, 都很不习惯。于是想尽了一切办法在windows上实用bash。

比如:virtual box 虚拟机,git bash, cygwin等等。虚拟机是彻底的方式,毕竟实际就是一台Linux电脑,只是需要ssh连上去即可。但它也有问题,要创建虚拟机,要划分内存和CPU,因此不能充分利用这些CCR资源。换言之,就是速度慢,性能低。

微软的WSL是windows下的一个linux子系统,与windows共享内存和CPU,因此可以更好地利用内存和CPU。而且它开机及自动启动,关机自然关闭。管理也比较方便。

要使用WSL,首先要安装WSL,其步骤参考微软的guide:

https://learn.microsoft.com/zh-cn/windows/wsl/install

安装好ubuntu后,ssh并不会默认开启,这跟虚拟机不一样,需要单独配置。

1.修改ssh_config文件:vi /etc/ssh/sshd_config。主要是以下两个地方:

1). Port 22 #默认即可,如果有端口占用可以自己修改
2). PasswordAuthentication yes # 允许用户名密码方式登录

2. 在 /etc/ssh路径下执行ssh-keygen -A,生成所需的key。

3. 启动sshd, /etc/init.d/ssh start 或service ssh start

4. 配置sshd自动启动,这比较麻烦,以下是摘抄自github上的一个解决方案:https://gist.github.com/dentechy/de2be62b55cfd234681921d5a8b6be11

 

  1)Create a sshd.bat file and edit it with the following commands:

 

    • vi sshd.bat
    • Add the following code: C:\Windows\System32\bash.exe -c "sudo /usr/sbin/sshd -D"
    • Save the file and move it to a more accessible location, e.g. mv ssh.bat /mnt/c/Users/YourUserName/Documents. Make sure to match your username! Take note of this location for the next step as in Windows language this corresponds to C:\Users\YourUserName\Documents
  1. )Create a sshd.vbs file and edit it with the following commands:

    • vi sshd.vbs
    • Add the following code, making sure to put in your actual user name:
    Set WinScriptHost = CreateObject("WScript.Shell")
    WinScriptHost.Run Chr(34) & "C:\Users\YourUserName\Documents\sshd.bat" & Chr(34), 0
    Set WinScriptHost = Nothing
    
    • Save the file and move it to a more accessible location, e.g. mv sshd.vbs /mnt/c/Users/YourUserName/Documents.
    • Open start menu, type run. Then type shell:startup. Copy the vbs file over to the Startup folder
  2. )Finally, you will need to configure the ssh server to start without requiring password. Run the command sudo visudo and add this line to the end of the file:

    • %sudo ALL=NOPASSWD: /usr/sbin/sshd

5.  安装eclipse

从官网下载CDT package,然后解压到指定的目录即可。

但是运行时会遇到以下错误:

SWT OS.java Error: Failed to load swt-pi3, loading swt-pi4 as fallback.

这是因为WSL中没有安装libswt-gtk-4-java。这是swt的基础库。安装即可。

apt install libswt-gtk-4-java

此后,wsl cli中仍然不能运行eclipse,会提示Cannot open display。

这是因为wsl中没有GUI。不过,我不需要wsl的GUI。 只需要在MobarXTerm中开启X11server就可以了。

然后在MobarXTerm中登录WSL, 运行eclipse就可以看到久违的eclipse界面了。

这再次体现了linux的优越性。

 

6. WSL的根文件系统的存储位置:

C:\Users\$UserName\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs

$UserName 替换为windows 用户名。

posted @ 2022-10-25 15:43  耕读编码  阅读(558)  评论(0编辑  收藏  举报