目录
自从 Windows 发布 WSL 子系统来,在 win 上敲代码越发方便了起来,这里使用 win10 安装配置 wsl2
升级到 WSL2 的先前操作
如果不关心 WSL 的版本可以不用管这一步
参考官方的做法
启用子系统功能
需要在 Power Shell 管理员身份运行
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
启用虚拟功能
需要在 Power Shell 管理员身份运行
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
下载 Linux 内核更新包
x86 版本:https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
ARM64 的需要别的地方下载
将 WSL 2 设置成默认版本
wsl --set-default-version 2
重启计算机
在 Microsoft Store 安装对应版本的 Linux 版本

这里下载了 22.04 的版本,下载完成之后打开 Open,按提示设置用户名密码即可
可以通过 wsl -l -v
来查看现在正在运行的版本信息
如果执行到这,其实就安装好 WSL 了,就可以不用管下一步的安装了
安装 WSL
默认安装
简单的安装可以直接输入 wsl --install
即下载最新的 Linux 内核,将 WSL 2 设置为默认值,并安装 Linux 发行版(默认安装 Ubuntu)。
这个命令仅在完全未安装 WSL 时才有效,否则会报命令不存在的错误
也可以运行 wsl --list --online
或者 wsl -l -o
来现实目前可用的 WSL 发行版,并且可以指定版本安装 wsl --install -d <DistroName>
(base) PS C:\\Users\\xxx> wsl --list --online The following is a list of valid distributions that can be installed. Install using 'wsl --install -d <Distro>'. NAME FRIENDLY NAME Ubuntu Ubuntu Debian Debian GNU/Linux kali-linux Kali Linux Rolling openSUSE-42 openSUSE Leap 42 SLES-12 SUSE Linux Enterprise Server v12 Ubuntu-16.04 Ubuntu 16.04 LTS Ubuntu-18.04 Ubuntu 18.04 LTS Ubuntu-20.04 Ubuntu 20.04 LTS
这里直接默认安装即输入 wsl --install
通过 Microsoft Store 安装
这里也可以直接用 Microsoft Store 搜索 Ubuntu 下载

之后进行安装,设置对应的账号密码即可,如果没有特殊需求,建议直接使用 root 用户,减少很多权限问题的麻烦事,毕竟自己电脑不需要这么多权限。
导出和导入
WSL 的默认安装路径是在 C 盘系统盘上的,会大量占用系统盘的内存,所以在开始用之前,建议先移动到别的盘符上之后再来用
导出
wsl --export <Distribution Name> <FileName>
例如,默认安装了 Ubuntu-22.04 版本,想要移动到别的盘符,可以先导出来
wsl --export Ubuntu-22.04 D:\\WSLSystem\\system\\ubuntu.tar
导入
将上面导出的重新导入,此时可以指定位置
wsl --import <Distribution Name> <InstallLocation> <FileName>
例如,导入上面所导出的并且存储在 D 盘
cd D:\\WSLSystem\\ wsl --import Ubuntu .\\ubuntu .\\system\\ubuntu.tar
会发现创建了一个 ext4
的文件,这个就是 WSL 的环境了
此时查看列表和版本信息
(base) PS C:\\Users\\xxx> wsl -l -v NAME STATE VERSION * Ubuntu Running 2 Ubuntu-22.04 Stopped 2
就会多个 WSL 发行版
设置默认 WSL ,之后直接使用 wsl
就会默认进指定版本
wsl --set-default Ubuntu
删除
对于旧的可以删掉,不过建议保留,两个环境可以捣鼓不同的东西
wsl --unregister <DistributionName>
**警告:**取消注册后,与该分发版关联的所有数据、设置和软件将永久丢失。
说明和配置
文件
在win上可以直接访问 WSL的文件系统
文件路径在 \\\\wsl$
上,不过只有在 WSL online 的情况下才能访问
Git 配置
直接正常设置git,之后替换 .ssh
的文件,就可以用之前电脑的git公钥私钥了
不过如果复制过来权限没有的话要给个执行权限,否则会报错:Load key "/home/lim/.ssh/id_rsa": bad permissions
赋予权限:
chmod 700 id_rsa
使用 ssh-add
将私钥添加到高速缓存中,如果遇到没有启用 ssh-agent 的话,会报错:
Could not open a connection to your authentication agent.
解决方法:执行 eval "$(ssh-agent)"
,或者直接添加到 .zshrc
或者 .bashrc
中就可以愉快的拉代码了
termial 配置
打算使用 zsh/oh-my-zsh+ agnoster 的搭配
参考:
Setting up Windows Subsystem for Linux with zsh + oh-my-zsh + ConEmu
安装 zsh/oh-my-zsh
chsh -s $(which zsh)
sudo apt-get install zsh
将 zsh 作为默认终端
chsh -s $(which zsh)
安装 oh-my-zsh
sh -c "$(curl -fsSL <https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh>)"
配置
修改 ~/.zshrc
文件,将主题修改成 agnoster
ZSH_THEME="agnoster"
解决 agnoster 主题乱码
下载相关的字体文件:https://github.com/powerline/fonts
# clone git clone <https://github.com/powerline/fonts.git> --depth=1 # install cd fonts .\\install.ps1
之后 Win 就会出现弹窗在安装字体
完成之后在 powershell 修改字体,这边选择了

推荐插件
git clone <https://github.com/zsh-users/zsh-syntax-highlighting.git> echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
之后修改 ~/.zshrc
中的 plugins ,添加上该插件
plugins=(git zsh-syntax-highlighting)
git clone <https://github.com/zsh-users/zsh-autosuggestions> ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
修改 plugins
plugins=( # other plugins... zsh-autosuggestions )
点击官网下载回来,或者
wget <https://mimosa-pudica.net/src/incr-0.2.zsh>
将插件移动到 oh-my-zsh 插件文件中
mv ~/incr-0.2.zsh ~/.oh-my-zsh/plugins/
之后在 ~/.zshrc
文件中添加上
source ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/incr*.zsh
可以很省事跳转文件夹
apt install autojump
之后还是要在 ~/.zshrc
中修改,添加上该插件
plugins=( git autojump # other plugins... )
oh-my-zsh 一些小配置
- 开启自动更新
~/.zshrc
中添加
DISABLE_UPDATE_PROMPT=true
手动更新:upgrade_oh_my_zsh
- 隐藏用户信息
在 ~/.zshrc
中添加
代理配置
在 WSL2 中,在网络上是作为独立两台机器使用的,所以要让 WSL2 能访问上 Windows 的网络
在 Ubuntu 中,可以在 cat /etc/resolv.conf
查看 Windows 的 IP
之后设置中就可以指向对应的IP使用,参考:
# proxy function proxy(){ host_ip=$(cat /etc/resolv.conf |grep "nameserver" |awk '{print $2}') export http_proxy="http://$host_ip:7890" export https_proxy=$http_proxy export all_proxy="socks5://$host_ip:7890" echo -e "open proxy: $http_proxy" } function unproxy(){ unset all_proxy unset https_proxy unset http_proxy echo -e "unset proxy" }
0 条评论