WSL是Windows Subsystem for Linux的简称。 windows 中直接使用linux? 硬盘如何共享?程序如何共享?
本文在windows的WSL2中安装好ubuntu20后,基本交互和使用的介绍。
访问:Ubuntu root
\\wsl$\Ubuntu
个人目录:
\\wsl$\Ubuntu\home\<yourname>
在windows terminal 设置中配置缺省进入目录
"startingDirectory": "//wsl$/Ubuntu/home/<yourname>/"
可以映射网络驱动器
\\wsl$\Ubuntu
wsl <linux-command>
如
wsl ls -la
/mnt/c/Users/<yourname>
如果要访问C:\projects\code\
cd ~
ln -s /mnt/c/projects/code/
带上exe后缀。如从windows打开当前文件夹
explorer.exe .
编辑bashrc
notepad.exe ~/.bashrc
用vscode打开目录:
code ~/projects/mywebsite
# 先备份/etc/apt/sources.list
cp /etc/apt/sources.list /etc/apt/sources.list.bak
# 替换镜像
sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
# 更新源
apt-get update
# 安装中文支持
apt-get install -y language-pack-zh-hans
# 设置默认语言
update-locale LANG=zh_CN.UTF-8
PS C:\Users\zhh> wsl.exe -l -v
NAME STATE VERSION
* Ubuntu Running 1
docker-desktop Running 2
docker-desktop-data Running 2
PS C:\Users\zhh> wsl.exe --set-version Ubuntu 2
sudo apt-get update
sudo apt-get install git-all
npm config set registry https://registry.npm.taobao.org
或者vim .npmrc
registry=https://registry.npm.taobao.org
到https://github.com/nvm-sh/nvm 查找最新版本。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.1/install.sh | bash
nvm --version
0.37.1
# 查询node版本
nvm ls
# 用nvm安装node
nvm install node
Now using node v15.3.0 (npm v7.0.14)
# 安装npx,npx也可以对包进行管理
npm install npx
或者单独用操作系统安装node环境,但这种方式不方便切换不同的node版本
sudo apt-get install build-essential
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
docker 类似虚拟机。所以不需要在windows和wsl各安装一份。 Docker Desktop for Windows 完全支持WSL2.
在windows安装Docker Desktop,并使docker
和docker compose
在win和wsl都可用。
可以在安装docker时配置支持WSL2,也可以在安装完毕后,在右下角图标中点settings,在general页配置选中Use the WSL 2 based engine . 在Resources页的WSL Integration 项,选中ubuntu。
VS Code WSL2 Integration 在vs code中执行 Ctrl + Shift + P,输入Terminal: Select Default Shell,然后WSL Bash。
vscode和其他程序一样,可以访问linux,通过\wsl$\。 如下插件可以简化linux和docker使用:
除了在命令行直接远程操作,还可以用remote window和左侧的Remote Explorer 图标访问容器和远程系统。
https://docs.docker.com/docker-for-windows/wsl/