前言
前段時間,我買了塊固態硬盤給我的筆記本電腦裝上(因為它太慢了,影響我游戲學習)。同時也重裝了系統,以前一直在用 Win8.1,在上面構建了我的開發環境:在 Win8.1 上安裝了 VMvare,在 VMware 上安裝了 Centos,在 Centos 上安裝了 Docker,然后在 Win8.1 上通過 Xshell 來連接虛擬機 Centos 控制 Docker 進行開發學習。
這次安裝的是 Win10,真香,win10 支持了 WSL,直接可以在 Win10 上安裝 Docker、Linux系統等,因此重新搭建了我的開發環境,首先把 Git 配置好。
什么是 WSL
WSL 是 Windows Subsystem for Linux 的縮寫,意思是 linux 版的 window 子系統。
引用自:微軟官網 https://docs.microsoft.com/zh-cn/windows/wsl/about
The Windows Subsystem for Linux lets developers run a GNU/Linux environment -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the overhead of a virtual machine.
You can:
- Choose your favorite GNU/Linux distributions from the Microsoft Store.
- Run common command-line tools such as grep, sed, awk, or other ELF-64 binaries.
- Run Bash shell scripts and GNU/Linux command-line applications including:
- Install additional software using own GNU/Linux distribution package manager.
- Invoke Windows applications using a Unix-like command-line shell.
- Invoke GNU/Linux applications on Windows.
簡單的說就是,Linux 的 Windows 子系統讓開發人員無需虛擬機就可以直接在 Windows 上運行 Linux 環境,包括大多數命令行工具、程序和應用。
使用 WSL 的好處是:
- 與在虛擬機下使用 Linux 相比,WSL 占用資源更少,更加流暢;
- WSL 可以對 Windows 文件系統下的文件直接進行讀寫,文件傳輸更方便;
- 剪貼板互通,可以直接在 Windows 下其它地方復制文本內容,粘貼到 WSL;
WSL 安裝 Ubuntu
-
通過
Microsoft Store
下載,自動安裝
-
自己去官網下載,防止 C 盤容量太大
去官網
下載后復制到D
盤,將后綴名改成.zip
,然后解壓,執行里面的exe
文件,進行安裝
安裝后在powershell
或者windows terminal
(Microsoft Store
上的另一個寶藏應用,直接去搜掃安裝就行了)
執行命令wsl -l -v
列出所有子系統
PS C:\Users\Administrator> wsl -l -v
NAME STATE VERSION
* Ubuntu-20.04 Running 2
docker-desktop Stopped 2
docker-desktop-data Stopped 2
執行命令wslconfig /setdefault Ubuntu-20.04
將Ubunru-20.04
設為默認子系統
PS C:\Users\Administrator> wslconfig /setdefault Ubuntu-20.04
執行命令wsl
進入子系統
PS C:\Users\Administrator> wsl
zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ neofetch
配置 zsh
查看一下
zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen
安裝 zsh
zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ sudo apt-get install zsh
zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen
/bin/zsh
/usr/bin/zsh
將 zsh 設為默認 shell
zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ sudo chsh -s /bin/zsh
推出再重新進入
zioyi@DESKTOP-Q4DJEQV:/mnt/c/Users/Administrator$ exit
logout
PS C:\Users\Administrator> wsl
➜ Administrator neofetch
配置 Git
- 安裝 git
➜ Administrator sudo apt-get install git
- 配置你的用戶名
➜ Administrator git config --global user.name xxx
- 配置你的郵箱地址
➜ Administrator git config --global user.email xxx@xxx.com
- 生產SSH密鑰
➜ Administrator ssh-keygen -t rsa -C "xxx@xxx.com"
➜ Administrator cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC12sKF6l8CKsYjrWCruS3fqPhSHExo6pcvzCRUYMotqY7KJvhIG45xGALedXn8ow8+jBfC6r9CpO6mzCpf18WsGf7omBvoQJa3rQeuO1KvZ7Ctd/EH+jJndLoFisH76vY4gm2OR0UU= xxx@xxx.com
- 復制公鑰的內容並且打開你的 github 設置界面,找到 SSHkey 設置,點擊右上角的New SSH key
6.測試
➜ Administrator ssh git@github.com
PTY allocation request failed on channel 0
Hi Zioyi! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
總結
本篇對 WSL 進行了簡單介紹,嘗試基於 WSL 安裝了 Ubuntu 系統,並在系統中安裝 zsh 和 git 工具,打造新時代的 Windows 開發環境。
相比於原來 Win8 的開發環境,這套真的好很多,在便捷性、資源使用、性能上都有很大的提升。