Windows WSL2開發環境,一點不遜色於Mac

一直使用Mac來做開發,但沒想到現在Windows也這麼好用。通過WSL,擁有了Windows酣暢的操作特性,又有了Linux的兼容性。作爲開發環境一點不遜色於Mac,甚至更易使用。下面就操練起來吧。

WSL2

1. 安裝

控制面板 -> 程序和功能 -> 啓用和關閉windows 功能, 勾選如下功能:
- 適用於Linux的Windows子系統
- 虛擬機平臺

確定。


2. 升級WSL

https://docs.microsoft.com/en-us/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package
下載 Linux 內核更新包 WSL2 Linux kernel update package for x64 machines
,並安裝。

設置wsl默認版本爲 `2`, PowerShell
```
wsl --set-default-version 2
```
  升級已安裝的分發,可使用wsl --set-version, PowerShell
```
wsl --set-version 分發版名稱 版本號
```

3. 安裝windows terminal

在windows appstore中安裝windows terminal

4. 安裝Linux系統

在windows appstore中安裝 ubuntu 22.04

並在windows terminal 中設置WSL的訪問

  • 命令行:wsl.exe -d Ubuntu-20.04
  • 啓動目錄: //wsl$/Ubuntu-20.04/home/{username}/

5. WSL開機啓動sshd

通過windows terminal進行ubuntu系統,並配置ssh開機啓動

# /etc/init.wsl
#!/bin/bash
/etc/init.d/ssh start

5. 將端口暴露

powershell:
PS C:\Users\mpan> netsh interface portproxy add v4tov4 listenport=22 connectaddress=127.0.0.1 connectport=22 listenaddress=* protocol=tcp

如果需要刪除該策略可執行

PS C:\Users\mpan> netsh interface portproxy delete v4tov4 listenport=22 protocol=tcp

如果磁盤空間緊張,可以使用pxlrbt/move-wsl工具遷移虛機到其它盤。

zsh

  1. 安裝
$ apt install zsh -y
$ chsh -s /bin/zsh
$ zsh
  1. 安裝 zinit 和插件
$ bash -c "$(curl --fail --show-error --silent --location https://raw.githubusercontent.com/zdharma-continuum/zinit/HEAD/scripts/install.sh)"
  1. 配置zsh
# ~/.zshrc
...
# Load powerlevel10k theme
zinit ice depth"1" # git clone depth
zinit light romkatv/powerlevel10k

# Plugin history-search-multi-word loaded with investigating.
zinit load zdharma-continuum/history-search-multi-word

# Two regular plugins loaded without investigating.
zinit light zsh-users/zsh-autosuggestions
zinit light zdharma-continuum/fast-syntax-highlighting

zinit snippet https://gist.githubusercontent.com/hightemp/5071909/raw/
zinit ice lucid wait='0'
zinit snippet https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/git/git.plugin.zsh

autoload -Uz compinit && compinit

## 解決粘貼慢的問題
pasteinit() {
  OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
  zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`?
}

pastefinish() {
  zle -N self-insert $OLD_SELF_INSERT
}
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish

# ~/.bashrc
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$HOME/shtool:$PATH:$HOME/go/bin:/home/mpan/.local/bin"
export BROWSER=wslview
source ${fpath[1]}/_kubectl
unsetopt prompt_cr prompt_sp

alias pbcopy="clip.exe"
alias pbpaste="powershell.exe -command 'Get-Clipboard'|head -n 1"
alias bd='ostr=`pbpaste`; echo ${ostr:0:-1}  | base64 -d'
alias b='ostr=`pbpaste`; echo -n ${ostr:0:-1} | base64'
alias gpush="git push origin HEAD:refs/for/main"

### ctrl+arrows
bindkey "\e[1;5C" forward-word
bindkey "\e[1;5D" backward-word

Docker

  1. 安裝並啓動

下載Docker Desktop安裝: Install Docker Desktop on Windows | Docker Documentation

  1. 配置

開啓 WSL2作爲基礎引擎

  1. 驗證, WSL Ubuntu
$ docker ps

Kubernetes

  1. Docker Desktop設置中開啓Kubernetes,即可獲得一個單節點的K8s集羣。
    如果鏡像無法下載,可參考: https://github.com/AliyunContainerService/k8s-for-docker-desktop 預先導入鏡像

  2. 可使用其它方式部署K8S/K3S,以kind爲例:
    安裝 kind

$ curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
$ sudo install kind-linux-amd64 /usr/local/bin/kind

創建集羣

$  kind  create cluster --name wsl2-k8s

查看集羣

$ kind get clusters

刪除集羣

$ kind delete cluster --name wsl2-k8s

vscode

  1. 安裝 https://code.visualstudio.com/
    下載vscode,並安裝
  2. 安裝插件
    安裝插件 Remote - WSL
  3. 通過遠程資源管理器,打開連接WSL的vscode windows


  4. 在新窗口中選擇WSL系統下的目錄,開始愉快編碼。

wsl掛載外部硬盤

  1. PowerShell 升級wsl,如果沒有'--mount' 命令的話
wsl --update
  1. PowerShell 查看所有磁盤
GET-CimInstance -query "SELECT * from Win32_DiskDrive"
  1. PowerShell 掛載磁盤,其中<DiskPath>的格式爲:\\.\PHYSICALDRIVE*
wsl --mount <DiskPath> --bare
  1. Linux 登錄wsl驗證
$ lsblk
  1. PowerShell 卸載磁盤,其中<DiskPath>的格式爲:\\.\PHYSICALDRIVE*
wsl --unmount <DiskPath>

小拓展:

  1. 插件:git history, gitlens, live server
  2. 添加快捷鍵。 命令:清除控制檯 綁定鍵:ctrl + k 當(條件):terminalFocus
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章